SetCellConverterFactory.java

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

  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.Optional;
  7. import java.util.Set;

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

  9. import com.gh.mygreen.xlsmapper.Configuration;
  10. import com.gh.mygreen.xlsmapper.cellconverter.BaseCellConverter;
  11. import com.gh.mygreen.xlsmapper.cellconverter.CellConverter;
  12. import com.gh.mygreen.xlsmapper.cellconverter.CellConverterFactory;
  13. import com.gh.mygreen.xlsmapper.cellconverter.CellConverterFactorySupport;
  14. import com.gh.mygreen.xlsmapper.cellconverter.TypeBindException;
  15. import com.gh.mygreen.xlsmapper.fieldaccessor.FieldAccessor;
  16. import com.gh.mygreen.xlsmapper.textformatter.TextFormatter;
  17. import com.gh.mygreen.xlsmapper.textformatter.TextParseException;
  18. import com.gh.mygreen.xlsmapper.util.Utils;

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