1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.commons.convert; |
20 | |
|
21 | |
import java.math.BigDecimal; |
22 | |
import java.math.BigInteger; |
23 | |
import java.text.DecimalFormat; |
24 | |
import java.text.NumberFormat; |
25 | |
import java.text.ParseException; |
26 | |
import java.util.Locale; |
27 | |
import java.util.TimeZone; |
28 | |
|
29 | |
|
30 | 2 | public class NumberConverters implements ConverterLoader { |
31 | |
|
32 | 1 | protected static final Class<?>[] classArray = {BigDecimal.class, BigInteger.class, Byte.class, Double.class, Integer.class, Float.class, Long.class, Short.class}; |
33 | |
|
34 | |
protected static Number fromString(String str, NumberFormat nf) throws ConversionException { |
35 | |
try { |
36 | 6 | return nf.parse(str); |
37 | 0 | } catch (ParseException e) { |
38 | 0 | throw new ConversionException(e); |
39 | |
} |
40 | |
} |
41 | |
|
42 | |
protected static <S, T> void registerConverter(Converter<S, T> converter) { |
43 | 160 | if (converter.getSourceClass() != converter.getTargetClass()) { |
44 | 144 | Converters.registerConverter(converter); |
45 | |
} |
46 | 160 | } |
47 | |
|
48 | |
@SuppressWarnings("unchecked") |
49 | |
public void loadConverters() { |
50 | 2 | Converters.loadContainedConverters(NumberConverters.class); |
51 | 18 | for (Class<?> sourceClass : classArray) { |
52 | 16 | registerConverter(new GenericNumberToBigDecimal(sourceClass)); |
53 | 16 | registerConverter(new GenericNumberToBigInteger(sourceClass)); |
54 | 16 | registerConverter(new GenericNumberToByte(sourceClass)); |
55 | 16 | registerConverter(new GenericNumberToDouble(sourceClass)); |
56 | 16 | registerConverter(new GenericNumberToInteger(sourceClass)); |
57 | 16 | registerConverter(new GenericNumberToFloat(sourceClass)); |
58 | 16 | registerConverter(new GenericNumberToLong(sourceClass)); |
59 | 16 | registerConverter(new GenericNumberToShort(sourceClass)); |
60 | 16 | registerConverter(new GenericSingletonToList(sourceClass)); |
61 | 16 | registerConverter(new GenericSingletonToSet(sourceClass)); |
62 | |
} |
63 | 2 | } |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | 16 | public static abstract class AbstractNumberToStringConverter<N extends Number> extends AbstractLocalizedConverter<N, String> { |
70 | |
public AbstractNumberToStringConverter(Class<N> sourceClass) { |
71 | 16 | super(sourceClass, String.class); |
72 | 16 | } |
73 | |
|
74 | |
public String convert(N obj) throws ConversionException { |
75 | 8 | return obj.toString(); |
76 | |
} |
77 | |
|
78 | |
public String convert(N obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException { |
79 | 8 | if (formatString == null) { |
80 | 8 | return format(obj, NumberFormat.getNumberInstance(locale)); |
81 | |
} else { |
82 | 0 | return format(obj, new DecimalFormat(formatString)); |
83 | |
} |
84 | |
} |
85 | |
|
86 | |
protected abstract String format(N obj, NumberFormat nf) throws ConversionException; |
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | 6 | public static abstract class AbstractStringToNumberConverter<N extends Number> extends AbstractLocalizedConverter<String, N> { |
94 | |
public AbstractStringToNumberConverter(Class<N> targetClass) { |
95 | 12 | super(String.class, targetClass); |
96 | 12 | } |
97 | |
|
98 | |
protected abstract N convert(Number number) throws ConversionException; |
99 | |
|
100 | |
public N convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException { |
101 | 6 | if (formatString == null) { |
102 | 6 | return convert(fromString(obj, NumberFormat.getNumberInstance(locale))); |
103 | |
} else { |
104 | 0 | return convert(fromString(obj, new DecimalFormat(formatString))); |
105 | |
} |
106 | |
} |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | 1 | public static class BigDecimalToString extends AbstractNumberToStringConverter<BigDecimal> { |
114 | |
public BigDecimalToString() { |
115 | 2 | super(BigDecimal.class); |
116 | 2 | } |
117 | |
|
118 | |
protected String format(BigDecimal obj, NumberFormat nf) throws ConversionException { |
119 | 1 | return nf.format(obj.doubleValue()); |
120 | |
} |
121 | |
} |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | 1 | public static class BigIntegerToString extends AbstractNumberToStringConverter<BigInteger> { |
128 | |
public BigIntegerToString() { |
129 | 2 | super(BigInteger.class); |
130 | 2 | } |
131 | |
|
132 | |
protected String format(BigInteger obj, NumberFormat nf) throws ConversionException { |
133 | 1 | return nf.format(obj.doubleValue()); |
134 | |
} |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | 1 | public static class ByteToString extends AbstractNumberToStringConverter<Byte> { |
142 | |
public ByteToString() { |
143 | 2 | super(Byte.class); |
144 | 2 | } |
145 | |
|
146 | |
protected String format(Byte obj, NumberFormat nf) throws ConversionException { |
147 | 1 | return nf.format(obj.floatValue()); |
148 | |
} |
149 | |
} |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | 1 | public static class DoubleToString extends AbstractNumberToStringConverter<Double> { |
156 | |
public DoubleToString() { |
157 | 2 | super(Double.class); |
158 | 2 | } |
159 | |
|
160 | |
protected String format(Double obj, NumberFormat nf) throws ConversionException { |
161 | 1 | return nf.format(obj.doubleValue()); |
162 | |
} |
163 | |
} |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | 1 | public static class FloatToString extends AbstractNumberToStringConverter<Float> { |
170 | |
public FloatToString() { |
171 | 2 | super(Float.class); |
172 | 2 | } |
173 | |
|
174 | |
protected String format(Float obj, NumberFormat nf) throws ConversionException { |
175 | 1 | return nf.format(obj.floatValue()); |
176 | |
} |
177 | |
} |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | 6 | public static class GenericNumberToBigDecimal<N extends Number> extends AbstractConverter<N, BigDecimal> { |
184 | |
public GenericNumberToBigDecimal(Class<N> sourceClass) { |
185 | 16 | super(sourceClass, BigDecimal.class); |
186 | 16 | } |
187 | |
|
188 | |
public BigDecimal convert(N obj) throws ConversionException { |
189 | 6 | return new BigDecimal(obj.doubleValue()); |
190 | |
} |
191 | |
} |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | 7 | public static class GenericNumberToBigInteger<N extends Number> extends AbstractConverter<N, BigInteger> { |
198 | |
public GenericNumberToBigInteger(Class<N> sourceClass) { |
199 | 16 | super(sourceClass, BigInteger.class); |
200 | 16 | } |
201 | |
|
202 | |
public BigInteger convert(N obj) throws ConversionException { |
203 | 7 | return BigInteger.valueOf(obj.longValue()); |
204 | |
} |
205 | |
} |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | 7 | public static class GenericNumberToByte<N extends Number> extends AbstractConverter<N, Byte> { |
212 | |
public GenericNumberToByte(Class<N> sourceClass) { |
213 | 16 | super(sourceClass, Byte.class); |
214 | 16 | } |
215 | |
|
216 | |
public Byte convert(N obj) throws ConversionException { |
217 | 7 | return Byte.valueOf(obj.byteValue()); |
218 | |
} |
219 | |
} |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | 7 | public static class GenericNumberToDouble<N extends Number> extends AbstractConverter<N, Double> { |
226 | |
public GenericNumberToDouble(Class<N> sourceClass) { |
227 | 16 | super(sourceClass, Double.class); |
228 | 16 | } |
229 | |
|
230 | |
public Double convert(N obj) throws ConversionException { |
231 | 7 | return obj.doubleValue(); |
232 | |
} |
233 | |
} |
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | 7 | public static class GenericNumberToFloat<N extends Number> extends AbstractConverter<N, Float> { |
240 | |
public GenericNumberToFloat(Class<N> sourceClass) { |
241 | 16 | super(sourceClass, Float.class); |
242 | 16 | } |
243 | |
|
244 | |
public Float convert(N obj) throws ConversionException { |
245 | 7 | return obj.floatValue(); |
246 | |
} |
247 | |
} |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | 7 | public static class GenericNumberToInteger<N extends Number> extends AbstractConverter<N, Integer> { |
254 | |
public GenericNumberToInteger(Class<N> sourceClass) { |
255 | 16 | super(sourceClass, Integer.class); |
256 | 16 | } |
257 | |
|
258 | |
public Integer convert(N obj) throws ConversionException { |
259 | 7 | return obj.intValue(); |
260 | |
} |
261 | |
} |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | 7 | public static class GenericNumberToLong<N extends Number> extends AbstractConverter<N, Long> { |
268 | |
public GenericNumberToLong(Class<N> sourceClass) { |
269 | 16 | super(sourceClass, Long.class); |
270 | 16 | } |
271 | |
|
272 | |
public Long convert(N obj) throws ConversionException { |
273 | 7 | return obj.longValue(); |
274 | |
} |
275 | |
} |
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | 7 | public static class GenericNumberToShort<N extends Number> extends AbstractConverter<N, Short> { |
282 | |
public GenericNumberToShort(Class<N> sourceClass) { |
283 | 16 | super(sourceClass, Short.class); |
284 | 16 | } |
285 | |
|
286 | |
public Short convert(N obj) throws ConversionException { |
287 | 7 | return obj.shortValue(); |
288 | |
} |
289 | |
} |
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | 1 | public static class IntegerToString extends AbstractNumberToStringConverter<Integer> { |
296 | |
public IntegerToString() { |
297 | 2 | super(Integer.class); |
298 | 2 | } |
299 | |
|
300 | |
protected String format(Integer obj, NumberFormat nf) throws ConversionException { |
301 | 1 | return nf.format(obj.intValue()); |
302 | |
} |
303 | |
} |
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | 1 | public static class LongToBigDecimal extends AbstractConverter<Long, BigDecimal> { |
310 | |
public LongToBigDecimal() { |
311 | 2 | super(Long.class, BigDecimal.class); |
312 | 2 | } |
313 | |
|
314 | |
public BigDecimal convert(Long obj) throws ConversionException { |
315 | 1 | return BigDecimal.valueOf(obj.longValue()); |
316 | |
} |
317 | |
} |
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | 1 | public static class LongToString extends AbstractNumberToStringConverter<Long> { |
324 | |
public LongToString() { |
325 | 2 | super(Long.class); |
326 | 2 | } |
327 | |
|
328 | |
protected String format(Long obj, NumberFormat nf) throws ConversionException { |
329 | 1 | return nf.format(obj.longValue()); |
330 | |
} |
331 | |
} |
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | 1 | public static class ShortToString extends AbstractNumberToStringConverter<Short> { |
338 | |
public ShortToString() { |
339 | 2 | super(Short.class); |
340 | 2 | } |
341 | |
|
342 | |
protected String format(Short obj, NumberFormat nf) throws ConversionException { |
343 | 1 | return nf.format(obj.floatValue()); |
344 | |
} |
345 | |
} |
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | 2 | public static class StringToBigDecimal extends AbstractStringToNumberConverter<BigDecimal> { |
352 | |
public StringToBigDecimal() { |
353 | 2 | super(BigDecimal.class); |
354 | 2 | } |
355 | |
|
356 | |
protected BigDecimal convert(Number number) throws ConversionException { |
357 | 1 | return BigDecimal.valueOf(number.doubleValue()); |
358 | |
} |
359 | |
|
360 | |
public BigDecimal convert(String obj) throws ConversionException { |
361 | 1 | return BigDecimal.valueOf(Double.valueOf(obj)); |
362 | |
} |
363 | |
} |
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | 2 | public static class StringToBigInteger extends AbstractStringToNumberConverter<BigInteger> { |
370 | |
public StringToBigInteger() { |
371 | 2 | super(BigInteger.class); |
372 | 2 | } |
373 | |
|
374 | |
protected BigInteger convert(Number number) throws ConversionException { |
375 | 1 | return BigInteger.valueOf(number.longValue()); |
376 | |
} |
377 | |
|
378 | |
public BigInteger convert(String obj) throws ConversionException { |
379 | 1 | return new BigInteger(obj); |
380 | |
} |
381 | |
} |
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | 1 | public static class StringToByte extends AbstractConverter<String, Byte> { |
388 | |
public StringToByte() { |
389 | 2 | super(String.class, Byte.class); |
390 | 2 | } |
391 | |
|
392 | |
public Byte convert(String obj) throws ConversionException { |
393 | 1 | return Byte.valueOf(obj); |
394 | |
} |
395 | |
} |
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | 2 | public static class StringToDouble extends AbstractStringToNumberConverter<Double> { |
402 | |
public StringToDouble() { |
403 | 2 | super(Double.class); |
404 | 2 | } |
405 | |
|
406 | |
protected Double convert(Number number) throws ConversionException { |
407 | 1 | return number.doubleValue(); |
408 | |
} |
409 | |
|
410 | |
public Double convert(String obj) throws ConversionException { |
411 | 1 | return Double.valueOf(obj); |
412 | |
} |
413 | |
} |
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | 2 | public static class StringToFloat extends AbstractStringToNumberConverter<Float> { |
420 | |
public StringToFloat() { |
421 | 2 | super(Float.class); |
422 | 2 | } |
423 | |
|
424 | |
protected Float convert(Number number) throws ConversionException { |
425 | 1 | return number.floatValue(); |
426 | |
} |
427 | |
|
428 | |
public Float convert(String obj) throws ConversionException { |
429 | 1 | return Float.valueOf(obj); |
430 | |
} |
431 | |
} |
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | 2 | public static class StringToInteger extends AbstractStringToNumberConverter<Integer> { |
438 | |
public StringToInteger() { |
439 | 2 | super(Integer.class); |
440 | 2 | } |
441 | |
|
442 | |
protected Integer convert(Number number) throws ConversionException { |
443 | 1 | return number.intValue(); |
444 | |
} |
445 | |
|
446 | |
public Integer convert(String obj) throws ConversionException { |
447 | 1 | return Integer.valueOf(obj); |
448 | |
} |
449 | |
} |
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | 2 | public static class StringToLong extends AbstractStringToNumberConverter<Long> { |
456 | |
public StringToLong() { |
457 | 2 | super(Long.class); |
458 | 2 | } |
459 | |
|
460 | |
protected Long convert(Number number) throws ConversionException { |
461 | 1 | return number.longValue(); |
462 | |
} |
463 | |
|
464 | |
public Long convert(String obj) throws ConversionException { |
465 | 1 | return Long.valueOf(obj); |
466 | |
} |
467 | |
} |
468 | |
|
469 | |
|
470 | |
|
471 | |
|
472 | |
|
473 | 1 | public static class StringToShort extends AbstractConverter<String, Short> { |
474 | |
public StringToShort() { |
475 | 2 | super(String.class, Short.class); |
476 | 2 | } |
477 | |
|
478 | |
public Short convert(String obj) throws ConversionException { |
479 | 1 | return Short.valueOf(obj); |
480 | |
} |
481 | |
} |
482 | |
} |