Package ratpack.func

Class Pair<L,​R>

  • Type Parameters:
    L - the left data type
    R - the right data type

    public final class Pair<L,​R>
    extends java.lang.Object
    A generic pair implementation that can be used to cumulatively aggregate a data structure during a promise pipeline.

    This can sometimes be useful when collecting facts about something as part of a data stream without using mutable data structures.

    
     import ratpack.func.Pair;
     import ratpack.exec.Promise;
     import ratpack.test.embed.EmbeddedApp;
    
     import static org.junit.Assert.assertEquals;
    
     public class Example {
    
       public static void main(String[] args) throws Exception {
         EmbeddedApp.fromHandler(ctx -> {
           int id = 1;
           int age = 21;
           String name = "John";
    
           Promise.value(id)
             .map(idValue -> Pair.of(idValue, age))
             .flatMap(pair -> Promise.value(name).map(pair::nestRight))
             .then(pair -> {
               int receivedId = pair.left;
               int receivedAge = pair.right.right;
               String receivedName = pair.right.left;
               ctx.render(receivedName + " [" + receivedId + "] - age: " + receivedAge);
             });
         }).test(httpClient -> {
           assertEquals("John [1] - age: 21", httpClient.getText());
         });
       }
     }
     

    • Field Summary

      Fields 
      Modifier and Type Field Description
      L left
      The left item of the pair.
      R right
      The right item of the pair.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object o)
      A pair is equal if its left and right items are equal to the left and right items of this respectively.
      L getLeft()
      The left item of the pair.
      R getRight()
      The right item of the pair.
      int hashCode()
      Hash code.
      L left()
      The left item of the pair.
      <T> Pair<T,​R> left​(T newLeft)
      Replaces the left item with the given item.
      <T> T map​(Function<? super Pair<L,​R>,​? extends T> function)
      Applies the given function to this, returning the result.
      <T> Pair<T,​R> mapLeft​(Function<? super L,​? extends T> function)
      Creates a new pair, with the left item being the result of applying the given function to the left item of this.
      <T> Pair<L,​T> mapRight​(Function<? super R,​? extends T> function)
      Creates a new pair, with the right item being the result of applying the given function to the right item of this.
      <T> Pair<Pair<T,​L>,​R> nestLeft​(T t)
      Creates a new pair, with pair(t, this.left) as the left item and the the right value of this as the right.
      <T> Pair<L,​Pair<T,​R>> nestRight​(T t)
      Creates a new pair, with pair(t, this.right) as the right item and the the left value of this as the left.
      static <L,​R>
      Pair<L,​R>
      of​(L left, R right)
      Creates a new pair.
      static <L,​R>
      Pair<L,​R>
      pair​(L left, R right)
      Creates a new pair.
      <T> Pair<T,​Pair<L,​R>> pushLeft​(T t)
      Creates a new pair, with this as the right item and the given value as the left.
      <T> Pair<Pair<L,​R>,​T> pushRight​(T t)
      Creates a new pair, with this as the left item and the given value as the right.
      R right()
      The right item of the pair.
      <T> Pair<L,​T> right​(T newRight)
      Replaces the right item with the given item.
      java.lang.String toString()
      Returns "Pair[«left.toString()»,«right.toString()»].
      static <L,​P extends Pair<L,​?>>
      Function<P,​L>
      unpackLeft()
      Convenience function for returning the left item of a pair.
      static <R,​P extends Pair<?,​R>>
      Function<P,​R>
      unpackRight()
      Convenience function for returning the right item of a pair.
      • Methods inherited from class java.lang.Object

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

      • left

        public final L left
        The left item of the pair.
      • right

        public final R right
        The right item of the pair.
    • Method Detail

      • of

        public static <L,​R> Pair<L,​R> of​(L left,
                                                     R right)
        Creates a new pair.
        Type Parameters:
        L - the type of the left item
        R - the type of the right item
        Parameters:
        left - the left item
        right - the right item
        Returns:
        a new pair
      • getLeft

        public L getLeft()
        The left item of the pair.
        Returns:
        the left item of the pair
      • getRight

        public R getRight()
        The right item of the pair.
        Returns:
        the right item of the pair
      • left

        public L left()
        The left item of the pair.
        Returns:
        the left item of the pair
      • right

        public R right()
        The right item of the pair.
        Returns:
        the right item of the pair
      • left

        public <T> Pair<T,​R> left​(T newLeft)
        Replaces the left item with the given item.
        Type Parameters:
        T - the new left type
        Parameters:
        newLeft - the replacement left side of the pair
        Returns:
        a new pair, with the given item replacing the left of this
      • right

        public <T> Pair<L,​T> right​(T newRight)
        Replaces the right item with the given item.
        Type Parameters:
        T - the new right type
        Parameters:
        newRight - the replacement right side of the pair
        Returns:
        a new pair, with the given item replacing the right of this
      • pair

        public static <L,​R> Pair<L,​R> pair​(L left,
                                                       R right)
        Creates a new pair.
        Type Parameters:
        L - the type of the left item
        R - the type of the right item
        Parameters:
        left - the left item
        right - the right item
        Returns:
        a new pair
      • pushLeft

        public <T> Pair<T,​Pair<L,​R>> pushLeft​(T t)
        Creates a new pair, with this as the right item and the given value as the left.
        Type Parameters:
        T - the type of the left value for the returned pair
        Parameters:
        t - the left value for the returned pair
        Returns:
        a new pair, with this as the right item and the given value as the left
      • pushRight

        public <T> Pair<Pair<L,​R>,​T> pushRight​(T t)
        Creates a new pair, with this as the left item and the given value as the right.
        Type Parameters:
        T - the type of the right value for the returned pair
        Parameters:
        t - the right value for the returned pair
        Returns:
        a new pair, with this as the left item and the given value as the right
      • nestLeft

        public <T> Pair<Pair<T,​L>,​R> nestLeft​(T t)
        Creates a new pair, with pair(t, this.left) as the left item and the the right value of this as the right.
        Type Parameters:
        T - the type of item to nest with the left item of this pair
        Parameters:
        t - the item to nest with the left item of this pair
        Returns:
        a new pair, with pair(t, this.left) as the left item and the the right value of this as the right
      • nestRight

        public <T> Pair<L,​Pair<T,​R>> nestRight​(T t)
        Creates a new pair, with pair(t, this.right) as the right item and the the left value of this as the left.
        Type Parameters:
        T - the type of item to nest with the right item of this pair
        Parameters:
        t - the item to nest with the right item of this pair
        Returns:
        a new pair, with pair(t, this.right) as the right item and the the left value of this as the left
      • mapLeft

        public <T> Pair<T,​R> mapLeft​(Function<? super L,​? extends T> function)
                                    throws java.lang.Exception
        Creates a new pair, with the left item being the result of applying the given function to the left item of this.

        The right value is unchanged.

        Type Parameters:
        T - the type of the new left value
        Parameters:
        function - a transformer for the left value
        Returns:
        a new pair, with the left item being the result of applying the given function to the left item of this
        Throws:
        java.lang.Exception - any thrown by function
      • mapRight

        public <T> Pair<L,​T> mapRight​(Function<? super R,​? extends T> function)
                                     throws java.lang.Exception
        Creates a new pair, with the right item being the result of applying the given function to the right item of this.

        The left value is unchanged.

        Type Parameters:
        T - the type of the new right value
        Parameters:
        function - a transformer for the right value
        Returns:
        a new pair, with the right item being the result of applying the given function to the right item of this
        Throws:
        java.lang.Exception - any thrown by function
      • map

        public <T> T map​(Function<? super Pair<L,​R>,​? extends T> function)
                  throws java.lang.Exception
        Applies the given function to this, returning the result.
        Type Parameters:
        T - the function result type
        Parameters:
        function - a function to apply to this
        Returns:
        the result of applying function to this
        Throws:
        java.lang.Exception - any thrown by function
      • unpackLeft

        public static <L,​P extends Pair<L,​?>> Function<P,​L> unpackLeft()
        Convenience function for returning the left item of a pair.
        Type Parameters:
        L - the type of the left item
        P - the pair type
        Returns:
        a function that when applied to a pair returns the left item
      • unpackRight

        public static <R,​P extends Pair<?,​R>> Function<P,​R> unpackRight()
        Convenience function for returning the right item of a pair.
        Type Parameters:
        R - the type of the right item
        P - the pair type
        Returns:
        a function that when applied to a pair returns the right item
      • equals

        public boolean equals​(java.lang.Object o)
        A pair is equal if its left and right items are equal to the left and right items of this respectively.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - the object to compare to
        Returns:
        the equality
      • hashCode

        public int hashCode()
        Hash code.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        hash code.
      • toString

        public java.lang.String toString()
        Returns "Pair[«left.toString()»,«right.toString()»].
        Overrides:
        toString in class java.lang.Object
        Returns:
        "Pair[«left.toString()»,«right.toString()»]