public class TreeNode<K,V> implements Serializable {
private static final long serialVersionUID = 1L;
private int level = 0;
private String uuid;
private K key;
private V value;
@JsonIgnore
private TreeNode<K,V> parent;
private List<TreeNode<K,V>> children;
@JsonIgnore
private HashMap<K, List<TreeNode<K,V>>> lookup;
public TreeNode(K key, V value) {
this.level = 0;
this.key = key;
this.uuid = UUID.randomUUID().toString();
this.value = value;
this.children = new LinkedList<TreeNode<K,V>>();
List<TreeNode<K, V>> thisAsList = new ArrayList<TreeNode<K,V>>();
thisAsList.add(this);
this.lookup = new HashMap<K, List<TreeNode<K,V>>>();
this.lookup.put(key, thisAsList);
}
public TreeNode<K,V> addChild(K key, V value) {
TreeNode<K,V> childNode = new TreeNode<K,V>(key, value);
childNode.level = level +1;
childNode.parent = this;
childNode.lookup = lookup;
childNode.uuid = UUID.randomUUID().toString();
this.children.add(childNode);
List<TreeNode<K, V>> l = lookup.get(childNode.key);
if(l==null){
l = new ArrayList<TreeNode<K,V>>();
lookup.put(childNode.key, l);
}
l.add(childNode);
return childNode;
}
public boolean isLeaf() {
return children.isEmpty() ;
}
public int getLevel() {
return level;
}
public TreeNode<K,V> getParent() {
return parent;
}
public V getValue() {
return value;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public List<TreeNode<K,V>> getChildren() {
return children;
}
public List<TreeNode<K, V>> getNodesByKey(K key) {
return lookup.get(key);
}
public K getKey() {
return key;
}
public List<TreeNode<K,V>> getLeafs() {
List<TreeNode<K,V>> ret = new ArrayList<TreeNode<K,V>>();
getLeafs(ret);
return ret;
}
private void getLeafs(List<TreeNode<K, V>> ret) {
if(children.isEmpty())
ret.add(this);
for (TreeNode<K, V> child : children) {
child.getLeafs(ret);
}
}
@Override
public String toString() {
return toString(true);
}
public String toString(boolean withChildren) {
if(key==null)
return super.toString();
StringBuffer ret = new StringBuffer();
for (int i = 0; i < level; i++) {
ret.append(" >");
}
ret.append(" " +key.toString());
if(withChildren){
for (TreeNode<K, V> child : children) {
ret.append("\n").append(child.toString());
}
}
return ret.toString();
}
public void setValue(V value) {
this.value = value;
}
public void remove(List<TreeNode<K, V>> nodes) {
for (TreeNode<K, V> n : nodes) {
removeChildren(n);
}
for (TreeNode<K, V> n : nodes) {
TreeNode<K, V> parent = n.getParent();
if(parent==null)
continue;
parent.children.remove(n);
}
}
private void removeChildren(TreeNode<K, V> node) {
lookup.remove(node.getUuid());
if(node.children.isEmpty())
return;
Iterator<TreeNode<K, V>> it = node.children.iterator();
while (it.hasNext()) {
TreeNode<K, V> child = (TreeNode<K, V>) it.next();
removeChildren(child);
it.remove();
}
}
public void clear() {
this.key = null;
this.value = null;
this.uuid = null;
for (TreeNode<K, V> child : children) {
child.clear();
}
this.children.clear();
this.lookup.clear();
}
public TreeNode<K, V> getNodeById(K key, String uuid) {
List<TreeNode<K, V>> nodes = getNodesByKey(key);
for (TreeNode<K, V> treeNode : nodes) {
if(uuid.equals(treeNode.getUuid()))
return treeNode;
}
return null;
}
public HashMap<K, List<TreeNode<K, V>>> getLookup() {
return lookup;
}
public void setLookup(HashMap<K, List<TreeNode<K, V>>> lookup) {
this.lookup = lookup;
}
public void setLevel(int level) {
this.level = level;
}
public void setKey(K key) {
this.key = key;
}
public void setParent(TreeNode<K, V> parent) {
this.parent = parent;
}
public void setChildren(List<TreeNode<K, V>> children) {
this.children = children;
}