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.CsvNumberRange;
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  /**
18   * アノテーション{@link CsvNumberRange}をハンドリングして、{@link NumberRange}を作成する。
19   *
20   * @since 2.0
21   * @author T.TSUCHIE
22   *
23   */
24  public class NumberRangeFactory<N extends Number & Comparable<N>> implements ConstraintProcessorFactory<CsvNumberRange> {
25      
26      @Override
27      public Optional<CellProcessor> create(final CsvNumberRange anno, final Optional<CellProcessor> next,
28              final FieldAccessor field, final TextFormatter<?> formatter, final Configuration config) {
29          
30          @SuppressWarnings("unchecked")
31          final TextFormatter<N> typeFormatter = (TextFormatter<N>)formatter;
32                  
33          final N min;
34          try {
35              min = typeFormatter.parse(anno.min());
36              
37          } catch(TextParseException e) {
38              throw new SuperCsvInvalidAnnotationException(anno, MessageBuilder.create("anno.attr.invalidType")
39                      .var("property", field.getNameWithClass())
40                      .varWithAnno("anno", anno.annotationType())
41                      .var("attrName", "min")
42                      .var("attrValue", anno.min())
43                      .varWithClass("type", field.getType())
44                      .var("pattern", typeFormatter.getPattern().orElseGet(null))
45                      .format(true), e);
46          }
47          
48          final N max;
49          try {
50              max = typeFormatter.parse(anno.max());
51              
52          } catch(TextParseException e) {
53              throw new SuperCsvInvalidAnnotationException(anno, MessageBuilder.create("anno.attr.invalidType")
54                      .var("property", field.getNameWithClass())
55                      .varWithAnno("anno", anno.annotationType())
56                      .var("attrName", "max")
57                      .var("attrValue", anno.max())
58                      .varWithClass("type", field.getType())
59                      .var("pattern", typeFormatter.getPattern().orElseGet(null))
60                      .format(true), e);
61          }
62          
63          if(min.compareTo(max) > 0) {
64              throw new SuperCsvInvalidAnnotationException(anno, MessageBuilder.create("anno.CsvNumberRange.minMaxWrong")
65                      .var("property", field.getNameWithClass())
66                      .varWithAnno("anno", anno.annotationType())
67                      .var("minValue", anno.min())
68                      .var("maxValue", anno.max())
69                      .format(true));
70          }
71          
72          final NumberRange<N> processor = next.map(n -> new NumberRange<N>(min, max, anno.inclusive(), typeFormatter, n))
73                  .orElseGet(() -> new NumberRange<N>(min, max, anno.inclusive(), typeFormatter));
74          
75          processor.setValidationMessage(anno.message());
76          
77          return Optional.of(processor);
78      }
79      
80  }