1 package com.gh.mygreen.xlsmapper.cellconverter.impl;
2
3 import java.util.Optional;
4
5 import org.apache.poi.ss.usermodel.Cell;
6
7 import com.gh.mygreen.xlsmapper.Configuration;
8 import com.gh.mygreen.xlsmapper.cellconverter.BaseCellConverter;
9 import com.gh.mygreen.xlsmapper.cellconverter.CellConverter;
10 import com.gh.mygreen.xlsmapper.cellconverter.CellConverterFactory;
11 import com.gh.mygreen.xlsmapper.cellconverter.CellConverterFactorySupport;
12 import com.gh.mygreen.xlsmapper.cellconverter.TypeBindException;
13 import com.gh.mygreen.xlsmapper.fieldaccessor.FieldAccessor;
14 import com.gh.mygreen.xlsmapper.textformatter.TextFormatter;
15 import com.gh.mygreen.xlsmapper.textformatter.TextParseException;
16
17
18
19
20
21
22
23
24 public class CharacterCellConverterFactory extends CellConverterFactorySupport<Character>
25 implements CellConverterFactory<Character> {
26
27 private static final char DEFAULT_VALUE = '\u0000';;
28
29 @Override
30 public CharacterCellConverter create(final FieldAccessor field, final Configuration config) {
31
32 final CharacterCellConverter cellConverter = new CharacterCellConverter(field, config);
33 setupCellConverter(cellConverter, field, config);
34
35 return cellConverter;
36 }
37
38 @Override
39 protected void setupCustom(final BaseCellConverter<Character> cellConverter, final FieldAccessor field, final Configuration config) {
40
41
42 }
43
44 @Override
45 protected TextFormatter<Character> createTextFormatter(final FieldAccessor field, final Configuration config) {
46
47 return new TextFormatter<Character>() {
48
49 @Override
50 public Character parse(final String text) throws TextParseException {
51
52 if(text.length() >= 1) {
53 return text.charAt(0);
54 }
55
56
57 if(field.getType().isPrimitive()) {
58 return DEFAULT_VALUE;
59
60 } else if(field.isComponentType() && field.getComponentType().isPrimitive()) {
61 return DEFAULT_VALUE;
62 }
63
64 return null;
65 }
66
67 @Override
68 public String format(final Character value) {
69 return value.toString();
70 }
71 };
72 }
73
74 public class CharacterCellConverter extends BaseCellConverter<Character> {
75
76 private CharacterCellConverter(final FieldAccessor field, final Configuration config) {
77 super(field, config);
78 }
79
80 @Override
81 protected Character parseCell(final Cell evaluatedCell, final String formattedValue) throws TypeBindException {
82
83 return this.textFormatter.parse(formattedValue);
84 }
85
86 @Override
87 protected void setupCell(final Cell cell, final Optional<Character> cellValue) throws TypeBindException {
88
89
90 if(cellValue.isPresent() && cellValue.get() != DEFAULT_VALUE) {
91 cell.setCellValue(cellValue.get().toString());
92
93 } else {
94 cell.setBlank();
95 }
96 }
97
98 }
99
100 }