From 5d0af2dad56e1c83d5da6d761a7a2df549273047 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sat, 2 Mar 2024 14:48:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20API=EF=BC=8C=E4=BD=BF=20Tr?= =?UTF-8?q?eeBuilder=20=E5=AE=9E=E4=BE=8B=E5=8F=AF=E4=BB=A5=E5=A4=8D?= =?UTF-8?q?=E7=94=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 +++++ .../plusone/commons/util/TreeBuilder.java | 27 +++++++++---------- .../commons/util/TreeBuilderTests.java | 16 ++++++----- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index d3a38df..bb64491 100644 --- a/pom.xml +++ b/pom.xml @@ -81,6 +81,13 @@ test + + com.google.code.gson + gson + 2.10.1 + test + + diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/TreeBuilder.java b/src/main/java/xyz/zhouxy/plusone/commons/util/TreeBuilder.java index 71790d4..6be4bfe 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/TreeBuilder.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/TreeBuilder.java @@ -11,32 +11,29 @@ import java.util.stream.Collectors; import xyz.zhouxy.plusone.commons.collection.CollectionTools; public class TreeBuilder { - private final Collection nodes; private final Function identityGetter; private final Function> parentIdentityGetter; - private final BiConsumer addChildrenMethod; + private final BiConsumer addChildMethod; - public TreeBuilder(Collection nodes, Function identityGetter, - Function> parentIdentityGetter, BiConsumer addChildren) { - this.nodes = nodes; + public TreeBuilder(Function identityGetter, Function> parentIdentityGetter, + BiConsumer addChild) { this.identityGetter = identityGetter; this.parentIdentityGetter = parentIdentityGetter; - this.addChildrenMethod = addChildren; + this.addChildMethod = addChild; } - public List buildTree() { + public List buildTree(Collection nodes) { Map identityNodeMap = CollectionTools.toHashMap(nodes, identityGetter); - List result = this.nodes.stream() + List result = nodes.stream() .filter(node -> !this.parentIdentityGetter.apply(node).isPresent()) .collect(Collectors.toList()); - for (T node : this.nodes) { - Optional parentIdentity = parentIdentityGetter.apply(node); - if (parentIdentity.isPresent() && identityNodeMap.containsKey(parentIdentity.get())) { - @SuppressWarnings("all") - TSubTree parentNode = (TSubTree) identityNodeMap.get(parentIdentity.get()); - addChildrenMethod.accept(parentNode, node); + nodes.forEach(node -> parentIdentityGetter.apply(node).ifPresent(parentIdentity -> { + if (identityNodeMap.containsKey(parentIdentity)) { + @SuppressWarnings("unchecked") + TSubTree parentNode = (TSubTree) identityNodeMap.get(parentIdentity); + addChildMethod.accept(parentNode, node); } - } + })); return result; } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/TreeBuilderTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/TreeBuilderTests.java index e3a4d43..14b3828 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/TreeBuilderTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/TreeBuilderTests.java @@ -8,12 +8,17 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.collect.Lists; +import com.google.gson.Gson; import lombok.ToString; class TreeBuilderTests { private static final Logger log = LoggerFactory.getLogger(TreeBuilderTests.class); + private final TreeBuilder treeBuilder = new TreeBuilder<>( + Menu::getMenuCode, + Menu::getParentMenuCode, + MenuList::addChild); @Test void testBuildTree() { @@ -31,13 +36,10 @@ class TreeBuilderTests { MenuItem.of("C", "C2", "二级菜单C2", "/c/c2"), MenuItem.of("C", "C3", "二级菜单C3", "/c/c3") ); - List menuTree = new TreeBuilder<>( - menus, - Menu::getMenuCode, - Menu::getParentMenuCode, - MenuList::addChild) - .buildTree(); - log.info("menuTree: {}", menuTree); + + List menuTree = treeBuilder.buildTree(menus); + log.info("menuTree: {}", new Gson().toJson(menuTree)); + } }