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

dcutils.tuples.Pair 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 two items.
* @author dca */ public class Pair implements Iterable, Comparator { /** The first item of the pair.
*/ private A first; /** The second item of the pair.
*/ private B second; /** * The constructor initializes the items of the pair.
* @param first The first item of the pair.
* @param second The second item of the pair.
*/ public Pair(A first, B second) { first(first); second(second); } // END constructor /** * The getter for the first item.
* Returns the first item of the pair.
* @return first The first item of the pair.
*/ public A first() { return first; } // END first /** * The setter for the first item.
* Sets the first item of the pair.
* @param first The first item of the pair.
* @return this.
*/ public Pair first(A first) { this.first = first; return this; } // END first /** * The getter for the second item.
* Returns the second item of the pair.
* @return second The second item of the pair.
*/ public B second() { return second; } // END second /** * The setter for the second item.
* Sets the second item of the pair.
* @param second The second item of the pair.
* @return this.
*/ public Pair second(B second) { this.second = second; return this; } // END second /** * Returns the pair 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 pair.
*/ @Override public String toString() { return Arrays.toString(toArray()); } // END toString /** * Decides if an object is equal to this pair.
* In order for an object to be equal to this pair:
* the object must also be a pair, must not be null, and each of its items must equal this pair'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 Pair) { Pair pair = (Pair)obj; return ( 0 == compare(first, pair.first()) && 0 == compare(second, pair.second()) ); } 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 Pair.
* @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 pair.
* The hash code uses prime numbers and the hash codes of this pair'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 pair.
* @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 pair's items.
* @return Object[] The object array of the pair's items.
*/ public Object[] toArray() { return new Object[] {first, second}; } // END toArray } // END class Pair