提交 | 用户 | 时间
|
722af2
|
1 |
package com.dl.common.utils; |
X |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import cn.hutool.core.lang.tree.Tree; |
|
5 |
import cn.hutool.core.lang.tree.TreeNodeConfig; |
|
6 |
import cn.hutool.core.lang.tree.TreeUtil; |
|
7 |
import cn.hutool.core.lang.tree.parser.NodeParser; |
|
8 |
import com.dl.common.utils.reflect.ReflectUtils; |
|
9 |
import lombok.AccessLevel; |
|
10 |
import lombok.NoArgsConstructor; |
|
11 |
|
|
12 |
import java.util.List; |
|
13 |
|
|
14 |
/** |
|
15 |
* 扩展 hutool TreeUtil 封装系统树构建 |
|
16 |
* |
|
17 |
* @author Lion Li |
|
18 |
*/ |
|
19 |
@NoArgsConstructor(access = AccessLevel.PRIVATE) |
|
20 |
public class TreeBuildUtils extends TreeUtil { |
|
21 |
|
|
22 |
/** |
|
23 |
* 根据前端定制差异化字段 |
|
24 |
*/ |
|
25 |
public static final TreeNodeConfig DEFAULT_CONFIG = TreeNodeConfig.DEFAULT_CONFIG.setNameKey("label"); |
|
26 |
|
|
27 |
public static <T, K> List<Tree<K>> build(List<T> list, NodeParser<T, K> nodeParser) { |
|
28 |
if (CollUtil.isEmpty(list)) { |
|
29 |
return null; |
|
30 |
} |
|
31 |
K k = ReflectUtils.invokeGetter(list.get(0), "parentId"); |
|
32 |
return TreeUtil.build(list, k, DEFAULT_CONFIG, nodeParser); |
|
33 |
} |
|
34 |
|
|
35 |
} |