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.CsvLengthBetween;
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 LengthBetweenFactory implements ConstraintProcessorFactory<CsvLengthBetween> {
24
25 @Override
26 public Optional<CellProcessor> create(final CsvLengthBetween anno, final Optional<CellProcessor> next,
27 final FieldAccessor field, final TextFormatter<?> formatter, final Configuration config) {
28
29 final int min = anno.min();
30 final int max = anno.max();
31
32 if(min > max) {
33 throw new SuperCsvInvalidAnnotationException(anno, MessageBuilder.create("anno.CsvLengthBetween.minMaxWrong")
34 .var("property", field.getNameWithClass())
35 .varWithAnno("anno", anno.annotationType())
36 .var("minValue", min)
37 .var("maxValue", max)
38 .format());
39 }
40
41 if(min < 0) {
42 throw new SuperCsvInvalidAnnotationException(anno, MessageBuilder.create("anno.attr.min")
43 .var("property", field.getNameWithClass())
44 .varWithAnno("anno", anno.annotationType())
45 .var("attrName", "min")
46 .var("attrValue", min)
47 .var("min", 0)
48 .format());
49 }
50
51 final LengthBetweenint/LengthBetween.html#LengthBetween">LengthBetween processor = next.map(n -> new LengthBetween(min, max, n))
52 .orElseGet(() -> new LengthBetween(min, max));
53 processor.setValidationMessage(anno.message());
54
55 return Optional.of(processor);
56 }
57
58 }