1
2
3
4
5
6
7
8
9
10
11 package org.csveed.bean.conversion;
12
13 import java.math.BigDecimal;
14 import java.math.BigInteger;
15 import java.nio.charset.Charset;
16 import java.text.NumberFormat;
17 import java.util.Currency;
18 import java.util.HashMap;
19 import java.util.Locale;
20 import java.util.Map;
21 import java.util.TimeZone;
22 import java.util.regex.Pattern;
23
24
25
26
27 public class DefaultConverters {
28
29
30 private Map<Class<?>, Converter> converters = new HashMap<>();
31
32
33
34
35 public DefaultConverters() {
36 registerConverters();
37 }
38
39
40
41
42
43
44
45
46
47 public Converter getConverter(Class clazz) {
48 return converters.get(clazz);
49 }
50
51
52
53
54 protected void registerConverters() {
55 addConverter(Charset.class, new CharsetConverter());
56 addConverter(Currency.class, new CurrencyConverter());
57 addConverter(Pattern.class, new PatternConverter());
58 addConverter(TimeZone.class, new TimeZoneConverter());
59 addConverter(byte[].class, new ByteArrayConverter());
60 addConverter(char[].class, new CharArrayConverter());
61 addConverter(char.class, new CharacterConverter(false));
62 addConverter(Character.class, new CharacterConverter(true));
63 addConverter(boolean.class, new CustomBooleanConverter(false));
64 addConverter(Boolean.class, new CustomBooleanConverter(true));
65 addConverter(byte.class,
66 new CustomNumberConverter(Byte.class, NumberFormat.getNumberInstance(Locale.US), false));
67 addConverter(Byte.class,
68 new CustomNumberConverter(Byte.class, NumberFormat.getNumberInstance(Locale.US), true));
69 addConverter(short.class,
70 new CustomNumberConverter(Short.class, NumberFormat.getNumberInstance(Locale.US), false));
71 addConverter(Short.class,
72 new CustomNumberConverter(Short.class, NumberFormat.getNumberInstance(Locale.US), true));
73 addConverter(int.class,
74 new CustomNumberConverter(Integer.class, NumberFormat.getNumberInstance(Locale.US), false));
75 addConverter(Integer.class,
76 new CustomNumberConverter(Integer.class, NumberFormat.getNumberInstance(Locale.US), true));
77 addConverter(long.class,
78 new CustomNumberConverter(Long.class, NumberFormat.getNumberInstance(Locale.US), false));
79 addConverter(Long.class,
80 new CustomNumberConverter(Long.class, NumberFormat.getNumberInstance(Locale.US), true));
81 addConverter(float.class,
82 new CustomNumberConverter(Float.class, NumberFormat.getNumberInstance(Locale.US), false));
83 addConverter(Float.class,
84 new CustomNumberConverter(Float.class, NumberFormat.getNumberInstance(Locale.US), true));
85 addConverter(double.class,
86 new CustomNumberConverter(Double.class, NumberFormat.getNumberInstance(Locale.US), false));
87 addConverter(Double.class,
88 new CustomNumberConverter(Double.class, NumberFormat.getNumberInstance(Locale.US), true));
89 addConverter(BigDecimal.class,
90 new CustomNumberConverter(BigDecimal.class, NumberFormat.getNumberInstance(Locale.US), true));
91 addConverter(BigInteger.class, new CustomNumberConverter(BigInteger.class, true));
92 addConverter(String.class, new StringConverter());
93 }
94
95
96
97
98
99
100
101
102
103 protected void addConverter(Class clazz, Converter converter) {
104 converters.put(clazz, converter);
105 }
106
107 }