ArrayCellConverterFactory.java

  1. package com.gh.mygreen.xlsmapper.cellconverter.impl;

  2. import java.lang.reflect.Array;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Optional;

  6. import org.apache.poi.ss.usermodel.Cell;

  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.  * 配列型を処理する{@link CellConverter}を作成するためのファクトリクラス。
  18.  *
  19.  * @since 2.0
  20.  * @author T.TSUCHIE
  21.  *
  22.  */
  23. public class ArrayCellConverterFactory extends CellConverterFactorySupport<Object[]>
  24.         implements CellConverterFactory<Object[]>{
  25.    
  26.     private ListCellConverterFactory listCellConverterFactory = new ListCellConverterFactory();
  27.    
  28.     @Override
  29.     public ArrayCellConverter create(final FieldAccessor field, final Configuration config) {
  30.        
  31.         final ArrayCellConverter cellConverter = new ArrayCellConverter(field, config);
  32.         setupCellConverter(cellConverter, field, config);
  33.        
  34.         return cellConverter;
  35.     }
  36.    
  37.     @Override
  38.     protected void setupCustom(final BaseCellConverter<Object[]> cellConverter, final FieldAccessor field,
  39.             final Configuration config) {
  40.         // 何もしない
  41.     }
  42.    
  43.     @SuppressWarnings("rawtypes")
  44.     @Override
  45.     protected TextFormatter<Object[]> createTextFormatter(final FieldAccessor field, final Configuration config) {
  46.        
  47.         TextFormatter<List> listFormatter = listCellConverterFactory.createTextFormatter(field, config);
  48.        
  49.         return new TextFormatter<Object[]>() {
  50.            
  51.             @SuppressWarnings("unchecked")
  52.             @Override
  53.             public Object[] parse(String text) throws TextParseException {
  54.                 List list = listFormatter.parse(text);
  55.                 return list.toArray((Object[])Array.newInstance(field.getComponentType(), list.size()));
  56.             }
  57.            
  58.             @Override
  59.             public String format(Object[] value) {
  60.                 return listFormatter.format(Arrays.asList(value));
  61.             }
  62.         };
  63.     }
  64.    
  65.     public class ArrayCellConverter extends BaseCellConverter<Object[]> {
  66.        
  67.         private ArrayCellConverter(final FieldAccessor field, final Configuration config) {
  68.             super(field, config);
  69.         }
  70.        
  71.         @Override
  72.         protected Object[] parseCell(final Cell evaluatedCell, final String formattedValue) throws TypeBindException {
  73.             try {
  74.                 return this.textFormatter.parse(formattedValue);
  75.             } catch(TextParseException e) {
  76.                 throw newTypeBindExceptionOnParse(e, evaluatedCell, formattedValue);
  77.             }
  78.         }
  79.        
  80.         @Override
  81.         protected void setupCell(final Cell cell, final Optional<Object[]> cellValue) throws TypeBindException {
  82.            
  83.             if(cellValue.isPresent()) {
  84.                 cell.setCellValue(textFormatter.format(cellValue.get()));
  85.             } else {
  86.                 cell.setBlank();
  87.             }
  88.            
  89.         }
  90.        
  91.     }
  92.    
  93. }