Class Either<L,R>
- java.lang.Object
-
- io.vavr.control.Either<L,R>
-
- Type Parameters:
L
- The type of the Left value of an Either.R
- The type of the Right value of an Either.
- Direct Known Subclasses:
Either.Left
,Either.Right
public abstract class Either<L,R> extends java.lang.Object implements Iterable<R>, Value<R>, java.io.Serializable
Either represents a value of two possible types. An Either is either aEither.Left
or aEither.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 Left("error"), the value is Left("error").- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Either.Left<L,R>
Deprecated.will be removed from the public APIstatic class
Either.LeftProjection<L,R>
Deprecated.Either is right-biased.static class
Either.Right<L,R>
Deprecated.will be removed from the public APIstatic class
Either.RightProjection<L,R>
Deprecated.Either is right-biased.
-
Method Summary
Modifier and Type Method Description <X,Y>
Either<X,Y>bimap(java.util.function.Function<? super L,? extends X> leftMapper, java.util.function.Function<? super R,? extends Y> rightMapper)
Maps either the left or the right side of this disjunction.Option<Either<L,R>>
filter(java.util.function.Predicate<? super R> predicate)
Filters this right-biasedEither
by testing a predicate.Option<Either<L,R>>
filterNot(java.util.function.Predicate<? super R> predicate)
Filters this right-biasedEither
by testing a predicate.Either<L,R>
filterOrElse(java.util.function.Predicate<? super R> predicate, java.util.function.Function<? super R,? extends L> zero)
Filters this right-biasedEither
by testing a predicate.<U> Either<L,U>
flatMap(java.util.function.Function<? super R,? extends Either<L,? extends U>> mapper)
FlatMaps this right-biased Either.<U> U
fold(java.util.function.Function<? super L,? extends U> leftMapper, java.util.function.Function<? super R,? extends U> rightMapper)
Folds either the left or the right side of this disjunction.abstract R
get()
Gets the right value if this is aRight
or throws if this is aLeft
.abstract L
getLeft()
Returns the left value.R
getOrElseGet(java.util.function.Function<? super L,? extends R> other)
Gets the Right value or an alternate value, if the projected Either is a Left.<X extends java.lang.Throwable>
RgetOrElseThrow(java.util.function.Function<? super L,X> exceptionFunction)
Gets the Right value or throws, if the projected Either is a Left.boolean
isAsync()
A right-biasedEither
's value is computed synchronously.boolean
isEmpty()
Checks, thisValue
is empty, i.e.boolean
isLazy()
A right-biasedEither
's value is computed eagerly.abstract boolean
isLeft()
Returns whether this Either is a Left.abstract boolean
isRight()
Returns whether this Either is a Right.boolean
isSingleValued()
A right-biasedEither
is single-valued.Iterator<R>
iterator()
Returns a richIterator
that allows us to perform intermediate, sequential operations known from Vavr's collection library.Either.LeftProjection<L,R>
left()
Deprecated.Either is right-biased.static <L,R>
Either<L,R>left(L left)
Constructs aEither.Left
<U> Either<L,U>
map(java.util.function.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.<U> Either<U,R>
mapLeft(java.util.function.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 widenedEither<? extends L, ? extends R>
toEither<L, R>
by performing a type-safe cast.Either<L,R>
orElse(Either<? extends L,? extends R> other)
Either<L,R>
orElse(java.util.function.Supplier<? extends Either<? extends L,? extends R>> supplier)
void
orElseRun(java.util.function.Consumer<? super L> action)
Runs an action in the case this is a projection on a Left value.Either<L,R>
peek(java.util.function.Consumer<? super R> action)
Performs the givenaction
on the first element if this is an eager implementation.Either<L,R>
peekLeft(java.util.function.Consumer<? super L> action)
Either.RightProjection<L,R>
right()
Deprecated.Either is right-biased.static <L,R>
Either<L,R>right(R right)
Constructs aEither.Right
static <L,R>
Either<Seq<L>,Seq<R>>sequence(java.lang.Iterable<? extends Either<? extends L,? extends R>> eithers)
Reduces manyEither
s into a singleEither
by transforming anIterable<Either<L, R>>
into aEither<Seq<L>, Seq<R>>
.static <L,R>
Either<L,Seq<R>>sequenceRight(java.lang.Iterable<? extends Either<? extends L,? extends R>> eithers)
Reduces manyEither
s into a singleEither
by transforming anIterable<Either<L, R>>
into aEither<L, Seq<R>>
.Either<R,L>
swap()
Converts aLeft
to aRight
vice versa by wrapping the value in a new type.Validation<L,R>
toValidation()
Returns this asValidation
.static <L,R,T>
Either<Seq<L>,Seq<R>>traverse(java.lang.Iterable<? extends T> values, java.util.function.Function<? super T,? extends Either<? extends L,? extends R>> mapper)
Maps the values of an iterable to a sequence of mapped values into a singleEither
by transforming anIterable<? extends T>
into aEither<Seq<U>>
.static <L,R,T>
Either<L,Seq<R>>traverseRight(java.lang.Iterable<? extends T> values, java.util.function.Function<? super T,? extends Either<? extends L,? extends R>> mapper)
Maps the values of an iterable to a sequence of mapped values into a singleEither
by transforming anIterable<? extends T>
into aEither<Seq<U>>
.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.vavr.Value
collect, collect, contains, corresponds, eq, equals, exists, forAll, forEach, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, hashCode, 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, toString, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVector
-
-
-
-
Method Detail
-
right
public static <L,R> Either<L,R> right(R right)
Constructs aEither.Right
- Type Parameters:
L
- Type of left value.R
- Type of right value.- Parameters:
right
- The value.- Returns:
- A new
Right
instance.
-
left
public static <L,R> Either<L,R> left(L left)
Constructs aEither.Left
- Type Parameters:
L
- Type of left value.R
- Type of right value.- Parameters:
left
- The value.- Returns:
- A new
Left
instance.
-
narrow
public static <L,R> Either<L,R> narrow(Either<? extends L,? extends R> either)
Narrows a widenedEither<? extends L, ? extends R>
toEither<L, R>
by performing a type-safe cast. This is eligible because immutable/read-only collections are covariant.- Type Parameters:
L
- Type of left value.R
- Type of right value.- Parameters:
either
- AEither
.- Returns:
- the given
either
instance as narrowed typeEither<L, R>
.
-
getLeft
public abstract L getLeft()
Returns the left value.- Returns:
- The left value.
- Throws:
java.util.NoSuchElementException
- if this is aRight
.
-
isLeft
public abstract boolean isLeft()
Returns whether this Either is a Left.- Returns:
- true, if this is a Left, false otherwise
-
isRight
public abstract boolean isRight()
Returns whether this Either is a Right.- Returns:
- true, if this is a Right, false otherwise
-
left
@Deprecated public final Either.LeftProjection<L,R> left()
Deprecated.Either is right-biased. Useswap()
instead of projections.Returns a LeftProjection of this Either.- Returns:
- a new LeftProjection of this
-
right
@Deprecated public final Either.RightProjection<L,R> right()
Deprecated.Either is right-biased. Useswap()
instead of projections.Returns a RightProjection of this Either.- Returns:
- a new RightProjection of this
-
bimap
public final <X,Y> Either<X,Y> bimap(java.util.function.Function<? super L,? extends X> leftMapper, java.util.function.Function<? super R,? extends Y> rightMapper)
Maps either the left or the right side of this disjunction.- Type Parameters:
X
- The new left type of the resulting EitherY
- The new right type of the resulting Either- Parameters:
leftMapper
- maps the left value if this is a LeftrightMapper
- maps the right value if this is a Right- Returns:
- A new Either instance
-
fold
public final <U> U fold(java.util.function.Function<? super L,? extends U> leftMapper, java.util.function.Function<? super R,? extends U> rightMapper)
Folds either the left or the right side of this disjunction.- Type Parameters:
U
- type of the folded value- Parameters:
leftMapper
- maps the left value if this is a LeftrightMapper
- maps the right value if this is a Right- Returns:
- A value of type U
-
sequence
public static <L,R> Either<Seq<L>,Seq<R>> sequence(java.lang.Iterable<? extends Either<? extends L,? extends R>> eithers)
Reduces manyEither
s into a singleEither
by transforming anIterable<Either<L, R>>
into aEither<Seq<L>, Seq<R>>
.If any of the given
Either
s is aEither.Left
thensequence
returns aEither.Left
containing a non-emptySeq
of all left values.If none of the given
Either
s is aEither.Left
thensequence
returns aEither.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")))
- Type Parameters:
L
- closure of all left types of the givenEither
sR
- closure of all right types of the givenEither
s- Parameters:
eithers
- AnIterable
ofEither
s- Returns:
- An
Either
of aSeq
of left or right values - Throws:
java.lang.NullPointerException
- ifeithers
is null
-
traverse
public static <L,R,T> Either<Seq<L>,Seq<R>> traverse(java.lang.Iterable<? extends T> values, java.util.function.Function<? super T,? extends Either<? extends L,? extends R>> mapper)
Maps the values of an iterable to a sequence of mapped values into a singleEither
by transforming anIterable<? extends T>
into aEither<Seq<U>>
.- Type Parameters:
L
- The mapped left value type.R
- The mapped right value type.T
- The type of the given values.- Parameters:
values
- AnIterable
of values.mapper
- A mapper of values to Eithers- Returns:
- A
Either
of aSeq
of results. - Throws:
java.lang.NullPointerException
- if values or f is null.
-
sequenceRight
public static <L,R> Either<L,Seq<R>> sequenceRight(java.lang.Iterable<? extends Either<? extends L,? extends R>> eithers)
Reduces manyEither
s into a singleEither
by transforming anIterable<Either<L, R>>
into aEither<L, Seq<R>>
.If any of the given
Either
s is aEither.Left
thensequenceRight
returns aEither.Left
containing the first left value (in iteration order).If none of the given
Either
s is aEither.Left
thensequenceRight
returns aEither.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")))
- Type Parameters:
L
- closure of all left types of the givenEither
sR
- closure of all right types of the givenEither
s- Parameters:
eithers
- AnIterable
ofEither
s- Returns:
- An
Either
of either aSeq
of right values or the first left value, if present. - Throws:
java.lang.NullPointerException
- ifeithers
is null
-
traverseRight
public static <L,R,T> Either<L,Seq<R>> traverseRight(java.lang.Iterable<? extends T> values, java.util.function.Function<? super T,? extends Either<? extends L,? extends R>> mapper)
Maps the values of an iterable to a sequence of mapped values into a singleEither
by transforming anIterable<? extends T>
into aEither<Seq<U>>
.- Type Parameters:
L
- The mapped left value type.R
- The mapped right value type.T
- The type of the given values.- Parameters:
values
- AnIterable
of values.mapper
- A mapper of values to Eithers- Returns:
- A
Either
of aSeq
of results. - Throws:
java.lang.NullPointerException
- if values or f is null.
-
getOrElseGet
public final R getOrElseGet(java.util.function.Function<? super L,? extends R> other)
Gets the Right value or an alternate value, if the projected Either is a Left.- Parameters:
other
- a function which converts a Left value to an alternative Right value- Returns:
- the right value, if the underlying Either is a Right or else the alternative Right value provided by
other
by applying the Left value.
-
orElseRun
public final void orElseRun(java.util.function.Consumer<? super L> action)
Runs an action in the case this is a projection on a Left value.- Parameters:
action
- an action which consumes a Left value
-
getOrElseThrow
public final <X extends java.lang.Throwable> R getOrElseThrow(java.util.function.Function<? super L,X> exceptionFunction) throws X extends java.lang.Throwable
Gets the Right value or throws, if the projected Either is a Left.- Type Parameters:
X
- a throwable type- Parameters:
exceptionFunction
- a function which creates an exception based on a Left value- Returns:
- the right value, if the underlying Either is a Right or else throws the exception provided by
exceptionFunction
by applying the Left value. - Throws:
X
- if the projected Either is a LeftX extends java.lang.Throwable
-
swap
public final Either<R,L> swap()
Converts aLeft
to aRight
vice versa by wrapping the value in a new type.- Returns:
- a new
Either
-
flatMap
public final <U> Either<L,U> flatMap(java.util.function.Function<? super R,? extends Either<L,? extends U>> mapper)
FlatMaps this right-biased Either.- Type Parameters:
U
- Component type of the mapped right value- Parameters:
mapper
- A mapper- Returns:
- this as
Either<L, U>
if this is a Left, otherwise the right mapping result - Throws:
java.lang.NullPointerException
- ifmapper
is null
-
map
public final <U> Either<L,U> map(java.util.function.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.import static io.vavr.API.*; // = Right("A") Right("a").map(String::toUpperCase); // = Left(1) Left(1).map(String::toUpperCase);
-
mapLeft
public final <U> Either<U,R> mapLeft(java.util.function.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.import static io.vavr.API.*; // = Left(2) Left(1).mapLeft(i -> i + 1); // = Right("a") Right("a").mapLeft(i -> i + 1);
- Type Parameters:
U
- Component type of the mapped right value- Parameters:
leftMapper
- A mapper- Returns:
- a mapped
Monad
- Throws:
java.lang.NullPointerException
- ifmapper
is null
-
filter
public final Option<Either<L,R>> filter(java.util.function.Predicate<? super R> predicate)
Filters this right-biasedEither
by testing a predicate.- Parameters:
predicate
- A predicate- Returns:
- a new
Option
instance - Throws:
java.lang.NullPointerException
- ifpredicate
is null
-
filterNot
public final Option<Either<L,R>> filterNot(java.util.function.Predicate<? super R> predicate)
Filters this right-biasedEither
by testing a predicate.- Parameters:
predicate
- A predicate- Returns:
- a new
Either
- Throws:
java.lang.NullPointerException
- ifpredicate
is null
-
filterOrElse
public final Either<L,R> filterOrElse(java.util.function.Predicate<? super R> predicate, java.util.function.Function<? super R,? extends L> zero)
Filters this right-biasedEither
by testing a predicate. If theEither
is aRight
and the predicate doesn't match, theEither
will be turned into aLeft
with contents computed by applying the filterVal function to theEither
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);
- Parameters:
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.- Returns:
- an
Either
instance - Throws:
java.lang.NullPointerException
- ifpredicate
is null
-
get
public abstract R get()
Gets the right value if this is aRight
or throws if this is aLeft
.
-
isEmpty
public final boolean isEmpty()
Description copied from interface:Value
Checks, thisValue
is empty, i.e. if the underlying value is absent.
-
orElse
public final Either<L,R> orElse(java.util.function.Supplier<? extends Either<? extends L,? extends R>> supplier)
-
isAsync
public final boolean isAsync()
A right-biasedEither
's value is computed synchronously.
-
isLazy
public final boolean isLazy()
A right-biasedEither
's value is computed eagerly.
-
isSingleValued
public final boolean isSingleValued()
A right-biasedEither
is single-valued.- Specified by:
isSingleValued
in interfaceValue<L>
- Returns:
true
-
iterator
public final Iterator<R> iterator()
Description copied from interface:Iterable
Returns a richIterator
that allows us to perform intermediate, sequential operations known from Vavr's collection library.
-
peek
public final Either<L,R> peek(java.util.function.Consumer<? super R> action)
Description copied from interface:Value
Performs the givenaction
on the first element if this is an eager implementation. Performs the givenaction
on all elements (the first immediately, successive deferred), if this is a lazy implementation.
-
toValidation
public final Validation<L,R> toValidation()
Returns this asValidation
.- Returns:
Validation.valid(get())
if this is right, otherwiseValidation.invalid(getLeft())
.
-
-