package org.craftercms.studio.impl.repository.mongodb.api.model;

import java.util.HashSet;
import java.util.Set;


/**
 * Tree node of type T.
 * @param <T> Any class that implements Comparable
 */
public class TreeNode<T extends Comparable<T>> {

    private T value;
    private TreeNode<T> parent;
    private Set<TreeNode<T>> children;

    public TreeNode() {
    }

    public TreeNode(final T value, final TreeNode<T> parent, final Set<TreeNode<T>> children) {
        this.value = value;
        this.parent = parent;
        this.children = children;
    }

    public void addChild(final T child) {
        addChild(new TreeNode<>(child, this, null));
    }

    public void addChild(final TreeNode<T> treeNode) {
        if (this.children == null) {
            children = new HashSet<TreeNode<T>>();
        }
        this.children.add(treeNode);
    }

    public T getValue() {
        return value;
    }

    public void setValue(final T value) {
        this.value = value;
    }

    public TreeNode<T> getParent() {
        return parent;
    }

    public void setParent(final TreeNode<T> parent) {
        this.parent = parent;
    }

    public Set<TreeNode<T>> getChildren() {
        return children;
    }

    public void setChildren(final Set<TreeNode<T>> children) {
        this.children = children;
    }
}
