Class Either<L,R>
java.lang.Object
com.github.darksoulq.abyssallib.common.util.Either<L,R>
- Type Parameters:
L- The type of the Left value.R- The type of the Right value.
- Direct Known Subclasses:
Either.Left, Either.Right
A container object which may hold one of two types of values: a Left or a Right.
By convention, the Left type represents a failure or error, while the Right type represents a success or the "correct" value.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidExecutes the appropriate consumer based on which side is present.<LL,RR> Either <LL, RR> flatMap(Function<? super L, ? extends Either<? extends LL, ? extends RR>> lf, Function<? super R, ? extends Either<? extends LL, ? extends RR>> rf) FlatMaps both sides into a new Either type.abstract <T> TReduces the Either to a single value by applying the matching function.abstract voidExecutes the consumer if this is a Left.abstract voidExecutes the consumer if this is a Right.left()static <L,R> Either <L, R> left(L value) Creates an instance of Either containing a Left value.abstract LleftOrElse(L def) Returns the Left value if present, otherwise returns the provided default.abstract LReturns the Left value or throws an exception if this is a Right.<LL,RR> Either <LL, RR> Transforms both the Left and Right values using the provided functions.Transforms the Left value using the provided function.Transforms the Right value using the provided function.right()static <L,R> Either <L, R> right(R value) Creates an instance of Either containing a Right value.abstract RrightOrElse(R def) Returns the Right value if present, otherwise returns the provided default.abstract RReturns the Right value or throws an exception if this is a Left.swap()Swaps the sides of the Either.
-
Method Details
-
left
Creates an instance of Either containing a Left value.- Type Parameters:
L- The type of the Left value.R- The type of the Right value.- Parameters:
value- The value to wrap.- Returns:
- A Left instance of Either.
-
right
Creates an instance of Either containing a Right value.- Type Parameters:
L- The type of the Left value.R- The type of the Right value.- Parameters:
value- The value to wrap.- Returns:
- A Right instance of Either.
-
left
-
right
-
leftOrElse
-
rightOrElse
-
leftOrThrow
Returns the Left value or throws an exception if this is a Right.- Returns:
- The Left value.
- Throws:
IllegalStateException- If this is a Right instance.
-
rightOrThrow
Returns the Right value or throws an exception if this is a Left.- Returns:
- The Right value.
- Throws:
IllegalStateException- If this is a Left instance.
-
mapLeft
-
mapRight
-
map
public <LL,RR> Either<LL,RR> map(Function<? super L, ? extends LL> lf, Function<? super R, ? extends RR> rf) Transforms both the Left and Right values using the provided functions.- Type Parameters:
LL- The new Left type.RR- The new Right type.- Parameters:
lf- The function to apply to a Left.rf- The function to apply to a Right.- Returns:
- A new transformed Either.
-
ifLeft
-
ifRight
-
apply
-
fold
public abstract <T> T fold(Function<? super L, ? extends T> lf, Function<? super R, ? extends T> rf) Reduces the Either to a single value by applying the matching function.- Type Parameters:
T- The result type.- Parameters:
lf- The function to apply to a Left.rf- The function to apply to a Right.- Returns:
- The result of the applied function.
-
flatMap
public <LL,RR> Either<LL,RR> flatMap(Function<? super L, ? extends Either<? extends LL, ? extends RR>> lf, Function<? super R, ? extends Either<? extends LL, ? extends RR>> rf) FlatMaps both sides into a new Either type.- Type Parameters:
LL- The new Left type.RR- The new Right type.- Parameters:
lf- The function providing a new Either from a Left.rf- The function providing a new Either from a Right.- Returns:
- The new Either instance.
-
swap
-