AbstractNumberCellConverter.java

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

  2. import java.math.BigDecimal;
  3. import java.math.MathContext;
  4. import java.util.Optional;

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

  7. import com.gh.mygreen.xlsmapper.Configuration;
  8. import com.gh.mygreen.xlsmapper.cellconverter.BaseCellConverter;
  9. import com.gh.mygreen.xlsmapper.cellconverter.CellStyleProxy;
  10. import com.gh.mygreen.xlsmapper.cellconverter.TypeBindException;
  11. import com.gh.mygreen.xlsmapper.fieldaccessor.FieldAccessor;
  12. import com.gh.mygreen.xlsmapper.textformatter.TextParseException;
  13. import com.gh.mygreen.xlsmapper.util.Utils;


  14. /**
  15.  * 数値型のConverterの抽象クラス。
  16.  * <p>数値型のConverterは、基本的にこのクラスを継承して作成する。</p>
  17.  *
  18.  * @version 2.0
  19.  * @author T.TSUCHIE
  20.  *
  21.  */
  22. public abstract class AbstractNumberCellConverter<T extends Number> extends BaseCellConverter<T> {

  23.     /**
  24.      * 書き込み時のExcelのセルの書式
  25.      */
  26.     private Optional<String> excelPattern = Optional.empty();

  27.     /**
  28.      * 数値を処理する際のコンテキスト
  29.      */
  30.     private MathContext mathContext;

  31.     public AbstractNumberCellConverter(final FieldAccessor field, final Configuration config) {
  32.         super(field, config);
  33.     }

  34.     @SuppressWarnings("unchecked")
  35.     @Override
  36.     protected T parseCell(final Cell evaluatedCell, final String formattedValue) throws TypeBindException {

  37.         if(evaluatedCell.getCellType().equals(CellType.NUMERIC)) {
  38.             try {
  39.                 return convertTypeValue(new BigDecimal(evaluatedCell.getNumericCellValue(), mathContext));

  40.             } catch(ArithmeticException e) {

  41.                 throw newTypeBindExceptionOnParse(e, evaluatedCell, formattedValue);
  42.             }

  43.         } else if(!formattedValue.isEmpty()) {
  44.             try {
  45.                 return textFormatter.parse(formattedValue);

  46.             } catch(TextParseException e) {
  47.                 throw newTypeBindExceptionOnParse(e, evaluatedCell, formattedValue);
  48.             }
  49.         }

  50.         // プリミティブ型の場合、値がnullの時は初期値を設定する
  51.         if(field.getType().isPrimitive()) {
  52.             return (T)Utils.getPrimitiveDefaultValue(field.getType());

  53.         } else if(field.isComponentType() && field.getComponentType().isPrimitive()) {
  54.             return (T)Utils.getPrimitiveDefaultValue(field.getComponentType());
  55.         }

  56.         return null;

  57.     }

  58.     /**
  59.      * その型における型に変換する
  60.      * BigDecimalから変換する際には、exactXXX()メソッドを呼ぶ。
  61.      *
  62.      * @param value 変換対象のBigDecimal
  63.      * @return 変換した値
  64.      * @throws ArithmeticException 変換する数値型に合わない場合
  65.      */
  66.     protected abstract T convertTypeValue(final BigDecimal value) throws ArithmeticException;

  67.     @Override
  68.     protected void setupCell(final Cell cell, final Optional<T> cellValue) throws TypeBindException {

  69.         // 書式を設定する
  70.         final CellStyleProxy cellStyle = new CellStyleProxy(cell);

  71.         excelPattern.ifPresent(pattern -> {
  72.             cellStyle.setDataFormat(pattern, getConfiguration().getCellFormatter());
  73.         });

  74.         if(cellValue.isPresent()) {
  75.             cell.setCellValue(cellValue.get().doubleValue());

  76.         } else {
  77.             cell.setBlank();
  78.         }

  79.     }

  80.     /**
  81.      * Excelの書式を設定する
  82.      * @param excelPattern excelの書式
  83.      */
  84.     public void setExcelPattern(String excelPattern) {
  85.         this.excelPattern = Optional.of(excelPattern);
  86.     }

  87.     /**
  88.      * 数値を処理する際のコンテキストを設定する
  89.      * @param mathContext
  90.      */
  91.     public void setMathContext(MathContext mathContext) {
  92.         this.mathContext = mathContext;
  93.     }

  94. }