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.CsvDateTimeMax;
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 CsvDateTimeMax}をハンドリングして、{@link DateTimeMax}を作成する。
18   * 
19   * @since 2.0
20   * @author T.TSUCHIE
21   *
22   */
23  public class DateTimeMaxFactory<T extends Comparable<T>> implements ConstraintProcessorFactory<CsvDateTimeMax> {
24      
25      @Override
26      public Optional<CellProcessor> create(final CsvDateTimeMax 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 max;
33          try {
34              max = 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 DateTimeMax<T> processor = next.map(n -> new DateTimeMax<T>(max, anno.inclusive(), typeFormatter, n))
48                  .orElseGet(() -> new DateTimeMax<T>(max, anno.inclusive(), typeFormatter));
49          processor.setValidationMessage(anno.message());
50          
51          return Optional.of(processor);
52      }
53      
54  }