LongCellConverterFactory.java

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

  2. import java.math.BigDecimal;
  3. import java.math.RoundingMode;

  4. import com.gh.mygreen.xlsmapper.Configuration;
  5. import com.gh.mygreen.xlsmapper.cellconverter.CellConverter;
  6. import com.gh.mygreen.xlsmapper.fieldaccessor.FieldAccessor;

  7. /**
  8.  * {@link Long}型を処理する{@link CellConverter}を作成するクラス。
  9.  *
  10.  * @since 2.0
  11.  * @author T.TSUCHIE
  12.  *
  13.  */
  14. public class LongCellConverterFactory extends AbstractNumberCellConverterFactory<Long> {
  15.    
  16.     @Override
  17.     public LongCellConverter create(final FieldAccessor field, final Configuration config) {
  18.        
  19.         final LongCellConverter cellConverter = new LongCellConverter(field, config, this);
  20.         setupCellConverter(cellConverter, field, config);
  21.        
  22.         return cellConverter;
  23.        
  24.     }
  25.    
  26.     @Override
  27.     protected Long convertTypeValue(final BigDecimal value) throws NumberFormatException, ArithmeticException {
  28.         // 少数以下を四捨五入
  29.         BigDecimal decimal = value.setScale(0, RoundingMode.HALF_UP);
  30.         return decimal.longValueExact();
  31.     }
  32.    
  33.     public class LongCellConverter extends AbstractNumberCellConverter<Long> {
  34.        
  35.         private final LongCellConverterFactory convererFactory;
  36.        
  37.         private LongCellConverter(final FieldAccessor field, final Configuration config,
  38.                 final LongCellConverterFactory convererFactory) {
  39.             super(field, config);
  40.             this.convererFactory = convererFactory;
  41.         }
  42.        
  43.         @Override
  44.         protected Long convertTypeValue(final BigDecimal value) {
  45.             return convererFactory.convertTypeValue(value);
  46.         }
  47.        
  48.     }
  49.    
  50. }