您的位置:首页 > 其它

jFinal生成动态条件的工具类中需要用到的几个方法

2014-09-15 17:48 519 查看
其实Model和Record本质也是属于map的,但是有些情况下还是需要将Model或者Record中的属性和值转换成一个Map键值对,因为在buildCondition的时候需要传字段和值,为了共用一个方法并且统一起来就将Model和Record全转换成一个map传给buildCondition,并且其他地方也有需要这样的转换,例如如果直接将一个Model传给前台页面貌似获取不到Model的属性的需要自己处理转换下!因此就写了这么两个方法。

1、将Model转换成Map

/**
* 将Model类转换为Map modelToMap
*
* @param 参数说明
* @return 返回对象
* @Exception 异常对象
*/
public static Map<String, Object> modelToMap(Model<?> model) {
Map<String, Object> map = new HashMap<String, Object>();
String[] names = model.getAttrNames();
for (String str : names) {
map.put(str, model.get(str));
}
return map;
}


2、将Record转换成Map

/**
* 将Record转换成Map recordToMap
*
* @param 参数说明
* @return 返回对象
* @Exception 异常对象
*/
public static Map<String, Object> recordToMap(Record record) {
Map<String, Object> map = new HashMap<String, Object>();
if (record != null) {
String[] columns = record.getColumnNames();
for (String col : columns) {
map.put(col, record.get(col));
}
}
return map;
}


3、判断对象是否为空

/**
* 判断对象或对象数组中每一个对象是否为空: 对象为null,字符序列长度为0,集合类、Map为empty
*
* @param obj
* @return
*/
@SuppressWarnings("unchecked")
public static boolean isNullOrEmpty(Object obj) {
if (obj == null) {
return true;
} else if (obj instanceof String && (obj.equals(""))) {
return true;
} else if (obj instanceof Short && ((Short) obj).shortValue() == 0) {
return true;
} else if (obj instanceof Integer && ((Integer) obj).intValue() == 0) {
return true;
} else if (obj instanceof Double && ((Double) obj).doubleValue() == 0) {
return true;
} else if (obj instanceof Float && ((Float) obj).floatValue() == 0) {
return true;
} else if (obj instanceof Long && ((Long) obj).longValue() == 0) {
return true;
} else if (obj instanceof Boolean && !((Boolean) obj)) {
return true;
} else if (obj instanceof Collection && ((Collection) obj).isEmpty()) {
return true;
} else if (obj instanceof Map && ((Map) obj).isEmpty()) {
return true;
} else if (obj instanceof Object[] && ((Object[]) obj).length == 0) {
return true;
}
return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: