All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.tankist88.carpenter.generator.dto.Node Maven / Gradle / Ivy

There is a newer version: 0.0.10
Show newest version
package com.github.tankist88.carpenter.generator.dto;

import java.util.*;

public class Node {
    private T value;
    private Node parent;
    private List> childs;

    private Comparator comparator;

    public Node() {
        this(null, null);
    }

    public Node(T value, Node parent) {
        this(value, parent, null);
    }

    public Node(T value, Node parent, Comparator comparator) {
        this.value = value;
        this.parent = parent;
        this.comparator = comparator;
    }

    public Node getParent() {
        return parent;
    }

    public List> getChilds() {
        if(childs == null) {
            childs = new ArrayList<>();
        }
        return childs;
    }

    public Node addChild(T value) {
        return addChild(value, null);
    }

    public Node addChild(T value, Comparator customComparator) {
        Node node = new Node<>(value, this, customComparator);
        getChilds().add(node);
        Collections.sort(getChilds(), getComparator());
        return node;
    }

    public T getValue() {
        return value;
    }

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

    public Comparator> getComparator() {
        if (comparator != null) {
            return new Comparator>() {
                @Override
                public int compare(Node o1, Node o2) {
                    return comparator.compare(o1.value, o2.value);
                }
            };
        } else {
            return new Comparator>() {
                @Override
                public int compare(Node o1, Node o2) {
                    Comparable c1 = (Comparable) o1.value;
                    Comparable c2 = (Comparable) o2.value;
                    return c1.compareTo(c2);
                }
            };
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Node node = (Node) o;
        return Objects.equals(value, node.value) &&
                Objects.equals(parent, node.parent);
    }

    @Override
    public int hashCode() {

        return Objects.hash(value, parent);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy