Class Pair<A,​B>

  • All Implemented Interfaces:
    java.io.Serializable

    public final class Pair<A,​B>
    extends java.lang.Object
    implements java.io.Serializable
    Represents a pair of objects.
    Since:
    1.0
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      Pair​(A left, B right)
      Constructor for Pair.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <A,​B>
      Pair<B,​B>
      ap​(Pair<A,​A> aa, Pair<java.util.function.Function<A,​B>,​java.util.function.Function<A,​B>> ff)
      Performs function application within an homogeneous pair (applicative functor pattern).
      <AA,​BB>
      Pair<AA,​BB>
      bimap​(java.util.function.Function<? super A,​? extends AA> fLeft, java.util.function.Function<? super B,​? extends BB> fRight)
      Applies functions to each of the left and right values to produce a new pair.
      boolean equals​(java.lang.Object o)
      <R> R fold​(java.util.function.BiFunction<? super A,​? super B,​R> f)
      Applies a function to left and right values to produce a single result.
      int hashCode()
      A left()
      Accessor method for the left value of the pair.
      <AA> Pair<AA,​B> leftMap​(java.util.function.Function<? super A,​AA> fLeft)
      Applies a function to the left value to produce a new Pair.
      static <A> java.util.function.Function<Pair<A,​?>,​A> leftValue()
      Function for accessing the left value of pairs.
      static <A,​B>
      Pair<B,​B>
      map​(Pair<A,​A> aa, java.util.function.Function<A,​B> f)
      Apply a function to both elements of an homogeneous pair.
      static <A,​B>
      Pair<A,​B>
      pair​(A left, B right)
      Factory method for static Pair growth.
      static <A,​B>
      java.util.function.BiFunction<A,​B,​Pair<A,​B>>
      pairs()
      Factory method for a Pair factory function.
      B right()
      Accessor method for the right value of the pair.q
      <BB> Pair<A,​BB> rightMap​(java.util.function.Function<? super B,​BB> fRight)
      Applies a function to the right value to produce a new Pair.
      static <B> java.util.function.Function<Pair<?,​B>,​B> rightValue()
      Function for accessing the right value of pairs.
      Pair<B,​A> swap()  
      java.lang.String toString()
      static <A,​B>
      java.lang.Iterable<Pair<A,​B>>
      zip​(java.lang.Iterable<A> as, java.lang.Iterable<B> bs)
      Zips two iterables into a single iterable that produces pairs.
      static <A,​B>
      java.util.Optional<Pair<A,​B>>
      zip​(java.util.Optional<A> oA, java.util.Optional<B> oB)
      Zips the two given optionals into an optional of a pair.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Pair

        public Pair​(A left,
                    B right)
        Constructor for Pair.
        Parameters:
        left - value, cannot be null
        right - value, cannot be null
    • Method Detail

      • pair

        public static <A,​B> Pair<A,​B> pair​(A left,
                                                       B right)
        Factory method for static Pair growth.
        Type Parameters:
        A - the left value type
        B - the right value type
        Parameters:
        left - value, cannot be null
        right - value, cannot be null
        Returns:
        the Pair containing the passed values
      • pairs

        public static <A,​B> java.util.function.BiFunction<A,​B,​Pair<A,​B>> pairs()
        Factory method for a Pair factory function.
        Type Parameters:
        A - the left value type
        B - the right value type
        Returns:
        a function that constructs Pairs
      • leftValue

        public static <A> java.util.function.Function<Pair<A,​?>,​A> leftValue()
        Function for accessing the left value of pairs.
        Type Parameters:
        A - the left value type
        Returns:
        a Function that given a Pair returns the left side value
        Since:
        1.1
      • rightValue

        public static <B> java.util.function.Function<Pair<?,​B>,​B> rightValue()
        Function for accessing the right value of pairs.
        Type Parameters:
        B - the right value type
        Returns:
        a Function that given a Pair returns the right side value
        Since:
        1.1
      • ap

        public static <A,​B> Pair<B,​B> ap​(Pair<A,​A> aa,
                                                     Pair<java.util.function.Function<A,​B>,​java.util.function.Function<A,​B>> ff)
        Performs function application within an homogeneous pair (applicative functor pattern).
        Parameters:
        aa - an homogeneous pair
        ff - The pair of functions to apply.
        Returns:
        A new pair after applying the given pair of functions through aa.
      • map

        public static <A,​B> Pair<B,​B> map​(Pair<A,​A> aa,
                                                      java.util.function.Function<A,​B> f)
        Apply a function to both elements of an homogeneous pair.
        Parameters:
        aa - an homogeneous pair
        f - function to apply to both elements of aa
        Returns:
        A new pair after applying the function to aa elements.
      • zip

        public static <A,​B> java.lang.Iterable<Pair<A,​B>> zip​(java.lang.Iterable<A> as,
                                                                          java.lang.Iterable<B> bs)
        Zips two iterables into a single iterable that produces pairs.
        Type Parameters:
        A - LHS type
        B - RHS type
        Parameters:
        as - left values
        bs - right values
        Returns:
        an iterable of pairs, only as long as the shortest input iterable.
        Since:
        1.1
      • zip

        public static <A,​B> java.util.Optional<Pair<A,​B>> zip​(java.util.Optional<A> oA,
                                                                          java.util.Optional<B> oB)
        Zips the two given optionals into an optional of a pair.
        Type Parameters:
        A - the first type
        B - the second type
        Parameters:
        oA - the first optional
        oB - the second optional
        Returns:
        empty if either or both optionals are empty
        Since:
        4.6.0
      • left

        public A left()
        Accessor method for the left value of the pair.
        Returns:
        a A object.
      • right

        public B right()
        Accessor method for the right value of the pair.q
        Returns:
        a B object.
      • swap

        public Pair<B,​A> swap()
        Returns:
        a new Pair wth the left and right values swapped.
        Since:
        6.1
      • fold

        public <R> R fold​(java.util.function.BiFunction<? super A,​? super B,​R> f)
        Applies a function to left and right values to produce a single result.
        Type Parameters:
        R - the result type
        Parameters:
        f - the function to apply to the values
        Returns:
        the result of the applied function
        Since:
        6.1
      • leftMap

        public <AA> Pair<AA,​B> leftMap​(java.util.function.Function<? super A,​AA> fLeft)
        Applies a function to the left value to produce a new Pair.
        Type Parameters:
        AA - The type of the new left value
        Parameters:
        fLeft - the function to be applied to the left value
        Returns:
        A new Pair containing the new left value, and the existing right value
        Since:
        6.1
      • rightMap

        public <BB> Pair<A,​BB> rightMap​(java.util.function.Function<? super B,​BB> fRight)
        Applies a function to the right value to produce a new Pair.
        Type Parameters:
        BB - The type of the new right value
        Parameters:
        fRight - the function to be applied to the right value
        Returns:
        A new Pair containing the existing left value, and the new right value
        Since:
        6.1
      • bimap

        public <AA,​BB> Pair<AA,​BB> bimap​(java.util.function.Function<? super A,​? extends AA> fLeft,
                                                     java.util.function.Function<? super B,​? extends BB> fRight)
        Applies functions to each of the left and right values to produce a new pair.
        Type Parameters:
        AA - the type of the new left value
        BB - the type of the new right value
        Parameters:
        fLeft - the function to be applied to the left value
        fRight - the function to be applied to the right value
        Returns:
        a new Pair containing the new left and right values
        Since:
        6.1
      • 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