Java中的基本数据类型转换
作者:荣-- 发布时间:[ 2016/5/26 10:28:08 ] 推荐标签:测试开发技术 Java
如果想要做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;
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11