Interface 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.
- All Superinterfaces:
Iterable<R>
,Serializable
,Value<R>
- All Known Implementing Classes:
Either.Left
,Either.Right
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).If the result of compute() is Left("error"), the value is Left("error").
- Author:
- Daniel Dietrich
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final class
TheLeft
version of anEither
.static final class
Deprecated.Either is right-biased.static final class
TheRight
version of anEither
.static final class
Deprecated.Either is right-biased. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptiondefault <X,
Y> Either <X, Y> Maps either the left or the right side of this disjunction.boolean
Clarifies that values have a proper equals() method implemented.Filters this right-biasedEither
by testing a predicate.Filters this right-biasedEither
by testing a predicate.FlatMaps this right-biased Either.default <U> U
Folds either the left or the right side of this disjunction.get()
Gets the right value if this is aRight
or throws if this is aLeft
.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.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-biasedEither
's value is computed synchronously.default boolean
isEmpty()
Checks, thisValue
is empty, i.e. if the underlying value is absent.default boolean
isLazy()
A right-biasedEither
'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
A right-biasedEither
is single-valued.iterator()
Returns a richio.vavr.collection.Iterator
.default Either.LeftProjection
<L, R> left()
Deprecated.Either is right-biased.static <L,
R> Either <L, R> left
(L left) Constructs aEither.Left
Maps the value of this Either if it is a Right, performs no operation if this is a Left.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> Narrows a widenedEither<? extends L, ? extends R>
toEither<L, R>
by performing a type-safe cast.default void
Runs an action in the case this is a projection on a Left value.Performs the givenaction
on the first element if this is an eager implementation.default Either.RightProjection
<L, R> right()
Deprecated.Either is right-biased.static <L,
R> Either <L, R> right
(R right) Constructs aEither.Right
Reduces manyEither
s into a singleEither
by transforming anIterable<Either<L, R>>
into aEither<Seq<L>, Seq<R>>
.sequenceRight
(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>>
.swap()
Converts aLeft
to aRight
vice versa by wrapping the value in a new type.toString()
Clarifies that values have a proper toString() method implemented.default Validation
<L, R> Returns this asValidation
.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 singleEither
by transforming anIterable<? extends T>
into aEither<Seq<U>>
.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 singleEither
by transforming anIterable<? extends T>
into aEither<Seq<U>>
.Methods inherited from interface io.vavr.Value
collect, collect, contains, corresponds, eq, exists, forAll, forEach, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, 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
-
Field Details
-
serialVersionUID
static final long serialVersionUID- See Also:
-
-
Method Details
-
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
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
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
L getLeft()Returns the left value.- Returns:
- The left value.
- Throws:
NoSuchElementException
- if this is aRight
.
-
isLeft
boolean isLeft()Returns whether this Either is a Left.- Returns:
- true, if this is a Left, false otherwise
-
isRight
boolean isRight()Returns whether this Either is a Right.- Returns:
- true, if this is a Right, false otherwise
-
left
Deprecated.Either is right-biased. Useswap()
instead of projections.Returns a LeftProjection of this Either.- Returns:
- a new LeftProjection of this
-
right
Deprecated.Either is right-biased. Useswap()
instead of projections.Returns a RightProjection of this Either.- Returns:
- a new RightProjection of this
-
bimap
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.- 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
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.- 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
static <L,R> Either<Seq<L>,Seq<R>> sequence(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:
NullPointerException
- ifeithers
is null
-
traverse
static <L,R, Either<Seq<L>,T> 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 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:
NullPointerException
- if values or f is null.
-
sequenceRight
static <L,R> Either<L,Seq<R>> sequenceRight(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:
NullPointerException
- ifeithers
is null
-
traverseRight
static <L,R, Either<L,T> 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 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:
NullPointerException
- if values or f is null.
-
getOrElseGet
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
Runs an action in the case this is a projection on a Left value.- Parameters:
action
- an action which consumes a Left value
-
getOrElseThrow
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 Left
-
swap
Converts aLeft
to aRight
vice versa by wrapping the value in a new type.- Returns:
- a new
Either
-
flatMap
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:
NullPointerException
- ifmapper
is null
-
map
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);
- Specified by:
map
in interfaceValue<L>
- Type Parameters:
U
- Component type of the mapped right value- Parameters:
mapper
- A mapper- Returns:
- a mapped
Monad
- Throws:
NullPointerException
- ifmapper
is null
-
mapLeft
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:
NullPointerException
- ifmapper
is null
-
filter
Filters this right-biasedEither
by testing a predicate.- Parameters:
predicate
- A predicate- Returns:
- a new
Option
instance - Throws:
NullPointerException
- ifpredicate
is null
-
filterOrElse
default Either<L,R> filterOrElse(Predicate<? super R> predicate, 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 zero 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:
NullPointerException
- ifpredicate
is null
-
get
R get()Gets the right value if this is aRight
or throws if this is aLeft
.- Specified by:
get
in interfaceValue<L>
- Returns:
- the right value
- Throws:
NoSuchElementException
- if this is aLeft
.
-
isEmpty
default boolean isEmpty()Description copied from interface:Value
Checks, thisValue
is empty, i.e. if the underlying value is absent. -
orElse
-
orElse
-
isAsync
default boolean isAsync()A right-biasedEither
's value is computed synchronously. -
isLazy
default boolean isLazy()A right-biasedEither
's value is computed eagerly. -
isSingleValued
default boolean isSingleValued()A right-biasedEither
is single-valued.- Specified by:
isSingleValued
in interfaceValue<L>
- Returns:
true
-
iterator
Description copied from interface:Value
Returns a richio.vavr.collection.Iterator
. -
peek
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. -
peekLeft
-
toValidation
Returns this asValidation
.- Returns:
Validation.valid(get())
if this is right, otherwiseValidation.invalid(getLeft())
.
-
equals
Description copied from interface:Value
Clarifies that values have a proper equals() method implemented. -
hashCode
int hashCode()Description copied from interface:Value
Clarifies that values have a proper hashCode() method implemented.See Object.hashCode().
-
toString
String toString()Description copied from interface:Value
Clarifies that values have a proper toString() method implemented.See Object.toString().
-