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.CsvLengthMax;
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.exception.SuperCsvInvalidAnnotationException;
13  import com.github.mygreen.supercsv.localization.MessageBuilder;
14  
15  
16  /**
17   * アノテーション{@link CsvLengthMax}をハンドリングして、CellProcessorの{@link LengthMax}を作成する。
18   *
19   * @since 2.0
20   * @author T.TSUCHIE
21   *
22   */
23  public class LengthMaxFactory implements ConstraintProcessorFactory<CsvLengthMax> {
24      
25      @Override
26      public Optional<CellProcessor> create(final CsvLengthMax anno, final Optional<CellProcessor> next,
27              final FieldAccessor field, final TextFormatter<?> formatter, final Configuration config) {
28          
29          final int max = anno.value();
30          
31          if(max <= 0) {
32              throw new SuperCsvInvalidAnnotationException(anno, MessageBuilder.create("anno.attr.min")
33                      .var("property", field.getNameWithClass())
34                      .varWithAnno("anno", anno.annotationType())
35                      .var("attrName", "value")
36                      .var("attrValue", max)
37                      .var("min", 1)
38                      .format());
39          }
40          
41          final LengthMaxstraint/LengthMax.html#LengthMax">LengthMax processor = next.map(n -> new LengthMax(max, n))
42                  .orElseGet(() -> new LengthMax(max));
43          processor.setValidationMessage(anno.message());
44          
45          return Optional.of(processor);
46      }
47      
48  }