mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
TreeUtil.buildSingle指定rootId节点存在时,作为根节点
This commit is contained in:
parent
be98b46349
commit
5cf262d85a
@ -2,7 +2,7 @@
|
|||||||
# 🚀Changelog
|
# 🚀Changelog
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.33(2024-09-29)
|
# 5.8.33(2024-10-02)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
* 【core 】 SyncFinisher增加setExecutorService方法(issue#IANKQ1@Gitee)
|
* 【core 】 SyncFinisher增加setExecutorService方法(issue#IANKQ1@Gitee)
|
||||||
@ -14,6 +14,7 @@
|
|||||||
* 【http 】 HttpRequest增加setFixedContentLength方法(issue#3462@Github)
|
* 【http 】 HttpRequest增加setFixedContentLength方法(issue#3462@Github)
|
||||||
* 【db 】 AbstractDb增加getDs方法(issue#IARKZL@Gitee)
|
* 【db 】 AbstractDb增加getDs方法(issue#IARKZL@Gitee)
|
||||||
* 【db 】 QrCodeUtil添加二维码logo支持配置圆角(pr#3747@Github)
|
* 【db 】 QrCodeUtil添加二维码logo支持配置圆角(pr#3747@Github)
|
||||||
|
* 【core 】 TreeUtil.buildSingle指定rootId节点存在时,作为根节点(issue#IAUSHR@Gitee)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【json 】 修复JSONConfig.setDateFormat设置后toBean无效问题(issue#3713@Github)
|
* 【json 】 修复JSONConfig.setDateFormat设置后toBean无效问题(issue#3713@Github)
|
||||||
|
@ -10,6 +10,7 @@ import cn.hutool.core.util.ObjectUtil;
|
|||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 树构建器
|
* 树构建器
|
||||||
@ -19,7 +20,7 @@ import java.util.Map;
|
|||||||
public class TreeBuilder<E> implements Builder<Tree<E>> {
|
public class TreeBuilder<E> implements Builder<Tree<E>> {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private final Tree<E> root;
|
private Tree<E> root;
|
||||||
private final Map<E, Tree<E>> idTreeMap;
|
private final Map<E, Tree<E>> idTreeMap;
|
||||||
private boolean isBuild;
|
private boolean isBuild;
|
||||||
|
|
||||||
@ -53,8 +54,17 @@ public class TreeBuilder<E> implements Builder<Tree<E>> {
|
|||||||
* @param config 配置
|
* @param config 配置
|
||||||
*/
|
*/
|
||||||
public TreeBuilder(E rootId, TreeNodeConfig config) {
|
public TreeBuilder(E rootId, TreeNodeConfig config) {
|
||||||
root = new Tree<>(config);
|
this(new Tree<E>(config).setId(rootId));
|
||||||
root.setId(rootId);
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造
|
||||||
|
*
|
||||||
|
* @param rootNode 根节点
|
||||||
|
* @since 5.8.33
|
||||||
|
*/
|
||||||
|
public TreeBuilder(Tree<E> rootNode) {
|
||||||
|
this.root = rootNode;
|
||||||
this.idTreeMap = new LinkedHashMap<>();
|
this.idTreeMap = new LinkedHashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,7 +167,28 @@ public class TreeBuilder<E> implements Builder<Tree<E>> {
|
|||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
public <T> TreeBuilder<E> append(List<T> list, NodeParser<T, E> nodeParser) {
|
public <T> TreeBuilder<E> append(List<T> list, NodeParser<T, E> nodeParser) {
|
||||||
return append(list, null, nodeParser);
|
checkBuilt();
|
||||||
|
|
||||||
|
final TreeNodeConfig config = this.root.getConfig();
|
||||||
|
final E rootId = this.root.getId();
|
||||||
|
final Map<E, Tree<E>> map = this.idTreeMap;
|
||||||
|
Tree<E> node;
|
||||||
|
for (T t : list) {
|
||||||
|
node = new Tree<>(config);
|
||||||
|
nodeParser.parse(t, node);
|
||||||
|
if (null != rootId && false == rootId.getClass().equals(node.getId().getClass())) {
|
||||||
|
throw new IllegalArgumentException("rootId type is node.getId().getClass()!");
|
||||||
|
}
|
||||||
|
// issue#IAUSHR 如果指定根节点存在,直接复用
|
||||||
|
if(Objects.equals(rootId, node.getId())){
|
||||||
|
this.root = node;
|
||||||
|
}else {
|
||||||
|
//非根节点
|
||||||
|
map.put(node.getId(), node);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return append(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -169,12 +200,14 @@ public class TreeBuilder<E> implements Builder<Tree<E>> {
|
|||||||
* @param nodeParser 节点转换器,用于定义一个Bean如何转换为Tree节点
|
* @param nodeParser 节点转换器,用于定义一个Bean如何转换为Tree节点
|
||||||
* @return this
|
* @return this
|
||||||
* @since 5.8.6
|
* @since 5.8.6
|
||||||
|
* @deprecated rootId参数可以不提供,在root节点中直接获取,请使用{@link #append(List, NodeParser)}
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public <T> TreeBuilder<E> append(List<T> list, E rootId, NodeParser<T, E> nodeParser) {
|
public <T> TreeBuilder<E> append(List<T> list, E rootId, NodeParser<T, E> nodeParser) {
|
||||||
checkBuilt();
|
checkBuilt();
|
||||||
|
|
||||||
final TreeNodeConfig config = this.root.getConfig();
|
final TreeNodeConfig config = this.root.getConfig();
|
||||||
final Map<E, Tree<E>> map = new LinkedHashMap<>(list.size(), 1);
|
final Map<E, Tree<E>> map = this.idTreeMap;
|
||||||
Tree<E> node;
|
Tree<E> node;
|
||||||
for (T t : list) {
|
for (T t : list) {
|
||||||
node = new Tree<>(config);
|
node = new Tree<>(config);
|
||||||
@ -182,7 +215,14 @@ public class TreeBuilder<E> implements Builder<Tree<E>> {
|
|||||||
if (null != rootId && false == 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()!");
|
throw new IllegalArgumentException("rootId type is node.getId().getClass()!");
|
||||||
}
|
}
|
||||||
map.put(node.getId(), node);
|
// issue#IAUSHR 如果指定根节点存在,直接复用
|
||||||
|
if(Objects.equals(rootId, node.getId())){
|
||||||
|
this.root = node;
|
||||||
|
}else {
|
||||||
|
//非根节点
|
||||||
|
map.put(node.getId(), node);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return append(map);
|
return append(map);
|
||||||
}
|
}
|
||||||
|
@ -124,7 +124,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, rootId, nodeParser).build();
|
.append(list, nodeParser).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -0,0 +1,60 @@
|
|||||||
|
package cn.hutool.core.lang.tree;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.tree.parser.NodeParser;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果指定rootId的节点已经存在,直接作为根节点
|
||||||
|
*/
|
||||||
|
public class IssueIAUSHRTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void buildSingleTest() {
|
||||||
|
final List<TestDept> list= new ArrayList<>();
|
||||||
|
TestDept sysDept = new TestDept();
|
||||||
|
sysDept.setDeptId(1L);
|
||||||
|
sysDept.setDeptName("A");
|
||||||
|
sysDept.setParentId(null);
|
||||||
|
list.add(sysDept);
|
||||||
|
|
||||||
|
sysDept = new TestDept();
|
||||||
|
sysDept.setDeptId(2L);
|
||||||
|
sysDept.setDeptName("B");
|
||||||
|
sysDept.setParentId(1L);
|
||||||
|
list.add(sysDept);
|
||||||
|
|
||||||
|
sysDept = new TestDept();
|
||||||
|
sysDept.setDeptId(3L);
|
||||||
|
sysDept.setDeptName("C");
|
||||||
|
sysDept.setParentId(1L);
|
||||||
|
list.add(sysDept);
|
||||||
|
|
||||||
|
TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
|
||||||
|
treeNodeConfig.setIdKey("deptId");
|
||||||
|
treeNodeConfig.setNameKey("deptName");
|
||||||
|
treeNodeConfig.setParentIdKey("parentId");
|
||||||
|
NodeParser<TestDept,Long> nodeParser= (dept, tree) ->
|
||||||
|
tree.setId(dept.getDeptId())
|
||||||
|
.setParentId(dept.getParentId())
|
||||||
|
.setName(dept.getDeptName());
|
||||||
|
Tree<Long> longTree = TreeUtil.buildSingle(list, 1L, treeNodeConfig, nodeParser);
|
||||||
|
|
||||||
|
assertEquals("A", longTree.getName());
|
||||||
|
assertEquals(2, longTree.getChildren().size());
|
||||||
|
assertEquals("B", longTree.getChildren().get(0).getName());
|
||||||
|
assertEquals("C", longTree.getChildren().get(1).getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class TestDept {
|
||||||
|
private Long deptId;
|
||||||
|
private String deptName;
|
||||||
|
private Long parentId;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user