mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
Tree add 类型校验
This commit is contained in:
parent
a60aa0ec47
commit
6196f83caa
@ -171,6 +171,32 @@ public class TreeBuilder<E> implements Builder<Tree<E>> {
|
|||||||
return append(map);
|
return append(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 增加节点列表,增加的节点是不带子节点的
|
||||||
|
*
|
||||||
|
* @param list Bean列表
|
||||||
|
* @param <T> Bean类型
|
||||||
|
* @param nodeParser 节点转换器,用于定义一个Bean如何转换为Tree节点
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public <T> TreeBuilder<E> append(List<T> list, E rootId, NodeParser<T, E> nodeParser) {
|
||||||
|
checkBuilt();
|
||||||
|
|
||||||
|
final TreeNodeConfig config = this.root.getConfig();
|
||||||
|
final Map<E, Tree<E>> map = new LinkedHashMap<>(list.size(), 1);
|
||||||
|
Tree<E> node;
|
||||||
|
for (T t : list) {
|
||||||
|
node = new Tree<>(config);
|
||||||
|
nodeParser.parse(t, node);
|
||||||
|
if (!rootId.getClass().equals(node.getId().getClass())) {
|
||||||
|
throw new IllegalArgumentException("rootId type is node.getId().getClass()!");
|
||||||
|
}
|
||||||
|
map.put(node.getId(), node);
|
||||||
|
}
|
||||||
|
return append(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 重置Builder,实现复用
|
* 重置Builder,实现复用
|
||||||
|
@ -123,7 +123,7 @@ public class TreeUtil {
|
|||||||
*/
|
*/
|
||||||
public static <T, E> Tree<E> buildSingle(List<T> list, E rootId, TreeNodeConfig treeNodeConfig, NodeParser<T, E> nodeParser) {
|
public static <T, E> Tree<E> buildSingle(List<T> list, E rootId, TreeNodeConfig treeNodeConfig, NodeParser<T, E> nodeParser) {
|
||||||
return TreeBuilder.of(rootId, treeNodeConfig)
|
return TreeBuilder.of(rootId, treeNodeConfig)
|
||||||
.append(list, nodeParser).build();
|
.append(list, rootId, nodeParser).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -0,0 +1,91 @@
|
|||||||
|
package cn.hutool.core.lang.tree;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Issues2538Test {
|
||||||
|
|
||||||
|
@org.junit.Test
|
||||||
|
public void issues2538Test() {
|
||||||
|
Test test1 = new Test();
|
||||||
|
test1.setId(1);
|
||||||
|
test1.setParentId(0);
|
||||||
|
test1.setName("1");
|
||||||
|
Test test2 = new Test();
|
||||||
|
test2.setId(2);
|
||||||
|
test2.setParentId(1);
|
||||||
|
test2.setName("1");
|
||||||
|
|
||||||
|
List<Test> list = new ArrayList<>();
|
||||||
|
|
||||||
|
list.add(test1);
|
||||||
|
list.add(test2);
|
||||||
|
// 配置
|
||||||
|
TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
|
||||||
|
// 自定义属性名 都要默认值的
|
||||||
|
treeNodeConfig.setIdKey("id");
|
||||||
|
treeNodeConfig.setDeep(Integer.MAX_VALUE);
|
||||||
|
treeNodeConfig.setParentIdKey("parentId");
|
||||||
|
|
||||||
|
// 转换器
|
||||||
|
List<Tree<Object>> treeNodes = TreeUtil.build(list, 0L,
|
||||||
|
treeNodeConfig,
|
||||||
|
(treeNode, tree) -> {
|
||||||
|
tree.setId(treeNode.getId());
|
||||||
|
tree.setParentId(treeNode.getParentId());
|
||||||
|
tree.setName(treeNode.getName());
|
||||||
|
tree.setWeight(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.assertNotNull(treeNodes);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 转换器
|
||||||
|
treeNodes = TreeUtil.build(list, "0",
|
||||||
|
treeNodeConfig,
|
||||||
|
(treeNode, tree) -> {
|
||||||
|
tree.setId(treeNode.getId());
|
||||||
|
tree.setParentId(treeNode.getParentId());
|
||||||
|
tree.setName(treeNode.getName());
|
||||||
|
tree.setWeight(null);
|
||||||
|
});
|
||||||
|
}catch (Exception e) {
|
||||||
|
Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Test {
|
||||||
|
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
private long parentId;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getParentId() {
|
||||||
|
return parentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParentId(long parentId) {
|
||||||
|
this.parentId = parentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user