Tree add 类型校验

This commit is contained in:
Looly 2022-08-20 01:06:37 +08:00
parent 0fc10205a0
commit b69ae38829
3 changed files with 6 additions and 39 deletions

View File

@ -10,6 +10,7 @@
* 【core 】 DateUtil.parseUTC支持只有时分的格式issue#I5M6DP@Gitee
* 【core 】 NumberUtil.parseInt忽略科学计数法issue#I5M55F@Gitee
* 【core 】 IterUtil.getFirst优化pr#753@Gitee
* 【core 】 增加Tree add 类型校验pr#2542@Github
*
### 🐞Bug修复
* 【http 】 修复https下可能的Patch、Get请求失效问题issue#I3Z3DH@Gitee

View File

@ -158,17 +158,7 @@ public class TreeBuilder<E> implements Builder<Tree<E>> {
* @return this
*/
public <T> TreeBuilder<E> append(List<T> list, 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);
map.put(node.getId(), node);
}
return append(map);
return append(list, null, nodeParser);
}
/**
@ -178,6 +168,7 @@ public class TreeBuilder<E> implements Builder<Tree<E>> {
* @param <T> Bean类型
* @param nodeParser 节点转换器用于定义一个Bean如何转换为Tree节点
* @return this
* @since 5.8.6
*/
public <T> TreeBuilder<E> append(List<T> list, E rootId, NodeParser<T, E> nodeParser) {
checkBuilt();
@ -188,7 +179,7 @@ public class TreeBuilder<E> implements Builder<Tree<E>> {
for (T t : list) {
node = new Tree<>(config);
nodeParser.parse(t, node);
if (!rootId.getClass().equals(node.getId().getClass())) {
if (null != rootId && false == rootId.getClass().equals(node.getId().getClass())) {
throw new IllegalArgumentException("rootId type is node.getId().getClass()!");
}
map.put(node.getId(), node);

View File

@ -1,5 +1,6 @@
package cn.hutool.core.lang.tree;
import lombok.Data;
import org.junit.Assert;
import java.util.ArrayList;
@ -56,36 +57,10 @@ public class Issues2538Test {
}
}
@Data
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;
}
}
}