View Javadoc
1   /*
2    * CSVeed (https://github.com/42BV/CSVeed)
3    *
4    * Copyright 2013-2023 CSVeed.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of The Apache Software License,
8    * Version 2.0 which accompanies this distribution, and is available at
9    * https://www.apache.org/licenses/LICENSE-2.0.txt
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   * The Class DefaultConverters.
26   */
27  public class DefaultConverters {
28  
29      /** The converters. */
30      private Map<Class<?>, Converter> converters = new HashMap<>();
31  
32      /**
33       * Instantiates a new default converters.
34       */
35      public DefaultConverters() {
36          registerConverters();
37      }
38  
39      /**
40       * Gets the converter.
41       *
42       * @param clazz
43       *            the clazz
44       *
45       * @return the converter
46       */
47      public Converter getConverter(Class clazz) {
48          return converters.get(clazz);
49      }
50  
51      /**
52       * Register converters.
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       * Adds the converter.
97       *
98       * @param clazz
99       *            the clazz
100      * @param converter
101      *            the converter
102      */
103     protected void addConverter(Class clazz, Converter converter) {
104         converters.put(clazz, converter);
105     }
106 
107 }