LengthMinFactory.java

  1. package com.github.mygreen.supercsv.cellprocessor.constraint;

  2. import java.util.Optional;

  3. import org.supercsv.cellprocessor.ift.CellProcessor;

  4. import com.github.mygreen.supercsv.annotation.constraint.CsvLengthMin;
  5. import com.github.mygreen.supercsv.builder.Configuration;
  6. import com.github.mygreen.supercsv.builder.FieldAccessor;
  7. import com.github.mygreen.supercsv.cellprocessor.ConstraintProcessorFactory;
  8. import com.github.mygreen.supercsv.cellprocessor.format.TextFormatter;
  9. import com.github.mygreen.supercsv.exception.SuperCsvInvalidAnnotationException;
  10. import com.github.mygreen.supercsv.localization.MessageBuilder;


  11. /**
  12.  * アノテーション{@link CsvLengthMin}をハンドリングして、CellProcessorの{@link LengthMin}を作成する。
  13.  *
  14.  * @since 2.0
  15.  * @author T.TSUCHIE
  16.  *
  17.  */
  18. public class LengthMinFactory implements ConstraintProcessorFactory<CsvLengthMin> {
  19.    
  20.     @Override
  21.     public Optional<CellProcessor> create(final CsvLengthMin anno, final Optional<CellProcessor> next,
  22.             final FieldAccessor field, final TextFormatter<?> formatter, final Configuration config) {
  23.        
  24.         final int min = anno.value();
  25.        
  26.         if(min < 0) {
  27.             throw new SuperCsvInvalidAnnotationException(anno, MessageBuilder.create("anno.attr.min")
  28.                     .var("property", field.getNameWithClass())
  29.                     .varWithAnno("anno", anno.annotationType())
  30.                     .var("attrName", "value")
  31.                     .var("attrValue", min)
  32.                     .var("min", 0)
  33.                     .format());
  34.         }
  35.        
  36.         final LengthMin processor = next.map(n -> new LengthMin(min, n))
  37.                 .orElseGet(() -> new LengthMin(min));
  38.         processor.setValidationMessage(anno.message());
  39.        
  40.         return Optional.of(processor);
  41.     }
  42.    
  43. }