information-system-model-do.../src/main/java/org/gcube/informationsystem/utils/documentation/knowledge/Tree.java

54 lines
1.2 KiB
Java

package org.gcube.informationsystem.utils.documentation.knowledge;
import java.util.HashMap;
import java.util.Set;
import org.gcube.informationsystem.types.reference.Type;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class Tree {
private Node root;
private boolean allowMultipleInheritance;
private HashMap<String, Node> locate = new HashMap<String, Node>();
public Tree(Type head) {
this.root = new Node(head);
this.locate.put(head.getName(), root);
this.allowMultipleInheritance = false;
}
public void setAllowMultipleInheritance(boolean allowMultipleInheritance) {
this.allowMultipleInheritance = allowMultipleInheritance;
}
public void addNode(Type t) {
Set<String> superClasses = t.getSuperClasses();
for(String superClass : superClasses) {
Node parent = locate.get(superClass);
if(parent==null) {
throw new RuntimeException("I can find parent Node for " + t.getName() + ". Missing parent is " + superClass);
}
Node node = new Node(t);
parent.addChild(node);
locate.put(t.getName(), node);
if(!allowMultipleInheritance) {
return;
}
}
}
public Node getRoot() {
return root;
}
@Override
public String toString() {
return root.toString();
}
}