如果想要做ORM,将数据映射为对象,则基本的数据类型转换是必要的功能,而Java似乎未提供该功能,只有自己下手来完成该功能.
  完成该功能的目标如下:
  1: 提供基本的数据转换
  2: 考虑转换的性能.
  下面是我的具体实现:
  1: 接口:
  public static <T> T Cast(Class<T> t, Object value)
  2: 依赖:需要依赖与获得数据默认值,因为在很多情况下,如果要转换的数据为null,调用者期望的是返回默认值.
  系统自动提供的默认值不能满足我们的需求,例如int的默认值为0,但是在sql查询中,如果查询失败,我们期望的是小于0的值,例如Select Count(1) from table1这样的操作.
  3: 实现:
/**
* 类型转换
* @param t
* @param value
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T Cast(Class<T> t, Object value) {
if (value == null) {
return GetDefault(t);
}
if (t == Object.class) {
return (T)value;
}
try
{
return Cast_i(t, value);
} catch(Exception ex) {
ex.printStackTrace();
return GetDefault(t);
}
}
private static String GetStr(Class<?> t, Object value) {
if (value == null) {
return "";
}
if (t == String.class){
return (String)value;
}else {
return value.toString();
}
}
@SuppressWarnings("unchecked")
private static <T> T Cast_i(Class<T> t, Object value) {
Class<?> objClass = value.getClass();
if (t == String.class) {
return (T)GetStr(objClass, value);
}
if (t == int.class) {
if (objClass == int.class) {
return (T)value;
}
String text = GetStr(objClass, value);
Object ret = Integer.parseInt(text);
return (T)ret;
}
if (t == Integer.class) {
if (objClass == Integer.class) {
return (T)value;
}
if (objClass == int.class) {
return (T)value;
}
String text = GetStr(objClass, value);
Object ret = Integer.parseInt(text);
return (T)ret;
}
if (t == short.class) {
if (objClass == int.class) {
return (T)value;
}
String text = GetStr(objClass, value);
Object ret = Short.parseShort(text);
return (T)ret;
}
if (t == boolean.class || t == Boolean.class) {
if (objClass == int.class) {
Object ret = (int)value >= 1? true:false;
return (T)ret;
}
if (objClass == Integer.class) {
Object ret = (Integer)value >= 1? true:false;
return (T)ret;
}
String text = GetStr(objClass, value);
Object ret = Integer.parseInt(text);
ret = (int)ret >= 1? true:false;
return (T)ret;
}
if (t == long.class) {
if (objClass == long.class) {
return (T)value;
}
String text = GetStr(objClass, value);
Object ret = Long.parseLong(text);
return (T)ret;
}
if (t == byte.class) {
if (objClass == byte.class) {
return (T)value;
}
String text = GetStr(objClass, value);
Object ret = Byte.parseByte(text);
return (T)ret;
}
if (t == double.class) {
if (objClass == double.class) {
return (T)value;
}
String text = GetStr(objClass, value);
Object ret = Double.parseDouble(text);
return (T)ret;
}
if (t == float.class) {
if (objClass == float.class) {
return (T)value;
}
String text = GetStr(objClass, value);
Object ret = Float.parseFloat(text);
return (T)ret;
}
if (t == BigDecimal.class) {
if (objClass == BigDecimal.class) {
return (T)value;
}
String text = GetStr(objClass, value);
return (T)new BigDecimal(text);
}
if (t == java.util.Date.class){
if (objClass == java.util.Date.class) {
return (T)value;
}
String text = GetStr(objClass, value);
return (T)HiTypeHelper.Convert2Date(text);
}
if (t == java.sql.Date.class){
if (objClass == java.sql.Date.class) {
return (T)value;
}
String text = GetStr(objClass, value);
return (T)HiTypeHelper.Convert2SqlDate(text);
}
if (t == Time.class) {
if (objClass == Time.class) {
return (T)value;
}
String text = GetStr(objClass, value);
Object ret = HiTypeHelper.Convert2Date(text).getTime();
return (T)ret;
}
if (t == Timestamp.class) {
if (objClass == Timestamp.class) {
return (T)value;
}
String text = GetStr(objClass, value);
return (T)Timestamp.valueOf(text);
}
return (T)value;
}