Class Either<L,R>
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
Either.Left, Either.Right
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(...).
- Since:
- 1.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) classHolds the common implementation for both projections.(package private) static final classfinal classA left projection of an either value.static interface(package private) static final classfinal classA right projection of an either value. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionFunction application on this projection's value.Deprecated.abstract <LL,RR> Either <LL, RR> Map the given functions across the appropriate side.final booleanTests whether the option contains a given value as an element.final booleanReturn `true` if this is a right value and applying the predicate to the contained value returns true.ReturnsNoneif this is a left or if the given predicatepdoes not hold for the contained value, otherwise, returns a right inSome.filterOrElse(Predicate<? super R> p, Supplier<? extends L> orElseSupplier) Return aRightif this is aRightand the contained values satisfies the given predicate.Binds the given function across the right hand side value if it is one.abstract <V> VApplies the function to the wrapped value, applying ifLeft if this is a Left and ifRight if this is a Right.final booleanReturnstrueif it is a left or the result of the application of the given predicate on the contained value.final voidDeprecated.useforEach(Consumer)insteadfinal voidPerform the given side-effect for the contained element if it is a right(package private) LgetLeft()final RGet the value if it is a right or call the supplier and return its value if not.final RDeprecated.since 4.3, usegetOr(Supplier)insteadgetOrElse(X other) Get the value if it is a right, otherwise returnsother.final RgetOrError(Supplier<String> msg) Get the contained value or throws an error with the supplied message if left.final RGet the value if it is right or null if not.getOrThrow(Supplier<X> ifUndefined) Get the contained value or throws the supplied throwable if left(package private) RgetRight()abstract booleanisLeft()Returnstrueif this either is a left,falseotherwise.abstract booleanisRight()Returnstrueif this either is a right,falseotherwise.final Either<L,R>.LeftProjection left()Projects this either as a left.static <L,R> Either <L, R> left(L left) left.Map the given function across the left hand side value if it is one.final LIf this is a left return the contained value, else return the result of applyingrightTransformeron rightMap the given function across the right hand side value if it is one.If this is a right, return the same right.If this is a right, return the same right.final Either<L,R>.RightProjection right()Projects this either as a right.static <L,R> Either <L, R> right(R right) right.final RIf this is a right return the contained value, else return the result of applyingleftTransformeron leftWill return the supplied Either if this one is right, otherwise this one if left.swap()If this is a left, then return the left value in right, or vice versa.toOption()Convert this Either to an Option.Convert this Either to anOptional.toStream()Convert this Either to aStream.final RDeprecated.In favor ofrightOr
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
-
Constructor Details
-
Either
Either()
-
-
Method Details
-
left
left.
- Type Parameters:
L- the LHS typeR- the RHS type- Parameters:
left- the value to be stored, must not be null- Returns:
- a Left containing the supplied value
- Since:
- 1.0
-
right
right.
- Type Parameters:
L- the LHS typeR- the RHS type- Parameters:
right- the value to be stored, must not be null- Returns:
- a Right containing the supplied value
- Since:
- 1.0
-
left
Projects this either as a left.- Returns:
- A left projection of this either.
-
right
Projects this either as a right.- Returns:
- A right projection of this either.
-
getOr
-
getOrElse
Deprecated.since 4.3, usegetOr(Supplier)insteadGet the value if it is a right or call the supplier and return its value if not.- Parameters:
supplier- called if this is a left- Returns:
- the wrapped value or the value from the
Supplier - Since:
- 2.3
-
getOrElse
-
getOrNull
Get the value if it is right or null if not.Although the use of null is discouraged, code written to use these must often interface with code that expects and returns nulls.
- Returns:
- the contained value or null
- Since:
- 2.3
-
getOrError
-
getOrThrow
Get the contained value or throws the supplied throwable if leftUsed when absolutely sure this is a right.
- Type Parameters:
X- exception type- Parameters:
ifUndefined- the supplier of the throwable.- Returns:
- the contained value.
- Throws:
X- the throwable the supplier creates if there is no value.- Since:
- 2.3
-
map
-
flatMap
Binds the given function across the right hand side value if it is one.- Type Parameters:
X- the RHS typeLL- result type- Parameters:
f- the function to bind.- Returns:
- A new either value after binding with the function applied if this is a Right.
- Since:
- 2.2
-
exists
-
forall
Returnstrueif it is a left or the result of the application of the given predicate on the contained value.- Parameters:
p- The predicate function to test on the contained value.- Returns:
trueif no value or returns the result of the application of the given function to the value.- Since:
- 2.3
-
contains
Tests whether the option contains a given value as an element.- Parameters:
elem- the element to test- Returns:
trueif the option has an element that is equal to elem,falseotherwise- Since:
- 6.1
-
foreach
Deprecated.useforEach(Consumer)insteadPerform the given side-effect for the contained element if it is a right- Parameters:
effect- the input to use for performing the effect on contained value.- Since:
- 2.3
-
forEach
-
orElse
-
orElse
-
valueOr
Deprecated.In favor ofrightOrIf this is a right return the contained value, else return the result of running function on left- Parameters:
or- Function to run if this is a left- Returns:
- contained value of R or result of
or - Since:
- 2.3
- See Also:
-
rightOr
If this is a right return the contained value, else return the result of applyingleftTransformeron left- Parameters:
leftTransformer- Function to run on left if this is a left- Returns:
- contained value of right or result of
leftTransformer - Since:
- 3.2
-
leftOr
If this is a left return the contained value, else return the result of applyingrightTransformeron right- Parameters:
rightTransformer- Function to run on right if this is a right- Returns:
- contained value of left or result of
rightTransformer - Since:
- 3.2
-
filter
ReturnsNoneif this is a left or if the given predicatepdoes not hold for the contained value, otherwise, returns a right inSome.- Parameters:
p- The predicate function to test on the right contained value.- Returns:
Noneif this is a left or if the given predicatepdoes not hold for the right contained value, otherwise, returns a right inSome.- Since:
- 2.3
-
filterOrElse
Return aRightif this is aRightand the contained values satisfies the given predicate. If this is aRightbut the predicate is not satisfied, return aLeftwith the value provided by the orElseSupplier. Return aLeftif this aLeftwith the contained value.- Parameters:
p- The predicate function to test on the right contained value.orElseSupplier- The supplier to execute when is a right, and predicate is unsatisfied- Returns:
- a new Either that will be either the existing left/right or a left with result of orElseSupplier
- Since:
- 4.7.0
-
toOptional
Convert this Either to anOptional. Returns withOptional.of(Object)if it is a right, otherwiseOptional.empty().- Returns:
- The right projection's value in
ofif it exists, otherwiseempty. - Since:
- 4.0
-
toOption
-
toStream
Convert this Either to aStream. Returns withStream.of(Object)if it is a right, otherwiseStream.empty().- Returns:
- The right projection's value in
ofif it exists, otherwiseempty. - Since:
- 4.5.0
-
sequence
-
apply
Deprecated.since 3.0 seeap(Either)Given a right containing a function from the right type<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.- Type Parameters:
X- the RHS type- Parameters:
either- The either of the function to apply if this is a Right.- Returns:
- The result of function application within either.
- Since:
- 2.6
-
ap
-
leftMap
Map the given function across the left hand side value if it is one.- Type Parameters:
X- the LHS type- Parameters:
f- The function to map.- Returns:
- A new either value after mapping with the function applied if this is a Left.
- Since:
- 2.2
-
isLeft
public abstract boolean isLeft()Returnstrueif this either is a left,falseotherwise.- Returns:
trueif this either is a left,falseotherwise.
-
isRight
public abstract boolean isRight()Returnstrueif this either is a right,falseotherwise.- Returns:
trueif this either is a right,falseotherwise.
-
swap
-
fold
Applies the function to the wrapped value, applying ifLeft if this is a Left and ifRight if this is a Right.- Type Parameters:
V- the destination type- Parameters:
ifLeft- the function to apply if this is a LeftifRight- the function to apply if this is a Right- Returns:
- the result of the applies function
-
bimap
public 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.- Type Parameters:
LL- the LHS typeRR- the RHS type- Parameters:
ifLeft- The function to map if this Either is a left.ifRight- The function to map if this Either is a right.- Returns:
- A new either value after mapping with the appropriate function applied.
- Since:
- 2.2
-
getLeft
L getLeft() -
getRight
R getRight()
-
ap(Either)