View Javadoc

1   /*
2    * Copyright 2004 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.ancientprogramming.fixedformat4j.format.impl;
17  
18  import com.ancientprogramming.fixedformat4j.format.AbstractFixedFormatter;
19  import com.ancientprogramming.fixedformat4j.format.FormatInstructions;
20  import com.ancientprogramming.fixedformat4j.annotation.Sign;
21  import org.apache.commons.lang.StringUtils;
22  
23  /**
24   * Apply signing to values
25   *
26   * @author Jacob von Eyben - http://www.ancientprogramming.com
27   * @since 1.1.0
28   */
29  public abstract class AbstractNumberFormatter<T> extends AbstractFixedFormatter<T> {
30  
31    /**
32     * Override and applies signing instead of align.
33     *
34     * @param value the value
35     * @param instructions the instructions
36     * @return the parsed object
37     */
38    public T parse(String value, FormatInstructions instructions) {
39      T result = null;
40      if (value != null) {
41        Sign signing = instructions.getFixedFormatNumberData().getSigning();
42        String rawString = signing.remove(value, instructions);
43        result = asObject(rawString, instructions);
44      }
45      return result;
46    }
47  
48    /**
49       * Override and applies signing instead of align.
50       *
51       * @param obj the object to format
52       * @param instructions the instructions
53       * @return the raw value
54       */
55      public String format(T obj, FormatInstructions instructions) {
56        return instructions.getFixedFormatNumberData().getSigning().apply(asString(obj, instructions), instructions);
57      }
58  }