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.CsvLengthMin;
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
18
19
20
21
22
23 public class LengthMinFactory implements ConstraintProcessorFactory<CsvLengthMin> {
24
25 @Override
26 public Optional<CellProcessor> create(final CsvLengthMin anno, final Optional<CellProcessor> next,
27 final FieldAccessor field, final TextFormatter<?> formatter, final Configuration config) {
28
29 final int min = anno.value();
30
31 if(min < 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", min)
37 .var("min", 0)
38 .format());
39 }
40
41 final LengthMinstraint/LengthMin.html#LengthMin">LengthMin processor = next.map(n -> new LengthMin(min, n))
42 .orElseGet(() -> new LengthMin(min));
43 processor.setValidationMessage(anno.message());
44
45 return Optional.of(processor);
46 }
47
48 }