From 146fb20881eebac2f74aa9428223e24af8117f38 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 1 Aug 2024 17:26:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0LambdaTreeNodeConfig=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/tree/LambdaTreeNodeConfig.java | 110 ++++++++++++++---- .../dromara/hutool/core/tree/TreeTest.java | 53 ++------- 2 files changed, 95 insertions(+), 68 deletions(-) diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/tree/LambdaTreeNodeConfig.java b/hutool-core/src/main/java/org/dromara/hutool/core/tree/LambdaTreeNodeConfig.java index b40086e6c..941e85dee 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/tree/LambdaTreeNodeConfig.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/tree/LambdaTreeNodeConfig.java @@ -1,3 +1,15 @@ +/* + * Copyright (c) 2024. looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * https://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + package org.dromara.hutool.core.tree; import org.dromara.hutool.core.func.LambdaUtil; @@ -11,61 +23,111 @@ import java.util.Objects; * 避免对字段名称硬编码 * * @author Earlman + * @param 方法对象类型 + * @param 返回值类型 */ -public class LambdaTreeNodeConfig extends TreeNodeConfig { - private SerFunction idKeyFun; +public class LambdaTreeNodeConfig extends TreeNodeConfig { + private static final long serialVersionUID = 1L; - private SerFunction parentIdKeyFun; + private SerFunction idKeyFun; + private SerFunction parentIdKeyFun; + private SerFunction> weightKeyFun; + private SerFunction nameKeyFun; + private SerFunction> childrenKeyFun; - private SerFunction> weightKeyFun; - - private SerFunction nameKeyFun; - - private SerFunction> childrenKeyFun; - - public SerFunction getIdKeyFun() { + /** + * 获取ID方法 + * @return ID方法 + */ + public SerFunction getIdKeyFun() { return idKeyFun; } - public void setIdKeyFun(SerFunction idKeyFun) { + /** + * 设置ID方法 + * @param idKeyFun ID方法 + * @return this + */ + public LambdaTreeNodeConfig setIdKeyFun(final SerFunction idKeyFun) { this.idKeyFun = idKeyFun; + return this; } - public SerFunction getParentIdKeyFun() { + /** + * 获取父ID方法 + * @return 父ID方法 + */ + public SerFunction getParentIdKeyFun() { return parentIdKeyFun; } - public void setParentIdKeyFun(SerFunction parentIdKeyFun) { + /** + * 设置父ID方法 + * @param parentIdKeyFun 父ID方法 + * @return this + */ + public LambdaTreeNodeConfig setParentIdKeyFun(final SerFunction parentIdKeyFun) { this.parentIdKeyFun = parentIdKeyFun; + return this; } - public SerFunction> getWeightKeyFun() { + /** + * 设置权重方法 + * @return 权重方法 + */ + public SerFunction> getWeightKeyFun() { return weightKeyFun; } - public void setWeightKeyFun(SerFunction> weightKeyFun) { + /** + * 设置权重方法 + * @param weightKeyFun 权重方法 + * @return this + */ + public LambdaTreeNodeConfig setWeightKeyFun(final SerFunction> weightKeyFun) { this.weightKeyFun = weightKeyFun; + return this; } - public SerFunction getNameKeyFun() { + /** + * 获取节点名称方法 + * @return 节点名称方法 + */ + public SerFunction getNameKeyFun() { return nameKeyFun; } - public void setNameKeyFun(SerFunction nameKeyFun) { + /** + * 设置节点名称方法 + * @param nameKeyFun 节点名称方法 + * @return this + */ + public LambdaTreeNodeConfig setNameKeyFun(final SerFunction nameKeyFun) { this.nameKeyFun = nameKeyFun; + return this; } - public SerFunction> getChildrenKeyFun() { + /** + * 获取子节点名称方法 + * @return 子节点名称方法 + */ + public SerFunction> getChildrenKeyFun() { return childrenKeyFun; } - public void setChildrenKeyFun(SerFunction> childrenKeyFun) { + /** + * 设置子节点名称方法 + * @param childrenKeyFun 子节点名称方法 + * @return this + */ + public LambdaTreeNodeConfig setChildrenKeyFun(final SerFunction> childrenKeyFun) { this.childrenKeyFun = childrenKeyFun; + return this; } @Override public String getIdKey() { - SerFunction serFunction = getIdKeyFun(); + final SerFunction serFunction = getIdKeyFun(); if (Objects.isNull(serFunction)) { return super.getIdKey(); } @@ -74,7 +136,7 @@ public class LambdaTreeNodeConfig extends TreeNodeConfig { @Override public String getParentIdKey() { - SerFunction serFunction = getParentIdKeyFun(); + final SerFunction serFunction = getParentIdKeyFun(); if (Objects.isNull(serFunction)) { return super.getParentIdKey(); } @@ -83,7 +145,7 @@ public class LambdaTreeNodeConfig extends TreeNodeConfig { @Override public String getWeightKey() { - SerFunction serFunction = getWeightKeyFun(); + final SerFunction serFunction = getWeightKeyFun(); if (Objects.isNull(serFunction)) { return super.getWeightKey(); } @@ -92,7 +154,7 @@ public class LambdaTreeNodeConfig extends TreeNodeConfig { @Override public String getNameKey() { - SerFunction serFunction = getNameKeyFun(); + final SerFunction serFunction = getNameKeyFun(); if (Objects.isNull(serFunction)) { return super.getNameKey(); } @@ -101,7 +163,7 @@ public class LambdaTreeNodeConfig extends TreeNodeConfig { @Override public String getChildrenKey() { - SerFunction serFunction = getChildrenKeyFun(); + final SerFunction serFunction = getChildrenKeyFun(); if (Objects.isNull(serFunction)) { return super.getChildrenKey(); } diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/tree/TreeTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/tree/TreeTest.java index 4164707f5..d8438e298 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/tree/TreeTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/tree/TreeTest.java @@ -12,6 +12,7 @@ package org.dromara.hutool.core.tree; +import lombok.Data; import org.dromara.hutool.core.collection.ListUtil; import org.dromara.hutool.core.lang.Console; import org.dromara.hutool.core.tree.parser.DefaultNodeParser; @@ -145,10 +146,13 @@ public class TreeTest { Assertions.assertEquals(7, ids2.size()); } + /** + * https://gitee.com/dromara/hutool/pulls/1248/ + */ @Test public void lambdaConfigTest() { // 配置自定义属性名 为null则取默认值 - LambdaTreeNodeConfig treeNodeConfig = new LambdaTreeNodeConfig<>(); + final LambdaTreeNodeConfig treeNodeConfig = new LambdaTreeNodeConfig<>(); treeNodeConfig.setChildrenKeyFun(CustomTreeNode::getChildrenNodes); treeNodeConfig.setIdKeyFun(CustomTreeNode::getNodeId); treeNodeConfig.setNameKeyFun(CustomTreeNode::getLabel); @@ -157,9 +161,9 @@ public class TreeTest { // 最大递归深度 treeNodeConfig.setDeep(3); - List> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig, new DefaultNodeParser<>()); + final List> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig, new DefaultNodeParser<>()); Assertions.assertEquals(treeNodes.size(), 2); - MapTree treeNode1 = treeNodes.get(1); + final MapTree treeNode1 = treeNodes.get(1); Assertions.assertNotNull(treeNode1); Assertions.assertNotNull(treeNode1.getConfig()); Assertions.assertEquals(treeNode1.getChildren().size(), 1); @@ -170,7 +174,8 @@ public class TreeTest { * * @author Earlman */ - private class CustomTreeNode { + @Data + static class CustomTreeNode { // 主键ID private String nodeId; // 节点名称 @@ -181,45 +186,5 @@ public class TreeTest { private Integer sortNo; // 子节点 private List childrenNodes; - - public String getNodeId() { - return nodeId; - } - - public void setNodeId(String nodeId) { - this.nodeId = nodeId; - } - - public String getLabel() { - return label; - } - - public void setLabel(String label) { - this.label = label; - } - - public String getParentNodeId() { - return parentNodeId; - } - - public void setParentNodeId(String parentNodeId) { - this.parentNodeId = parentNodeId; - } - - public Integer getSortNo() { - return sortNo; - } - - public void setSortNo(Integer sortNo) { - this.sortNo = sortNo; - } - - public List getChildrenNodes() { - return childrenNodes; - } - - public void setChildrenNodes(List childrenNodes) { - this.childrenNodes = childrenNodes; - } } }