1 package com.github.mygreen.supercsv.cellprocessor.constraint;
2
3 import java.util.Arrays;
4 import java.util.List;
5 import java.util.Optional;
6 import java.util.stream.Collectors;
7
8 import org.supercsv.cellprocessor.ift.CellProcessor;
9
10 import com.github.mygreen.supercsv.annotation.constraint.CsvLengthExact;
11 import com.github.mygreen.supercsv.builder.Configuration;
12 import com.github.mygreen.supercsv.builder.FieldAccessor;
13 import com.github.mygreen.supercsv.cellprocessor.ConstraintProcessorFactory;
14 import com.github.mygreen.supercsv.cellprocessor.format.TextFormatter;
15 import com.github.mygreen.supercsv.exception.SuperCsvInvalidAnnotationException;
16 import com.github.mygreen.supercsv.localization.MessageBuilder;
17
18
19
20
21
22
23
24
25
26 public class LengthExactFactory implements ConstraintProcessorFactory<CsvLengthExact> {
27
28 @Override
29 public Optional<CellProcessor> create(final CsvLengthExact anno, final Optional<CellProcessor> next,
30 final FieldAccessor field, final TextFormatter<?> formatter, final Configuration config) {
31
32 final List<Integer> requriedLength = Arrays.stream(anno.value())
33 .filter(l -> l >= 0)
34 .sorted()
35 .distinct()
36 .boxed()
37 .collect(Collectors.toList());
38
39 if(requriedLength.isEmpty()) {
40 throw new SuperCsvInvalidAnnotationException(anno, MessageBuilder.create("anno.attr.required")
41 .var("property", field.getNameWithClass())
42 .varWithAnno("anno", anno.annotationType())
43 .var("attrName", "value")
44 .format());
45 }
46
47 final LengthExactraint/LengthExact.html#LengthExact">LengthExact processor = next.map(n -> new LengthExact(requriedLength, n))
48 .orElseGet(() -> new LengthExact(requriedLength));
49 processor.setValidationMessage(anno.message());
50
51 return Optional.of(processor);
52 }
53
54 }