L
- The type of the Left value of an Either.R
- The type of the Right value of an Either.public interface Either<L,R> extends Value<R>, Serializable
Either.Left
or a
Either.Right
.
If the given Either is a Right and projected to a Left, the Left operations have no effect on the Right value.
If the given Either is a Left and projected to a Right, the Right operations have no effect on the Left value.
If a Left is projected to a Left or a Right is projected to a Right, the operations have an effect.
Example: A compute() function, which results either in an Integer value (in the case of success) or in an error message of type String (in the case of failure). By convention the success case is Right and the failure is Left.
Either<String,Integer> value = compute().right().map(i -> i * 2).toEither();
If the result of compute() is Right(1), the value is Right(2).Modifier and Type | Interface and Description |
---|---|
static class |
Either.Left<L,R>
The
Left version of an Either . |
static class |
Either.LeftProjection<L,R>
Deprecated.
Either is right-biased. Use
swap() instead of projections. |
static class |
Either.Right<L,R>
The
Right version of an Either . |
static class |
Either.RightProjection<L,R>
Deprecated.
Either is right-biased. Use
swap() instead of projections. |
Modifier and Type | Field and Description |
---|---|
static long |
serialVersionUID |
Modifier and Type | Method and Description |
---|---|
default <X,Y> Either<X,Y> |
bimap(Function<? super L,? extends X> leftMapper,
Function<? super R,? extends Y> rightMapper)
Maps either the left or the right side of this disjunction.
|
boolean |
equals(Object o)
Clarifies that values have a proper equals() method implemented.
|
default Option<Either<L,R>> |
filter(Predicate<? super R> predicate)
Filters this right-biased
Either by testing a predicate. |
default Either<L,R> |
filterOrElse(Predicate<? super R> predicate,
Function<? super R,? extends L> zero)
Filters this right-biased
Either by testing a predicate. |
default <U> Either<L,U> |
flatMap(Function<? super R,? extends Either<L,? extends U>> mapper)
FlatMaps this right-biased Either.
|
default <U> U |
fold(Function<? super L,? extends U> leftMapper,
Function<? super R,? extends U> rightMapper)
Folds either the left or the right side of this disjunction.
|
R |
get()
Gets the right value if this is a
Right or throws if this is a Left . |
L |
getLeft()
Returns the left value.
|
default R |
getOrElseGet(Function<? super L,? extends R> other)
Gets the Right value or an alternate value, if the projected Either is a Left.
|
default <X extends Throwable> |
getOrElseThrow(Function<? super L,X> exceptionFunction)
Gets the Right value or throws, if the projected Either is a Left.
|
int |
hashCode()
Clarifies that values have a proper hashCode() method implemented.
|
default boolean |
isAsync()
A right-biased
Either 's value is computed synchronously. |
default boolean |
isEmpty()
Checks, this
Value is empty, i.e. |
default boolean |
isLazy()
A right-biased
Either 's value is computed eagerly. |
boolean |
isLeft()
Returns whether this Either is a Left.
|
boolean |
isRight()
Returns whether this Either is a Right.
|
default boolean |
isSingleValued()
A right-biased
Either is single-valued. |
default Iterator<R> |
iterator()
Returns a rich
io.vavr.collection.Iterator . |
default Either.LeftProjection<L,R> |
left()
Deprecated.
Either is right-biased. Use
swap() instead of projections. |
static <L,R> Either<L,R> |
left(L left)
Constructs a
Either.Left |
default <U> Either<L,U> |
map(Function<? super R,? extends U> mapper)
Maps the value of this Either if it is a Right, performs no operation if this is a Left.
|
default <U> Either<U,R> |
mapLeft(Function<? super L,? extends U> leftMapper)
Maps the value of this Either if it is a Left, performs no operation if this is a Right.
|
static <L,R> Either<L,R> |
narrow(Either<? extends L,? extends R> either)
Narrows a widened
Either<? extends L, ? extends R> to Either<L, R>
by performing a type-safe cast. |
default Either<L,R> |
orElse(Either<? extends L,? extends R> other) |
default Either<L,R> |
orElse(Supplier<? extends Either<? extends L,? extends R>> supplier) |
default void |
orElseRun(Consumer<? super L> action)
Runs an action in the case this is a projection on a Left value.
|
default Either<L,R> |
peek(Consumer<? super R> action)
Performs the given
action on the first element if this is an eager implementation. |
default Either<L,R> |
peekLeft(Consumer<? super L> action) |
default Either.RightProjection<L,R> |
right()
Deprecated.
Either is right-biased. Use
swap() instead of projections. |
static <L,R> Either<L,R> |
right(R right)
Constructs a
Either.Right |
static <L,R> Either<Seq<L>,Seq<R>> |
sequence(Iterable<? extends Either<? extends L,? extends R>> eithers)
Reduces many
Either s into a single Either by transforming an
Iterable<Either<L, R>> into a Either<Seq<L>, Seq<R>> . |
static <L,R> Either<L,Seq<R>> |
sequenceRight(Iterable<? extends Either<? extends L,? extends R>> eithers)
Reduces many
Either s into a single Either by transforming an
Iterable<Either<L, R>> into a Either<L, Seq<R>> . |
default Either<R,L> |
swap()
Converts a
Left to a Right vice versa by wrapping the value in a new type. |
String |
toString()
Clarifies that values have a proper toString() method implemented.
|
default Validation<L,R> |
toValidation()
Returns this as
Validation . |
static <L,R,T> Either<Seq<L>,Seq<R>> |
traverse(Iterable<? extends T> values,
Function<? super T,? extends Either<? extends L,? extends R>> mapper)
Maps the values of an iterable to a sequence of mapped values into a single
Either by
transforming an Iterable<? extends T> into a Either<Seq<U>> . |
static <L,R,T> Either<L,Seq<R>> |
traverseRight(Iterable<? extends T> values,
Function<? super T,? extends Either<? extends L,? extends R>> mapper)
Maps the values of an iterable to a sequence of mapped values into a single
Either by
transforming an Iterable<? extends T> into a Either<Seq<U>> . |
collect, collect, contains, corresponds, eq, exists, forAll, forEach, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, narrow, out, out, spliterator, stderr, stdout, stringPrefix, toArray, toCharSeq, toCompletableFuture, toEither, toEither, toInvalid, toInvalid, toJavaArray, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaMap, toJavaOptional, toJavaParallelStream, toJavaSet, toJavaSet, toJavaStream, toLeft, toLeft, toLinkedMap, toLinkedMap, toLinkedSet, toList, toMap, toMap, toOption, toPriorityQueue, toPriorityQueue, toQueue, toRight, toRight, toSet, toSortedMap, toSortedMap, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toStream, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVector
static final long serialVersionUID
static <L,R> Either<L,R> right(R right)
Either.Right
L
- Type of left value.R
- Type of right value.right
- The value.Right
instance.static <L,R> Either<L,R> left(L left)
Either.Left
L
- Type of left value.R
- Type of right value.left
- The value.Left
instance.static <L,R> Either<L,R> narrow(Either<? extends L,? extends R> either)
Either<? extends L, ? extends R>
to Either<L, R>
by performing a type-safe cast. This is eligible because immutable/read-only
collections are covariant.L
- Type of left value.R
- Type of right value.either
- A Either
.either
instance as narrowed type Either<L, R>
.L getLeft()
NoSuchElementException
- if this is a Right
.boolean isLeft()
boolean isRight()
@Deprecated default Either.LeftProjection<L,R> left()
swap()
instead of projections.@Deprecated default Either.RightProjection<L,R> right()
swap()
instead of projections.default <X,Y> Either<X,Y> bimap(Function<? super L,? extends X> leftMapper, Function<? super R,? extends Y> rightMapper)
X
- The new left type of the resulting EitherY
- The new right type of the resulting EitherleftMapper
- maps the left value if this is a LeftrightMapper
- maps the right value if this is a Rightdefault <U> U fold(Function<? super L,? extends U> leftMapper, Function<? super R,? extends U> rightMapper)
U
- type of the folded valueleftMapper
- maps the left value if this is a LeftrightMapper
- maps the right value if this is a Rightstatic <L,R> Either<Seq<L>,Seq<R>> sequence(Iterable<? extends Either<? extends L,? extends R>> eithers)
Either
s into a single Either
by transforming an
Iterable<Either<L, R>>
into a Either<Seq<L>, Seq<R>>
.
If any of the given Either
s is a Either.Left
then sequence
returns a
Either.Left
containing a non-empty Seq
of all left values.
If none of the given Either
s is a Either.Left
then sequence
returns a
Either.Right
containing a (possibly empty) Seq
of all right values.
// = Right(Seq())
Either.sequence(List.empty())
// = Right(Seq(1, 2))
Either.sequence(List.of(Either.right(1), Either.right(2)))
// = Left(Seq("x"))
Either.sequence(List.of(Either.right(1), Either.left("x")))
L
- closure of all left types of the given Either
sR
- closure of all right types of the given Either
seithers
- An Iterable
of Either
sEither
of a Seq
of left or right valuesNullPointerException
- if eithers
is nullstatic <L,R,T> Either<Seq<L>,Seq<R>> traverse(Iterable<? extends T> values, Function<? super T,? extends Either<? extends L,? extends R>> mapper)
Either
by
transforming an Iterable<? extends T>
into a Either<Seq<U>>
.
L
- The mapped left value type.R
- The mapped right value type.T
- The type of the given values.values
- An Iterable
of values.mapper
- A mapper of values to EithersEither
of a Seq
of results.NullPointerException
- if values or f is null.static <L,R> Either<L,Seq<R>> sequenceRight(Iterable<? extends Either<? extends L,? extends R>> eithers)
Either
s into a single Either
by transforming an
Iterable<Either<L, R>>
into a Either<L, Seq<R>>
.
If any of the given Either
s is a Either.Left
then sequenceRight
returns a
Either.Left
containing the first left value (in iteration order).
If none of the given Either
s is a Either.Left
then sequenceRight
returns a
Either.Right
containing a (possibly empty) Seq
of all right values.
// = Right(Seq())
Either.sequenceRight(List.empty())
// = Right(Seq(1, 2))
Either.sequenceRight(List.of(Either.right(1), Either.right(2)))
// = Left("x1")
Either.sequenceRight(List.of(Either.right(1), Either.left("x1"), Either.left("x2")))
L
- closure of all left types of the given Either
sR
- closure of all right types of the given Either
seithers
- An Iterable
of Either
sEither
of either a Seq
of right values or the first left value, if present.NullPointerException
- if eithers
is nullstatic <L,R,T> Either<L,Seq<R>> traverseRight(Iterable<? extends T> values, Function<? super T,? extends Either<? extends L,? extends R>> mapper)
Either
by
transforming an Iterable<? extends T>
into a Either<Seq<U>>
.
L
- The mapped left value type.R
- The mapped right value type.T
- The type of the given values.values
- An Iterable
of values.mapper
- A mapper of values to EithersEither
of a Seq
of results.NullPointerException
- if values or f is null.default R getOrElseGet(Function<? super L,? extends R> other)
other
- a function which converts a Left value to an alternative Right valueother
by applying the Left value.default void orElseRun(Consumer<? super L> action)
action
- an action which consumes a Left valuedefault <X extends Throwable> R getOrElseThrow(Function<? super L,X> exceptionFunction) throws X extends Throwable
X
- a throwable typeexceptionFunction
- a function which creates an exception based on a Left valueexceptionFunction
by applying the Left value.X
- if the projected Either is a LeftX extends Throwable
default Either<R,L> swap()
Left
to a Right
vice versa by wrapping the value in a new type.Either
default <U> Either<L,U> flatMap(Function<? super R,? extends Either<L,? extends U>> mapper)
U
- Component type of the mapped right valuemapper
- A mapperEither<L, U>
if this is a Left, otherwise the right mapping resultNullPointerException
- if mapper
is nulldefault <U> Either<L,U> map(Function<? super R,? extends U> mapper)
import static io.vavr.API.*;
// = Right("A")
Right("a").map(String::toUpperCase);
// = Left(1)
Left(1).map(String::toUpperCase);
map
in interface Value<R>
U
- Component type of the mapped right valuemapper
- A mapperMonad
NullPointerException
- if mapper
is nulldefault <U> Either<U,R> mapLeft(Function<? super L,? extends U> leftMapper)
import static io.vavr.API.*;
// = Left(2)
Left(1).mapLeft(i -> i + 1);
// = Right("a")
Right("a").mapLeft(i -> i + 1);
U
- Component type of the mapped right valueleftMapper
- A mapperMonad
NullPointerException
- if mapper
is nulldefault Option<Either<L,R>> filter(Predicate<? super R> predicate)
Either
by testing a predicate.
predicate
- A predicateOption
instanceNullPointerException
- if predicate
is nulldefault Either<L,R> filterOrElse(Predicate<? super R> predicate, Function<? super R,? extends L> zero)
Either
by testing a predicate.
If the Either
is a Right
and the predicate doesn't match, the
Either
will be turned into a Left
with contents computed by applying
the filterVal function to the Either
value.
import static io.vavr.API.*;
// = Left("bad: a")
Right("a").filterOrElse(i -> false, val -> "bad: " + val);
// = Right("a")
Right("a").filterOrElse(i -> true, val -> "bad: " + val);
predicate
- A predicatezero
- A function that turns a right value into a left value if the right value does not make it through the filter.Either
instanceNullPointerException
- if predicate
is nullR get()
Right
or throws if this is a Left
.get
in interface Value<R>
NoSuchElementException
- if this is a Left
.default boolean isEmpty()
Value
Value
is empty, i.e. if the underlying value is absent.default boolean isAsync()
Either
's value is computed synchronously.default boolean isLazy()
Either
's value is computed eagerly.default boolean isSingleValued()
Either
is single-valued.isSingleValued
in interface Value<R>
true
default Iterator<R> iterator()
Value
io.vavr.collection.Iterator
.default Either<L,R> peek(Consumer<? super R> action)
Value
action
on the first element if this is an eager implementation.
Performs the given action
on all elements (the first immediately, successive deferred),
if this is a lazy implementation.default Validation<L,R> toValidation()
Validation
.Validation.valid(get())
if this is right, otherwise Validation.invalid(getLeft())
.boolean equals(Object o)
Value
int hashCode()
Value
See Object.hashCode().
Copyright © 2019. All Rights Reserved.