View Javadoc
1   package org.csveed.bean.conversion;
2   
3   import java.math.BigDecimal;
4   import java.math.BigInteger;
5   import java.nio.charset.Charset;
6   import java.text.NumberFormat;
7   import java.util.Currency;
8   import java.util.HashMap;
9   import java.util.Locale;
10  import java.util.Map;
11  import java.util.TimeZone;
12  import java.util.regex.Pattern;
13  
14  public class DefaultConverters {
15  
16      private Map<Class<?>, Converter> converters = new HashMap<>();
17  
18      public DefaultConverters() {
19          registerConverters();
20      }
21  
22      public Converter getConverter(Class clazz) {
23          return converters.get(clazz);
24      }
25  
26      protected void registerConverters() {
27          addConverter(Charset.class, new CharsetConverter());
28          addConverter(Currency.class, new CurrencyConverter());
29          addConverter(Pattern.class, new PatternConverter());
30          addConverter(TimeZone.class, new TimeZoneConverter());
31          addConverter(byte[].class, new ByteArrayConverter());
32          addConverter(char[].class, new CharArrayConverter());
33          addConverter(char.class, new CharacterConverter(false));
34          addConverter(Character.class, new CharacterConverter(true));
35          addConverter(boolean.class, new CustomBooleanConverter(false));
36          addConverter(Boolean.class, new CustomBooleanConverter(true));
37          addConverter(byte.class, new CustomNumberConverter(Byte.class, NumberFormat.getNumberInstance(Locale.US), false));
38          addConverter(Byte.class, new CustomNumberConverter(Byte.class, NumberFormat.getNumberInstance(Locale.US), true));
39          addConverter(short.class, new CustomNumberConverter(Short.class, NumberFormat.getNumberInstance(Locale.US), false));
40          addConverter(Short.class, new CustomNumberConverter(Short.class, NumberFormat.getNumberInstance(Locale.US), true));
41          addConverter(int.class, new CustomNumberConverter(Integer.class, NumberFormat.getNumberInstance(Locale.US), false));
42          addConverter(Integer.class, new CustomNumberConverter(Integer.class, NumberFormat.getNumberInstance(Locale.US), true));
43          addConverter(long.class, new CustomNumberConverter(Long.class, NumberFormat.getNumberInstance(Locale.US), false));
44          addConverter(Long.class, new CustomNumberConverter(Long.class, NumberFormat.getNumberInstance(Locale.US), true));
45          addConverter(float.class, new CustomNumberConverter(Float.class, NumberFormat.getNumberInstance(Locale.US), false));
46          addConverter(Float.class, new CustomNumberConverter(Float.class, NumberFormat.getNumberInstance(Locale.US), true));
47          addConverter(double.class, new CustomNumberConverter(Double.class, NumberFormat.getNumberInstance(Locale.US), false));
48          addConverter(Double.class, new CustomNumberConverter(Double.class, NumberFormat.getNumberInstance(Locale.US), true));
49          addConverter(BigDecimal.class, new CustomNumberConverter(BigDecimal.class, NumberFormat.getNumberInstance(Locale.US), true));
50          addConverter(BigInteger.class, new CustomNumberConverter(BigInteger.class, true));
51          addConverter(String.class, new StringConverter());
52      }
53  
54      protected void addConverter(Class clazz, Converter converter) {
55          converters.put(clazz, converter);
56      }
57  
58  }