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.annotation;
17  
18  import com.ancientprogramming.fixedformat4j.format.FormatInstructions;
19  import org.apache.commons.lang.StringUtils;
20  
21  /**
22   * Sign defines where to place a sign defining a positive or negative number.
23   * Is to be used in formatters operating numbers.
24   * 
25   * @author Jacob von Eyben - http://www.ancientprogramming.com
26   * @since 1.1.0
27   */
28  public enum Sign {
29  
30    /**
31     * Doesn't do anything with signs.
32     * This just delegate to the {@link Align} defined in {@link FormatInstructions}.
33     */
34    NOSIGN {
35      public String apply(String value, FormatInstructions instructions) {
36        return instructions.getAlignment().apply(value, instructions.getLength(), instructions.getPaddingChar());
37      }
38  
39      public String remove(String value, FormatInstructions instructions) {
40        String result =  instructions.getAlignment().remove(value, instructions.getPaddingChar());
41        if (StringUtils.isEmpty(result)) {
42          result = "0";
43        }
44        return result;
45  
46      }
47    },
48  
49    /**
50     * Prepend the sign to the string
51     */
52    PREPEND {
53      public String apply(String value, FormatInstructions instructions) {
54        String sign = StringUtils.substring(value, 0, 1);
55        if ("-".equals(sign)) {
56          value = StringUtils.substring(value, 1);
57        } else {
58          sign = "+";
59        }
60        String result = instructions.getAlignment().apply(value, instructions.getLength(), instructions.getPaddingChar());
61        return sign + StringUtils.substring(result, 1);
62      }
63  
64      public String remove(String value, FormatInstructions instructions) {
65        String sign = StringUtils.substring(value, 0, 1);
66        String valueWithoutSign = StringUtils.substring(value, 1);
67        String result = instructions.getAlignment().remove(valueWithoutSign, instructions.getPaddingChar());
68        if (removeSign(instructions, sign, result)) {
69          sign = "";
70        }
71  
72        if (StringUtils.isEmpty(result)) {
73          result = "0";
74        }
75        return sign + result;
76      }
77    },
78  
79    /**
80     * Append the sign to the string
81     */
82    APPEND {
83      public String apply(String value, FormatInstructions instructions) {
84        String sign = StringUtils.substring(value, value.length()-1, value.length());
85        if ("-".equals(sign)) {
86          value = StringUtils.substring(value, 0, value.length()-1);
87        } else {
88          sign = "+";
89        }
90        String result = instructions.getAlignment().apply(value, instructions.getLength(), instructions.getPaddingChar());
91        return StringUtils.substring(result, 1) + sign;
92  
93      }
94      public String remove(String value, FormatInstructions instructions) {
95        String sign = StringUtils.substring(value, value.length()-1);
96        String valueWithoutSign = StringUtils.substring(value, 0, value.length()-1);
97        String result = instructions.getAlignment().remove(valueWithoutSign, instructions.getPaddingChar());
98        if (removeSign(instructions, sign, result)) {
99          sign = "";
100       }
101       if (StringUtils.isEmpty(result)) {
102         result = "0";
103       }
104       return sign + result;
105     }
106   };
107 
108   /**
109    *remove sign in three cases:
110    * 1. positive sign
111    * 2. the unsigned value is empty (can happen if paddingchar is 0 and the value is zero)
112    * 3. the unsigned value is 0 (can happen if paddingchar isn't 0 and the value is zero)
113    * @param instructions
114    * @param sign
115    * @param valueWithoutSign
116    * @return
117    */
118   private static boolean removeSign(FormatInstructions instructions, String sign, String valueWithoutSign) {
119     return instructions.getFixedFormatNumberData().getPositiveSign().equals(sign.charAt(0)) ||
120         StringUtils.isEmpty(valueWithoutSign) ||
121         "0".equals(valueWithoutSign);
122   }
123 
124 
125   public abstract String apply(String value, FormatInstructions instructions);
126 
127   public abstract String remove(String value, FormatInstructions instructions);
128 }