1 package com.github.mygreen.supercsv.builder.standard;
2
3 import java.util.Optional;
4
5 import com.github.mygreen.supercsv.annotation.format.CsvEnumFormat;
6 import com.github.mygreen.supercsv.builder.AbstractProcessorBuilder;
7 import com.github.mygreen.supercsv.builder.Configuration;
8 import com.github.mygreen.supercsv.builder.FieldAccessor;
9 import com.github.mygreen.supercsv.cellprocessor.format.EnumFormatter;
10
11
12
13
14
15
16
17
18
19 public class EnumProcessorBuilder<T extends Enum<T>> extends AbstractProcessorBuilder<T> {
20
21 public EnumProcessorBuilder() {
22 super();
23
24 }
25
26 @Override
27 protected void init() {
28 super.init();
29
30
31 }
32
33 @SuppressWarnings("unchecked")
34 @Override
35 protected EnumFormatter<T> getDefaultFormatter(final FieldAccessor field, final Configuration config) {
36
37 final Optional<CsvEnumFormat> formatAnno = field.getAnnotation(CsvEnumFormat.class);
38 final Optional<String> selector = formatAnno.map(a -> a.selector().isEmpty() ? null : a.selector());
39 final boolean ignoreCase = formatAnno.map(a -> a.ignoreCase()).orElse(false);
40
41 final EnumFormatter<T> formatter;
42 if(selector.isPresent()) {
43 formatter = new EnumFormatter<T>((Class<T>)field.getType(), ignoreCase, selector.get());
44 } else {
45 formatter = new EnumFormatter<T>((Class<T>)field.getType(), ignoreCase);
46 }
47
48 formatAnno.ifPresent(a -> formatter.setValidationMessage(a.message()));
49
50 return formatter;
51 }
52
53
54 }