L
- the left data typeR
- the right data typepublic final class Pair<L,R> extends Object
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());
});
}
}
Modifier and Type | Field and Description |
---|---|
L |
left
The left item of the pair.
|
R |
right
The right item of the pair.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(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.
|
String |
toString()
Returns "Pair[«left.toString()»,«right.toString()»]
|
static <L,P extends Pair<L,?>> |
unpackLeft()
Convenience function for returning the left item of a pair.
|
static <R,P extends Pair<?,R>> |
unpackRight()
Convenience function for returning the right item of a pair.
|
public final L left
public final R right
public static <L,R> Pair<L,R> of(L left, R right)
L
- the type of the left itemR
- the type of the right itemleft
- the left itemright
- the right itempublic L getLeft()
public R getRight()
public L left()
public R right()
public <T> Pair<T,R> left(T newLeft)
T
- the new left typenewLeft
- the replacement left side of the pairpublic <T> Pair<L,T> right(T newRight)
T
- the new right typenewRight
- the replacement right side of the pairpublic static <L,R> Pair<L,R> pair(L left, R right)
L
- the type of the left itemR
- the type of the right itemleft
- the left itemright
- the right itempublic <T> Pair<T,Pair<L,R>> pushLeft(T t)
this
as the right item and the given value as the left.T
- the type of the left value for the returned pairt
- the left value for the returned pairthis
as the right item and the given value as the leftpublic <T> Pair<Pair<L,R>,T> pushRight(T t)
this
as the left item and the given value as the right.T
- the type of the right value for the returned pairt
- the right value for the returned pairthis
as the left item and the given value as the rightpublic <T> Pair<Pair<T,L>,R> nestLeft(T t)
pair(t, this.left)
as the left item and the the right value of this
as the right.T
- the type of item to nest with the left item of this
pairt
- the item to nest with the left item of this
pairpair(t, this.left)
as the left item and the the right value of this
as the rightpublic <T> Pair<L,Pair<T,R>> nestRight(T t)
pair(t, this.right)
as the right item and the the left value of this
as the left.T
- the type of item to nest with the right item of this
pairt
- the item to nest with the right item of this
pairpair(t, this.right)
as the right item and the the left value of this
as the leftpublic <T> Pair<T,R> mapLeft(Function<? super L,? extends T> function) throws Exception
this
.
The right value is unchanged.
T
- the type of the new left valuefunction
- a transformer for the left valuethis
Exception
- any thrown by function
public <T> Pair<L,T> mapRight(Function<? super R,? extends T> function) throws Exception
this
.
The left value is unchanged.
T
- the type of the new right valuefunction
- a transformer for the right valuethis
Exception
- any thrown by function
public <T> T map(Function<? super Pair<L,R>,? extends T> function) throws Exception
this
, returning the result.T
- the function result typefunction
- a function to apply to this
function
to this
Exception
- any thrown by function
public static <L,P extends Pair<L,?>> Function<P,L> unpackLeft()
L
- the type of the left itemP
- the pair typepublic static <R,P extends Pair<?,R>> Function<P,R> unpackRight()
R
- the type of the right itemP
- the pair typepublic boolean equals(Object o)
this
respectively.