Package ratpack.func
Class Pair<L,R>
- java.lang.Object
-
- ratpack.func.Pair<L,R>
-
- Type Parameters:
L
- the left data typeR
- the right data type
public final class Pair<L,R> extends 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.jupiter.api.Assertions.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()); }); } }
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object o)
A pair is equal if its left and right items are equal to the left and right items ofthis
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 tothis
, 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 ofthis
.<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 ofthis
.<T> Pair<Pair<T,L>,R>
nestLeft(T t)
Creates a new pair, withpair(t, this.left)
as the left item and the the right value ofthis
as the right.<T> Pair<L,Pair<T,R>>
nestRight(T t)
Creates a new pair, withpair(t, this.right)
as the right item and the the left value ofthis
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, withthis
as the right item and the given value as the left.<T> Pair<Pair<L,R>,T>
pushRight(T t)
Creates a new pair, withthis
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.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.
-
-
-
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 itemR
- the type of the right item- Parameters:
left
- the left itemright
- 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 itemR
- the type of the right item- Parameters:
left
- the left itemright
- the right item- Returns:
- a new pair
-
pushLeft
public <T> Pair<T,Pair<L,R>> pushLeft(T t)
Creates a new pair, withthis
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, withthis
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, withpair(t, this.left)
as the left item and the the right value ofthis
as the right.- Type Parameters:
T
- the type of item to nest with the left item ofthis
pair- Parameters:
t
- the item to nest with the left item ofthis
pair- Returns:
- a new pair, with
pair(t, this.left)
as the left item and the the right value ofthis
as the right
-
nestRight
public <T> Pair<L,Pair<T,R>> nestRight(T t)
Creates a new pair, withpair(t, this.right)
as the right item and the the left value ofthis
as the left.- Type Parameters:
T
- the type of item to nest with the right item ofthis
pair- Parameters:
t
- the item to nest with the right item ofthis
pair- Returns:
- a new pair, with
pair(t, this.right)
as the right item and the the left value ofthis
as the left
-
mapLeft
public <T> Pair<T,R> mapLeft(Function<? super L,? extends T> function) throws Exception
Creates a new pair, with the left item being the result of applying the given function to the left item ofthis
.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:
Exception
- any thrown byfunction
-
mapRight
public <T> Pair<L,T> mapRight(Function<? super R,? extends T> function) throws Exception
Creates a new pair, with the right item being the result of applying the given function to the right item ofthis
.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:
Exception
- any thrown byfunction
-
map
public <T> T map(Function<? super Pair<L,R>,? extends T> function) throws Exception
Applies the given function tothis
, returning the result.- Type Parameters:
T
- the function result type- Parameters:
function
- a function to apply tothis
- Returns:
- the result of applying
function
tothis
- Throws:
Exception
- any thrown byfunction
-
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 itemP
- 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 itemP
- the pair type- Returns:
- a function that when applied to a pair returns the right item
-
equals
public boolean equals(Object o)
A pair is equal if its left and right items are equal to the left and right items ofthis
respectively.
-
-