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
- Returns:
- An Optional containing the Left value if present, otherwise empty.
-
right
- Returns:
- An Optional containing the Right value if present, otherwise empty.
-
leftOrElse
Returns the Left value if present, otherwise returns the provided default.- Parameters:
def- The default value.- Returns:
- The Left value or the default.
-
rightOrElse
Returns the Right value if present, otherwise returns the provided default.- Parameters:
def- The default value.- Returns:
- The Right value or the default.
-
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
Transforms the Left value using the provided function.- Type Parameters:
LL- The new Left type.- Parameters:
f- The transformation function.- Returns:
- A new Either with the transformed Left or the original Right.
-
mapRight
Transforms the Right value using the provided function.- Type Parameters:
RR- The new Right type.- Parameters:
f- The transformation function.- Returns:
- A new Either with the transformed Right or the original Left.
-
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
Executes the consumer if this is a Left.- Parameters:
c- The consumer to execute.
-
ifRight
Executes the consumer if this is a Right.- Parameters:
c- The consumer to execute.
-
apply
Executes the appropriate consumer based on which side is present.- Parameters:
lc- The consumer for a Left value.rc- The consumer for a Right value.
-
fold
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
Swaps the sides of the Either.- Returns:
- A new Either where the Left is the old Right and vice versa.
-