Interface Either<L,R>
- All Known Implementing Classes:
Either.Left,Either.Right
public interface Either<L,R>
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classTheLeftversion of anEither.static final classTheRightversion of anEither. -
Method Summary
Modifier and TypeMethodDescriptiondefault <U> UFolds either the left or the right side of this disjunction.get()Gets the right value if this is aRightor throws if this is aLeft.getLeft()Returns the left value.default Rdefault booleanisEmpty()booleanisLeft()Returns whether this Either is a Left.booleanisRight()Returns whether this Either is a Right.static <L,R> Either<L, R> left(L left) Constructs aEither.LeftMaps the left value if the projected Either is a Left.Maps the value of this Either if it is a Left, performs no operation if this is a Right.static <L,R> Either<L, R> right(R right) Constructs aEither.Rightswap()Converts aLeftto aRightvice versa by wrapping the value in a new type.
-
Method Details
-
right
Constructs aEither.Right- Type Parameters:
L- Type of left value.R- Type of right value.- Parameters:
right- The value.- Returns:
- A new
Rightinstance.
-
left
Constructs aEither.Left- Type Parameters:
L- Type of left value.R- Type of right value.- Parameters:
left- The value.- Returns:
- A new
Leftinstance.
-
getLeft
L getLeft()Returns the left value.- Returns:
- The left value.
- Throws:
NoSuchElementException- if this is aRight.
-
isLeft
boolean isLeft()Returns whether this Either is a Left.- Returns:
- true, if this is a Left, false otherwise
-
isRight
boolean isRight()Returns whether this Either is a Right.- Returns:
- true, if this is a Right, false otherwise
-
get
R get()Gets the right value if this is aRightor throws if this is aLeft.- Returns:
- the right value
- Throws:
NoSuchElementException- if this is aLeft.
-
swap
Converts aLeftto aRightvice versa by wrapping the value in a new type.- Returns:
- a new
Either
-
isEmpty
default boolean isEmpty() -
getOrNull
-
mapLeft
Maps the value of this Either if it is a Left, performs no operation if this is a Right.- Type Parameters:
U- Component type of the mapped right value- Parameters:
leftMapper- A mapper- Returns:
- a mapped
Monad - Throws:
NullPointerException- ifmapperis null
-
map
Maps the left value if the projected Either is a Left.- Type Parameters:
U- The new type of a Left value- Parameters:
mapper- A mapper which takes a left value and returns a value of type U- Returns:
- A new LeftProjection
-
fold
default <U> U fold(Function<? super L, ? extends U> leftMapper, Function<? super R, ? extends U> rightMapper) Folds either the left or the right side of this disjunction.- Type Parameters:
U- type of the folded value- Parameters:
leftMapper- maps the left value if this is a LeftrightMapper- maps the right value if this is a Right- Returns:
- A value of type U
-