1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.validator.routines;
18
19 import java.text.DecimalFormat;
20 import java.text.DecimalFormatSymbols;
21 import java.text.Format;
22 import java.text.NumberFormat;
23 import java.util.Locale;
24
25 import org.apache.commons.validator.GenericValidator;
26
27
28
29
30
31
32
33
34
35 public abstract class AbstractNumberValidator extends AbstractFormatValidator {
36
37 private static final long serialVersionUID = -3088817875906765463L;
38
39
40 public static final int STANDARD_FORMAT = 0;
41
42
43 public static final int CURRENCY_FORMAT = 1;
44
45
46 public static final int PERCENT_FORMAT = 2;
47
48
49
50
51 private final boolean allowFractions;
52
53
54
55
56 private final int formatType;
57
58
59
60
61
62
63
64
65
66
67
68
69 public AbstractNumberValidator(final boolean strict, final int formatType, final boolean allowFractions) {
70 super(strict);
71 this.allowFractions = allowFractions;
72 this.formatType = formatType;
73 }
74
75
76
77
78
79
80
81
82 protected int determineScale(final NumberFormat format) {
83 if (!isStrict()) {
84 return -1;
85 }
86 if (!isAllowFractions() || format.isParseIntegerOnly()) {
87 return 0;
88 }
89 final int minimumFraction = format.getMinimumFractionDigits();
90 final int maximumFraction = format.getMaximumFractionDigits();
91 if (minimumFraction != maximumFraction) {
92 return -1;
93 }
94 int scale = minimumFraction;
95 if (format instanceof DecimalFormat) {
96 final int multiplier = ((DecimalFormat) format).getMultiplier();
97 if (multiplier == 100) {
98 scale += 2;
99 } else if (multiplier == 1000) {
100 scale += 3;
101 }
102 } else if (formatType == PERCENT_FORMAT) {
103 scale += 2;
104 }
105 return scale;
106 }
107
108
109
110
111
112
113
114
115 protected Format getFormat(final Locale locale) {
116 final NumberFormat formatter;
117 switch (formatType) {
118 case CURRENCY_FORMAT:
119 if (locale == null) {
120 formatter = NumberFormat.getCurrencyInstance();
121 } else {
122 formatter = NumberFormat.getCurrencyInstance(locale);
123 }
124 break;
125 case PERCENT_FORMAT:
126 if (locale == null) {
127 formatter = NumberFormat.getPercentInstance();
128 } else {
129 formatter = NumberFormat.getPercentInstance(locale);
130 }
131 break;
132 default:
133 if (locale == null) {
134 formatter = NumberFormat.getInstance();
135 } else {
136 formatter = NumberFormat.getInstance(locale);
137 }
138 if (!isAllowFractions()) {
139 formatter.setParseIntegerOnly(true);
140 }
141 break;
142 }
143 return formatter;
144 }
145
146
147
148
149
150
151
152
153
154
155 @Override
156 protected Format getFormat(final String pattern, final Locale locale) {
157 final NumberFormat formatter;
158 final boolean usePattern = !GenericValidator.isBlankOrNull(pattern);
159 if (!usePattern) {
160 formatter = (NumberFormat) getFormat(locale);
161 } else if (locale == null) {
162 formatter = new DecimalFormat(pattern);
163 } else {
164 final DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
165 formatter = new DecimalFormat(pattern, symbols);
166 }
167 if (!isAllowFractions()) {
168 formatter.setParseIntegerOnly(true);
169 }
170 return formatter;
171 }
172
173
174
175
176
177
178
179 public int getFormatType() {
180 return formatType;
181 }
182
183
184
185
186
187
188
189
190 public boolean isAllowFractions() {
191 return allowFractions;
192 }
193
194
195
196
197
198
199
200
201
202
203 public boolean isInRange(final Number value, final Number min, final Number max) {
204 return minValue(value, min) && maxValue(value, max);
205 }
206
207
208
209
210
211
212
213
214
215
216 @Override
217 public boolean isValid(final String value, final String pattern, final Locale locale) {
218 return parse(value, pattern, locale) != null;
219 }
220
221
222
223
224
225
226
227
228
229 public boolean maxValue(final Number value, final Number max) {
230 if (isAllowFractions()) {
231 return value.doubleValue() <= max.doubleValue();
232 }
233 return value.longValue() <= max.longValue();
234 }
235
236
237
238
239
240
241
242
243
244 public boolean minValue(final Number value, final Number min) {
245 if (isAllowFractions()) {
246 return value.doubleValue() >= min.doubleValue();
247 }
248 return value.longValue() >= min.longValue();
249 }
250
251
252
253
254
255
256
257
258
259
260 protected Object parse(String value, final String pattern, final Locale locale) {
261 value = value == null ? null : value.trim();
262 final String value1 = value;
263 if (GenericValidator.isBlankOrNull(value1)) {
264 return null;
265 }
266 final Format formatter = getFormat(pattern, locale);
267 return parse(value, formatter);
268
269 }
270
271
272
273
274
275
276
277
278
279
280 @Override
281 protected abstract Object processParsedValue(Object value, Format formatter);
282 }