cn.woodwhales.common.business.tree.TreeNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of woodwhales-common Show documentation
Show all versions of woodwhales-common Show documentation
https://github.com/woodwhales
package cn.woodwhales.common.business.tree;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import static java.util.Objects.nonNull;
import static java.util.stream.Collectors.toList;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
/**
* @author woodwhales on 2020-12-11
* 通用节点容器对象
*/
public class TreeNode {
/**
* ID
*/
private K id;
/**
* 父节点ID
*/
private K parentId;
/**
* 当前节点的名称
*/
private String name;
/**
* 源数据
*/
private T data;
/**
* 子节点集合
*/
private List> children;
/**
* 排序值
*/
private int sort;
private TreeNode() {
}
/**
* 构建 TreeNode 对象
* @param data 源数据
* @param treeNodeGenerator TreeNode 生成器
* @param 节点 id 类型
* @param 源数据类型
* @return TreeNode
*/
public static TreeNode build(T data, TreeNodeGenerator treeNodeGenerator) {
return build(data, treeNodeGenerator, false);
}
/**
* 构建 TreeNode 对象
* @param data 原始数据
* @param treeNodeGenerator 树节点生成器
* @param withData 是否携带数据
* @param key泛型
* @param value泛型
* @return 返回数节点对象,根节点
*/
public static TreeNode build(T data,
TreeNodeGenerator treeNodeGenerator,
boolean withData) {
TreeNode treeNode = new TreeNode<>();
treeNode.setId(treeNodeGenerator.getId(data));
if(withData) {
treeNode.setData(data);
}
treeNode.setName(treeNodeGenerator.getName(data));
treeNode.setParentId(treeNodeGenerator.getParentId(data));
treeNode.setSort(treeNodeGenerator.getSort(data));
return treeNode;
}
public static Map toMap(TreeNode treeNode,
final TreeNodeAttributeMapper treeAttributeMapper,
final Function extraFunction,
final boolean withData,
final boolean needDropData) {
Map map = new HashMap<>(7);
Function overNodeIdFunction = treeAttributeMapper.getOverNodeIdFunction();
T data = treeNode.getData();
// 设置当前节点id
if(nonNull(overNodeIdFunction) && nonNull(data)) {
map.put(treeAttributeMapper.getNodeId(), overNodeIdFunction.apply(data));
} else {
map.put(treeAttributeMapper.getNodeId(), treeNode.getId());
}
// 设置当前节点的名称
map.put(treeAttributeMapper.getNodeName(), treeNode.getName());
// 设置当前节点的父节点
map.put(treeAttributeMapper.getParentId(), treeNode.getParentId());
// 设置当前节点的排序值
map.put(treeAttributeMapper.getSortName(), treeNode.getSort());
// 设置当前节点的子节点
if(isNotEmpty(treeNode.getChildren())) {
map.put(treeAttributeMapper.getChildrenName(), treeNode.getChildren().stream()
.map(node -> TreeNode.toMap(node, treeAttributeMapper, extraFunction, withData, needDropData))
.collect(toList()));
} else {
map.put(treeAttributeMapper.getChildrenName(), null);
}
// 设置当前节点是否携带源数据
if(withData) {
map.put(treeAttributeMapper.getDataName(), data);
} else {
map.put(treeAttributeMapper.getDataName(), null);
}
// 设置当前节点的扩展数据
if(nonNull(extraFunction) && nonNull(data)) {
map.put(treeAttributeMapper.getExtraName(), extraFunction.apply(data));
} else {
map.put(treeAttributeMapper.getExtraName(), null);
}
if(needDropData) {
map.remove(treeAttributeMapper.getDataName());
}
return map;
}
public K getId() {
return id;
}
public K getParentId() {
return parentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public T getData() {
return data;
}
public Integer getSort() {
return sort;
}
public void setId(K id) {
this.id = id;
}
public void setParentId(K parentId) {
this.parentId = parentId;
}
public void setData(T data) {
this.data = data;
}
public void setSort(int sort) {
this.sort = sort;
}
public void setChildren(List> children) {
this.children = children;
}
public List> getChildren() {
return children;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy