package com.vci.ubcs.starter.util.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;
|
|
public class ForestNodeManagerOid<T extends INodeOid<T>> {
|
private final ImmutableMap<String, T> nodeMap;
|
private final Map<String, Object> parentIdMap = Maps.newHashMap();
|
|
public ForestNodeManagerOid(List<T> nodes) {
|
this.nodeMap = Maps.uniqueIndex(nodes, INodeOid::getOid);
|
}
|
|
public INodeOid<T> getTreeNodeAt(String id) {
|
return this.nodeMap.containsKey(id) ? (INodeOid)this.nodeMap.get(id) : null;
|
}
|
|
public void addParentId(String parentId) {
|
this.parentIdMap.put(parentId, "");
|
}
|
|
public List<T> getRoot() {
|
List<T> roots = new ArrayList();
|
this.nodeMap.forEach((key, node) -> {
|
if (node.getParentOid() == null || this.parentIdMap.containsKey(node.getOid())) {
|
roots.add(node);
|
}
|
|
});
|
return roots;
|
}
|
}
|