Package ch.rfin.util
Class Pair<T1,T2>
java.lang.Object
ch.rfin.util.Pair<T1,T2>
public final class Pair<T1,T2>
extends java.lang.Object
A Pair of values. This class is immutable (and therefore thread safe) if
T1 and T2 are immutable.
-
Field Summary
-
Method Summary
Modifier and Type Method Description voidaccept(java.util.function.BiConsumer<? super T1,? super T2> c)Use the binary consumer to consume this pair.<R> Rapply(java.util.function.BiFunction<? super T1,? super T2,? extends R> f)Apply the binary function to this pair.booleanequals(java.lang.Object o)T1get_1()(a,b).get_1() = a.T2get_2()(a,b).get_2() = b.inthashCode()<U, R> Pair<U,R>map(java.util.function.Function<? super T1,? extends U> f, java.util.function.Function<? super T2,? extends R> g)Allowsq = p.map(f, g);instead ofq = Pair.of(f.apply(p._1), g.apply(p._2)).<U> Pair<U,T2>map_1(java.util.function.Function<? super T1,? extends U> f)Allowsq = p.map_1(f);instead ofq = Pair.of(f.apply(p._1), p._2).<U> Pair<T1,U>map_2(java.util.function.Function<? super T2,? extends U> f)Allowsq = p.map_2(f);instead ofq = Pair.of(p._1, f.apply(p._2)).static <T> Pair<T,T>of(T both)Special case factory method for making a duplicate pair (x -> (x,x)).static <T1, T2> Pair<T1,T2>of(T1 first, T2 second)Factory method.static <T> Pair<T,T>pair(T both)More descriptive alias forof(Object); useful for static imports.static <T1, T2> Pair<T1,T2>pair(T1 first, T2 second)More descriptive alias forof(Object, Object); useful for static imports.Pair<T2,T1>swap()(a,b).swap() = (b,a)booleantest(java.util.function.BiPredicate<? super T1,? super T2> p)Use the binary predicate to test this pair.java.lang.StringtoString()<U> Pair<U,T2>with_1(U newFirst)(a,b).with_1(c) = (c,b)<U> Pair<T1,U>with_2(U newSecond)(a,b).with_2(c) = (a,c)Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Field Details
-
_1
-
_2
-
-
Method Details
-
of
Factory method. -
of
Special case factory method for making a duplicate pair (x -> (x,x)). -
pair
More descriptive alias forof(Object, Object); useful for static imports. -
pair
More descriptive alias forof(Object); useful for static imports. -
get_1
(a,b).get_1() = a. -
get_2
(a,b).get_2() = b. -
with_1
(a,b).with_1(c) = (c,b) -
with_2
(a,b).with_2(c) = (a,c) -
swap
(a,b).swap() = (b,a) -
map_1
Allowsq = p.map_1(f);instead ofq = Pair.of(f.apply(p._1), p._2). -
map_2
Allowsq = p.map_2(f);instead ofq = Pair.of(p._1, f.apply(p._2)). -
map
public <U, R> Pair<U,R> map(java.util.function.Function<? super T1,? extends U> f, java.util.function.Function<? super T2,? extends R> g)Allowsq = p.map(f, g);instead ofq = Pair.of(f.apply(p._1), g.apply(p._2)). -
apply
Apply the binary function to this pair. Unfortunately, java.util.function.BiFunction cannot take a pair of values. So doingf.apply(pair) instead of f.apply(pair._1, pair._2)is not possible. This at least allowspair.apply(f).- See Also:
Pairs.function(BiFunction)
-
test
Use the binary predicate to test this pair. Unfortunately, java.util.function.BiPredicate cannot take a pair of values. So doingp.test(pair) instead of p.test(pair._1, pair._2)is not possible. This at least allowspair.test(p).- See Also:
Pairs.predicate(BiPredicate)
-
accept
Use the binary consumer to consume this pair. Unfortunately, java.util.function.BiConsumer cannot take a pair of values. So doingc.accept(pair) instead of c.accept(pair._1, pair._2)is not possible. This at least allowspair.accept(c).- See Also:
Pairs.consumer(BiConsumer)
-
toString
public java.lang.String toString()- Overrides:
toStringin classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()- Overrides:
hashCodein classjava.lang.Object
-