mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
refactor(core): TreeBuildUtils => TreeUtils
This commit is contained in:
@@ -24,6 +24,7 @@ import cn.hutool.core.lang.tree.parser.NodeParser;
|
|||||||
import cn.hutool.core.text.CharSequenceUtil;
|
import cn.hutool.core.text.CharSequenceUtil;
|
||||||
import cn.hutool.core.util.ReflectUtil;
|
import cn.hutool.core.util.ReflectUtil;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
@@ -31,14 +32,18 @@ import java.util.stream.Collectors;
|
|||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 扩展 hutool TreeUtil 封装树构建
|
* 树工具类
|
||||||
*
|
*
|
||||||
* @author Lion Li
|
* <p>
|
||||||
|
* 扩展 Hutool TreeUtil 封装树构建
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author Lion Li(<a href="https://gitee.com/dromara/RuoYi-Vue-Plus">RuoYi-Vue-Plus</a>)
|
||||||
* @author lishuyan
|
* @author lishuyan
|
||||||
*/
|
*/
|
||||||
public class TreeBuildUtils extends TreeUtil {
|
public class TreeUtils extends TreeUtil {
|
||||||
|
|
||||||
private TreeBuildUtils() {
|
private TreeUtils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -52,7 +57,7 @@ public class TreeBuildUtils extends TreeUtil {
|
|||||||
*/
|
*/
|
||||||
public static <T, K> List<Tree<K>> build(List<T> list, NodeParser<T, K> nodeParser) {
|
public static <T, K> List<Tree<K>> build(List<T> list, NodeParser<T, K> nodeParser) {
|
||||||
if (CollUtil.isEmpty(list)) {
|
if (CollUtil.isEmpty(list)) {
|
||||||
return CollUtil.newArrayList();
|
return new ArrayList<>(0);
|
||||||
}
|
}
|
||||||
K k = ReflectUtil.invoke(list.get(0), CharSequenceUtil.genGetter("parentId"));
|
K k = ReflectUtil.invoke(list.get(0), CharSequenceUtil.genGetter("parentId"));
|
||||||
return TreeUtil.build(list, k, TreeNodeConfig.DEFAULT_CONFIG, nodeParser);
|
return TreeUtil.build(list, k, TreeNodeConfig.DEFAULT_CONFIG, nodeParser);
|
||||||
@@ -70,7 +75,7 @@ public class TreeBuildUtils extends TreeUtil {
|
|||||||
*/
|
*/
|
||||||
public static <T, K> List<Tree<K>> build(List<T> list, K parentId, NodeParser<T, K> nodeParser) {
|
public static <T, K> List<Tree<K>> build(List<T> list, K parentId, NodeParser<T, K> nodeParser) {
|
||||||
if (CollUtil.isEmpty(list)) {
|
if (CollUtil.isEmpty(list)) {
|
||||||
return CollUtil.newArrayList();
|
return new ArrayList<>(0);
|
||||||
}
|
}
|
||||||
return TreeUtil.build(list, parentId, TreeNodeConfig.DEFAULT_CONFIG, nodeParser);
|
return TreeUtil.build(list, parentId, TreeNodeConfig.DEFAULT_CONFIG, nodeParser);
|
||||||
}
|
}
|
||||||
@@ -91,7 +96,7 @@ public class TreeBuildUtils extends TreeUtil {
|
|||||||
TreeNodeConfig treeNodeConfig,
|
TreeNodeConfig treeNodeConfig,
|
||||||
NodeParser<T, K> nodeParser) {
|
NodeParser<T, K> nodeParser) {
|
||||||
if (CollUtil.isEmpty(list)) {
|
if (CollUtil.isEmpty(list)) {
|
||||||
return CollUtil.newArrayList();
|
return new ArrayList<>(0);
|
||||||
}
|
}
|
||||||
return TreeUtil.build(list, parentId, treeNodeConfig, nodeParser);
|
return TreeUtil.build(list, parentId, treeNodeConfig, nodeParser);
|
||||||
}
|
}
|
||||||
@@ -112,7 +117,7 @@ public class TreeBuildUtils extends TreeUtil {
|
|||||||
Function<T, K> getParentId,
|
Function<T, K> getParentId,
|
||||||
NodeParser<T, K> parser) {
|
NodeParser<T, K> parser) {
|
||||||
if (CollUtil.isEmpty(list)) {
|
if (CollUtil.isEmpty(list)) {
|
||||||
return CollUtil.newArrayList();
|
return new ArrayList<>(0);
|
||||||
}
|
}
|
||||||
Set<K> rootParentIds = CollUtils.mapToSet(list, getParentId);
|
Set<K> rootParentIds = CollUtils.mapToSet(list, getParentId);
|
||||||
rootParentIds.removeAll(CollUtils.mapToSet(list, getId));
|
rootParentIds.removeAll(CollUtils.mapToSet(list, getId));
|
||||||
@@ -140,7 +145,7 @@ public class TreeBuildUtils extends TreeUtil {
|
|||||||
TreeNodeConfig treeNodeConfig,
|
TreeNodeConfig treeNodeConfig,
|
||||||
NodeParser<T, K> parser) {
|
NodeParser<T, K> parser) {
|
||||||
if (CollUtil.isEmpty(list)) {
|
if (CollUtil.isEmpty(list)) {
|
||||||
return CollUtil.newArrayList();
|
return new ArrayList<>(0);
|
||||||
}
|
}
|
||||||
Set<K> rootParentIds = CollUtils.mapToSet(list, getParentId);
|
Set<K> rootParentIds = CollUtils.mapToSet(list, getParentId);
|
||||||
rootParentIds.removeAll(CollUtils.mapToSet(list, getId));
|
rootParentIds.removeAll(CollUtils.mapToSet(list, getId));
|
||||||
@@ -159,9 +164,9 @@ public class TreeBuildUtils extends TreeUtil {
|
|||||||
*/
|
*/
|
||||||
public static <K> List<Tree<K>> getLeafNodes(List<Tree<K>> nodes) {
|
public static <K> List<Tree<K>> getLeafNodes(List<Tree<K>> nodes) {
|
||||||
if (CollUtil.isEmpty(nodes)) {
|
if (CollUtil.isEmpty(nodes)) {
|
||||||
return CollUtil.newArrayList();
|
return new ArrayList<>(0);
|
||||||
}
|
}
|
||||||
return nodes.stream().flatMap(TreeBuildUtils::extractLeafNodes).collect(Collectors.toList());
|
return nodes.stream().flatMap(TreeUtils::extractLeafNodes).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -176,7 +181,7 @@ public class TreeBuildUtils extends TreeUtil {
|
|||||||
return Stream.of(node);
|
return Stream.of(node);
|
||||||
} else {
|
} else {
|
||||||
// 递归调用,获取所有子节点的叶子节点
|
// 递归调用,获取所有子节点的叶子节点
|
||||||
return node.getChildren().stream().flatMap(TreeBuildUtils::extractLeafNodes);
|
return node.getChildren().stream().flatMap(TreeUtils::extractLeafNodes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@@ -35,7 +35,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import top.continew.starter.core.constant.StringConstants;
|
import top.continew.starter.core.constant.StringConstants;
|
||||||
import top.continew.starter.core.exception.BusinessException;
|
import top.continew.starter.core.exception.BusinessException;
|
||||||
import top.continew.starter.core.util.ReflectUtils;
|
import top.continew.starter.core.util.ReflectUtils;
|
||||||
import top.continew.starter.core.util.TreeBuildUtils;
|
import top.continew.starter.core.util.TreeUtils;
|
||||||
import top.continew.starter.core.util.validation.ValidationUtils;
|
import top.continew.starter.core.util.validation.ValidationUtils;
|
||||||
import top.continew.starter.data.base.BaseMapper;
|
import top.continew.starter.data.base.BaseMapper;
|
||||||
import top.continew.starter.data.service.impl.ServiceImpl;
|
import top.continew.starter.data.service.impl.ServiceImpl;
|
||||||
@@ -127,8 +127,8 @@ public class CrudServiceImpl<M extends BaseMapper<T>, T extends BaseIdDO, L, D,
|
|||||||
Function<L, Long> getParentId = createMethodReference(listClass, CharSequenceUtil.genGetter(treeField
|
Function<L, Long> getParentId = createMethodReference(listClass, CharSequenceUtil.genGetter(treeField
|
||||||
.parentIdKey()));
|
.parentIdKey()));
|
||||||
// 构建多根节点树
|
// 构建多根节点树
|
||||||
return TreeBuildUtils.buildMultiRoot(list, getId, getParentId, treeNodeConfig, (node,
|
return TreeUtils.buildMultiRoot(list, getId, getParentId, treeNodeConfig, (node,
|
||||||
tree) -> buildTreeField(isSimple, node, tree, treeField));
|
tree) -> buildTreeField(isSimple, node, tree, treeField));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -39,7 +39,7 @@ import top.continew.starter.core.constant.StringConstants;
|
|||||||
import top.continew.starter.core.exception.BusinessException;
|
import top.continew.starter.core.exception.BusinessException;
|
||||||
import top.continew.starter.core.util.ClassUtils;
|
import top.continew.starter.core.util.ClassUtils;
|
||||||
import top.continew.starter.core.util.ReflectUtils;
|
import top.continew.starter.core.util.ReflectUtils;
|
||||||
import top.continew.starter.core.util.TreeBuildUtils;
|
import top.continew.starter.core.util.TreeUtils;
|
||||||
import top.continew.starter.core.util.validation.CheckUtils;
|
import top.continew.starter.core.util.validation.CheckUtils;
|
||||||
import top.continew.starter.core.util.validation.ValidationUtils;
|
import top.continew.starter.core.util.validation.ValidationUtils;
|
||||||
import top.continew.starter.data.mapper.BaseMapper;
|
import top.continew.starter.data.mapper.BaseMapper;
|
||||||
@@ -132,8 +132,8 @@ public class CrudServiceImpl<M extends BaseMapper<T>, T extends BaseIdDO, L, D,
|
|||||||
Function<L, Long> getParentId = createMethodReference(listClass, CharSequenceUtil.genGetter(treeField
|
Function<L, Long> getParentId = createMethodReference(listClass, CharSequenceUtil.genGetter(treeField
|
||||||
.parentIdKey()));
|
.parentIdKey()));
|
||||||
// 构建多根节点树
|
// 构建多根节点树
|
||||||
return TreeBuildUtils.buildMultiRoot(list, getId, getParentId, treeNodeConfig, (node,
|
return TreeUtils.buildMultiRoot(list, getId, getParentId, treeNodeConfig, (node,
|
||||||
tree) -> buildTreeField(isSimple, node, tree, treeField));
|
tree) -> buildTreeField(isSimple, node, tree, treeField));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user