Package edu.stanford.nlp.util
Class Pair<T1,T2>
- java.lang.Object
-
- edu.stanford.nlp.util.Pair<T1,T2>
-
- All Implemented Interfaces:
PrettyLoggable,Serializable,Comparable<Pair<T1,T2>>
- Direct Known Subclasses:
Interval,UnorderedPair
public class Pair<T1,T2> extends Object implements Comparable<Pair<T1,T2>>, Serializable, PrettyLoggable
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.
- Version:
- 2002/08/25
- Author:
- Dan Klein, Christopher Manning (added stuff from Kristina's, rounded out)
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPair.ByFirstPairComparator<T1,T2>Compares aPairto anotherPairaccording to the first object of the pair only This function will work providing the first element of thePairis comparable, otherwise will throw aClassCastException.static classPair.ByFirstReversePairComparator<T1,T2>Compares aPairto anotherPairaccording to the first object of the pair only in decreasing order This function will work providing the first element of thePairis comparable, otherwise will throw aClassCastExceptionstatic classPair.BySecondPairComparator<T1,T2>Compares aPairto anotherPairaccording to the second object of the pair only This function will work providing the first element of thePairis comparable, otherwise will throw aClassCastExceptionstatic classPair.BySecondReversePairComparator<T1,T2>Compares aPairto anotherPairaccording to the second object of the pair only in decreasing order This function will work providing the first element of thePairis comparable, otherwise will throw aClassCastException
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description List<Object>asList()intcompareTo(Pair<T1,T2> another)Compares thisPairto another object.booleanequals(Object o)T1first()inthashCode()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.voidprettyLog(Redwood.RedwoodChannels channels, String description)Pretty logs the current object to specific Redwood channels.voidsave(DataOutputStream out)Write a string representation of a Pair to a DataStream.T2second()voidsetFirst(T1 o)voidsetSecond(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.StringtoString()
-
-
-
Method Detail
-
first
public T1 first()
-
second
public T2 second()
-
setFirst
public void setFirst(T1 o)
-
setSecond
public void setSecond(T2 o)
-
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. ThetoString()method is called on each of the pair of objects and aStringrepresentation is written. This might not allow one to recover the pair of objects unless they are of typeString.
-
compareTo
public int compareTo(Pair<T1,T2> another)
Compares thisPairto another object. If the object is aPair, this function will work providing the elements of thePairare themselves comparable. It will then return a value based on the pair of objects, wherep > q iff p.first() > q.first() || (p.first().equals(q.first()) && p.second() > q.second()). If the other object is not aPair, it throws aClassCastException.- Specified by:
compareToin interfaceComparable<T1>- Parameters:
another- theObjectto be compared.- Returns:
- the value
0if the argument is aPairequal to thisPair; a value less than0if the argument is aPairgreater than thisPair; and a value greater than0if the argument is aPairless than thisPair. - Throws:
ClassCastException- if the argument is not aPair.- 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 objectsecond- The second object- Returns:
- An MutableInternedPair, with given first and second
-
prettyLog
public void prettyLog(Redwood.RedwoodChannels channels, String description)
Pretty logs the current object to specific Redwood channels.- Specified by:
prettyLogin interfacePrettyLoggable- Parameters:
channels- the channels which should be logged to -- all logging calls should use logging methods on the channels (e.g. channels.log(), etc.)description- The description of the object. It will potentially identify the object's functional role or (failing that) its class. This is typically used as a track name surrounding the contents of this object.
-
-