Package loxia.support.excel.convertor

Source Code of loxia.support.excel.convertor.DoubleConvertor

package loxia.support.excel.convertor;

import loxia.support.excel.definition.ExcelCell;
import loxia.support.excel.exception.ErrorCode;
import loxia.support.excel.exception.ExcelManipulateException;


public class DoubleConvertor implements DataConvertor<Double> {

  public Double convert(Object value, int sheetNo, String cellIndex,
      ExcelCell cellDefinition) throws ExcelManipulateException {
    if(value == null && cellDefinition.isMandatory())
      throw new ExcelManipulateException(ErrorCode.WRONG_DATA_NULL,
          new Object[]{sheetNo + 1, cellIndex,
          null,cellDefinition.getPattern(),
          cellDefinition.getChoiceString()});
    if(value == null) return null;
    if(value instanceof String){
      String str = (String) value;
      str = str.trim();
      if(str.length() == 0){
        if(cellDefinition.isMandatory())
          throw new ExcelManipulateException(ErrorCode.WRONG_DATA_NULL,
              new Object[]{sheetNo + 1, cellIndex,
              null,cellDefinition.getPattern(),
              cellDefinition.getChoiceString()});
        else
          return null;
      }else{
        try {
          return new Double((String)value);
        } catch (NumberFormatException e) {
          throw new ExcelManipulateException(ErrorCode.WRONG_DATA_TYPE_NUMBER,
              new Object[]{sheetNo + 1, cellIndex,
              value,cellDefinition.getPattern(),
              cellDefinition.getChoiceString()});
        }
      }
    }else if(value instanceof Double){
      return (Double)value;
    }else
      throw new ExcelManipulateException(ErrorCode.WRONG_DATA_TYPE_NUMBER,
          new Object[]{sheetNo + 1, cellIndex,
          value,cellDefinition.getPattern(),
          cellDefinition.getChoiceString()});
  }

  public String getDataTypeAbbr() {
    return "double";
  }

  public Class<Double> supportClass() {
    return Double.class;
  }

}
TOP

Related Classes of loxia.support.excel.convertor.DoubleConvertor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.