View Javadoc
1   package com.github.mygreen.supercsv.cellprocessor.constraint;
2   
3   import java.util.Optional;
4   
5   import org.supercsv.cellprocessor.ift.CellProcessor;
6   
7   import com.github.mygreen.supercsv.annotation.constraint.CsvDateTimeMin;
8   import com.github.mygreen.supercsv.builder.Configuration;
9   import com.github.mygreen.supercsv.builder.FieldAccessor;
10  import com.github.mygreen.supercsv.cellprocessor.ConstraintProcessorFactory;
11  import com.github.mygreen.supercsv.cellprocessor.format.TextFormatter;
12  import com.github.mygreen.supercsv.cellprocessor.format.TextParseException;
13  import com.github.mygreen.supercsv.exception.SuperCsvInvalidAnnotationException;
14  import com.github.mygreen.supercsv.localization.MessageBuilder;
15  
16  /**
17   * アノテーション{@link CsvDateTimeMin}をハンドリングして、{@link DateTimeMin}を作成する。
18   * 
19   * @since 2.0
20   * @author T.TSUCHIE
21   *
22   */
23  public class DateTimeMinFactory<T extends Comparable<T>> implements ConstraintProcessorFactory<CsvDateTimeMin> {
24      
25      @Override
26      public Optional<CellProcessor> create(final CsvDateTimeMin anno, final Optional<CellProcessor> next,
27              final FieldAccessor field, final TextFormatter<?> formatter, final Configuration config) {
28          
29          @SuppressWarnings("unchecked")
30          final TextFormatter<T> typeFormatter = (TextFormatter<T>)formatter;
31          
32          final T min;
33          try {
34              min = typeFormatter.parse(anno.value());
35              
36          } catch(TextParseException e) {
37              throw new SuperCsvInvalidAnnotationException(anno, MessageBuilder.create("anno.attr.invalidType")
38                      .var("property", field.getNameWithClass())
39                      .varWithAnno("anno", anno.annotationType())
40                      .var("attrName", "value")
41                      .var("attrValue", anno.value())
42                      .varWithClass("type", field.getType())
43                      .var("pattern", typeFormatter.getPattern().orElseGet(null))
44                      .format(true), e);
45          }
46          
47          final DateTimeMin<T> processor = next.map(n -> new DateTimeMin<T>(min, anno.inclusive(), typeFormatter, n))
48                  .orElseGet(() -> new DateTimeMin<T>(min, anno.inclusive(), typeFormatter));
49          processor.setValidationMessage(anno.message());
50          
51          return Optional.of(processor);
52      }
53      
54  }