java.lang.Object
net.luis.utils.util.Either<L,R>
- Type Parameters:
L
- The type of the left valueR
- The type of the right value
- Direct Known Subclasses:
Either.Left
,Either.Right
Represents one of two possible values.
An instance of
An instance of
Either
is either an instance of Either.Left
or Either.Right
.
Sealed to prevent external instantiation and extension.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class
Left implementation of the either.(package private) static final class
Right implementation of the either. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Either()
Private because this class is sealed and not instantiable from outside. -
Method Summary
Modifier and TypeMethodDescriptionabstract void
Performs the given action if this either instance is a left instance.abstract void
Performs the given action if this either instance is a right instance.abstract boolean
isLeft()
Checks if this either instance is a left instance.abstract boolean
isRight()
Checks if this either instance is a right instance.left()
Returns the left value as anOptional
.static <L,
R> @NotNull Either <L, R> left
(L value) Creates a new either instance with the given value as the left value.abstract @UnknownNullability L
Returns the left value or throws an exception if it is a right either.abstract <C,
D> @NotNull Either <C, D> mapBoth
(@NotNull Function<? super L, ? extends C> leftMapper, @NotNull Function<? super R, ? extends D> rightMapper) Maps the left and right values of this either instance to new values.Maps the left value of this either instance to a new value.
If this is a right either, the mapping is not applied.Maps the right value of this either instance to a new value.
If this is a left either, the mapping is not applied.abstract <T> T
mapTo
(@NotNull Function<? super L, ? extends T> leftMapper, @NotNull Function<? super R, ? extends T> rightMapper) Maps the left and right values of this either instance to a new value.right()
Returns the right value as anOptional
.static <L,
R> @NotNull Either <L, R> right
(R value) Creates a new either instance with the given value as the right value.abstract @UnknownNullability R
Returns the right value or throws an exception if it is a left either.swap()
Swaps the left and right values of this either instance.
-
Constructor Details
-
Either
private Either()Private because this class is sealed and not instantiable from outside.
-
-
Method Details
-
left
Creates a new either instance with the given value as the left value.- Type Parameters:
L
- The type of the left valueR
- The type of the right value- Parameters:
value
- The left value- Returns:
- The created (left) either instance
-
right
Creates a new either instance with the given value as the right value.- Type Parameters:
L
- The type of the left valueR
- The type of the right value- Parameters:
value
- The right value- Returns:
- The created (right) either instance
-
isLeft
public abstract boolean isLeft()Checks if this either instance is a left instance.- Returns:
- True if this either instance is a left instance, false otherwise
-
isRight
public abstract boolean isRight()Checks if this either instance is a right instance.- Returns:
- True if this either instance is a right instance, false otherwise
-
ifLeft
Performs the given action if this either instance is a left instance.- Parameters:
action
- The action to perform- Throws:
NullPointerException
- If the action is null
-
ifRight
Performs the given action if this either instance is a right instance.- Parameters:
action
- The action to perform- Throws:
NullPointerException
- If the action is null
-
left
Returns the left value as anOptional
.- Returns:
- The left value
-
right
Returns the right value as anOptional
.- Returns:
- The right value
-
leftOrThrow
Returns the left value or throws an exception if it is a right either.- Returns:
- The left value
- Throws:
IllegalStateException
- If the left value is not present
-
rightOrThrow
Returns the right value or throws an exception if it is a left either.- Returns:
- The right value
- Throws:
IllegalStateException
- If the right value is not present
-
swap
Swaps the left and right values of this either instance.- Returns:
- A new either instance with the swapped values
-
mapBoth
@NotNull public abstract <C,D> @NotNull Either<C,D> mapBoth(@NotNull @NotNull Function<? super L, ? extends C> leftMapper, @NotNull @NotNull Function<? super R, ? extends D> rightMapper) Maps the left and right values of this either instance to new values.- Type Parameters:
C
- The new type of the left valueD
- The new type of the right value- Parameters:
leftMapper
- The mapper for the left valuerightMapper
- The mapper for the right value- Returns:
- A new either instance with the mapped values
- Throws:
NullPointerException
- If the mapper used in this either implementation is null
-
mapTo
public abstract <T> T mapTo(@NotNull @NotNull Function<? super L, ? extends T> leftMapper, @NotNull @NotNull Function<? super R, ? extends T> rightMapper) Maps the left and right values of this either instance to a new value.- Type Parameters:
T
- The type of the mapped value- Parameters:
leftMapper
- The mapper for the left valuerightMapper
- The mapper for the right value- Returns:
- The mapped value
- Throws:
NullPointerException
- If the mapper used in this either implementation is null
-
mapLeft
Maps the left value of this either instance to a new value.
If this is a right either, the mapping is not applied.- Type Parameters:
T
- The new type of the left value- Parameters:
mapper
- The mapper for the left value- Returns:
- A new either instance with the mapped left value
- Throws:
NullPointerException
- If the mapper is null
-
mapRight
Maps the right value of this either instance to a new value.
If this is a left either, the mapping is not applied.- Type Parameters:
T
- The new type of the right value- Parameters:
mapper
- The mapper for the right value- Returns:
- A new either instance with the mapped right value
- Throws:
NullPointerException
- If the mapper is null
-