info.javelot.functionalj.tuple
Class Pair<A,B>

java.lang.Object
  extended by info.javelot.functionalj.tuple.Pair<A,B>
All Implemented Interfaces:
Serializable, Comparable<Pair>
Direct Known Subclasses:
Triple

public class Pair<A,B>
extends Object
implements Comparable<Pair>, Serializable

Used to create a tuple of two objects. Tuples can be used anywhere there is a need to group objects together, and Pairs are used by the Tuples.zip(Collection, Collection) method.

Author:
Copyright © 2006 Frederic Daoud
See Also:
Serialized Form

Field Summary
 Function1<A,Pair<A,B>> first
          Function that returns the first element of the tuple.
 Function2<Pair<A,B>,A,B> pair
          Function that creates a Pair.
 Function1<B,Pair<A,B>> second
          Function that returns the second element of the tuple.
 
Constructor Summary
Pair()
          Default constructor.
Pair(A p_first, B p_second)
          Creates a tuple containing the two given objects.
 
Method Summary
 int compareTo(Pair p_pair)
          Sort tuples according to the objects that they contain.
 boolean equals(Object p_object)
          Determines if the given object is equal to this object, by comparing the two objects of each tuple using their respective equals(Object) methods.
 A getFirst()
          Returns the first object of the tuple.
 B getSecond()
          Returns the second object of the tuple.
protected  List<String> getStrings()
          Returns a list of the String representations of the objects of the tuple.
 int hashCode()
          Returns the hash code associated with this object.
 void setFirst(A p_first)
          Sets the first object of the tuple.
 void setSecond(B p_second)
          Sets the second object of the tuple.
 String toString()
          Returns a String representation of the Pair.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

pair

public Function2<Pair<A,B>,A,B> pair
Function that creates a Pair.


first

public Function1<A,Pair<A,B>> first
Function that returns the first element of the tuple.


second

public Function1<B,Pair<A,B>> second
Function that returns the second element of the tuple.

Constructor Detail

Pair

public Pair()
Default constructor.


Pair

public Pair(A p_first,
            B p_second)
Creates a tuple containing the two given objects.

Parameters:
p_first - the first object of the pair. A null value is accepted.
p_second - the second object of the pair. A null value is accepted.
Method Detail

getFirst

public A getFirst()
Returns the first object of the tuple.

Returns:
the first object of the tuple, which may be null.

setFirst

public void setFirst(A p_first)
Sets the first object of the tuple.

Parameters:
p_first - the first object of the tuple.

getSecond

public B getSecond()
Returns the second object of the tuple.

Returns:
the second object of the tuple, which may be null.

setSecond

public void setSecond(B p_second)
Sets the second object of the tuple.

Parameters:
p_second - the second object of the tuple.

hashCode

public int hashCode()
Returns the hash code associated with this object.

Thanks to Paul Field for suggesting the improvement on this hash function.

Overrides:
hashCode in class Object
Returns:
the hash code associated with this object, which is calculated by adding together the hash codes of the objects contained in the tuple, multiplying by 31 each time. A null value is considered to have* a hash code of 0. Thus, if this pair contains two null values, this method returns 0.

equals

public boolean equals(Object p_object)
Determines if the given object is equal to this object, by comparing the two objects of each tuple using their respective equals(Object) methods. Corresponding null values are also considered equal.

Overrides:
equals in class Object
Parameters:
p_object - the object against which to compare this tuple.
Returns:
true if the pairs contain the same values; false otherwise.

compareTo

public int compareTo(Pair p_pair)
Sort tuples according to the objects that they contain. If the contained in the tuples do not implement the Comparable interface, they are left unsorted.

Specified by:
compareTo in interface Comparable<Pair>

getStrings

protected List<String> getStrings()
Returns a list of the String representations of the objects of the tuple. Subclasses override this method to specify additional objects. The toString() method uses this to construct the String representation of the tuple.

Returns:
a list of the String representations of the objects of the tuple.

toString

public String toString()
Returns a String representation of the Pair.

Overrides:
toString in class Object
Returns:
a String representation of the Pair of the form "(String, String)".