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").
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classTheLeftversion of anEither.static final classDeprecated.Either is right-biased.static final classTheRightversion of anEither.static final classDeprecated.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.booleanClarifies that values have a proper equals() method implemented.Filters this right-biasedEitherby testing a predicate.Filters this right-biasedEitherby testing a predicate.FlatMaps this right-biased Either.default <U> UFolds either the left or the right side of this disjunction.get()Gets the right value if this is aRightor throws if this is aLeft.getLeft()Returns the left value.default RgetOrElseGet(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.inthashCode()Clarifies that values have a proper hashCode() method implemented.default booleanisAsync()A right-biasedEither's value is computed synchronously.default booleanisEmpty()Checks, thisValueis empty, i.e.default booleanisLazy()A right-biasedEither's value is computed eagerly.booleanisLeft()Returns whether this Either is a Left.booleanisRight()Returns whether this Either is a Right.default booleanA right-biasedEitheris 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.LeftMaps 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 voidRuns an action in the case this is a projection on a Left value.Performs the givenactionon 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.RightReduces manyEithers into a singleEitherby transforming anIterable<Either<L, R>>into aEither<Seq<L>, Seq<R>>.sequenceRight(Iterable<? extends Either<? extends L, ? extends R>> eithers) Reduces manyEithers into a singleEitherby transforming anIterable<Either<L, R>>into aEither<L, Seq<R>>.swap()Converts aLeftto aRightvice 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 singleEitherby 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 singleEitherby transforming anIterable<? extends T>into aEither<Seq<U>>.Methods inherited from interface 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
Rightinstance.
-
left
Constructs aEither.Left- Type Parameters:
L- Type of left value.R- Type of right value.- Parameters:
left- The value.- Returns:
- A new
Leftinstance.
-
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
eitherinstance 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 manyEithers into a singleEitherby transforming anIterable<Either<L, R>>into aEither<Seq<L>, Seq<R>>.If any of the given
Eithers is aEither.Leftthensequencereturns aEither.Leftcontaining a non-emptySeqof all left values.If none of the given
Eithers is aEither.Leftthensequencereturns aEither.Rightcontaining a (possibly empty)Seqof 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 givenEithersR- closure of all right types of the givenEithers- Parameters:
eithers- AnIterableofEithers- Returns:
- An
Eitherof aSeqof left or right values - Throws:
NullPointerException- ifeithersis 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 singleEitherby 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- AnIterableof values.mapper- A mapper of values to Eithers- Returns:
- A
Eitherof aSeqof 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 manyEithers into a singleEitherby transforming anIterable<Either<L, R>>into aEither<L, Seq<R>>.If any of the given
Eithers is aEither.LeftthensequenceRightreturns aEither.Leftcontaining the first left value (in iteration order).If none of the given
Eithers is aEither.LeftthensequenceRightreturns aEither.Rightcontaining a (possibly empty)Seqof 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 givenEithersR- closure of all right types of the givenEithers- Parameters:
eithers- AnIterableofEithers- Returns:
- An
Eitherof either aSeqof right values or the first left value, if present. - Throws:
NullPointerException- ifeithersis 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 singleEitherby 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- AnIterableof values.mapper- A mapper of values to Eithers- Returns:
- A
Eitherof aSeqof 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
otherby applying the Left value.
-
orElseRun
-
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
exceptionFunctionby applying the Left value. - Throws:
X- if the projected Either is a Left
-
swap
-
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- ifmapperis 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:
mapin interfaceValue<L>- Type Parameters:
U- Component type of the mapped right value- Parameters:
mapper- A mapper- Returns:
- a mapped
Monad - Throws:
NullPointerException- ifmapperis 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- ifmapperis null
-
filter
-
filterOrElse
default Either<L,R> filterOrElse(Predicate<? super R> predicate, Function<? super R, ? extends L> zero) Filters this right-biasedEitherby testing a predicate. If theEitheris aRightand the predicate doesn't match, theEitherwill be turned into aLeftwith contents computed by applying the zero function to theEithervalue.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
Eitherinstance - Throws:
NullPointerException- ifpredicateis null
-
get
R get()Gets the right value if this is aRightor throws if this is aLeft.- Specified by:
getin interfaceValue<L>- Returns:
- the right value
- Throws:
NoSuchElementException- if this is aLeft.
-
isEmpty
-
orElse
-
orElse
-
isAsync
-
isLazy
-
isSingleValued
default boolean isSingleValued()A right-biasedEitheris single-valued.- Specified by:
isSingleValuedin interfaceValue<L>- Returns:
true
-
iterator
-
peek
Description copied from interface:ValuePerforms the givenactionon the first element if this is an eager implementation. Performs the givenactionon 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:ValueClarifies that values have a proper equals() method implemented. -
hashCode
-
toString
-