Interface Either<L,R>
- Type Parameters:
L- the type of the left valueR- the type of the right value
- All Known Implementing Classes:
Either.Left, Either.Right
Either.Left or a Either.Right.
Either<L,R> is a neutral two-track type: neither side carries error
semantics. Use it when a computation legitimately returns one of two value types
without implying that one side is a failure. For error-handling use cases prefer
Result, which is semantically opinionated (Ok = success,
Err = failure) and offers richer recovery operations.
By convention, map, flatMap, and related operations act on
the right side. Use swap() to flip the sides when you need
to operate on the left.
This interface is @NullMarked: all values — left and right — are
non-null by default.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionAppliesmapperto the right value and returns the resultingEither, leaving aEither.Leftunchanged.default <T> Tdefault LgetLeft()Returns the left value.default RgetRight()Returns the right value.default booleanisLeft()Returnstrueif this is aEither.Left.default booleanisRight()Returnstrueif this is aEither.Right.static <L,R> Either <L, R> left(L value) Creates aEither.Leftinstance wrapping the given value.Maps the right value usingmapper, leaving aEither.Leftunchanged.Maps the left value usingmapper, leaving aEither.Rightunchanged.static <L,R> Either <L, R> right(R value) Creates aEither.Rightinstance wrapping the given value.swap()Swaps the two sides:Either.LeftbecomesEither.Rightand vice-versa.toOption()Converts thisEitherto anOption:Either.RightbecomesOption.some(Object),Either.LeftbecomesOption.none().toResult()Converts thisEitherto aResult.Converts thisEitherto aValidated.
-
Method Details
-
left
Creates aEither.Leftinstance wrapping the given value.- Type Parameters:
L- the left typeR- the right type- Parameters:
value- the non-null left value- Returns:
- an
Eithercontaining the left value - Throws:
NullPointerException- ifvalueisnull
-
right
Creates aEither.Rightinstance wrapping the given value.- Type Parameters:
L- the left typeR- the right type- Parameters:
value- the non-null right value- Returns:
- an
Eithercontaining the right value - Throws:
NullPointerException- ifvalueisnull
-
isLeft
default boolean isLeft()Returnstrueif this is aEither.Left.- Returns:
trueforLeft,falseforRight
-
isRight
default boolean isRight()Returnstrueif this is aEither.Right.- Returns:
trueforRight,falseforLeft
-
getLeft
Returns the left value.- Returns:
- the left value
- Throws:
NoSuchElementException- if this is aEither.Right
-
getRight
Returns the right value.- Returns:
- the right value
- Throws:
NoSuchElementException- if this is aEither.Left
-
fold
-
map
Maps the right value usingmapper, leaving aEither.Leftunchanged.- Type Parameters:
R2- the new right type- Parameters:
mapper- function applied to the right value- Returns:
- a new
Eitherwith the mapped right value, or the originalLeft
-
mapLeft
Maps the left value usingmapper, leaving aEither.Rightunchanged.- Type Parameters:
L2- the new left type- Parameters:
mapper- function applied to the left value- Returns:
- a new
Eitherwith the mapped left value, or the originalRight
-
flatMap
default <R2> Either<L,R2> flatMap(Function<? super R, ? extends Either<? extends L, ? extends R2>> mapper) Appliesmapperto the right value and returns the resultingEither, leaving aEither.Leftunchanged. This is the monadic bind for the right track.- Type Parameters:
R2- the new right type- Parameters:
mapper- function that returns anEitherfor the right value- Returns:
- the result of
mapperapplied to the right value, or the originalLeft
-
swap
Swaps the two sides:Either.LeftbecomesEither.Rightand vice-versa.- Returns:
- an
Either<R,L>with the sides swapped
-
peek
-
peekLeft
-
toOption
Converts thisEitherto anOption:Either.RightbecomesOption.some(Object),Either.LeftbecomesOption.none().- Returns:
- an
Optioncontaining the right value, or empty
-
toResult
Converts thisEitherto aResult.Either.Rightmaps toResult.Ok;Either.Leftmaps toResult.Err. This is the inverse ofResult.toEither().- Returns:
- a
Result<R, L>equivalent of thisEither
-
toValidated
Converts thisEitherto aValidated.Either.Rightmaps toValidated.valid(A);Either.Leftmaps toValidated.invalid(E). Useful for bringing a neutralEitherinto the error-accumulating validation world.- Returns:
- a
Validated<L, R>equivalent of thisEither
-