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

dcutils.tuples.Sextet Maven / Gradle / Ivy

There is a newer version: 1.8.6
Show newest version
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 six items.
* @author dca */ public class Sextet implements Iterable, Comparator { /** The first item of the sextet.
*/ private A first; /** The second item of the sextet.
*/ private B second; /** The third item of the sextet.
*/ private C third; /** The fourth item of the sextet.
*/ private D fourth; /** The fifth item of the sextet.
*/ private E fifth; /** The sixth item of the sextet.
*/ private F sixth; /** * The constructor initializes the items of the sextet.
* @param first The first item of the sextet.
* @param second The second item of the sextet.
* @param third The third item of the sextet.
* @param fourth The fourth item of the sextet.
* @param fifth The fifth item of the sextet.
* @param sixth The sixth item of the sextet.
*/ public Sextet(A first, B second, C third, D fourth, E fifth, F sixth) { first(first); second(second); third(third); fourth(fourth); fifth(fifth); sixth(sixth); } // END constructor /** * The getter for the first item.
* Returns the first item of the sextet.
* @return first The first item of the sextet.
*/ public A first() { return first; } // END first /** * The setter for the first item.
* Sets the first item of the sextet.
* @param first The first item of the sextet.
* @return this.
*/ public Sextet first(A first) { this.first = first; return this; } // END first /** * The getter for the second item.
* Returns the second item of the sextet.
* @return second The second item of the sextet.
*/ public B second() { return second; } // END second /** * The setter for the second item.
* Sets the second item of the sextet.
* @param second The second item of the sextet.
* @return this.
*/ public Sextet second(B second) { this.second = second; return this; } // END second /** * The getter for the third item.
* Returns the third item of the sextet.
* @return third The third item of the sextet.
*/ public C third() { return third; } // END third /** * The setter for the third item.
* Sets the third item of the sextet.
* @param third The third item of the sextet.
* @return this.
*/ public Sextet third(C third) { this.third = third; return this; } // END third /** * The getter for the fourth item.
* Returns the fourth item of the sextet.
* @return fourth The fourth item of the sextet.
*/ public D fourth() { return fourth; } // END fourth /** * The setter for the fourth item.
* Sets the fourth item of the sextet.
* @param fourth The fourth item of the sextet.
* @return this.
*/ public Sextet fourth(D fourth) { this.fourth = fourth; return this; } // END fourth /** * The getter for the fifth item.
* Returns the fifth item of the sextet.
* @return fifth The fifth item of the sextet.
*/ public E fifth() { return fifth; } // END fifth /** * The setter for the fifth item.
* Sets the fifth item of the sextet.
* @param fifth The fifth item of the sextet.
* @return this.
*/ public Sextet fifth(E fifth) { this.fifth = fifth; return this; } // END fifth /** * The getter for the sixth item.
* Returns the sixth item of the sextet.
* @return sixth The sixth item of the sextet.
*/ public F sixth() { return sixth; } // END sixth /** * The setter for the sixth item.
* Sets the sixth item of the sextet.
* @param sixth The sixth item of the sextet.
* @return this.
*/ public Sextet sixth(F sixth) { this.sixth = sixth; return this; } // END sixth /** * Returns the sextet information as a tuple.
* The format of the tuple is: [first, second, third, fourth, fifth, sixth].
* If any of the items are null, then the word "null" will be shown in its place.
* @return String A tuple format of the sextet.
*/ @Override public String toString() { return Arrays.toString(toArray()); } // END toString /** * Decides if an object is equal to this sextet.
* In order for an object to be equal to this sextet:
* the object must also be a sextet, must not be null, and each of its items must equal this sextet'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 Sextet) { Sextet sex = (Sextet)obj; return ( 0 == compare(first, sex.first()) && 0 == compare(second, sex.second()) && 0 == compare(third, sex.third()) && 0 == compare(fourth, sex.fourth()) && 0 == compare(fifth, sex.fifth()) && 0 == compare(sixth, sex.sixth()) ); } 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 Sextet.
* @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 sextet.
* The hash code uses prime numbers and the hash codes of this sextet'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 sextet.
* @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 sextet's items.
* @return Object[] The object array of the sextet's items.
*/ public Object[] toArray() { return new Object[] {first, second, third, fourth, fifth, sixth}; } // END toArray } // END class Sextet