Package io.vavr.control
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.
- All Implemented Interfaces:
Iterable<R>
,java.io.Serializable
,java.lang.Iterable<R>
public abstract class Either<L,R> extends java.lang.Object implements Iterable<R>, java.io.Serializable
Either represents a value of two possible types. An Either is either aEither.Left
or aEither.Right
.- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description <T,A>
Tcollect(java.util.stream.Collector<? super R,A,T> collector)
Collects the underlying value (if present) using the providedcollector
.static <L,R>
Either<L,R>cond(boolean test, java.util.function.Supplier<? extends L> leftSupplier, java.util.function.Supplier<? extends R> rightSupplier)
Conditionally returns either aLeft
or aRight
, depending of the giventest
value.abstract boolean
equals(java.lang.Object that)
Checks if thisEither
is equal to the given objecto
.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,Either<L,? extends U>> mapper)
FlatMaps this right-biased Either.<U> U
fold(java.util.function.Function<? super L,? extends U> ifLeft, java.util.function.Function<? super R,? extends U> ifRight)
Folds either the left or the right side of this disjunction.abstract R
get()
Deprecated.Unsafe operation (but not marked for removal).abstract L
getLeft()
Deprecated.Unsafe operation (but not marked for removal).R
getOrElse(R other)
Gets the Right value or an alternate value, if the Either is a Left.R
getOrElseGet(java.util.function.Function<? super L,? extends R> other)
Gets the Right value or an alternate value, if the Either is a Left.<X extends java.lang.Throwable>
RgetOrElseThrow(java.util.function.Function<? super L,X> exceptionProvider)
Gets the Right value or throws, if this Either is a Left.abstract int
hashCode()
Computes the hash of thisEither
.abstract boolean
isLeft()
Returns whether this Either is a Left.abstract boolean
isRight()
Returns whether this Either is a Right.Iterator<R>
iterator()
Returns anIterator
that allows us to perform intermediate, sequential operations known from Vavr's collection library.static <L,R>
Either<L,R>left(L left)
Constructs aLeft
Either.<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> mapper)
Maps the value of this Either if it is a Left, performs no operation if this is a Right.Either<L,R>
onLeft(java.util.function.Consumer<? super L> action)
Applies an action to this left value if this is aLeft
, otherwise nothing happens.Either<L,R>
onRight(java.util.function.Consumer<? super R> action)
Applies an action to this right value if this is aRight
, otherwise nothing happens.Either<L,R>
orElse(java.util.function.Supplier<Either<? extends L,? extends R>> supplier)
static <L,R>
Either<L,R>right(R right)
Constructs aRight
Either.java.util.stream.Stream<R>
stream()
Converts thisEither
to aStream
.Either<R,L>
swap()
Converts aLeft
to aRight
vice versa by wrapping the value in a new type.Option<R>
toOption()
Converts thisEither
to anOption
.java.util.Optional<R>
toOptional()
Converts thisEither
to anOptional
.abstract java.lang.String
toString()
Returns a string representation of thisEither
.Try<R>
toTry(java.util.function.Function<? super L,? extends java.lang.Throwable> leftMapper)
Converts thisEither
to aTry
.<U> Either<L,U>
transform(java.util.function.Function<? super L,? extends Either<L,? extends U>> ifLeft, java.util.function.Function<? super R,? extends Either<L,? extends U>> ifRight)
Transforms thisEither
by applying eitherifRight
to this right value orifLeft
to this left value.
-
-
-
Method Detail
-
left
public static <L,R> Either<L,R> left(L left)
Constructs aLeft
Either.- Type Parameters:
L
- Type of left value.R
- Type of right value.- Parameters:
left
- The value.- Returns:
- A new
Left
instance.
-
right
public static <L,R> Either<L,R> right(R right)
Constructs aRight
Either.- Type Parameters:
L
- Type of left value.R
- Type of right value.- Parameters:
right
- The value.- Returns:
- A new
Right
instance.
-
cond
public static <L,R> Either<L,R> cond(boolean test, java.util.function.Supplier<? extends L> leftSupplier, java.util.function.Supplier<? extends R> rightSupplier)
Conditionally returns either aLeft
or aRight
, depending of the giventest
value.- Type Parameters:
L
- type of a left valueR
- type of a right value- Parameters:
test
- a boolean conditionleftSupplier
- left value supplierrightSupplier
- right value supplier- Returns:
Either.right(rightSupplier.get())
iftest
is true, otherwiseEither.left(leftSupplier.get())
-
collect
public <T,A> T collect(java.util.stream.Collector<? super R,A,T> collector)
Collects the underlying value (if present) using the providedcollector
.Shortcut for
.stream().collect(collector)
.- Type Parameters:
A
- the mutable accumulation type of the reduction operationT
- the result type of the reduction operation- Parameters:
collector
- Collector performing reduction- Returns:
- the reduction result of type
T
- Throws:
java.lang.NullPointerException
- if the givencollector
is null
-
filterOrElse
public 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.- Parameters:
predicate
- A predicatezero
- a function that transforms the right value to a left value, if it does not make it through the given filterpredicate
- Returns:
- an
Either
instance - Throws:
java.lang.NullPointerException
- ifpredicate
is null
-
flatMap
public <U> Either<L,U> flatMap(java.util.function.Function<? super R,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
-
fold
public <U> U fold(java.util.function.Function<? super L,? extends U> ifLeft, java.util.function.Function<? super R,? extends U> ifRight)
Folds either the left or the right side of this disjunction.- Type Parameters:
U
- type of the folded value- Parameters:
ifLeft
- maps the left value if this is a LeftifRight
- maps the right value if this is a Right- Returns:
- A value of type U
-
get
@Deprecated public abstract R get()
Deprecated.Unsafe operation (but not marked for removal). Usefold(Function, Function)
,getOrElse(Object)
,getOrElseGet(Function)
orgetOrElseThrow(Function)
instead. Other alternatives areonRight(Consumer)
,Iterable.forEach(Consumer)
or iteration using a for-loop.Gets the right value if this is aRight
or throws if this is aLeft
.- Returns:
- the right value
- Throws:
java.util.NoSuchElementException
- if this is aLeft
.
-
getLeft
@Deprecated public abstract L getLeft()
Deprecated.Unsafe operation (but not marked for removal). Usefold(Function, Function)
instead. An alternative isonLeft(Consumer)
.Gets the left value if this is aLeft
or throws if this is aRight
.- Returns:
- The left value.
- Throws:
java.util.NoSuchElementException
- if this is aRight
.
-
getOrElse
public R getOrElse(R other)
Gets the Right value or an alternate value, if the Either is a Left.- Parameters:
other
- an alternative right value- Returns:
- the right value, if the underlying Either is a Right or else the alternative Right value
other
-
getOrElseGet
public R getOrElseGet(java.util.function.Function<? super L,? extends R> other)
Gets the Right value or an alternate value, if the 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. - Throws:
java.lang.NullPointerException
- ifother
is null
-
getOrElseThrow
public <X extends java.lang.Throwable> R getOrElseThrow(java.util.function.Function<? super L,X> exceptionProvider) throws X extends java.lang.Throwable
Gets the Right value or throws, if this Either is a Left.- Type Parameters:
X
- a throwable type- Parameters:
exceptionProvider
- 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
exceptionProvider
by applying the Left value. - Throws:
X
- if this Either is a LeftX extends java.lang.Throwable
-
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
-
iterator
public Iterator<R> iterator()
Description copied from interface:Iterable
Returns anIterator
that allows us to perform intermediate, sequential operations known from Vavr's collection library.
-
map
public <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.// = Right("A") Either.right("a").map(String::toUpperCase); // = Left(1) Either.left(1).map(String::toUpperCase);
- Type Parameters:
U
- Component type of the mapped right value- Parameters:
mapper
- A mapper- Returns:
- a mapped
Monad
- Throws:
java.lang.NullPointerException
- ifmapper
is null
-
mapLeft
public <U> Either<U,R> mapLeft(java.util.function.Function<? super L,? extends U> mapper)
Maps the value of this Either if it is a Left, performs no operation if this is a Right.// = Left(2) Either.left(1).mapLeft(i -> i + 1); // = Right("a") Either.right("a").mapLeft(i -> i + 1);
- Type Parameters:
U
- Component type of the mapped right value- Parameters:
mapper
- A mapper- Returns:
- a mapped
Monad
- Throws:
java.lang.NullPointerException
- ifmapper
is null
-
onLeft
public Either<L,R> onLeft(java.util.function.Consumer<? super L> action)
Applies an action to this left value if this is aLeft
, otherwise nothing happens.- Parameters:
action
- An action that takes a left value and performs a side-effect- Returns:
- this
Either
- Throws:
java.lang.NullPointerException
- if the givenaction
is null
-
onRight
public Either<L,R> onRight(java.util.function.Consumer<? super R> action)
Applies an action to this right value if this is aRight
, otherwise nothing happens.- Parameters:
action
- An action that takes a right value and performs a side-effect- Returns:
- this
Either
- Throws:
java.lang.NullPointerException
- if the givenaction
is null
-
orElse
public Either<L,R> orElse(java.util.function.Supplier<Either<? extends L,? extends R>> supplier)
-
stream
public java.util.stream.Stream<R> stream()
Converts thisEither
to aStream
.- Returns:
Stream.of(get()
if this is aRight
, otherwiseStream.empty()
-
swap
public Either<R,L> swap()
Converts aLeft
to aRight
vice versa by wrapping the value in a new type.- Returns:
- a new
Either
-
toOption
public Option<R> toOption()
Converts thisEither
to anOption
.- Returns:
Option.some(get()
if this is aRight
, otherwiseOption.none()
-
toOptional
public java.util.Optional<R> toOptional()
Converts thisEither
to anOptional
.- Returns:
Optional.ofNullable(get())
if this is aRight
, otherwiseOptional.empty()
-
toTry
public Try<R> toTry(java.util.function.Function<? super L,? extends java.lang.Throwable> leftMapper)
Converts thisEither
to aTry
.- Parameters:
leftMapper
- a function that maps a left value to aThrowable
- Returns:
Try.success(get()
if this is aRight
, otherwiseTry.failure(leftMapper.apply(getLeft())
- Throws:
java.lang.NullPointerException
- if the givenleftMapper
is null
-
transform
public <U> Either<L,U> transform(java.util.function.Function<? super L,? extends Either<L,? extends U>> ifLeft, java.util.function.Function<? super R,? extends Either<L,? extends U>> ifRight)
Transforms thisEither
by applying eitherifRight
to this right value orifLeft
to this left value.- Type Parameters:
U
- type of the transformed right value- Parameters:
ifLeft
- maps the left value if this is aLeft
ifRight
- maps the right value if this is aRight
- Returns:
- A new
Either
instance - Throws:
java.lang.NullPointerException
- if one of the givenifRight
orifLeft
is null
-
equals
public abstract boolean equals(java.lang.Object that)
Checks if thisEither
is equal to the given objecto
.- Overrides:
equals
in classjava.lang.Object
- Parameters:
that
- an object, may be null- Returns:
- true, if
this
andthat
both are aRight
and the underlying values are equal or ifthis
andthat
both are aLeft
and the underlying values are equal. Otherwise it returns false.
-
hashCode
public abstract int hashCode()
Computes the hash of thisEither
.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
31 + Objects.hashCode(get())
if this is aRight
, otherwise31 + Objects.hashCode(getLeft())
-
toString
public abstract java.lang.String toString()
Returns a string representation of thisEither
.- Overrides:
toString
in classjava.lang.Object
- Returns:
"Right(" + get() + ")"
if this is aRight
, otherwise"Left(" + getLeft() + ")"
-
-