CalendarCellConverterFactory.java

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

  2. import java.util.Calendar;
  3. import java.util.Date;
  4. import java.util.Optional;

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

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

  15. /**
  16.  * {@link Calendar}を処理する{@link CellConverter}を作成するためのファクトリクラス。
  17.  *
  18.  * @since 2.0
  19.  * @author T.TSUCHIE
  20.  *
  21.  */
  22. public class CalendarCellConverterFactory extends CellConverterFactorySupport<Calendar>
  23.         implements CellConverterFactory<Calendar> {
  24.    
  25.     private DateCellConverterFactory dateCellConverterFactory = new DateCellConverterFactory();
  26.    
  27.     @Override
  28.     public CalendarCellConverter create(final FieldAccessor field, final Configuration config) {
  29.        
  30.         final DateCellConverterFactory.DateCellConverter dateCellConverter = dateCellConverterFactory.create(field, config);
  31.        
  32.         final CalendarCellConverter cellConverter = new CalendarCellConverter(field, config, dateCellConverter);
  33.         setupCellConverter(cellConverter, field, config);
  34.        
  35.         return cellConverter;
  36.     }
  37.    
  38.     @Override
  39.     protected void setupCustom(final BaseCellConverter<Calendar> cellConverter, final FieldAccessor field, final Configuration config) {
  40.        
  41.         // 何もなし
  42.        
  43.     }
  44.    
  45.     @Override
  46.     protected TextFormatter<Calendar> createTextFormatter(final FieldAccessor field, final Configuration config) {
  47.        
  48.         final TextFormatter<Date> dateTextFormatter = dateCellConverterFactory.createTextFormatter(field, config);
  49.        
  50.         return new TextFormatter<Calendar>() {
  51.            
  52.             @Override
  53.             public Calendar parse(final String text) throws TextParseException {
  54.                
  55.                 Date date = dateTextFormatter.parse(text);
  56.                 Calendar cal= Calendar.getInstance();
  57.                 cal.setTime(date);
  58.                
  59.                 return cal;
  60.             }
  61.            
  62.             @Override
  63.             public String format(final Calendar value) {
  64.                
  65.                 return dateTextFormatter.format(value.getTime());
  66.             }
  67.         };
  68.     }
  69.    
  70.     public class CalendarCellConverter extends BaseCellConverter<Calendar> {
  71.        
  72.         private final  DateCellConverterFactory.DateCellConverter dateCellConverter;
  73.        
  74.         private CalendarCellConverter(final FieldAccessor field, final Configuration config,
  75.                 final DateCellConverterFactory.DateCellConverter dateCellConverter) {
  76.             super(field, config);
  77.             this.dateCellConverter = dateCellConverter;
  78.         }
  79.        
  80.         @Override
  81.         protected Calendar parseCell(final Cell evaluatedCell, final String formattedValue) throws TypeBindException {
  82.            
  83.             Date date = dateCellConverter.parseCell(evaluatedCell, formattedValue);
  84.             if(date != null) {
  85.                 Calendar cal= Calendar.getInstance();
  86.                 cal.setTime(date);
  87.                
  88.                 return cal;
  89.             }
  90.            
  91.             return null;
  92.         }
  93.        
  94.         @Override
  95.         protected void setupCell(final Cell cell, final Optional<Calendar> cellValue) throws TypeBindException {
  96.            
  97.             Optional<Date> dateCellValue = cellValue.map(c -> c.getTime());
  98.             dateCellConverter.setupCell(cell, dateCellValue);
  99.         }
  100.        
  101.     }
  102. }