增加LambdaTreeNodeConfig类

This commit is contained in:
Looly
2024-08-01 17:26:30 +08:00
parent 62529d0ba7
commit 146fb20881
2 changed files with 95 additions and 68 deletions

View File

@@ -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; package org.dromara.hutool.core.tree;
import org.dromara.hutool.core.func.LambdaUtil; import org.dromara.hutool.core.func.LambdaUtil;
@@ -11,61 +23,111 @@ import java.util.Objects;
* 避免对字段名称硬编码 * 避免对字段名称硬编码
* *
* @author Earlman * @author Earlman
* @param <T> 方法对象类型
* @param <R> 返回值类型
*/ */
public class LambdaTreeNodeConfig<R, T> extends TreeNodeConfig { public class LambdaTreeNodeConfig<T, R> extends TreeNodeConfig {
private SerFunction<R, T> idKeyFun; private static final long serialVersionUID = 1L;
private SerFunction<R, T> parentIdKeyFun; private SerFunction<T, R> idKeyFun;
private SerFunction<T, R> parentIdKeyFun;
private SerFunction<T, Comparable<?>> weightKeyFun;
private SerFunction<T, CharSequence> nameKeyFun;
private SerFunction<T, List<T>> childrenKeyFun;
private SerFunction<R, Comparable<?>> weightKeyFun; /**
* 获取ID方法
private SerFunction<R, CharSequence> nameKeyFun; * @return ID方法
*/
private SerFunction<R, List<R>> childrenKeyFun; public SerFunction<T, R> getIdKeyFun() {
public SerFunction<R, T> getIdKeyFun() {
return idKeyFun; return idKeyFun;
} }
public void setIdKeyFun(SerFunction<R, T> idKeyFun) { /**
* 设置ID方法
* @param idKeyFun ID方法
* @return this
*/
public LambdaTreeNodeConfig<T, R> setIdKeyFun(final SerFunction<T, R> idKeyFun) {
this.idKeyFun = idKeyFun; this.idKeyFun = idKeyFun;
return this;
} }
public SerFunction<R, T> getParentIdKeyFun() { /**
* 获取父ID方法
* @return 父ID方法
*/
public SerFunction<T, R> getParentIdKeyFun() {
return parentIdKeyFun; return parentIdKeyFun;
} }
public void setParentIdKeyFun(SerFunction<R, T> parentIdKeyFun) { /**
* 设置父ID方法
* @param parentIdKeyFun 父ID方法
* @return this
*/
public LambdaTreeNodeConfig<T, R> setParentIdKeyFun(final SerFunction<T, R> parentIdKeyFun) {
this.parentIdKeyFun = parentIdKeyFun; this.parentIdKeyFun = parentIdKeyFun;
return this;
} }
public SerFunction<R, Comparable<?>> getWeightKeyFun() { /**
* 设置权重方法
* @return 权重方法
*/
public SerFunction<T, Comparable<?>> getWeightKeyFun() {
return weightKeyFun; return weightKeyFun;
} }
public void setWeightKeyFun(SerFunction<R, Comparable<?>> weightKeyFun) { /**
* 设置权重方法
* @param weightKeyFun 权重方法
* @return this
*/
public LambdaTreeNodeConfig<T, R> setWeightKeyFun(final SerFunction<T, Comparable<?>> weightKeyFun) {
this.weightKeyFun = weightKeyFun; this.weightKeyFun = weightKeyFun;
return this;
} }
public SerFunction<R, CharSequence> getNameKeyFun() { /**
* 获取节点名称方法
* @return 节点名称方法
*/
public SerFunction<T, CharSequence> getNameKeyFun() {
return nameKeyFun; return nameKeyFun;
} }
public void setNameKeyFun(SerFunction<R, CharSequence> nameKeyFun) { /**
* 设置节点名称方法
* @param nameKeyFun 节点名称方法
* @return this
*/
public LambdaTreeNodeConfig<T, R> setNameKeyFun(final SerFunction<T, CharSequence> nameKeyFun) {
this.nameKeyFun = nameKeyFun; this.nameKeyFun = nameKeyFun;
return this;
} }
public SerFunction<R, List<R>> getChildrenKeyFun() { /**
* 获取子节点名称方法
* @return 子节点名称方法
*/
public SerFunction<T, List<T>> getChildrenKeyFun() {
return childrenKeyFun; return childrenKeyFun;
} }
public void setChildrenKeyFun(SerFunction<R, List<R>> childrenKeyFun) { /**
* 设置子节点名称方法
* @param childrenKeyFun 子节点名称方法
* @return this
*/
public LambdaTreeNodeConfig<T, R> setChildrenKeyFun(final SerFunction<T, List<T>> childrenKeyFun) {
this.childrenKeyFun = childrenKeyFun; this.childrenKeyFun = childrenKeyFun;
return this;
} }
@Override @Override
public String getIdKey() { public String getIdKey() {
SerFunction<?, ?> serFunction = getIdKeyFun(); final SerFunction<?, ?> serFunction = getIdKeyFun();
if (Objects.isNull(serFunction)) { if (Objects.isNull(serFunction)) {
return super.getIdKey(); return super.getIdKey();
} }
@@ -74,7 +136,7 @@ public class LambdaTreeNodeConfig<R, T> extends TreeNodeConfig {
@Override @Override
public String getParentIdKey() { public String getParentIdKey() {
SerFunction<?, ?> serFunction = getParentIdKeyFun(); final SerFunction<?, ?> serFunction = getParentIdKeyFun();
if (Objects.isNull(serFunction)) { if (Objects.isNull(serFunction)) {
return super.getParentIdKey(); return super.getParentIdKey();
} }
@@ -83,7 +145,7 @@ public class LambdaTreeNodeConfig<R, T> extends TreeNodeConfig {
@Override @Override
public String getWeightKey() { public String getWeightKey() {
SerFunction<?, ?> serFunction = getWeightKeyFun(); final SerFunction<?, ?> serFunction = getWeightKeyFun();
if (Objects.isNull(serFunction)) { if (Objects.isNull(serFunction)) {
return super.getWeightKey(); return super.getWeightKey();
} }
@@ -92,7 +154,7 @@ public class LambdaTreeNodeConfig<R, T> extends TreeNodeConfig {
@Override @Override
public String getNameKey() { public String getNameKey() {
SerFunction<?, ?> serFunction = getNameKeyFun(); final SerFunction<?, ?> serFunction = getNameKeyFun();
if (Objects.isNull(serFunction)) { if (Objects.isNull(serFunction)) {
return super.getNameKey(); return super.getNameKey();
} }
@@ -101,7 +163,7 @@ public class LambdaTreeNodeConfig<R, T> extends TreeNodeConfig {
@Override @Override
public String getChildrenKey() { public String getChildrenKey() {
SerFunction<?, ?> serFunction = getChildrenKeyFun(); final SerFunction<?, ?> serFunction = getChildrenKeyFun();
if (Objects.isNull(serFunction)) { if (Objects.isNull(serFunction)) {
return super.getChildrenKey(); return super.getChildrenKey();
} }

View File

@@ -12,6 +12,7 @@
package org.dromara.hutool.core.tree; package org.dromara.hutool.core.tree;
import lombok.Data;
import org.dromara.hutool.core.collection.ListUtil; import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.lang.Console; import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.tree.parser.DefaultNodeParser; import org.dromara.hutool.core.tree.parser.DefaultNodeParser;
@@ -145,10 +146,13 @@ public class TreeTest {
Assertions.assertEquals(7, ids2.size()); Assertions.assertEquals(7, ids2.size());
} }
/**
* https://gitee.com/dromara/hutool/pulls/1248/
*/
@Test @Test
public void lambdaConfigTest() { public void lambdaConfigTest() {
// 配置自定义属性名 为null则取默认值 // 配置自定义属性名 为null则取默认值
LambdaTreeNodeConfig<CustomTreeNode, String> treeNodeConfig = new LambdaTreeNodeConfig<>(); final LambdaTreeNodeConfig<CustomTreeNode, String> treeNodeConfig = new LambdaTreeNodeConfig<>();
treeNodeConfig.setChildrenKeyFun(CustomTreeNode::getChildrenNodes); treeNodeConfig.setChildrenKeyFun(CustomTreeNode::getChildrenNodes);
treeNodeConfig.setIdKeyFun(CustomTreeNode::getNodeId); treeNodeConfig.setIdKeyFun(CustomTreeNode::getNodeId);
treeNodeConfig.setNameKeyFun(CustomTreeNode::getLabel); treeNodeConfig.setNameKeyFun(CustomTreeNode::getLabel);
@@ -157,9 +161,9 @@ public class TreeTest {
// 最大递归深度 // 最大递归深度
treeNodeConfig.setDeep(3); treeNodeConfig.setDeep(3);
List<MapTree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig, new DefaultNodeParser<>()); final List<MapTree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig, new DefaultNodeParser<>());
Assertions.assertEquals(treeNodes.size(), 2); Assertions.assertEquals(treeNodes.size(), 2);
MapTree<String> treeNode1 = treeNodes.get(1); final MapTree<String> treeNode1 = treeNodes.get(1);
Assertions.assertNotNull(treeNode1); Assertions.assertNotNull(treeNode1);
Assertions.assertNotNull(treeNode1.getConfig()); Assertions.assertNotNull(treeNode1.getConfig());
Assertions.assertEquals(treeNode1.getChildren().size(), 1); Assertions.assertEquals(treeNode1.getChildren().size(), 1);
@@ -170,7 +174,8 @@ public class TreeTest {
* *
* @author Earlman * @author Earlman
*/ */
private class CustomTreeNode { @Data
static class CustomTreeNode {
// 主键ID // 主键ID
private String nodeId; private String nodeId;
// 节点名称 // 节点名称
@@ -181,45 +186,5 @@ public class TreeTest {
private Integer sortNo; private Integer sortNo;
// 子节点 // 子节点
private List<CustomTreeNode> childrenNodes; private List<CustomTreeNode> 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<CustomTreeNode> getChildrenNodes() {
return childrenNodes;
}
public void setChildrenNodes(List<CustomTreeNode> childrenNodes) {
this.childrenNodes = childrenNodes;
}
} }
} }