EnumCellConverterFactory.java

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

  2. import java.util.Optional;

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

  4. import com.gh.mygreen.xlsmapper.Configuration;
  5. import com.gh.mygreen.xlsmapper.annotation.XlsEnumConverter;
  6. import com.gh.mygreen.xlsmapper.cellconverter.BaseCellConverter;
  7. import com.gh.mygreen.xlsmapper.cellconverter.CellConverter;
  8. import com.gh.mygreen.xlsmapper.cellconverter.CellConverterFactory;
  9. import com.gh.mygreen.xlsmapper.cellconverter.CellConverterFactorySupport;
  10. import com.gh.mygreen.xlsmapper.cellconverter.TypeBindException;
  11. import com.gh.mygreen.xlsmapper.fieldaccessor.FieldAccessor;
  12. import com.gh.mygreen.xlsmapper.textformatter.EnumFormatter;
  13. import com.gh.mygreen.xlsmapper.textformatter.TextFormatter;
  14. import com.gh.mygreen.xlsmapper.textformatter.TextParseException;

  15. /**
  16.  * 列挙型を処理する{@link CellConverter}を作成するためのファクトリクラス。
  17.  *
  18.  * @since 2.0
  19.  * @author T.TSUCHIE
  20.  *
  21.  */
  22. @SuppressWarnings("rawtypes")
  23. public class EnumCellConverterFactory extends CellConverterFactorySupport<Enum>
  24.         implements CellConverterFactory<Enum> {

  25.     @Override
  26.     public EnumCellConverter create(final FieldAccessor field, final Configuration config) {
  27.        
  28.         final EnumCellConverter cellConverter = new EnumCellConverter(field, config);
  29.         setupCellConverter(cellConverter, field, config);
  30.        
  31.         return cellConverter;
  32.     }
  33.    
  34.     @Override
  35.     protected void setupCustom(final BaseCellConverter<Enum> cellConverter, final FieldAccessor field, final Configuration config) {
  36.         // 何もしない
  37.        
  38.     }
  39.    
  40.     @SuppressWarnings("unchecked")
  41.     @Override
  42.     protected TextFormatter<Enum> createTextFormatter(final FieldAccessor field, final Configuration config) {
  43.        
  44.         Optional<XlsEnumConverter> converterAnno = field.getAnnotation(XlsEnumConverter.class);
  45.        
  46.         return converterAnno.map(anno -> anno.aliasMethod().isEmpty() ?
  47.                 new EnumFormatter(field.getType(), anno.ignoreCase())
  48.                 : new EnumFormatter(field.getType(), anno.ignoreCase(), anno.aliasMethod()))
  49.                 .orElseGet(() -> new EnumFormatter(field.getType()));
  50.        
  51.     }
  52.    
  53.     public class EnumCellConverter extends BaseCellConverter<Enum> {
  54.        
  55.         private EnumCellConverter(final FieldAccessor field, final Configuration config) {
  56.             super(field, config);
  57.         }
  58.        
  59.         @Override
  60.         protected Enum parseCell(final Cell evaluatedCell, final String formattedValue) throws TypeBindException {
  61.             if(formattedValue.isEmpty()) {
  62.                 return null;
  63.             }
  64.            
  65.             try {
  66.                 return this.textFormatter.parse(formattedValue);
  67.             } catch(TextParseException e) {
  68.                 throw newTypeBindExceptionOnParse(e, evaluatedCell, formattedValue)
  69.                     .addAllMessageVars(e.getErrorVariables());
  70.             }
  71.         }
  72.        
  73.         @Override
  74.         protected void setupCell(final Cell cell, final Optional<Enum> cellValue) throws TypeBindException {
  75.            
  76.             if(cellValue.isPresent()) {
  77.                 cell.setCellValue(textFormatter.format(cellValue.get()));
  78.                
  79.             } else {
  80.                 cell.setBlank();
  81.             }
  82.            
  83.         }
  84.        
  85.     }
  86.    
  87. }