package org.gcube.informationsystem.tree; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * @author Luca Frosini (ISTI - CNR) */ public class Tree { private boolean allowMultipleInheritance; private Node root; private NodeInformation ni; private Map> locate; public Tree(T t, NodeInformation ni) { this.allowMultipleInheritance = true; this.ni = ni; this.locate = new HashMap<>(); this.root = new Node<>(t); this.root.setTree(this); String identifier = ni.getIdentifier(t); this.locate.put(identifier, root); } public void setAllowMultipleInheritance(boolean allowMultipleInheritance) { this.allowMultipleInheritance = allowMultipleInheritance; } public NodeInformation getNodeInformation() { return ni; } public Node addNode(T t) { String identifier = ni.getIdentifier(t); if(locate.containsKey(identifier)) { // has been already added return locate.get(identifier); } Node node = new Node<>(t); node.setTree(this); Set parentIdentifiers = ni.getParentIdentifiers(root.getNodeElement(), t); for(String parentIdentifier : parentIdentifiers) { Node parentNode = locate.get(parentIdentifier); if(parentNode==null) { throw new RuntimeException("I can find parent for " + identifier + ". Missing parent is " + parentIdentifier); } parentNode.addChild(node); if(!allowMultipleInheritance) { break; } } this.locate.put(identifier, node); return node; } public Node getRoot() { return root; } @Override public String toString() { return root.toString(); } public void elaborate(NodeElaborator nodeElaborator) throws Exception { root.elaborate(nodeElaborator); } }