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