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.FormatInstructions;
19  
20  import java.text.DecimalFormat;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.commons.logging.Log;
25  
26  /**
27   * Base class for formatting decimal data
28   *
29   * @author Jacob von Eyben - http://www.ancientprogramming.com
30   * @since 1.0.0
31   */
32  public abstract class AbstractDecimalFormatter<T> extends AbstractNumberFormatter<T> {
33  
34    private static final Log LOG = LogFactory.getLog(AbstractDecimalFormatter.class);
35  
36    protected static final char DECIMAL_SEPARATOR;
37    protected static final char GROUPING_SEPARATOR;
38    protected static final String ZERO_STRING;
39    protected static final DecimalFormat FORMATTER;
40  
41    static {
42      FORMATTER = new DecimalFormat();
43      FORMATTER.setDecimalSeparatorAlwaysShown(true);
44  
45      DECIMAL_SEPARATOR = FORMATTER.getDecimalFormatSymbols().getDecimalSeparator();
46      GROUPING_SEPARATOR = FORMATTER.getDecimalFormatSymbols().getGroupingSeparator();
47      ZERO_STRING = "0" + DECIMAL_SEPARATOR + "0";
48  
49    }
50  
51    public String asString(T obj, FormatInstructions instructions) {
52  
53      String rawString = obj != null ? FORMATTER.format(obj) : ZERO_STRING;
54      if (LOG.isDebugEnabled()) {
55        LOG.debug("rawString: " + rawString + " - G[" + GROUPING_SEPARATOR + "] D[" + DECIMAL_SEPARATOR + "]");
56      }
57      rawString = rawString.replaceAll("\\" + GROUPING_SEPARATOR, "");
58      boolean useDecimalDelimiter = instructions.getFixedFormatDecimalData().isUseDecimalDelimiter();
59  
60      String beforeDelimiter = rawString.substring(0, rawString.indexOf(DECIMAL_SEPARATOR));
61      String afterDelimiter = rawString.substring(rawString.indexOf(DECIMAL_SEPARATOR)+1, rawString.length());
62      if (LOG.isDebugEnabled()) {
63        LOG.debug("beforeDelimiter[" + beforeDelimiter + "], afterDelimiter[" + afterDelimiter + "]");
64      }
65  
66      int decimals = instructions.getFixedFormatDecimalData().getDecimals();
67      //trim decimals
68      afterDelimiter = StringUtils.substring(afterDelimiter, 0, decimals);
69      afterDelimiter = StringUtils.rightPad(afterDelimiter, decimals, '0');
70  
71      String delimiter = useDecimalDelimiter ? "" + instructions.getFixedFormatDecimalData().getDecimalDelimiter() : "";
72      String result = beforeDelimiter + delimiter + afterDelimiter;
73      if (LOG.isDebugEnabled()) {
74        LOG.debug("result[" + result + "]");
75      }
76      return result;
77    }
78   
79    protected String getStringToConvert(String string, FormatInstructions instructions) {
80      String toConvert;
81      boolean useDecimalDelimiter = instructions.getFixedFormatDecimalData().isUseDecimalDelimiter();
82      if(useDecimalDelimiter) {
83        char delimiter = instructions.getFixedFormatDecimalData().getDecimalDelimiter();
84        toConvert = string.replace(delimiter, '.'); //convert to normal delimiter
85      } else {
86        int decimals = instructions.getFixedFormatDecimalData().getDecimals();
87        if (decimals > 0 && string.length() >= decimals) {
88          String beforeDelimiter = string.substring(0, string.length()-decimals);
89          String afterDelimiter = string.substring(string.length()-decimals, string.length());
90          toConvert = beforeDelimiter + '.' + afterDelimiter;
91        } else {
92          toConvert = string;
93        }
94  
95      }
96      return toConvert;
97    }
98  }