package com.infoclinika.pdx.projection;

import java.util.Objects;

/**
 * Projection for single cluster node.
 */
public class ClusterNode {
    private final Integer index;
    private final Integer parentIndex;
    private final float coordinate;

    /**
     * Initializes all fields.
     */
    public ClusterNode(Integer index, Integer parentIndex, float coordinate) {
        this.index = index;
        this.parentIndex = parentIndex;
        this.coordinate = coordinate;
    }

    public Integer getIndex() {
        return index;
    }

    /**
     * Node with parent index `0` is the root node, so parent index
     * should be replaced with `null`.
     */
    public Integer getParentIndex() {
        return parentIndex == 0
               ? null
               : parentIndex;
    }

    public float getCoordinate() {
        return coordinate;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        final ClusterNode that = (ClusterNode) o;
        return Float.compare(that.getCoordinate(), getCoordinate()) == 0
               && Objects.equals(getIndex(), that.getIndex())
               && Objects.equals(getParentIndex(), that.getParentIndex());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getIndex(), getParentIndex(), getCoordinate());
    }

    @Override
    public String toString() {
        return "ClusterNode{"
               + "index=" + index
               + ", parentIndex=" + parentIndex
               + ", coordinate=" + coordinate
               + '}';
    }
}
