package com.qianwen.core.tool.node;
|
|
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.Maps;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
import com.qianwen.core.tool.node.INode;
|
import com.qianwen.core.tool.utils.StringPool;
|
|
/* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/node/ForestNodeManager.class */
|
public class ForestNodeManager<T extends INode<T>> {
|
private final ImmutableMap<Long, T> nodeMap;
|
private final Map<Long, Object> parentIdMap = Maps.newHashMap();
|
|
public ForestNodeManager(List<T> nodes) {
|
this.nodeMap = Maps.uniqueIndex(nodes, (v0) -> {
|
return v0.getId();
|
});
|
}
|
|
public INode<T> getTreeNodeAt(Long id) {
|
if (this.nodeMap.containsKey(id))
|
return (INode<T>) this.nodeMap.get(id);
|
|
return null;
|
}
|
|
public void addParentId(Long parentId) {
|
this.parentIdMap.put(parentId, StringPool.EMPTY);
|
}
|
|
public List<T> getRoot() {
|
|
List<T> roots = new ArrayList<>();
|
this.nodeMap.forEach((key, node) -> {
|
if (node.getParentId().longValue() == 0L || this.parentIdMap.containsKey(node.getId()))
|
roots.add(node);
|
});
|
return roots;
|
}
|
}
|