L - the type of the left valueR - the type of the right valuepublic class Either<L,R> extends Object
Only one value can be present at any given type.
This class can be used as a monad to interact and chain functions to be executed over the possible return values.
Most likely the left type represent an error or failure result and the right value represent a successful result.
| Modifier and Type | Method and Description |
|---|---|
void |
apply(Consumer<? super L> leftConsumer,
Consumer<? super R> rightConsumer)
Receives a
Consumer functions for both, the left and right value and applies the one over the value that is present. |
void |
applyLeft(Consumer<? super L> consumer) |
void |
applyRight(Consumer<? super R> consumer) |
static <L,R> Either<L,R> |
empty()
Creates an
Either that doesn't have any left or right value. |
L |
getLeft() |
R |
getRight() |
<T> Optional<T> |
getValue() |
boolean |
isLeft() |
boolean |
isRight() |
static <L,R> Either<L,R> |
left(L value)
Creates an
Either with a left value. |
static <L,R> Either<L,R> |
left(L value,
Class<R> rightClass)
Creates an
Either with a left value and forcing a class for its empty right. |
<T> Either<T,R> |
mapLeft(Function<? super L,? extends T> func)
Allows to execute a function over the left value if it is present
|
<T> Either<L,T> |
mapRight(Function<? super R,? extends T> func)
Allows to execute a function over the right value if it is present
|
<T> T |
reduce(Function<? super L,? extends T> leftFunc,
Function<? super R,? extends T> rightFunc)
Allows to reduce to a single value using left and right functions with the same return type.
|
static <L,R> Either<L,R> |
right(Class<L> leftClass,
R value)
Creates an
Either with a right value and forcing a class for its empty left. |
static <L,R> Either<L,R> |
right(R value)
Creates an
Either with a right value. |
String |
toString() |
public static <L,R> Either<L,R> empty()
Either that doesn't have any left or right value.Either instancepublic static <L,R> Either<L,R> left(L value)
Either with a left value.L - the left value typeR - the right value typevalue - the left valueEither instancepublic static <L,R> Either<L,R> left(L value, Class<R> rightClass)
Either with a left value and forcing a class for its empty right.
This is useful because it avoids the casting afterwards which can clutter the code and affect its readability.
L - the left value typeR - the right value typevalue - the left valueEither instancepublic static <L,R> Either<L,R> right(R value)
Either with a right value.L - the left value typeR - the right value typevalue - the right valueEither instancepublic static <L,R> Either<L,R> right(Class<L> leftClass, R value)
Either with a right value and forcing a class for its empty left.
This is useful because it avoids the casting afterwards which can clutter the code and affect its readability.
L - the left value typeR - the right value typevalue - the right valueEither instancepublic <T> T reduce(Function<? super L,? extends T> leftFunc, Function<? super R,? extends T> rightFunc)
T - the return type of the function.leftFunc - the function to apply to the left valuerightFunc - the function to apply to the left valuenull if none were set.public <T> Either<T,R> mapLeft(Function<? super L,? extends T> func)
T - the return type of the function.func - the function to apply to the left valueEither created from the result of applying the function.public <T> Either<L,T> mapRight(Function<? super R,? extends T> func)
T - the return type of the function.func - the function to apply to the right valueEither created from the result of applying the function.public void apply(Consumer<? super L> leftConsumer, Consumer<? super R> rightConsumer)
Consumer functions for both, the left and right value and applies the one over the value that is present.leftConsumer - the function to apply to the left valuerightConsumer - the function to apply to the right valuepublic boolean isLeft()
public boolean isRight()
public L getLeft()
public R getRight()
public <T> Optional<T> getValue()
Copyright © 2024 MuleSoft, Inc.. All rights reserved.