|
@@ -33,9 +33,10 @@ public class EntityUtil {
|
|
|
|
|
|
/**
|
|
|
* 将list中元素的某一成员组装成list返回。注意:会去重!
|
|
|
- * @param list 元素列表
|
|
|
+ *
|
|
|
+ * @param list 元素列表
|
|
|
* @param fieldName 成员变量的field
|
|
|
- * @param <T> 元素类型
|
|
|
+ * @param <T> 元素类型
|
|
|
* @return 返回该字段组成的list
|
|
|
*/
|
|
|
public static <T> List makeListByFieldName(List<T> list, String fieldName) {
|
|
@@ -50,7 +51,7 @@ public class EntityUtil {
|
|
|
}
|
|
|
try {
|
|
|
for (T o : list) {
|
|
|
- if(!returnList.contains(field.get(o))) {
|
|
|
+ if (!returnList.contains(field.get(o))) {
|
|
|
returnList.add(field.get(o));
|
|
|
}
|
|
|
}
|
|
@@ -63,9 +64,10 @@ public class EntityUtil {
|
|
|
|
|
|
/**
|
|
|
* 将Collection中元素的某一成员组装成Set返回
|
|
|
+ *
|
|
|
* @param collection 元素列表
|
|
|
- * @param fieldName 成员变量的field
|
|
|
- * @param <T> 元素类型
|
|
|
+ * @param fieldName 成员变量的field
|
|
|
+ * @param <T> 元素类型
|
|
|
* @return 返回该字段组成的LinkedHashSet。若元素中不存在名为fieldName的成员变量,则返回EmptySet
|
|
|
*/
|
|
|
public static <T> LinkedHashSet makeLinkedSetByFieldName(Collection<T> collection, String fieldName) {
|
|
@@ -96,12 +98,12 @@ public class EntityUtil {
|
|
|
* 将list中的元素放到Map<M, N>以建立 key - value 索引<p>
|
|
|
* modified from com.tqmall.saint.biz.util.EntityUtil#makeEntityMap(java.util.List, java.lang.String)
|
|
|
*
|
|
|
- * @param collection Collection<V> 元素列表
|
|
|
- * @param keyFieldName String 元素的属性名称, 该属性的值作为Map的key
|
|
|
+ * @param collection Collection<V> 元素列表
|
|
|
+ * @param keyFieldName String 元素的属性名称, 该属性的值作为Map的key
|
|
|
* @param valueFieldName String 元素的属性名称, 该属性的值作为Map的value
|
|
|
- * @param <M> key类型
|
|
|
- * @param <N> value类型
|
|
|
- * @param <V> 列表元素类型
|
|
|
+ * @param <M> key类型
|
|
|
+ * @param <N> value类型
|
|
|
+ * @param <V> 列表元素类型
|
|
|
* @return
|
|
|
*/
|
|
|
@SuppressWarnings("unchecked")
|
|
@@ -132,19 +134,18 @@ public class EntityUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
* 将list中的元素放到Map<K, V>以建立 key - value 索引<p>
|
|
|
*
|
|
|
- * @param list List<V> 元素列表
|
|
|
+ * @param list List<V> 元素列表
|
|
|
* @param keyFieldName String 元素的属性名称, 该属性的值作为索引key
|
|
|
- * @param <K> key类型
|
|
|
- * @param <V> value类型
|
|
|
- * @return Map<K, V> key - value 索引
|
|
|
+ * @param <K> key类型
|
|
|
+ * @param <V> value类型
|
|
|
+ * @return Map<K , V> key - value 索引
|
|
|
*/
|
|
|
@SuppressWarnings("unchecked")
|
|
|
public static <K, V> Map<K, V> makeEntityMap(List<V> list, String keyFieldName) {
|
|
|
Map<K, V> map = new HashMap<>();
|
|
|
- if(list == null || list.size() == 0) {
|
|
|
+ if (list == null || list.size() == 0) {
|
|
|
return map;
|
|
|
}
|
|
|
try {
|
|
@@ -160,32 +161,31 @@ public class EntityUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
* 将list中的元素放到Map<String, V>以建立 key - value 索引<p>
|
|
|
*
|
|
|
- * @param list List<V> 元素列表
|
|
|
- * @param splitVar 属性之间间隔
|
|
|
+ * @param list List<V> 元素列表
|
|
|
+ * @param splitVar 属性之间间隔
|
|
|
* @param keyFieldNames String 元素的属性名称动态数组, 依次循环该属性的值作为索引key
|
|
|
- * @param <V> value类型
|
|
|
- * @return Map<String, V> key - value 索引
|
|
|
+ * @param <V> value类型
|
|
|
+ * @return Map<String , V> key - value 索引
|
|
|
*/
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- public static <V> Map<String, V> makeEntityMapByKeys(List<V> list,String splitVar , String... keyFieldNames) {
|
|
|
+ public static <V> Map<String, V> makeEntityMapByKeys(List<V> list, String splitVar, String... keyFieldNames) {
|
|
|
Map<String, V> map = new HashMap<>();
|
|
|
- if(list == null || list.size() == 0 || keyFieldNames == null || keyFieldNames.length==0 || StringUtil.isEmpty(splitVar)) {
|
|
|
+ if (list == null || list.size() == 0 || keyFieldNames == null || keyFieldNames.length == 0 || StringUtil.isEmpty(splitVar)) {
|
|
|
return map;
|
|
|
}
|
|
|
try {
|
|
|
List<Method> getterList = new ArrayList<>();
|
|
|
- for(String key : keyFieldNames){
|
|
|
- getterList.add(getMethod(list.get(0).getClass(),key, "get"));
|
|
|
+ for (String key : keyFieldNames) {
|
|
|
+ getterList.add(getMethod(list.get(0).getClass(), key, "get"));
|
|
|
}
|
|
|
for (V item : list) {
|
|
|
- StringBuffer keys= new StringBuffer("");
|
|
|
- for (int i=0;i<getterList.size();i++){
|
|
|
+ StringBuffer keys = new StringBuffer("");
|
|
|
+ for (int i = 0; i < getterList.size(); i++) {
|
|
|
keys.append(getterList.get(i).invoke(item));
|
|
|
- if(i<getterList.size()-1){
|
|
|
+ if (i < getterList.size() - 1) {
|
|
|
keys.append(splitVar);
|
|
|
}
|
|
|
}
|
|
@@ -197,19 +197,19 @@ public class EntityUtil {
|
|
|
}
|
|
|
return map;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
- *
|
|
|
* 将list中的元素放到Map<K, List<V>> 以建立 key - List<value> 索引<p>
|
|
|
*
|
|
|
- * @param list List<V> 元素列表
|
|
|
+ * @param list List<V> 元素列表
|
|
|
* @param keyFieldName String 元素的属性名称, 该属性的值作为索引key
|
|
|
- * @param <K> key类型
|
|
|
- * @param <V> value类型
|
|
|
- * @return Map<K, V> key - value 索引
|
|
|
+ * @param <K> key类型
|
|
|
+ * @param <V> value类型
|
|
|
+ * @return Map<K , V> key - value 索引
|
|
|
*/
|
|
|
public static <K, V> Map<K, List<V>> makeEntityListMap(List<V> list, String keyFieldName) {
|
|
|
Map<K, List<V>> map = new LinkedHashMap<>();
|
|
|
- if(list == null || list.size() == 0) {
|
|
|
+ if (list == null || list.size() == 0) {
|
|
|
return map;
|
|
|
}
|
|
|
try {
|
|
@@ -243,37 +243,38 @@ public class EntityUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 比较两个对象改变了的属性值,然后以string拼接返回
|
|
|
+ * 比较两个对象改变了的属性值,然后以string拼接返回
|
|
|
+ *
|
|
|
* @param oldObj 对象1
|
|
|
* @param newObj 对象2
|
|
|
* @return 改变的属性值拼接的字符串
|
|
|
*/
|
|
|
- public static <T> String compareToObjProperty(T oldObj,T newObj){
|
|
|
+ public static <T> String compareToObjProperty(T oldObj, T newObj) {
|
|
|
String modifiedStr = "";
|
|
|
Field[] fields = oldObj.getClass().getDeclaredFields();
|
|
|
- for(Field field : fields){
|
|
|
+ for (Field field : fields) {
|
|
|
try {
|
|
|
- if(!Modifier.isStatic(field.getModifiers())){
|
|
|
- String tempFieldType =field.getType().getName();
|
|
|
+ if (!Modifier.isStatic(field.getModifiers())) {
|
|
|
+ String tempFieldType = field.getType().getName();
|
|
|
Method tempMethod = getMethod(oldObj.getClass(), field.getName(), "get");
|
|
|
- if(field.getName().equals("ATTRIBUTE_ORDER_SN")){
|
|
|
+ if (field.getName().equals("ATTRIBUTE_ORDER_SN")) {
|
|
|
System.out.println("aa");
|
|
|
}
|
|
|
- if(tempMethod!=null){
|
|
|
+ if (tempMethod != null) {
|
|
|
Object tempOld = tempMethod.invoke(oldObj);
|
|
|
Object tempNew = tempMethod.invoke(newObj);
|
|
|
- if(findDifference(tempOld,tempNew)){
|
|
|
- if(tempFieldType.equals("java.util.Date")){
|
|
|
+ if (findDifference(tempOld, tempNew)) {
|
|
|
+ if (tempFieldType.equals("java.util.Date")) {
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd mm:HH:ss");
|
|
|
- modifiedStr += field.getName() + "[" + ( tempOld == null ? null: sdf.format(tempOld)) + "," +
|
|
|
+ modifiedStr += field.getName() + "[" + (tempOld == null ? null : sdf.format(tempOld)) + "," +
|
|
|
(tempNew == null ? null : sdf.format(tempNew)) + "],";
|
|
|
} else {
|
|
|
- modifiedStr += field.getName()+"["+tempOld+","+ tempNew+"],";
|
|
|
+ modifiedStr += field.getName() + "[" + tempOld + "," + tempNew + "],";
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- }catch (Exception e) {
|
|
|
+ } catch (Exception e) {
|
|
|
log.error("compareTo error", e);
|
|
|
}
|
|
|
}
|
|
@@ -282,7 +283,8 @@ public class EntityUtil {
|
|
|
|
|
|
/**
|
|
|
* 获取所有field,不包含field,修改自FieldUtils的getAllFieldsList方法
|
|
|
- * @param cls 类
|
|
|
+ *
|
|
|
+ * @param cls 类
|
|
|
* @param forceAccess 是否包含private的field
|
|
|
* @return
|
|
|
*/
|
|
@@ -306,7 +308,8 @@ public class EntityUtil {
|
|
|
|
|
|
/**
|
|
|
* 获取所有field,包含所有父类,来自FieldUtils的getAllFieldsList方法
|
|
|
- * @param cls 类
|
|
|
+ *
|
|
|
+ * @param cls 类
|
|
|
* @param forceAccess 是否包含private的field
|
|
|
* @return
|
|
|
*/
|
|
@@ -333,7 +336,8 @@ public class EntityUtil {
|
|
|
|
|
|
/**
|
|
|
* 获取类型的field
|
|
|
- * @param cls 类型
|
|
|
+ *
|
|
|
+ * @param cls 类型
|
|
|
* @param checkSupers 是否要获取父类的field
|
|
|
* @param forceAccess 是否包含private的field
|
|
|
* @return
|
|
@@ -348,12 +352,13 @@ public class EntityUtil {
|
|
|
|
|
|
/**
|
|
|
* 比较两个对象改变了的属性值,然后以string拼接返回
|
|
|
- * @param oldObj 对象1
|
|
|
- * @param newObj 对象2
|
|
|
- * @param formatter 格式(默认为%s[%s,%s],第一个%s对应fieldname,第二个%对应对象1的fieldname的值,第三个%s对应对象2的fieldname的值)
|
|
|
+ *
|
|
|
+ * @param oldObj 对象1
|
|
|
+ * @param newObj 对象2
|
|
|
+ * @param formatter 格式(默认为%s[%s,%s],第一个%s对应fieldname,第二个%对应对象1的fieldname的值,第三个%s对应对象2的fieldname的值)
|
|
|
* @param checkSupers 是否需要比较所有父类(无视继承的接口)
|
|
|
* @param forceAccess 是否需要比较private成员变量
|
|
|
- * @param <T> 要比较的对象类型
|
|
|
+ * @param <T> 要比较的对象类型
|
|
|
* @return
|
|
|
*/
|
|
|
public static <T> String compareToObjProperty(T oldObj, T newObj, String formatter, final boolean checkSupers, final boolean forceAccess) {
|
|
@@ -382,7 +387,7 @@ public class EntityUtil {
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- log.error("compareTo error",e);
|
|
|
+ log.error("compareTo error", e);
|
|
|
}
|
|
|
}
|
|
|
return builder.toString();
|
|
@@ -390,33 +395,34 @@ public class EntityUtil {
|
|
|
|
|
|
/**
|
|
|
* 比较两个对象的值是否不同
|
|
|
+ *
|
|
|
* @param obj1 对象1
|
|
|
* @param obj2 对象2
|
|
|
- * @param <T> object
|
|
|
+ * @param <T> object
|
|
|
* @return 若俩对象的值不相同则为true,反之为false
|
|
|
*/
|
|
|
- public static <T> Boolean findDifference(T obj1,T obj2){
|
|
|
- if(obj1 == null && obj2 == null){
|
|
|
+ public static <T> Boolean findDifference(T obj1, T obj2) {
|
|
|
+ if (obj1 == null && obj2 == null) {
|
|
|
return false;
|
|
|
}
|
|
|
- if(obj1 == null || obj2 == null){
|
|
|
+ if (obj1 == null || obj2 == null) {
|
|
|
return true;
|
|
|
}
|
|
|
- if(obj1 instanceof BigDecimal){
|
|
|
- return ((BigDecimal) obj1).compareTo((BigDecimal)obj2) != 0;
|
|
|
- }else {
|
|
|
+ if (obj1 instanceof BigDecimal) {
|
|
|
+ return ((BigDecimal) obj1).compareTo((BigDecimal) obj2) != 0;
|
|
|
+ } else {
|
|
|
return !obj1.equals(obj2);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static <V,K> Map<V, K> makeEntityMapNew(List<Map<String,Object>> hashMap,String keyFieldName) {
|
|
|
+ public static <V, K> Map<V, K> makeEntityMapNew(List<Map<String, Object>> hashMap, String keyFieldName) {
|
|
|
Map<V, K> map = new HashMap<>();
|
|
|
- if(hashMap == null || hashMap.size() == 0) {
|
|
|
+ if (hashMap == null || hashMap.size() == 0) {
|
|
|
return map;
|
|
|
}
|
|
|
try {
|
|
|
- for(Map linkedHashMap:hashMap){
|
|
|
- map.put((V)linkedHashMap.get(keyFieldName).toString(),(K)linkedHashMap);
|
|
|
+ for (Map linkedHashMap : hashMap) {
|
|
|
+ map.put((V) linkedHashMap.get(keyFieldName).toString(), (K) linkedHashMap);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
log.error("makeEntityListMap error list is " + hashMap, e);
|
|
@@ -426,14 +432,14 @@ public class EntityUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public static Map<Integer, Map> makeEntityMapSpecial(List<Map> hashMap,String keyFieldName) {
|
|
|
+ public static Map<Integer, Map> makeEntityMapSpecial(List<Map> hashMap, String keyFieldName) {
|
|
|
Map<Integer, Map> map = new HashMap<>();
|
|
|
- if(hashMap == null || hashMap.size() == 0) {
|
|
|
+ if (hashMap == null || hashMap.size() == 0) {
|
|
|
return map;
|
|
|
}
|
|
|
try {
|
|
|
- for(Map linkedHashMap:hashMap){
|
|
|
- map.put(Integer.valueOf(linkedHashMap.get(keyFieldName).toString()),linkedHashMap);
|
|
|
+ for (Map linkedHashMap : hashMap) {
|
|
|
+ map.put(Integer.valueOf(linkedHashMap.get(keyFieldName).toString()), linkedHashMap);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
log.error("makeEntityListMap error list is " + hashMap, e);
|