edu.stanford.nlp.util
Class Pair<T1,T2>

java.lang.Object
  extended by edu.stanford.nlp.util.Pair<T1,T2>
All Implemented Interfaces:
Serializable, Comparable<Pair<T1,T2>>
Direct Known Subclasses:
Interval

public class Pair<T1,T2>
extends Object
implements Comparable<Pair<T1,T2>>, Serializable

Pair is a Class for holding mutable pairs of objects. Implementation note: on a 32-bit JVM uses ~ 8 (this) + 4 (first) + 4 (second) = 16 bytes. on a 64-bit JVM uses ~ 16 (this) + 8 (first) + 8 (second) = 32 bytes. Many applications use a lot of Pairs so it's good to keep this number small.

Author:
Dan Klein, Christopher Manning (added stuff from Kristina's, rounded out)
See Also:
Serialized Form

Field Summary
 T1 first
          Direct access is deprecated.
 T2 second
          Direct access is deprecated.
 
Constructor Summary
Pair()
           
Pair(T1 first, T2 second)
           
 
Method Summary
 int compareTo(Pair<T1,T2> another)
          Compares this Pair to another object.
 boolean equals(Object o)
           
 T1 first()
           
 int hashCode()
           
static Pair<String,String> internedStringPair(String first, String second)
          Returns an MutableInternedPair where the Strings have been interned.
static
<X,Y> Pair<X,Y>
makePair(X x, Y y)
          Returns a Pair constructed from X and Y.
static Pair<String,String> readStringPair(DataInputStream in)
          Read a string representation of a Pair from a DataStream.
 void save(DataOutputStream out)
          Write a string representation of a Pair to a DataStream.
 T2 second()
           
 void setFirst(T1 o)
           
 void setSecond(T2 o)
           
static Pair<String,String> stringIntern(Pair<String,String> p)
          If first and second are Strings, then this returns an MutableInternedPair where the Strings have been interned, and if this Pair is serialized and then deserialized, first and second are interned upon deserialization.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

first

public T1 first
Direct access is deprecated. Use first().


second

public T2 second
Direct access is deprecated. Use second().

Constructor Detail

Pair

public Pair()

Pair

public Pair(T1 first,
            T2 second)
Method Detail

first

public T1 first()

second

public T2 second()

setFirst

public void setFirst(T1 o)

setSecond

public void setSecond(T2 o)

toString

public String toString()
Overrides:
toString in class Object

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

readStringPair

public static Pair<String,String> readStringPair(DataInputStream in)
Read a string representation of a Pair from a DataStream. This might not work correctly unless the pair of objects are of type String.


makePair

public static <X,Y> Pair<X,Y> makePair(X x,
                                       Y y)
Returns a Pair constructed from X and Y. Convenience method; the compiler will disambiguate the classes used for you so that you don't have to write out potentially long class names.


save

public void save(DataOutputStream out)
Write a string representation of a Pair to a DataStream. The toString() method is called on each of the pair of objects and a String representation is written. This might not allow one to recover the pair of objects unless they are of type String.


compareTo

public int compareTo(Pair<T1,T2> another)
Compares this Pair to another object. If the object is a Pair, this function will work providing the elements of the Pair are themselves comparable. It will then return a value based on the pair of objects, where p > q iff p.first() > q.first() || (p.first().equals(q.first()) && p.second() > q.second()). If the other object is not a Pair, it throws a ClassCastException.

Specified by:
compareTo in interface Comparable<Pair<T1,T2>>
Parameters:
another - the Object to be compared.
Returns:
the value 0 if the argument is a Pair equal to this Pair; a value less than 0 if the argument is a Pair greater than this Pair; and a value greater than 0 if the argument is a Pair less than this Pair.
Throws:
ClassCastException - if the argument is not a Pair.
See Also:
Comparable

stringIntern

public static Pair<String,String> stringIntern(Pair<String,String> p)
If first and second are Strings, then this returns an MutableInternedPair where the Strings have been interned, and if this Pair is serialized and then deserialized, first and second are interned upon deserialization.

Parameters:
p - A pair of Strings
Returns:
MutableInternedPair, with same first and second as this.

internedStringPair

public static Pair<String,String> internedStringPair(String first,
                                                     String second)
Returns an MutableInternedPair where the Strings have been interned. This is a factory method for creating an MutableInternedPair. It requires the arguments to be Strings. If this Pair is serialized and then deserialized, first and second are interned upon deserialization.

Note: I put this in thinking that its use might be faster than calling x = new Pair(a, b).stringIntern() but it's not really clear whether this is true.

Parameters:
first - The first object
second - The second object
Returns:
An MutableInternedPair, with given first and second


Stanford NLP Group