1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.validator.validwhen;
22
23 import java.math.BigDecimal;
24 import java.util.HashMap;
25 import java.util.Map;
26
27
28
29
30
31
32
33
34 public enum ValidWhenComparison {
35
36 LESS_EQUAL(ValidWhenParser.LESSEQUALSIGN) {
37 @Override
38 protected boolean compareTo(int compareTo) {
39 return compareTo <= 0;
40 }
41 },
42
43 LESS_THAN(ValidWhenParser.LESSTHANSIGN) {
44 @Override
45 protected boolean compareTo(int compareTo) {
46 return compareTo < 0;
47 }
48 },
49
50 EQUAL(ValidWhenParser.EQUALSIGN) {
51 @Override
52 protected boolean compareWithNull(Object v1, Object v2) {
53 return v1 == v2;
54 }
55
56 @Override
57 protected boolean compareTo(int compareTo) {
58 return compareTo == 0;
59 }
60 },
61
62 GREATER_THAN(ValidWhenParser.GREATERTHANSIGN) {
63 @Override
64 protected boolean compareTo(int compareTo) {
65 return compareTo > 0;
66 }
67 },
68
69 GREATER_EQUAL(ValidWhenParser.GREATEREQUALSIGN) {
70 @Override
71 protected boolean compareTo(int compareTo) {
72 return compareTo >= 0;
73 }
74 },
75
76 NOT_EQUAL(ValidWhenParser.NOTEQUALSIGN) {
77 @Override
78 protected boolean compareWithNull(Object v1, Object v2) {
79 return v1 != v2;
80 }
81
82 @Override
83 protected boolean compareTo(int compareTo) {
84 return compareTo != 0;
85 }
86 };
87
88
89
90
91 private final static Map<Integer, ValidWhenComparison> COMP_MAPPER = new HashMap<>();
92
93 static {
94 for (ValidWhenComparison comp : ValidWhenComparison.values()) {
95 COMP_MAPPER.put(comp.tokenNum, comp);
96 }
97 }
98
99
100
101
102
103
104
105
106
107
108 public static ValidWhenComparison getComp(int tokenNum) {
109 return COMP_MAPPER.get(tokenNum);
110 }
111
112
113
114
115 private final int tokenNum;
116
117
118
119
120
121
122 private ValidWhenComparison(int tokenNum) {
123 this.tokenNum = tokenNum;
124 }
125
126
127
128
129
130
131
132
133
134
135
136 protected boolean compareWithNull(Object v1, Object v2) {
137 return false;
138 }
139
140
141
142
143
144
145
146
147
148
149
150 protected <T extends Comparable<T>> boolean compareTo(T v1, T v2) {
151 return compareTo(v1.compareTo(v2));
152 }
153
154
155
156
157
158
159
160
161
162
163
164 protected abstract boolean compareTo(int compareTo);
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 public boolean compare(ValidWhenResult<?> v1, ValidWhenResult<?> v2) {
186 Object o1 = v1.getValue();
187 Object o2 = v2.getValue();
188
189 if (o1 == null || o2 == null) {
190 return compareWithNull(o1, o2);
191 }
192
193 try {
194 BigDecimal i1 = convertBigDecimal(o1);
195 BigDecimal i2 = convertBigDecimal(o2);
196
197 return compareTo(i1, i2);
198 } catch (NumberFormatException e) {
199
200 }
201
202 String s1 = v1.toString();
203 String s2 = v2.toString();
204 return compareTo(s1, s2);
205 }
206
207
208
209
210
211
212
213
214
215
216
217 private static BigDecimal convertBigDecimal(Object val) {
218 if (val instanceof BigDecimal) {
219 return (BigDecimal)val;
220 } else if (val instanceof Integer) {
221 return new BigDecimal((Integer)val);
222 } else {
223 return new BigDecimal(val.toString());
224 }
225 }
226 }