diff --git a/commons/common-util/src/main/java/com/schbrain/common/util/TreeUtils.java b/commons/common-util/src/main/java/com/schbrain/common/util/TreeUtils.java index 6aa78d25db61a6dc38233a163124d48811a85da5..8897a0b4fa47d24e2ba1ee5dae91959191fa1ce4 100644 --- a/commons/common-util/src/main/java/com/schbrain/common/util/TreeUtils.java +++ b/commons/common-util/src/main/java/com/schbrain/common/util/TreeUtils.java @@ -51,34 +51,19 @@ public class TreeUtils { return doBuildTree(keyExtractor, childrenSetter, childMapper, parentWithSubNodes, childrenComparator, parentId); } - private static > Map> buildParentWithSubNodes(C nodes, - Function parentKeyExtractor, - @Nullable K parentId) { - Map> parentWithSubNodes = StreamUtils.groupBy(nodes, parentKeyExtractor, true); - if (parentId == null) { - // groupBy 不允许 key 为空,当 parentId 为空时,单独处理下 - List subNodes = StreamUtils.filterToList(nodes, node -> parentKeyExtractor.apply(node) == null); - parentWithSubNodes.put(null, subNodes); - } - return parentWithSubNodes; - } - - /** - * @param preBrotherFlag true: previousId false:nextId - */ public static > List buildTree(C nodes, Function keyExtractor, Function parentKeyExtractor, Function brotherKeyExtractor, BiConsumer> childrenSetter, - @Nullable K parentId, @Nullable K rootBrotherId, + @Nullable K parentId, + @Nullable K rootBrotherId, boolean preBrotherFlag) { if (CollectionUtils.isEmpty(nodes)) { return new ArrayList<>(); } Map> parentWithSubNodes = buildParentWithSubNodes(nodes, parentKeyExtractor, parentId); - return doBuildTree(keyExtractor, brotherKeyExtractor, childrenSetter, parentWithSubNodes, parentId, - rootBrotherId, preBrotherFlag); + return doBuildTree(keyExtractor, brotherKeyExtractor, childrenSetter, parentWithSubNodes, parentId, rootBrotherId, preBrotherFlag); } public static List getParents(E self, Collection nodes, Function keyMapper, Function parentMapper, boolean includeSelf) { @@ -107,6 +92,16 @@ public class TreeUtils { return nodes; } + private static > Map> buildParentWithSubNodes(C nodes, Function parentKeyExtractor, @Nullable K parentId) { + Map> parentWithSubNodes = StreamUtils.groupBy(nodes, parentKeyExtractor, true); + if (parentId == null) { + // groupBy 不允许 key 为空,当 parentId 为空时,单独处理下 + List subNodes = StreamUtils.filterToList(nodes, node -> parentKeyExtractor.apply(node) == null); + parentWithSubNodes.put(null, subNodes); + } + return parentWithSubNodes; + } + private static void doBuildNodeList(Collection tree, Function> childGetter, Function mapper, List nodes) { if (CollectionUtils.isEmpty(tree)) { return; @@ -144,32 +139,31 @@ public class TreeUtils { return treeList; } - private static void sort(List data, Comparator comparator) { - if (comparator != null && CollectionUtils.isNotEmpty(data)) { - ListUtil.sort(data, comparator); - } - } - private static List doBuildTree(Function keyExtractor, Function brotherKeyExtractor, BiConsumer> childrenSetter, Map> parentWithSubNodes, - K parentId, K rootBrotherId, boolean preBrotherFlag) { + K parentId, K rootBrotherId, + boolean preBrotherFlag) { List subNodes = parentWithSubNodes.remove(parentId); if (CollectionUtils.isEmpty(subNodes)) { return Collections.emptyList(); } subNodes.forEach(subNode -> { - List children = doBuildTree(keyExtractor, brotherKeyExtractor, childrenSetter, - parentWithSubNodes, keyExtractor.apply(subNode), rootBrotherId, preBrotherFlag); + List children = doBuildTree(keyExtractor, brotherKeyExtractor, childrenSetter, parentWithSubNodes, keyExtractor.apply(subNode), rootBrotherId, preBrotherFlag); childrenSetter.accept(subNode, children); }); sort(subNodes, keyExtractor, brotherKeyExtractor, rootBrotherId, preBrotherFlag); return subNodes; } - private static void sort(List dataList, Function keyExtractor, Function brotherKeyExtractor, - K rootBrotherId, boolean preBrotherFlag) { + private static void sort(List data, Comparator comparator) { + if (comparator != null && CollectionUtils.isNotEmpty(data)) { + ListUtil.sort(data, comparator); + } + } + + private static void sort(List dataList, Function keyExtractor, Function brotherKeyExtractor, K rootBrotherId, boolean preBrotherFlag) { if (CollectionUtils.isEmpty(dataList)) { return; } @@ -185,11 +179,10 @@ public class TreeUtils { break; } } - //避免脏数据,则直接返回原列表 + // 避免脏数据,则直接返回原列表 if (sortList.size() != dataList.size()) { - System.out.println(JacksonUtils.toJsonString(sortList)); log.warn("Sort Warning:{}", JacksonUtils.toJsonString(dataList)); - return ; + return; } if (!preBrotherFlag) { Collections.reverse(sortList); diff --git a/pom.xml b/pom.xml index 569f63d634a441cb40777656d18f44ab91c35878..f6d07c2f7019782aead7467bd0b353012e7d8bc7 100644 --- a/pom.xml +++ b/pom.xml @@ -705,41 +705,35 @@ - - - - schbrain - - true - - - - libs-public - ${maven.repository.url}/maven-public - - true - - - true - always - - - + + + libs-public + ${maven.repository.url}/maven-public + + true + + + true + always + + + - - - libs-public - ${maven.repository.url}/maven-public - - true - - - true - always - - - - + + + libs-public + ${maven.repository.url}/maven-public + + true + + + true + always + + + + + dev