package foundation.value;
|
|
import java.math.BigDecimal;
|
import java.math.BigInteger;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
import foundation.server.config.DBaseType;
|
import foundation.util.Util;
|
|
|
public class DoubleTranslator extends ITranslator {
|
|
private static Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?([eE][+-]?\\d+)?$");
|
|
@Override
|
public Double load(Object value){
|
try {
|
return toDouble(value);
|
}catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
return null;
|
|
}
|
|
@Override
|
public Double load(String value) throws Exception {
|
if (Util.isEmpty(value)) {
|
return null;
|
}
|
|
return Double.valueOf(value);
|
}
|
|
@Override
|
public String toString(Object value) throws Exception {
|
if (Util.isEmpty(value) || value == null || value == ValueType.Null) {
|
return null;
|
}
|
|
return value.toString();
|
}
|
|
@Override
|
public String toSqlString(DBaseType dbaseType, Object value) throws Exception {
|
if (Util.isEmpty(value) || value == null || value == ValueType.Null) {
|
return "null";
|
}
|
|
return String.valueOf(value);
|
}
|
|
@Override
|
public String toJSONString(Object value) throws Exception {
|
if (Util.isEmpty(value) || value == null || value == ValueType.Null) {
|
return "null";
|
}
|
|
return value.toString();
|
}
|
|
@Override
|
public String toSchemaString(Object value) throws Exception {
|
if (Util.isEmpty(value) || value == null || value == ValueType.Null) {
|
return "null";
|
}
|
|
return value.toString();
|
}
|
|
public Double toDouble(Object value) throws Exception {
|
if (Util.isEmpty(value) || value == null || value == ValueType.Null) {
|
return null;
|
}
|
|
if (value instanceof Double) {
|
return (Double) value;
|
}
|
else if (value instanceof Float) {
|
return (Double) value;
|
}
|
else if (value instanceof Short) {
|
return (Double) value;
|
}
|
else if (value instanceof BigDecimal) {
|
return ((BigDecimal)value).doubleValue();
|
}
|
else if (value instanceof String) {
|
return Double.valueOf((String)value);
|
}
|
else if (value instanceof Integer) {
|
return Double.valueOf((Integer)value);
|
}
|
else if (value instanceof Long) {
|
return ((Long)value).doubleValue();
|
}
|
else if (value instanceof Byte) {
|
return Double.valueOf((Byte) value);
|
}
|
else if (value instanceof BigInteger) {
|
BigInteger integerValue = (BigInteger)value;
|
String integerString = integerValue.toString();
|
int length = integerString.length();
|
|
if (length > 16) {
|
throw new Exception(" value length has over the max length if double");
|
}
|
|
return Double.parseDouble(integerString);
|
}
|
|
return (Double)value;
|
}
|
|
@Override
|
public Object toTranslatorTypeValue(Object value) throws Exception {
|
if (Util.isEmpty(value)) {
|
return Double.valueOf(0);
|
}
|
return (Double)value;
|
}
|
|
@Override
|
public String checkType(Object object) {
|
String value = String.valueOf(object);
|
Matcher matcher = pattern.matcher(value);
|
|
if (matcher.find()) {
|
return null;
|
}
|
|
return "数字无法识别";
|
}
|
|
/*
|
@Override
|
public Integer toInteger(Object value) throws Exception {
|
if (value == null || value == ValueType.Null) {
|
return null;
|
}
|
|
return ((Double)value).intValue();
|
}
|
|
|
|
@Override
|
public BigDecimal toBigDecimal(Object value) throws Exception {
|
if (value == null || value == ValueType.Null) {
|
return null;
|
}
|
|
return BigDecimal.valueOf((Double)value);
|
}
|
|
@Override
|
public Boolean toBoolean(Object value) throws Exception {
|
if (value == null || value == ValueType.Null) {
|
return null;
|
}
|
|
return (Double)value > 0;
|
}
|
|
@Override
|
public Date toDate(Object value) throws Exception {
|
return null;
|
}
|
|
@Override
|
public Clob toClob(Object object) throws Exception {
|
return null;
|
}
|
*/
|
|
@Override
|
public Class<?> getDataClass() {
|
return Double.class;
|
}
|
}
|