1 package com.gh.mygreen.xlsmapper.cellconverter.impl;
2
3 import java.lang.reflect.Array;
4 import java.util.Arrays;
5 import java.util.List;
6 import java.util.Optional;
7
8 import org.apache.poi.ss.usermodel.Cell;
9
10 import com.gh.mygreen.xlsmapper.Configuration;
11 import com.gh.mygreen.xlsmapper.cellconverter.BaseCellConverter;
12 import com.gh.mygreen.xlsmapper.cellconverter.CellConverter;
13 import com.gh.mygreen.xlsmapper.cellconverter.CellConverterFactory;
14 import com.gh.mygreen.xlsmapper.cellconverter.CellConverterFactorySupport;
15 import com.gh.mygreen.xlsmapper.cellconverter.TypeBindException;
16 import com.gh.mygreen.xlsmapper.fieldaccessor.FieldAccessor;
17 import com.gh.mygreen.xlsmapper.textformatter.TextFormatter;
18 import com.gh.mygreen.xlsmapper.textformatter.TextParseException;
19
20
21
22
23
24
25
26
27 public class ArrayCellConverterFactory extends CellConverterFactorySupport<Object[]>
28 implements CellConverterFactory<Object[]>{
29
30 private ListCellConverterFactoryterFactory.html#ListCellConverterFactory">ListCellConverterFactory listCellConverterFactory = new ListCellConverterFactory();
31
32 @Override
33 public ArrayCellConverter create(final FieldAccessor field, final Configuration config) {
34
35 final ArrayCellConverter cellConverter = new ArrayCellConverter(field, config);
36 setupCellConverter(cellConverter, field, config);
37
38 return cellConverter;
39 }
40
41 @Override
42 protected void setupCustom(final BaseCellConverter<Object[]> cellConverter, final FieldAccessor field,
43 final Configuration config) {
44
45 }
46
47 @SuppressWarnings("rawtypes")
48 @Override
49 protected TextFormatter<Object[]> createTextFormatter(final FieldAccessor field, final Configuration config) {
50
51 TextFormatter<List> listFormatter = listCellConverterFactory.createTextFormatter(field, config);
52
53 return new TextFormatter<Object[]>() {
54
55 @SuppressWarnings("unchecked")
56 @Override
57 public Object[] parse(String text) throws TextParseException {
58 List list = listFormatter.parse(text);
59 return list.toArray((Object[])Array.newInstance(field.getComponentType(), list.size()));
60 }
61
62 @Override
63 public String format(Object[] value) {
64 return listFormatter.format(Arrays.asList(value));
65 }
66 };
67 }
68
69 public class ArrayCellConverter extends BaseCellConverter<Object[]> {
70
71 private ArrayCellConverter(final FieldAccessor field, final Configuration config) {
72 super(field, config);
73 }
74
75 @Override
76 protected Object[] parseCell(final Cell evaluatedCell, final String formattedValue) throws TypeBindException {
77 try {
78 return this.textFormatter.parse(formattedValue);
79 } catch(TextParseException e) {
80 throw newTypeBindExceptionOnParse(e, evaluatedCell, formattedValue);
81 }
82 }
83
84 @Override
85 protected void setupCell(final Cell cell, final Optional<Object[]> cellValue) throws TypeBindException {
86
87 if(cellValue.isPresent()) {
88 cell.setCellValue(textFormatter.format(cellValue.get()));
89 } else {
90 cell.setBlank();
91 }
92
93 }
94
95 }
96
97 }