Class Pair<A,B>
- java.lang.Object
-
- com.opengamma.strata.collect.tuple.Pair<A,B>
-
- Type Parameters:
A
- the first element typeB
- the second element type
- All Implemented Interfaces:
Tuple
,Serializable
,Comparable<Pair<A,B>>
,org.joda.beans.Bean
,org.joda.beans.ImmutableBean
public final class Pair<A,B> extends Object implements org.joda.beans.ImmutableBean, Tuple, Comparable<Pair<A,B>>, Serializable
An immutable pair consisting of two elements.This implementation refers to the elements as 'first' and 'second'. The elements cannot be null.
Although the implementation is immutable, there is no restriction on the objects that may be stored. If mutable objects are stored in the pair, then the pair itself effectively becomes mutable.
Some primitive specializations of this class are provided, such as
DoublesPair
.This class is immutable and thread-safe if the stored objects are immutable.
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <C,D>
Pair<A,B>combinedWith(Pair<C,D> other, BiFunction<? super A,? super C,? extends A> combinerFirst, BiFunction<? super B,? super D,? extends B> combinerSecond)
Combines this instance with another.static <A,B>
BinaryOperator<Pair<A,B>>combining(BiFunction<? super A,? super A,? extends A> combinerFirst, BiFunction<? super B,? super B,? extends B> combinerSecond)
Returns a combiner of pair instances.int
compareTo(Pair<A,B> other)
Compares the pair based on the first element followed by the second element.ImmutableList<Object>
elements()
Gets the elements from this pair as a list.boolean
equals(Object obj)
A
getFirst()
Gets the first element in this pair.B
getSecond()
Gets the second element in this pair.int
hashCode()
static Pair.Meta
meta()
The meta-bean forPair
.Pair.Meta<A,B>
metaBean()
static <R,S>
Pair.Meta<R,S>metaPair(Class<R> cls1, Class<S> cls2)
The meta-bean forPair
.static <A,B>
Pair<A,B>of(A first, B second)
Obtains a pair inferring the types.int
size()
Gets the number of elements held by this pair.String
toString()
Gets the pair using a standard string format.
-
-
-
Method Detail
-
of
public static <A,B> Pair<A,B> of(A first, B second)
Obtains a pair inferring the types.- Type Parameters:
A
- the first element typeB
- the second element type- Parameters:
first
- the first elementsecond
- the second element- Returns:
- a pair formed from the two parameters
-
combining
public static <A,B> BinaryOperator<Pair<A,B>> combining(BiFunction<? super A,? super A,? extends A> combinerFirst, BiFunction<? super B,? super B,? extends B> combinerSecond)
Returns a combiner of pair instances.This is useful if you have a stream of
Pair<A, B>
and would like to reduce.e.g
pairList.stream() .reduce(Pair.combining(A::combinedWith, B::combinedWith))
- Type Parameters:
A
- the type of the first valuesB
- the type of the second values- Parameters:
combinerFirst
- the combiner of first valuescombinerSecond
- the combiner of second values- Returns:
- the combiner of pair instance
-
combinedWith
public <C,D> Pair<A,B> combinedWith(Pair<C,D> other, BiFunction<? super A,? super C,? extends A> combinerFirst, BiFunction<? super B,? super D,? extends B> combinerSecond)
Combines this instance with another.- Type Parameters:
C
- the type of the first value in the other instanceD
- the type of the second value in the other instance- Parameters:
other
- the other paircombinerFirst
- the combiner of first valuescombinerSecond
- the combiner of second values- Returns:
- the combined pair instance
-
size
public int size()
Gets the number of elements held by this pair.
-
elements
public ImmutableList<Object> elements()
Gets the elements from this pair as a list.The list returns each element in the pair in order.
-
compareTo
public int compareTo(Pair<A,B> other)
Compares the pair based on the first element followed by the second element.The element types must be
Comparable
.- Specified by:
compareTo
in interfaceComparable<A>
- Parameters:
other
- the other pair- Returns:
- negative if this is less, zero if equal, positive if greater
- Throws:
ClassCastException
- if either object is not comparable
-
toString
public String toString()
Gets the pair using a standard string format.The standard format is '[$first, $second]'. Spaces around the values are trimmed.
-
meta
public static Pair.Meta meta()
The meta-bean forPair
.- Returns:
- the meta-bean, not null
-
metaPair
public static <R,S> Pair.Meta<R,S> metaPair(Class<R> cls1, Class<S> cls2)
The meta-bean forPair
.- Type Parameters:
R
- the first generic typeS
- the second generic type- Parameters:
cls1
- the first generic typecls2
- the second generic type- Returns:
- the meta-bean, not null
-
getFirst
public A getFirst()
Gets the first element in this pair.- Returns:
- the value of the property, not null
-
getSecond
public B getSecond()
Gets the second element in this pair.- Returns:
- the value of the property, not null
-
-