|
@@ -7,16 +7,13 @@ import api.common.pojo.po.model.VehiclePO;
|
|
import api.common.pojo.vo.model.VehicleVO;
|
|
import api.common.pojo.vo.model.VehicleVO;
|
|
import api.common.pojo.vo.system.DictVO;
|
|
import api.common.pojo.vo.system.DictVO;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
+import lombok.SneakyThrows;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.Field;
|
|
-import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
|
import java.lang.reflect.Method;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
-import java.util.function.BiConsumer;
|
|
|
|
-import java.util.function.Function;
|
|
|
|
|
|
|
|
-@SuppressWarnings("all")
|
|
|
|
public class ObjectUtil {
|
|
public class ObjectUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -26,19 +23,19 @@ public class ObjectUtil {
|
|
boolean isNullFlag = true;
|
|
boolean isNullFlag = true;
|
|
if (obj != null) {
|
|
if (obj != null) {
|
|
if (obj instanceof List) {
|
|
if (obj instanceof List) {
|
|
- isNullFlag = isNull((List) obj);
|
|
|
|
|
|
+ isNullFlag = isNull((List<?>) obj);
|
|
} else if (obj instanceof Set) {
|
|
} else if (obj instanceof Set) {
|
|
- isNullFlag = isNull((Set) obj);
|
|
|
|
|
|
+ isNullFlag = isNull((Set<?>) obj);
|
|
} else if (obj instanceof Object[]) {
|
|
} else if (obj instanceof Object[]) {
|
|
- isNullFlag = isNull((Object[]) ((Object[]) obj));
|
|
|
|
|
|
+ isNullFlag = isNull(obj);
|
|
} else if (obj instanceof Map) {
|
|
} else if (obj instanceof Map) {
|
|
- isNullFlag = isNull((Map) obj);
|
|
|
|
|
|
+ isNullFlag = isNull((Map<?, ?>) obj);
|
|
} else if (obj instanceof String) {
|
|
} else if (obj instanceof String) {
|
|
isNullFlag = isNull((String) obj);
|
|
isNullFlag = isNull((String) obj);
|
|
} else if (obj instanceof Integer) {
|
|
} else if (obj instanceof Integer) {
|
|
- isNullFlag = isNull((Integer) obj);
|
|
|
|
|
|
+ isNullFlag = false;
|
|
} else if (obj instanceof Boolean) {
|
|
} else if (obj instanceof Boolean) {
|
|
- isNullFlag = isNull((Boolean) obj);
|
|
|
|
|
|
+ isNullFlag = false;
|
|
} else if (obj instanceof StringBuffer) {
|
|
} else if (obj instanceof StringBuffer) {
|
|
isNullFlag = isNull((StringBuffer) obj);
|
|
isNullFlag = isNull((StringBuffer) obj);
|
|
} else if (obj instanceof JsonNode) {
|
|
} else if (obj instanceof JsonNode) {
|
|
@@ -91,7 +88,7 @@ public class ObjectUtil {
|
|
}
|
|
}
|
|
|
|
|
|
public static List<DictVO> DictListToTree(List<DictVO> list) {
|
|
public static List<DictVO> DictListToTree(List<DictVO> list) {
|
|
- List<DictVO> treeList = new ArrayList<DictVO>();
|
|
|
|
|
|
+ List<DictVO> treeList = new ArrayList<>();
|
|
for (DictVO dictVO : list) {
|
|
for (DictVO dictVO : list) {
|
|
if ("0".equals(dictVO.getPid())) {
|
|
if ("0".equals(dictVO.getPid())) {
|
|
treeList.add(findChildren(dictVO, list));
|
|
treeList.add(findChildren(dictVO, list));
|
|
@@ -262,60 +259,20 @@ public class ObjectUtil {
|
|
return vehiclePO;
|
|
return vehiclePO;
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * list转tree结构,
|
|
|
|
- *
|
|
|
|
- * @param list List<T>类型数据源
|
|
|
|
- * @param rootId tree的根节点id
|
|
|
|
- * @param getId <T>类型获取id的方法
|
|
|
|
- * @param getPid <T>类型获取父id的方法
|
|
|
|
- * @param getChildren <T>类型获取子对象的方法
|
|
|
|
- * @param setChildren <T>类型设置子对象的方法
|
|
|
|
- * @param <T> 该对象满足父子关系的相关字段:id 和 父id
|
|
|
|
- * @param <String> id的数据类型为String
|
|
|
|
- * @return 树形结构的List<T>,多根型
|
|
|
|
- */
|
|
|
|
- public static <T, String> List<T> listToTree(List<T> list, String rootId, Function<T, String> getId, Function<T, String> getPid, Function<T, List<T>> getChildren, BiConsumer<T, List<T>> setChildren) {
|
|
|
|
- List<T> treeList = new ArrayList<T>();
|
|
|
|
- for (T node : list) {
|
|
|
|
- if (rootId.equals(getPid.apply(node))) {
|
|
|
|
- treeList.add(findChildren(node, list, getId, getPid, getChildren, setChildren));
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return treeList;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * list转tree递归调用
|
|
|
|
- */
|
|
|
|
- private static <T, String> T findChildren(T parent, List<T> list, Function<T, String> getId, Function<T, String> getPid, Function<T, List<T>> getChildren, BiConsumer<T, List<T>> setChildren) {
|
|
|
|
- for (T node : list)
|
|
|
|
- if (getId.apply(parent).equals(getPid.apply(node))) {
|
|
|
|
- if (getChildren.apply(parent) == null || getChildren.apply(parent).size() == 0) {
|
|
|
|
- setChildren.accept(parent, new ArrayList<T>());
|
|
|
|
- }
|
|
|
|
- getChildren.apply(parent).add(findChildren(node, list, getId, getPid, getChildren, setChildren));
|
|
|
|
- }
|
|
|
|
- return parent;
|
|
|
|
- }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* list转tree结构,<T>类型需要在属性上添加注解,标识树形结构:@WId 、@WParentId、 @WChildren;
|
|
* list转tree结构,<T>类型需要在属性上添加注解,标识树形结构:@WId 、@WParentId、 @WChildren;
|
|
- *
|
|
|
|
- * @param list
|
|
|
|
- * @param <T>
|
|
|
|
- * @return
|
|
|
|
*/
|
|
*/
|
|
public static <T> List<T> listToTree(List<T> list) {
|
|
public static <T> List<T> listToTree(List<T> list) {
|
|
if (isNull(list)) {
|
|
if (isNull(list)) {
|
|
- return new LinkedList<T>();
|
|
|
|
|
|
+ return new LinkedList<>();
|
|
}
|
|
}
|
|
return listToTree(list, "0");
|
|
return listToTree(list, "0");
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
public static <T> List<T> listToTree(List<T> list, String root) {
|
|
public static <T> List<T> listToTree(List<T> list, String root) {
|
|
try {
|
|
try {
|
|
- List<T> treeList = new LinkedList<T>();
|
|
|
|
|
|
+ List<T> treeList = new LinkedList<>();
|
|
Class<?> tClass = null;
|
|
Class<?> tClass = null;
|
|
for (T t : list) {
|
|
for (T t : list) {
|
|
tClass = t.getClass();
|
|
tClass = t.getClass();
|
|
@@ -346,7 +303,7 @@ public class ObjectUtil {
|
|
}
|
|
}
|
|
//解析列表
|
|
//解析列表
|
|
for (T node : list) {
|
|
for (T node : list) {
|
|
- if (root.equals((String) getParentId.invoke(node))) {
|
|
|
|
|
|
+ if (root.equals(getParentId.invoke(node))) {
|
|
treeList.add(findChildren(node, list, getId, getParentId, getChildren, setChildren));
|
|
treeList.add(findChildren(node, list, getId, getParentId, getChildren, setChildren));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -360,14 +317,17 @@ public class ObjectUtil {
|
|
/**
|
|
/**
|
|
* list转tree递归调用
|
|
* list转tree递归调用
|
|
*/
|
|
*/
|
|
- private static <T> T findChildren(T parent, List<T> list, Method getId, Method getParentId, Method getChildren, Method setChildren) throws InvocationTargetException, IllegalAccessException {
|
|
|
|
|
|
+ @SneakyThrows
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
+ private static <T> T findChildren(T parent, List<T> list, Method getId, Method getParentId, Method getChildren, Method setChildren) {
|
|
for (T node : list) {
|
|
for (T node : list) {
|
|
String id = (String) getId.invoke(parent);
|
|
String id = (String) getId.invoke(parent);
|
|
String parentId = (String) getParentId.invoke(node);
|
|
String parentId = (String) getParentId.invoke(node);
|
|
if (id.equals(parentId)) {
|
|
if (id.equals(parentId)) {
|
|
- List<T> children = (List<T>) getChildren.invoke(parent);
|
|
|
|
|
|
+ final Object invoke = getChildren.invoke(parent);
|
|
|
|
+ List<T> children = (List<T>) invoke;
|
|
if (ObjectUtil.isNull(children)) {
|
|
if (ObjectUtil.isNull(children)) {
|
|
- children = new ArrayList<T>();
|
|
|
|
|
|
+ children = new ArrayList<>();
|
|
setChildren.invoke(parent, children);
|
|
setChildren.invoke(parent, children);
|
|
}
|
|
}
|
|
children.add(findChildren(node, list, getId, getParentId, getChildren, setChildren));
|
|
children.add(findChildren(node, list, getId, getParentId, getChildren, setChildren));
|