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

    Fields
    Modifier and Type Field Description
    T1 _1  
    T2 _2  
  • Method Summary

    Modifier and Type Method Description
    void accept​(java.util.function.BiConsumer<? super T1,​? super T2> c)
    Use the binary consumer to consume this pair.
    <R> R apply​(java.util.function.BiFunction<? super T1,​? super T2,​R> f)
    Apply the binary function to this pair.
    boolean equals​(java.lang.Object o)  
    T1 get_1()
    (a,b).get_1() = a.
    T2 get_2()
    (a,b).get_2() = b.
    int hashCode()  
    <U,​ R> Pair<U,​R> map​(java.util.function.Function<? super T1,​U> f, java.util.function.Function<? super T2,​R> g)
    Allows q = p.map(f, g); instead of q = Pair.of(f.apply(p._1), g.apply(p._2)).
    <U> Pair<U,​T2> map_1​(java.util.function.Function<? super T1,​U> f)
    Allows q = p.map_1(f); instead of q = Pair.of(f.apply(p._1), p._2).
    <U> Pair<T1,​U> map_2​(java.util.function.Function<? super T2,​U> f)
    Allows q = p.map_2(f); instead of q = 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 for of(Object); useful for static imports.
    static <T1,​ T2> Pair<T1,​T2> pair​(T1 first, T2 second)
    More descriptive alias for of(Object, Object); useful for static imports.
    Pair<T2,​T1> swap()
    (a,b).swap() = (b,a)
    boolean test​(java.util.function.BiPredicate<? super T1,​? super T2> p)
    Use the binary predicate to test this pair.
    java.lang.String toString()  
    <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

      public final T1 _1
    • _2

      public final T2 _2
  • Method Details

    • of

      public static <T1,​ T2> Pair<T1,​T2> of​(T1 first, T2 second)
      Factory method.
    • of

      public static <T> Pair<T,​T> of​(T both)
      Special case factory method for making a duplicate pair (x -> (x,x)).
    • pair

      public static <T1,​ T2> Pair<T1,​T2> pair​(T1 first, T2 second)
      More descriptive alias for of(Object, Object); useful for static imports.
    • pair

      public static <T> Pair<T,​T> pair​(T both)
      More descriptive alias for of(Object); useful for static imports.
    • get_1

      public T1 get_1()
      (a,b).get_1() = a.
    • get_2

      public T2 get_2()
      (a,b).get_2() = b.
    • with_1

      public <U> Pair<U,​T2> with_1​(U newFirst)
      (a,b).with_1(c) = (c,b)
    • with_2

      public <U> Pair<T1,​U> with_2​(U newSecond)
      (a,b).with_2(c) = (a,c)
    • swap

      public Pair<T2,​T1> swap()
      (a,b).swap() = (b,a)
    • map_1

      public <U> Pair<U,​T2> map_1​(java.util.function.Function<? super T1,​U> f)
      Allows q = p.map_1(f); instead of q = Pair.of(f.apply(p._1), p._2).
    • map_2

      public <U> Pair<T1,​U> map_2​(java.util.function.Function<? super T2,​U> f)
      Allows q = p.map_2(f); instead of q = Pair.of(p._1, f.apply(p._2)).
    • map

      public <U,​ R> Pair<U,​R> map​(java.util.function.Function<? super T1,​U> f, java.util.function.Function<? super T2,​R> g)
      Allows q = p.map(f, g); instead of q = Pair.of(f.apply(p._1), g.apply(p._2)).
    • apply

      public <R> R apply​(java.util.function.BiFunction<? super T1,​? super T2,​R> f)
      Apply the binary function to this pair. Unfortunately, java.util.function.BiFunction cannot take a pair of values. So doing f.apply(pair) instead of f.apply(pair._1, pair._2) is not possible. This at least allows pair.apply(f).
      See Also:
      Pairs.function(BiFunction)
    • test

      public boolean test​(java.util.function.BiPredicate<? super T1,​? super T2> p)
      Use the binary predicate to test this pair. Unfortunately, java.util.function.BiPredicate cannot take a pair of values. So doing p.test(pair) instead of p.test(pair._1, pair._2) is not possible. This at least allows pair.test(p).
      See Also:
      Pairs.predicate(BiPredicate)
    • accept

      public void accept​(java.util.function.BiConsumer<? super T1,​? super T2> c)
      Use the binary consumer to consume this pair. Unfortunately, java.util.function.BiConsumer cannot take a pair of values. So doing c.accept(pair) instead of c.accept(pair._1, pair._2) is not possible. This at least allows pair.accept(c).
      See Also:
      Pairs.consumer(BiConsumer)
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object
    • equals

      public boolean equals​(java.lang.Object o)
      Overrides:
      equals in class java.lang.Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class java.lang.Object