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

dcutils.tuples.Triplet Maven / Gradle / Ivy

package dcutils.tuples;

// Import Java JDK Classes
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

/**
 * Contains exactly three items.
* @author dca */ public class Triplet implements Iterable, Comparator { /** The first item of the triplet.
*/ private A first; /** The second item of the triplet.
*/ private B second; /** The third item of the triplet.
*/ private C third; /** * The constructor initializes the items of the triplet.
* @param first The first item of the triplet.
* @param second The second item of the triplet.
* @param third The third item of the triplet.
*/ public Triplet(A first, B second, C third) { first(first); second(second); third(third); } // END constructor /** * The getter for the first item.
* Returns the first item of the triplet.
* @return first The first item of the triplet.
*/ public A first() { return first; } // END first /** * The setter for the first item.
* Sets the first item of the triplet.
* @param first The first item of the triplet.
* @return this.
*/ public Triplet first(A first) { this.first = first; return this; } // END first /** * The getter for the second item.
* Returns the second item of the triplet.
* @return second The second item of the triplet.
*/ public B second() { return second; } // END second /** * The setter for the second item.
* Sets the second item of the triplet.
* @param second The second item of the triplet.
* @return this.
*/ public Triplet second(B second) { this.second = second; return this; } // END second /** * The getter for the third item.
* Returns the third item of the triplet.
* @return third The third item of the triplet.
*/ public C third() { return third; } // END third /** * The setter for the third item.
* Sets the third item of the triplet.
* @param third The third item of the triplet.
* @return this.
*/ public Triplet third(C third) { this.third = third; return this; } // END third /** * Returns the triplet information as a tuple.
* The format of the tuple is: [first, second, third].
* If any of the items are null, then the word "null" will be shown in its place.
* @return String A tuple format of the triplet.
*/ @Override public String toString() { return Arrays.toString(toArray()); } // END toString /** * Decides if an object is equal to this triplet.
* In order for an object to be equal to this triplet:
* the object must also be a triplet, must not be null, and each of its items must equal this triplet's items.
* @param obj The item to check for value equality.
* @return True or false
*/ @Override public boolean equals(Object obj) { if(null == obj) { return false; } else if(obj instanceof Triplet) { Triplet tri = (Triplet)obj; return ( 0 == compare(first, tri.first()) && 0 == compare(second, tri.second()) && 0 == compare(third, tri.third()) ); } else { return false; } // END if/else } // END equals /** * Compares two arguments.
* If they are the same, "0" will be returned.
* If they are different, "-1" will be returned.
* This method is used primarily by the equals(Object) method to compare internal fields to those of another Triplet.
* @param lhs The left-hand-side argument.
* @param rhs The right-hand-side argument.
* @see #equals(Object) */ @Override public int compare(Object lhs, Object rhs) { if(null == lhs ^ null == rhs) return -1; // If one or the other are null -> they are not the same. else if(null == lhs && null == rhs) return 0; // If both are null -> they are the same. else return (lhs.equals(rhs) ? 0 : -1); // If both are defined and equal -> they are the same. } // END compare /** * Creates a hash code for this triplet.
* The hash code uses prime numbers and the hash codes of this triplet's items.
* @return integer The hash code.
*/ @Override public int hashCode() { int hashCode = 101; for(Object item : this) { hashCode = (11 * hashCode) + (null == item ? -1 : item.hashCode()); } // END loop return hashCode; } // END hashCode /** * Returns an iterator over the items in this triplet.
* @return The iterator.
*/ @Override public Iterator iterator() { final List ITEMS = new ArrayList(); Collections.addAll(ITEMS, toArray()); return ITEMS.iterator(); } // END iterator /** * Creates a new array and defines it with the triplet's items.
* @return Object[] The object array of the triplet's items.
*/ public Object[] toArray() { return new Object[] {first, second, third}; } // END toArray } // END class Triplet