1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
23
24
25
26
27
28 public enum Sign {
29
30
31
32
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
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
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
110
111
112
113
114
115
116
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 }