package no.uio.ifi.trackfind.frontend.data;

import java.util.AbstractMap;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Stream;

public class TreeNode implements Comparable<TreeNode> {

    private Map.Entry<String, Object> node;

    public TreeNode(Map<String, Object> root) {
        this.node = new AbstractMap.SimpleEntry<>(null, root);
    }

    private TreeNode(Map.Entry<String, Object> node) {
        this.node = node;
    }

    @SuppressWarnings("unchecked")
    public Stream<TreeNode> fetchChildren() {
        Object value = node.getValue();
        if (value instanceof Map) {
            return ((Map<String, Object>) value).entrySet().stream().map(TreeNode::new);
        } else if (value instanceof Collection) {
            return ((Collection<String>) value).stream().map(s -> new AbstractMap.SimpleEntry<>(s, null)).map(TreeNode::new);
        } else {
            return Stream.empty();
        }
    }

    public boolean hasChildren() {
        return node.getValue() != null;
    }

    @Override
    public int compareTo(TreeNode that) {
        return String.valueOf(this).compareTo(String.valueOf(that));
    }

    @Override
    public String toString() {
        return node.getKey();
    }

}

@@ -209,16 +209,45 @@
 ode%3E %7B%0A%0A
+    private TreeNode parent;%0A
     priv
@@ -327,24 +327,52 @@
 ct%3E root) %7B%0A
+        this.parent = null;%0A
         this
@@ -450,16 +450,33 @@
 reeNode(
+TreeNode parent, 
 Map.Entr
@@ -518,19 +518,113 @@
 his.
-node = node
+parent = parent;%0A        this.node = node;%0A    %7D%0A%0A    public TreeNode getParent() %7B%0A        return parent
 ;%0A  
@@ -854,37 +854,50 @@
 tream().map(
-TreeNode::new
+e -%3E new TreeNode(this, e)
 );%0A        %7D
@@ -1051,21 +1051,34 @@
 map(
-TreeNode::new
+e -%3E new TreeNode(this, e)
 );%0A 
@@ -1168,19 +1168,14 @@
 ean 
-hasChildren
+isLeaf
 () %7B
@@ -1210,9 +1210,9 @@
 e() 
-!
+=
 = nu

Refactor TreeNode.