1 package com.github.mygreen.supercsv.cellprocessor.conversion;
2
3 import java.util.Optional;
4
5 import org.supercsv.cellprocessor.ift.CellProcessor;
6 import org.supercsv.cellprocessor.ift.StringCellProcessor;
7
8 import com.github.mygreen.supercsv.annotation.conversion.CsvLeftPad;
9 import com.github.mygreen.supercsv.builder.Configuration;
10 import com.github.mygreen.supercsv.builder.FieldAccessor;
11 import com.github.mygreen.supercsv.cellprocessor.ConversionProcessorFactory;
12 import com.github.mygreen.supercsv.cellprocessor.format.TextFormatter;
13 import com.github.mygreen.supercsv.exception.SuperCsvInvalidAnnotationException;
14 import com.github.mygreen.supercsv.localization.MessageBuilder;
15
16
17
18
19
20
21
22
23 public class LeftPadFactory implements ConversionProcessorFactory<CsvLeftPad> {
24
25 @Override
26 public Optional<CellProcessor> create(final CsvLeftPad anno, final Optional<CellProcessor> next,
27 final FieldAccessor field, final TextFormatter<?> formatter, final Configuration config) {
28
29 if(anno.size() <= 0) {
30 throw new SuperCsvInvalidAnnotationException(anno, MessageBuilder.create("anno.attr.min")
31 .var("property", field.getNameWithClass())
32 .varWithAnno("anno", anno.annotationType())
33 .var("attrName", "size")
34 .var("attrValue", anno.size())
35 .var("min", 1)
36 .format());
37 }
38
39 final LeftPadonversion/LeftPad.html#LeftPad">LeftPad processor = next.map(n -> new LeftPad(anno.size(), anno.padChar(), (StringCellProcessor)n))
40 .orElseGet(() -> new LeftPad(anno.size(), anno.padChar()));
41
42 return Optional.of(processor);
43 }
44
45 }