public abstract class Either<L,R> extends Object implements Serializable
Left or Right.
Checking which type an Either is can be done by calling the @
isLeft() and isRight() methods.
An Either can be used to express a success or failure case. By convention, Right is used to store the success value, (you can use the play on words "right" == "correct" as a mnemonic) and Left is used to store failure values (such as exceptions).
While this class is public and abstract it does not expose a constructor as only the concrete Left and Right subclasses are meant to be used.
Either is immutable, but does not force immutability on contained objects; if the contained objects are mutable then equals and hashcode methods should not be relied on.
Since 2.2, there have been some right-biased methods added. With 2.3 the
available right-biased methods has increased. The purpose of these is that
you can do something like either.map(...) directly, which is
identical to calling either.right().map(...).
| Modifier and Type | Class and Description |
|---|---|
class |
Either.LeftProjection
A left projection of an either value.
|
static interface |
Either.Projection<A,B,L,R> |
class |
Either.RightProjection
A right projection of an either value.
|
| Modifier and Type | Method and Description |
|---|---|
<X> Either<L,X> |
ap(Either<L,Function<R,X>> either)
Function application on this projection's value.
|
<X> Either<L,X> |
apply(Either<L,Function<R,X>> either)
Deprecated.
|
abstract <LL,RR> Either<LL,RR> |
bimap(Function<? super L,? extends LL> ifLeft,
Function<? super R,? extends RR> ifRight)
Map the given functions across the appropriate side.
|
boolean |
exists(Predicate<? super R> p)
Return `true` if this is a right value and applying the
predicate to the contained value returns true.
|
Option<Either<L,R>> |
filter(Predicate<? super R> p)
Returns
None if this is a left or if the given predicate
p does not hold for the contained value, otherwise, returns a
right in Some. |
<X,LL extends L> |
flatMap(Function<? super R,Either<LL,X>> f)
Binds the given function across the right hand side value if it is one.
|
abstract <V> V |
fold(Function<? super L,V> ifLeft,
Function<? super R,V> ifRight)
Applies the function to the wrapped value, applying ifLeft if this is a
Left and ifRight if this is a Right.
|
boolean |
forall(Predicate<? super R> p)
Returns
true if it is a left or the result of the application
of the given predicate on the contained value. |
void |
forEach(Consumer<? super R> effect)
Perform the given side-effect for the contained element if it is a right
|
void |
foreach(Effect<? super R> effect)
Deprecated.
use
forEach(Consumer) instead |
R |
getOr(Supplier<? extends R> supplier)
Get the value if it is a right or call the supplier and return its value if
not.
|
R |
getOrElse(Supplier<? extends R> supplier)
Deprecated.
since 4.3, use
getOr(Supplier) instead |
<X extends R> |
getOrElse(X other)
Get the value if it is a right, otherwise returns
other. |
R |
getOrError(Supplier<String> msg)
Get the contained value or throws an error with the supplied message if
left.
|
R |
getOrNull()
Get the value if it is right or null if not.
|
<X extends Throwable> |
getOrThrow(Supplier<X> ifUndefined)
Get the contained value or throws the supplied throwable if left
|
abstract boolean |
isLeft()
Returns
true if this either is a left, false
otherwise. |
abstract boolean |
isRight()
Returns
true if this either is a right, false
otherwise. |
Either.LeftProjection |
left()
Projects this either as a left.
|
static <L,R> Either<L,R> |
left(L left)
left.
|
<X> Either<X,R> |
leftMap(Function<? super L,X> f)
Map the given function across the left hand side value if it is one.
|
L |
leftOr(Function<R,? extends L> rightTransformer)
If this is a left return the contained value, else return the result of
applying
rightTransformer on right |
<X> Either<L,X> |
map(Function<? super R,X> f)
Map the given function across the right hand side value if it is one.
|
Either<L,R> |
orElse(Either<? extends L,? extends R> orElse)
If this is a right, return the same right.
|
Either<L,R> |
orElse(Supplier<? extends Either<? extends L,? extends R>> orElse)
If this is a right, return the same right.
|
Either.RightProjection |
right()
Projects this either as a right.
|
static <L,R> Either<L,R> |
right(R right)
right.
|
R |
rightOr(Function<L,? extends R> leftTransformer)
If this is a right return the contained value, else return the result of
applying
leftTransformer on left |
<X> Either<L,X> |
sequence(Either<L,X> e)
Will return the supplied Either if this one is right, otherwise this one if
left.
|
abstract Either<R,L> |
swap()
If this is a left, then return the left value in right, or vice versa.
|
Option<R> |
toOption()
Convert this Either to an Option.
|
Optional<R> |
toOptional()
Convert this Either to an
Optional. |
Stream<R> |
toStream()
Convert this Either to a
Stream. |
R |
valueOr(Function<L,? extends R> or)
Deprecated.
In favor of
rightOr |
public static <L,R> Either<L,R> left(L left)
left.
L - the LHS typeR - the RHS typeleft - the value to be stored, must not be nullpublic static <L,R> Either<L,R> right(R right)
right.
L - the LHS typeR - the RHS typeright - the value to be stored, must not be nullpublic final Either.LeftProjection left()
public final Either.RightProjection right()
public final R getOr(Supplier<? extends R> supplier)
supplier - called if this is a leftSupplier@Deprecated public final R getOrElse(Supplier<? extends R> supplier)
getOr(Supplier) insteadsupplier - called if this is a leftSupplierpublic final <X extends R> R getOrElse(X other)
other.X - default typeother - value to return if this is a leftotherpublic final R getOrNull()
Although the use of null is discouraged, code written to use these must often interface with code that expects and returns nulls.
public final R getOrError(Supplier<String> msg)
Used when absolutely sure this is a right.
msg - the message for the error.public final <X extends Throwable> R getOrThrow(Supplier<X> ifUndefined) throws X extends Throwable
Used when absolutely sure this is a right.
X - exception typeifUndefined - the supplier of the throwable.X - the throwable the supplier creates if there is no value.X extends Throwablepublic final <X> Either<L,X> map(Function<? super R,X> f)
X - the RHS typef - The function to map .public final <X,LL extends L> Either<L,X> flatMap(Function<? super R,Either<LL,X>> f)
X - the RHS typeLL - result typef - the function to bind.public final boolean exists(Predicate<? super R> p)
p - the predicate to test.true if right and the predicate returns true for the right
value, false otherwise.public final boolean forall(Predicate<? super R> p)
true if it is a left or the result of the application
of the given predicate on the contained value.p - The predicate function to test on the contained value.true if no value or returns the result of the
application of the given function to the value.@Deprecated public final void foreach(Effect<? super R> effect)
forEach(Consumer) insteadeffect - the input to use for performing the effect on contained
value.public final void forEach(Consumer<? super R> effect)
effect - the input to use for performing the effect on contained
value.public final Either<L,R> orElse(Either<? extends L,? extends R> orElse)
orElse
.orElse - either to return if this is leftorElsepublic final Either<L,R> orElse(Supplier<? extends Either<? extends L,? extends R>> orElse)
orElse.orElse - either to return if this is leftorElse@Deprecated public final R valueOr(Function<L,? extends R> or)
rightOror - Function to run if this is a leftorrightOr(Function)public final R rightOr(Function<L,? extends R> leftTransformer)
leftTransformer on leftleftTransformer - Function to run on left if this is a leftleftTransformerpublic final L leftOr(Function<R,? extends L> rightTransformer)
rightTransformer on rightrightTransformer - Function to run on right if this is a rightrightTransformerpublic final Option<Either<L,R>> filter(Predicate<? super R> p)
None if this is a left or if the given predicate
p does not hold for the contained value, otherwise, returns a
right in Some.p - The predicate function to test on the right contained value.None if this is a left or if the given predicate
p does not hold for the right contained value, otherwise,
returns a right in Some.public final Optional<R> toOptional()
Optional. Returns with
Optional.of(Object) if it is a right, otherwise
Optional.empty().of if it exists,
otherwise empty.public final Option<R> toOption()
Some with a value if
it is a right, otherwise None.Some if it exists,
otherwise None.public final Stream<R> toStream()
Stream. Returns with
Stream.of(Object) if it is a right, otherwise
Stream.empty().of if it exists,
otherwise empty.public <X> Either<L,X> sequence(Either<L,X> e)
X - the RHS typee - The value to bind with.@Deprecated public <X> Either<L,X> apply(Either<L,Function<R,X>> either)
ap(io.atlassian.fugue.Either<L, java.util.function.Function<R, X>>)<R> to a
new type <X> apply that function to the value inside this either.
When any of the input values are left return that left value.X - the RHS typeeither - The either of the function to apply if this is a Right.public <X> Either<L,X> ap(Either<L,Function<R,X>> either)
X - the RHS typeeither - The either of the function to apply on this projection's
value.public final <X> Either<X,R> leftMap(Function<? super L,X> f)
X - the LHS typef - The function to map.public abstract boolean isLeft()
true if this either is a left, false
otherwise.true if this either is a left, false
otherwise.public abstract boolean isRight()
true if this either is a right, false
otherwise.true if this either is a right, false
otherwise.public abstract Either<R,L> swap()
public abstract <V> V fold(Function<? super L,V> ifLeft, Function<? super R,V> ifRight)
V - the destination typeifLeft - the function to apply if this is a LeftifRight - the function to apply if this is a Rightpublic abstract <LL,RR> Either<LL,RR> bimap(Function<? super L,? extends LL> ifLeft, Function<? super R,? extends RR> ifRight)
LL - the LHS typeRR - the RHS typeifLeft - The function to map if this Either is a left.ifRight - The function to map if this Either is a right.Copyright © 2017 Atlassian. All rights reserved.