package foundation.translator; import java.math.BigDecimal; import java.util.Date; import foundation.server.config.DBaseType; import foundation.util.Util; public class BooleanTranslator extends ITranslator { private static ITranslator helper; static { helper = new StringTranslator(); } @Override public String toString(Object value) throws Exception { if (value == null || value == ValueType.Null) { return null; } Boolean bool = false; if (value instanceof Boolean) { bool = (Boolean) value; } else { bool = helper.toBoolean(value, false); } return bool.toString(); } @Override public String toSqlString(DBaseType dbaseType, Object value) throws Exception { if (value == null || value == ValueType.Null) { return "null"; } Boolean bool = false; if (value instanceof Boolean) { bool = (Boolean) value; } else { bool = helper.toBoolean(value, false); } if (bool) { return Util.quotedStr("T"); } else { return Util.quotedStr("F"); } } @Override public String toJSONString(Object value) throws Exception { if (value == null || value == ValueType.Null) { return "null"; } Boolean bool = false; if (value instanceof Boolean) { bool = (Boolean) value; } else { bool = helper.toBoolean(value, false); } return bool.toString(); } @Override public String toSchemaString(Object value) throws Exception { if (value == null || value == ValueType.Null) { return ""; } Boolean bool = false; if (value instanceof Boolean) { bool = (Boolean) value; } else { bool = helper.toBoolean(value, false); } return bool.toString(); } @Override public Integer toInteger(Object object) throws Exception { throw new Exception("can not translate boolean (" + object + ") to Integer"); } @Override public Double toDouble(Object object) throws Exception { throw new Exception("can not translate boolean (" + object + ") to Double"); } @Override public BigDecimal toBigDecimal(Object object) throws Exception { throw new Exception("can not translate boolean (" + object + ") to BigDecimal"); } public Boolean toBoolean(Object object) throws Exception { if (object == null || object == ValueType.Null) { return false; } if (object instanceof String) { String value = String.valueOf(object).toLowerCase(); if ("t".equals(value)) { return true; } if ("true".equals(value)) { return true; } if ("y".equals(value)) { return true; } if ("yes".equals(value)) { return true; } if ("是".equals(value)) { return true; } if ("1".equals(value)) { return true; } return false; } else if (object instanceof Integer) { Integer value = (Integer) object; return value == 1; } else if (object instanceof Boolean) { return (Boolean) object; } return Boolean.valueOf(object.toString()); } @Override public Date toDate(Object object) throws Exception { throw new Exception("can not translate boolean (" + object + ") to Date"); } @Override public Object toTranslatorTypeValue(Object value) throws Exception { if (value == null) { return null; } if (value instanceof Boolean) { return (Boolean)value ? "T" : "F"; } return (String)value; } @Override public Object loadObjectFrom(String value) throws Exception { if (Util.isEmpty(value)) { return null; } value = value.toLowerCase(); if ("t".equals(value)) { return true; } if ("true".equals(value)) { return true; } if ("y".equals(value)) { return true; } if ("yes".equals(value)) { return true; } return false; } @Override public Class getDataClass() { return Boolean.class; } }