public class Pair<T1,T2> extends java.lang.Object implements java.lang.Comparable<Pair<T1,T2>>, java.io.Serializable, PrettyLoggable
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.
Modifier and Type | Class and Description |
---|---|
static class |
Pair.ByFirstPairComparator<T1,T2>
Compares a
Pair to another Pair according to the first object of the pair only
This function will work providing the first element of the Pair is comparable,
otherwise will throw a ClassCastException . |
static class |
Pair.ByFirstReversePairComparator<T1,T2>
Compares a
Pair to another Pair according to the first object of the pair only in decreasing order
This function will work providing
the first element of the Pair is comparable, otherwise will throw a ClassCastException |
static class |
Pair.BySecondPairComparator<T1,T2>
Compares a
Pair to another Pair according to the second object of the pair only
This function will work providing
the first element of the Pair is comparable, otherwise will throw a ClassCastException |
static class |
Pair.BySecondReversePairComparator<T1,T2>
Compares a
Pair to another Pair according to the second object of the pair only in decreasing order
This function will work providing
the first element of the Pair is comparable, otherwise will throw a ClassCastException |
Modifier and Type | Field and Description |
---|---|
T1 |
first
Direct access is deprecated.
|
T2 |
second
Direct access is deprecated.
|
Modifier and Type | Method and Description |
---|---|
java.util.List<java.lang.Object> |
asList() |
int |
compareTo(Pair<T1,T2> another)
Compares this
Pair to another object. |
boolean |
equals(java.lang.Object o) |
T1 |
first() |
int |
hashCode() |
static Pair<java.lang.String,java.lang.String> |
internedStringPair(java.lang.String first,
java.lang.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.
|
void |
prettyLog(Redwood.RedwoodChannels channels,
java.lang.String description)
Pretty logs the current object to specific Redwood channels.
|
void |
save(java.io.DataOutputStream out)
Write a string representation of a Pair to a DataStream.
|
T2 |
second() |
void |
setFirst(T1 o) |
void |
setSecond(T2 o) |
static Pair<java.lang.String,java.lang.String> |
stringIntern(Pair<java.lang.String,java.lang.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.
|
java.lang.String |
toString() |
public T1 first
public T2 second
public T1 first()
public T2 second()
public void setFirst(T1 o)
public void setSecond(T2 o)
public java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.util.List<java.lang.Object> asList()
public static <X,Y> Pair<X,Y> makePair(X x, Y y)
public void save(java.io.DataOutputStream out)
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
.public int compareTo(Pair<T1,T2> another)
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
.compareTo
in interface java.lang.Comparable<Pair<T1,T2>>
another
- the Object
to be compared.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
.java.lang.ClassCastException
- if the argument is not a
Pair
.Comparable
public static Pair<java.lang.String,java.lang.String> stringIntern(Pair<java.lang.String,java.lang.String> p)
p
- A pair of Stringspublic static Pair<java.lang.String,java.lang.String> internedStringPair(java.lang.String first, java.lang.String second)
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.
first
- The first objectsecond
- The second objectpublic void prettyLog(Redwood.RedwoodChannels channels, java.lang.String description)
prettyLog
in interface PrettyLoggable
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.