CharacterCellConverterFactory.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.cellconverter.BaseCellConverter;
  6. import com.gh.mygreen.xlsmapper.cellconverter.CellConverter;
  7. import com.gh.mygreen.xlsmapper.cellconverter.CellConverterFactory;
  8. import com.gh.mygreen.xlsmapper.cellconverter.CellConverterFactorySupport;
  9. import com.gh.mygreen.xlsmapper.cellconverter.TypeBindException;
  10. import com.gh.mygreen.xlsmapper.fieldaccessor.FieldAccessor;
  11. import com.gh.mygreen.xlsmapper.textformatter.TextFormatter;
  12. import com.gh.mygreen.xlsmapper.textformatter.TextParseException;

  13. /**
  14.  *  char/Character を処理する{@link CellConverter}を作成するためのファクトリクラス。
  15.  *
  16.  * @since 2.0
  17.  * @author T.TSUCHIE
  18.  *
  19.  */
  20. public class CharacterCellConverterFactory extends CellConverterFactorySupport<Character>
  21.         implements CellConverterFactory<Character> {
  22.    
  23.     private static final char DEFAULT_VALUE = '\u0000';;
  24.    
  25.     @Override
  26.     public CharacterCellConverter create(final FieldAccessor field, final Configuration config) {
  27.        
  28.         final CharacterCellConverter cellConverter = new CharacterCellConverter(field, config);
  29.         setupCellConverter(cellConverter, field, config);
  30.        
  31.         return cellConverter;
  32.     }
  33.    
  34.     @Override
  35.     protected void setupCustom(final BaseCellConverter<Character> cellConverter, final FieldAccessor field, final Configuration config) {
  36.         // 何もしない
  37.        
  38.     }
  39.    
  40.     @Override
  41.     protected TextFormatter<Character> createTextFormatter(final FieldAccessor field, final Configuration config) {
  42.        
  43.         return new TextFormatter<Character>() {
  44.            
  45.             @Override
  46.             public Character parse(final String text) throws TextParseException {
  47.                
  48.                 if(text.length() >= 1) {
  49.                     return text.charAt(0);
  50.                 }
  51.                
  52.                 // 値が空のとき
  53.                 if(field.getType().isPrimitive()) {
  54.                     return DEFAULT_VALUE;
  55.                    
  56.                 } else if(field.isComponentType() && field.getComponentType().isPrimitive()) {
  57.                     return DEFAULT_VALUE;
  58.                 }
  59.                
  60.                 return null;
  61.             }
  62.            
  63.             @Override
  64.             public String format(final Character value) {
  65.                 return value.toString();
  66.             }
  67.         };
  68.     }
  69.    
  70.     public class CharacterCellConverter extends BaseCellConverter<Character> {
  71.        
  72.         private CharacterCellConverter(final FieldAccessor field, final Configuration config) {
  73.             super(field, config);
  74.         }
  75.        
  76.         @Override
  77.         protected Character parseCell(final Cell evaluatedCell, final String formattedValue) throws TypeBindException {
  78.            
  79.             return this.textFormatter.parse(formattedValue);
  80.         }
  81.        
  82.         @Override
  83.         protected void setupCell(final Cell cell, final Optional<Character> cellValue) throws TypeBindException {
  84.            
  85.             // \u0000 は、初期値として空と判定する。
  86.             if(cellValue.isPresent() && cellValue.get() != DEFAULT_VALUE) {
  87.                 cell.setCellValue(cellValue.get().toString());
  88.                
  89.             } else {
  90.                 cell.setBlank();
  91.             }
  92.         }
  93.        
  94.     }
  95.    
  96. }