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 IntegerTranslator extends ITranslator {
|
|
private static Pattern pattern = Pattern.compile("^-?\\d+?([eE][+-]?\\d+)?$");
|
|
@Override
|
public Integer load(Object value){
|
try {
|
if (value == null || value == ValueType.Null) {
|
return null;
|
}
|
|
if (value instanceof Integer) {
|
return (Integer)value;
|
}
|
else if (value instanceof Long) {
|
return ((Long)value).intValue();
|
}
|
else if (value instanceof BigInteger) {
|
BigInteger bigInteger = (BigInteger)value;
|
if (bigInteger.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0 ||
|
bigInteger.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) {
|
throw new Exception("BigInteger is out of range for Integer");
|
}
|
return bigInteger.intValue();
|
}
|
else if (value instanceof String) {
|
return Integer.valueOf((String)value);
|
}
|
}catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
return (int)value;
|
|
}
|
|
@Override
|
public Integer load(String value) throws Exception {
|
if (Util.isEmpty(value)) {
|
return null;
|
}
|
|
return Integer.valueOf(value);
|
}
|
|
public String toString(Object value) throws Exception {
|
if (value == null || value == ValueType.Null) {
|
return null;
|
}
|
|
return value.toString();
|
}
|
|
public String toSqlString(DBaseType dbaseType, Object value) throws Exception {
|
if (value == null || value == ValueType.Null) {
|
return "null";
|
}
|
|
return value.toString();
|
}
|
|
public String toJSONString(Object value) throws Exception {
|
if (value == null || value == ValueType.Null) {
|
return "null";
|
}
|
|
return value.toString();
|
}
|
|
@Override
|
public String toSchemaString(Object value) throws Exception {
|
if (value == null || value == ValueType.Null) {
|
return "";
|
}
|
|
return value.toString();
|
}
|
|
public Integer toInteger(Object value) throws Exception {
|
if (value == null || value == ValueType.Null) {
|
return null;
|
}
|
|
if (value instanceof Integer) {
|
return (Integer)value;
|
}
|
else if (value instanceof Long) {
|
((Long)value).intValue();
|
}
|
else if (value instanceof BigInteger) {
|
BigInteger bigInteger = (BigInteger)value;
|
if (bigInteger.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0 ||
|
bigInteger.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) {
|
throw new Exception("BigInteger is out of range for Integer");
|
}
|
return bigInteger.intValue();
|
}
|
else if (value instanceof BigDecimal) {
|
return ((BigDecimal)value).intValue();
|
}
|
else if (value instanceof Double) {
|
return ((Double)value).intValue();
|
}
|
else if (value instanceof String) {
|
return Integer.getInteger((String)value);
|
}
|
else if (value instanceof Boolean) {
|
return (boolean)value ? 1 : 0;
|
}
|
|
return Integer.valueOf(String.valueOf(value));
|
}
|
|
@Override
|
public Object toTranslatorTypeValue(Object value) throws Exception {
|
return (Integer)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 Class<?> getDataClass() {
|
return Integer.class;
|
}
|
|
}
|