L
- the left element typeR
- the right element typepublic abstract class Pair<L,R> extends java.lang.Object implements java.util.Map.Entry<L,R>, java.lang.Comparable<Pair<L,R>>, java.io.Serializable
This class is an abstract implementation defining the basic API.
It refers to the elements as 'left' and 'right'. It also implements the
Map.Entry
interface where the key is 'left' and the value is 'right'.
Subclass implementations may be mutable or immutable. However, there is no restriction on the type of the stored objects that may be stored. If mutable objects are stored in the pair, then the pair itself effectively becomes mutable.
Modifier and Type | Field and Description |
---|---|
static Pair<?,?>[] |
EMPTY_ARRAY
An empty array.
|
Constructor and Description |
---|
Pair() |
Modifier and Type | Method and Description |
---|---|
<E extends java.lang.Throwable> |
accept(FailableBiConsumer<L,R,E> consumer)
Accepts this key and value as arguments to the given consumer.
|
<V,E extends java.lang.Throwable> |
apply(FailableBiFunction<L,R,V,E> function)
Applies this key and value as arguments to the given function.
|
int |
compareTo(Pair<L,R> other)
Compares the pair based on the left element followed by the right element.
|
static <L,R> Pair<L,R>[] |
emptyArray()
Returns the empty array singleton that can be assigned without compiler warning.
|
boolean |
equals(java.lang.Object obj)
Compares this pair to another based on the two elements.
|
L |
getKey()
Gets the key from this pair.
|
abstract L |
getLeft()
Gets the left element from this pair.
|
abstract R |
getRight()
Gets the right element from this pair.
|
R |
getValue()
Gets the value from this pair.
|
int |
hashCode()
Returns a suitable hash code.
|
static <L,R> Pair<L,R> |
of(L left,
R right)
Creates an immutable pair of two objects inferring the generic types.
|
static <L,R> Pair<L,R> |
of(java.util.Map.Entry<L,R> pair)
Creates an immutable pair from a map entry.
|
static <L,R> Pair<L,R> |
ofNonNull(L left,
R right)
Creates an immutable pair of two non-null objects inferring the generic types.
|
java.lang.String |
toString()
Returns a String representation of this pair using the format
($left,$right) . |
java.lang.String |
toString(java.lang.String format)
Formats the receiver using the given format.
|
public static final Pair<?,?>[] EMPTY_ARRAY
Consider using emptyArray()
to avoid generics warnings.
public static <L,R> Pair<L,R>[] emptyArray()
L
- the left element typeR
- the right element typepublic static <L,R> Pair<L,R> of(L left, R right)
This factory allows the pair to be created using inference to obtain the generic types.
L
- the left element typeR
- the right element typeleft
- the left element, may be nullright
- the right element, may be nullpublic static <L,R> Pair<L,R> of(java.util.Map.Entry<L,R> pair)
This factory allows the pair to be created using inference to obtain the generic types.
L
- the left element typeR
- the right element typepair
- the map entry.public static <L,R> Pair<L,R> ofNonNull(L left, R right)
This factory allows the pair to be created using inference to obtain the generic types.
L
- the left element typeR
- the right element typeleft
- the left element, may not be nullright
- the right element, may not be nulljava.lang.NullPointerException
- if any input is nullpublic <E extends java.lang.Throwable> void accept(FailableBiConsumer<L,R,E> consumer) throws E extends java.lang.Throwable
E
- The kind of thrown exception or error.consumer
- the consumer to call.E
- Thrown when the consumer fails.E extends java.lang.Throwable
public <V,E extends java.lang.Throwable> V apply(FailableBiFunction<L,R,V,E> function) throws E extends java.lang.Throwable
V
- The function return type.E
- The kind of thrown exception or error.function
- the consumer to call.E
- Thrown when the consumer fails.E extends java.lang.Throwable
public int compareTo(Pair<L,R> other)
Comparable
.public boolean equals(java.lang.Object obj)
public final L getKey()
This method implements the Map.Entry
interface returning the
left element as the key.
public abstract L getLeft()
When treated as a key-value pair, this is the key.
public abstract R getRight()
When treated as a key-value pair, this is the value.
public R getValue()
This method implements the Map.Entry
interface returning the
right element as the value.
public int hashCode()
Map.Entry
.public java.lang.String toString()
($left,$right)
.toString
in class java.lang.Object
public java.lang.String toString(java.lang.String format)
This uses Formattable
to perform the formatting. Two variables may
be used to embed the left and right elements. Use %1$s
for the left
element (key) and %2$s
for the right element (value).
The default format used by toString()
is (%1$s,%2$s)
.
format
- the format string, optionally containing %1$s
and %2$s
, not nullCopyright © 2010 - 2023 Adobe. All Rights Reserved