package com.project.common.core.absclasses;
|
|
|
import com.project.common.core.interfaces.ITreeNode;
|
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.Maps;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
|
public class TreeNodeManager<T extends ITreeNode<T>>
|
{
|
|
private final ImmutableMap<Long, T> nodeMap;
|
private final Map<Long, Object> parentIdMap = Maps.newHashMap();
|
|
public TreeNodeManager(List<T> nodes) {
|
this.nodeMap = Maps.uniqueIndex(nodes, ITreeNode::getId);
|
}
|
|
public ITreeNode<T> getTreeNodeAt(Long id) {
|
return this.nodeMap.containsKey(id) ? (ITreeNode) this.nodeMap.get(id) : null;
|
}
|
|
public void addParentId(Long parentId) {
|
this.parentIdMap.put(parentId, "");
|
}
|
|
public List<T> getRoot() {
|
List<T> roots = new ArrayList();
|
this.nodeMap.forEach((key, node) -> {
|
if (node.getParentId() == 0L || this.parentIdMap.containsKey(node.getId())) {
|
roots.add(node);
|
}
|
|
});
|
return roots;
|
}
|
}
|