edu.neu.ccs.quick
Class Pair

java.lang.Object
  extended byedu.neu.ccs.quick.Pair

public final class Pair
extends Object

Pair constructs an immutable pair object from two non-null objects that are referred to as A and B.

The methods equals and hashCode are designed so that two Pair objects whose contents are equal will be considered equal and will return the same hash code.

Since:
2.3
Version:
2.3

Field Summary
private  Object a
          Item A of the pair.
private  Object b
          Item B of the pair.
 
Constructor Summary
Pair(Object[] data)
          Constructor that constructs a pair using a non-null array of Object such that the array has 2 non-null items.
Pair(Object a, Object b)
          Constructor that constructs a pair using two non-null Objects.
 
Method Summary
 boolean equals(Object entity)
          If the given entity is an instance of Pair and if the corresponding items in this pair and the given entity are equal, then returns true; otherwise returns false.
 Object getA()
          Returns the A object in the pair.
static Object[] getAObjects(Pair[] pairs)
          Returns the array of A-objects from this array of pairs.
 Object getB()
          Returns the B object in the pair.
static Object[] getBObjects(Pair[] pairs)
          Returns the array of B-objects from this array of pairs.
 int hashCode()
          Returns a hash code value for this pair that is computed as the exclusive OR of the hash code values for the two items in the pair.
static Pair makePair(Object[] data)
          Factory method that returns a Pair if the data array is non-null and has exactly two non-null items; otherwise returns null.
static Pair makePair(Object a, Object b)
          Factory method that returns a Pair if the two objects are non-null; otherwise returns null.
 Object[] toArray()
          Returns the contents of the pair as an array of Object of length 2.
static Pair[] toArray(Object[][] array)
          Returns the contents of the given Object[][] array as a Pair[] array.
static Object[][] toArray(Pair[] pairs)
          Returns the contents of the given Pair[] array as an Object[][] array.
 String toString()
          Returns the String constructed as: (astring, bstring), where astring is the toString for item A and bstring is the toString for item B.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

a

private Object a
Item A of the pair.


b

private Object b
Item B of the pair.

Constructor Detail

Pair

public Pair(Object a,
            Object b)

Constructor that constructs a pair using two non-null Objects.

Parameters:
a - the A object for the pair
b - the B object for the pair
Throws:
NullPointerException - if either data item is null

Pair

public Pair(Object[] data)

Constructor that constructs a pair using a non-null array of Object such that the array has 2 non-null items.

Parameters:
data - the array with exactly two non-null items
Throws:
NullPointerException - if data is null or if an array item in data is null
IllegalArgumentException - if data does not have length 2
Method Detail

getA

public Object getA()
Returns the A object in the pair.

Returns:
object A in the pair

getB

public Object getB()
Returns the B object in the pair.

Returns:
object B in the pair

equals

public boolean equals(Object entity)

If the given entity is an instance of Pair and if the corresponding items in this pair and the given entity are equal, then returns true; otherwise returns false.

Parameters:
entity - the entity to compare to this pair
Returns:
true if the given entity is a pair with equal items to those in this pair

hashCode

public int hashCode()

Returns a hash code value for this pair that is computed as the exclusive OR of the hash code values for the two items in the pair.

Returns:
the hash code

toString

public String toString()

Returns the String constructed as: (astring, bstring), where astring is the toString for item A and bstring is the toString for item B.

Returns:
the string formed as (A, B) from the items A, B in the pair

toArray

public Object[] toArray()

Returns the contents of the pair as an array of Object of length 2.

Returns:
the pair as an array

makePair

public static Pair makePair(Object a,
                            Object b)

Factory method that returns a Pair if the two objects are non-null; otherwise returns null.

Does not throw any exceptions.

Parameters:
a - the A object for the pair
b - the B object for the pair

makePair

public static Pair makePair(Object[] data)

Factory method that returns a Pair if the data array is non-null and has exactly two non-null items; otherwise returns null.

Does not throw any exceptions.

Parameters:
data - the data with which to form the pair if possible
See Also:
Pair(Object[])

toArray

public static Object[][] toArray(Pair[] pairs)

Returns the contents of the given Pair[] array as an Object[][] array.

Parameters:
pairs - the 1-dimensional pair array
Returns:
the 2-dimensional object array

toArray

public static Pair[] toArray(Object[][] array)

Returns the contents of the given Object[][] array as a Pair[] array.

Uses makePair(Object[]) to make the individual items in the returned Pair[] array. In this way, items in the original array that cannot make a suitable pair will map to null.

Parameters:
array - the 2-dimensional object array
Returns:
the 1-dimensional pair array

getAObjects

public static Object[] getAObjects(Pair[] pairs)
Returns the array of A-objects from this array of pairs.

Parameters:
pairs - the 1-dimensional pair array
Returns:
the array of A-objects

getBObjects

public static Object[] getBObjects(Pair[] pairs)
Returns the array of B-objects from this array of pairs.

Parameters:
pairs - the 1-dimensional pair array
Returns:
the array of B-objects