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

org.unlaxer.jaddress.model.Triplet Maven / Gradle / Ivy

The newest version!
package org.unlaxer.jaddress.model;

import java.io.Serializable;
import java.util.Objects;
import java.util.Optional;

public class Triplet implements Comparable> , Serializable{
	
	private static final long serialVersionUID = -4269236485816513769L;
	
	final T left;
	final T center;
	final T right;
	final int hashCode;
	
	public Triplet(T left, T center, T right) {
		super();
		this.left = left;
		this.center = center;
		this.right = right;
        int result = 1;
        result = 31 * result + Objects.hash(left);
        result = 31 * result + Objects.hash(center);
        result = 31 * result + Objects.hash(right);
        hashCode = result;
	}
	
	public Optional left(){
		return Optional.ofNullable(left);
	}
	
	public Optional center(){
		return Optional.ofNullable(center);
	}

	public Optional right(){
		return Optional.ofNullable(right);
	}

	
	@SuppressWarnings("unchecked")
    private static > int compareTo(Triplet o1, Triplet o2) {
        final Triplet t1 = (Triplet) o1;
        final Triplet t2 = (Triplet) o2;

        final int check1 = t1.left.compareTo(t2.left);
        if (check1 != 0) {
            return check1;
        }

        final int check2 = t1.center.compareTo(t2.center);
        if (check2 != 0) {
            return check2;
        }

        final int check3 = t1.right.compareTo(t2.right);
        if (check3 != 0) {
            return check3;
        }
        return 0;
    }

	@Override
	public int compareTo(Triplet other) {
		return Triplet.compareTo(this, other);
	}

	@Override
	public int hashCode() {
		return hashCode;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		@SuppressWarnings("unchecked")
		Triplet other = (Triplet) obj;
		if (center == null) {
			if (other.center != null)
				return false;
		} else if (!center.equals(other.center))
			return false;
		if (left == null) {
			if (other.left != null)
				return false;
		} else if (!left.equals(other.left))
			return false;
		if (right == null) {
			if (other.right != null)
				return false;
		} else if (!right.equals(other.right))
			return false;
		return true;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy