View Javadoc
1   package com.github.mygreen.supercsv.builder;
2   
3   import java.math.BigDecimal;
4   import java.math.BigInteger;
5   import java.sql.Time;
6   import java.sql.Timestamp;
7   import java.time.LocalDate;
8   import java.time.LocalDateTime;
9   import java.time.LocalTime;
10  import java.time.MonthDay;
11  import java.time.OffsetDateTime;
12  import java.time.OffsetTime;
13  import java.time.Year;
14  import java.time.YearMonth;
15  import java.time.ZonedDateTime;
16  import java.util.Calendar;
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import com.github.mygreen.supercsv.builder.standard.BigDecimalProcessorBuilder;
21  import com.github.mygreen.supercsv.builder.standard.BigIntegerProcessorBuilder;
22  import com.github.mygreen.supercsv.builder.standard.BooleanProcessorBuilder;
23  import com.github.mygreen.supercsv.builder.standard.ByteProcessorBuilder;
24  import com.github.mygreen.supercsv.builder.standard.CalendarProcessorBuilder;
25  import com.github.mygreen.supercsv.builder.standard.CharacterProcessorBuilder;
26  import com.github.mygreen.supercsv.builder.standard.DateProcessorBuilder;
27  import com.github.mygreen.supercsv.builder.standard.DoubleProcessorBuilder;
28  import com.github.mygreen.supercsv.builder.standard.EnumProcessorBuilder;
29  import com.github.mygreen.supercsv.builder.standard.FloatProcessorBuilder;
30  import com.github.mygreen.supercsv.builder.standard.IntegerProcessorBuilder;
31  import com.github.mygreen.supercsv.builder.standard.LongProcessorBuilder;
32  import com.github.mygreen.supercsv.builder.standard.ShortProcessorBuilder;
33  import com.github.mygreen.supercsv.builder.standard.SqlDateProcessorBuilder;
34  import com.github.mygreen.supercsv.builder.standard.StringProcessorBuilder;
35  import com.github.mygreen.supercsv.builder.standard.TimeProcessorBuilder;
36  import com.github.mygreen.supercsv.builder.standard.TimestampProcessorBuilder;
37  import com.github.mygreen.supercsv.builder.time.LocalDateProcessorBuilder;
38  import com.github.mygreen.supercsv.builder.time.LocalDateTimeProcessorBuilder;
39  import com.github.mygreen.supercsv.builder.time.LocalTimeProcessorBuilder;
40  import com.github.mygreen.supercsv.builder.time.MonthDayProcessorBuilder;
41  import com.github.mygreen.supercsv.builder.time.OffsetDateTimeProcessorBuilder;
42  import com.github.mygreen.supercsv.builder.time.OffsetTimeProcessorBuilder;
43  import com.github.mygreen.supercsv.builder.time.YearMonthProcessorBuilder;
44  import com.github.mygreen.supercsv.builder.time.YearProcessorBuilder;
45  import com.github.mygreen.supercsv.builder.time.ZonedDateTimeProcessorBuilder;
46  import com.github.mygreen.supercsv.util.Utils;
47  
48  /**
49   * 各タイプに対して、登録された{@link ProcessorBuilder}を解決するクラス。
50   * 
51   * @version 2.1
52   * @author T.TSUCHIE
53   *
54   */
55  public class ProcessorBuilderResolver {
56      
57      private Map<Class<?>, ProcessorBuilder<?>> builderMap = new HashMap<>();
58      
59      /**
60       * デフォルトコンストラクタ。
61       * <p>{@link #init()}メソッドが呼ばれる。
62       */
63      public ProcessorBuilderResolver() {
64          init();
65      }
66      
67      /**
68       * 標準の{@link ProcessorBuilder}を登録緒する。
69       */
70      @SuppressWarnings({"unchecked", "rawtypes"})
71      public void init() {
72          
73          register(String.class, new StringProcessorBuilder());
74          
75          register(Character.class, new CharacterProcessorBuilder());
76          register(char.class, new CharacterProcessorBuilder());
77          
78          register(Boolean.class, new BooleanProcessorBuilder());
79          register(boolean.class, new BooleanProcessorBuilder());
80          
81          register(Byte.class, new ByteProcessorBuilder());
82          register(byte.class, new ByteProcessorBuilder());
83          register(Short.class, new ShortProcessorBuilder());
84          register(short.class, new ShortProcessorBuilder());
85          register(Integer.class, new IntegerProcessorBuilder());
86          register(int.class, new IntegerProcessorBuilder());
87          register(Long.class, new LongProcessorBuilder());
88          register(long.class, new LongProcessorBuilder());
89          register(Float.class, new FloatProcessorBuilder());
90          register(float.class, new FloatProcessorBuilder());
91          register(Double.class, new DoubleProcessorBuilder());
92          register(double.class, new DoubleProcessorBuilder());
93          
94          register(BigDecimal.class, new BigDecimalProcessorBuilder());
95          register(BigInteger.class, new BigIntegerProcessorBuilder());
96          
97          register(Calendar.class, new CalendarProcessorBuilder());
98          register(java.util.Date.class, new DateProcessorBuilder());
99          register(java.sql.Date.class, new SqlDateProcessorBuilder());
100         register(Time.class, new TimeProcessorBuilder());
101         register(Timestamp.class, new TimestampProcessorBuilder());
102         
103         register(Enum.class, new EnumProcessorBuilder());
104         
105         // Java8 Date and Tiem API
106         register(LocalDateTime.class, new LocalDateTimeProcessorBuilder());
107         register(LocalDate.class, new LocalDateProcessorBuilder());
108         register(LocalTime.class, new LocalTimeProcessorBuilder());
109         register(ZonedDateTime.class, new ZonedDateTimeProcessorBuilder());
110         register(OffsetDateTime.class, new OffsetDateTimeProcessorBuilder());
111         register(OffsetTime.class, new OffsetTimeProcessorBuilder());
112         register(Year.class, new YearProcessorBuilder());
113         register(YearMonth.class, new YearMonthProcessorBuilder());
114         register(MonthDay.class, new MonthDayProcessorBuilder());
115         
116         // Joda-Time
117         if(Utils.isEnabledJodaTime()) {
118             register(org.joda.time.LocalDateTime.class, new com.github.mygreen.supercsv.builder.joda.LocalDateTimeProcessorBuilder());
119             register(org.joda.time.LocalDate.class, new com.github.mygreen.supercsv.builder.joda.LocalDateProcessorBuilder());
120             register(org.joda.time.LocalTime.class, new com.github.mygreen.supercsv.builder.joda.LocalTimeProcessorBuilder());
121             register(org.joda.time.DateTime.class, new com.github.mygreen.supercsv.builder.joda.DateTimeProcessorBuilder());
122             register(org.joda.time.YearMonth.class, new com.github.mygreen.supercsv.builder.joda.YearMonthProcessorBuilder());
123             register(org.joda.time.MonthDay.class, new com.github.mygreen.supercsv.builder.joda.MonthDayProcessorBuilder());
124         }
125         
126     }
127     
128     /**
129      * 指定したクラスタイプに対する{@link ProcessorBuilder}を取得する。
130      * 
131      * @param <T> 対象のオブジェクトタイプ
132      * @param type クラスタイプ。
133      * @return 解決できない場合は、{@literal null}を返す。
134      */
135     @SuppressWarnings("unchecked")
136     public <T> ProcessorBuilder<T> resolve(final Class<T> type) {
137         
138         ProcessorBuilder<?> builder = builderMap.get(type);
139         if(builder == null && Enum.class.isAssignableFrom(type)) {
140             // 列挙型の場合は、具象化されたクラスタイプである必要があるため、別途取得する
141             builder = builderMap.get(Enum.class);
142         }
143         
144         return (ProcessorBuilder<T>) builder;
145     }
146     
147     /**
148      * {@link ProcessorBuilder}を登録する。
149      * <p>既に登録済みのものがある場合、新しい値に置き換えられます。
150      * 
151      * @param <T> 対象のオブジェクトタイプ
152      * @param type クラスタイプ。
153      * @param builder {@link ProcessorBuilder}の実装。
154      * @return 以前に登録されている値を返す。登録済みのものが内場合は{@literal null}を返す。
155      */
156     @SuppressWarnings("unchecked")
157     public <T> ProcessorBuilder<T> register(final Class<T> type, final ProcessorBuilder<T> builder) {
158         return (ProcessorBuilder<T>)this.builderMap.put(type, builder);
159     }
160     
161 }