Interface Either<L,R>
- Type Parameters:
L- the left typeR- the right type
- All Known Implementing Classes:
Either.Left, Either.Right
@NullMarked
@AsOf("3.1.0")
public sealed interface Either<L,R>
permits Either.Left<L,R>, Either.Right<L,R>
Either one type or the other.
- Since:
- 3.1.0
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final recordThe left value.static final recordThe right value. -
Method Summary
Modifier and TypeMethodDescriptionconsumeLeft(Consumer<? super L> consumer) Consumes the left value if present, returning this either.consumeRight(Consumer<? super R> consumer) Consumes the right value if present, returning this either.default booleanempty()Checks if this either is empty.default <U> UCollapses both arms into a single result.left()Gets the left value if this is a left.static <L,R> Either <L, R> left(L value) Creates a left value.leftOrElse(L newValue) Left or else.leftOverride(L value) Returns a left value holdingvalue, discarding any right value this either currently holds.right()Gets the right value if this is a right.static <L,R> Either <L, R> right(R value) Creates a right value.rightOrElse(R newValue) Right or else.rightOverride(R value) Returns a right value holdingvalue, discarding any left value this either currently holds.
-
Method Details
-
left
-
right
-
fold
@AsOf("3.1.0") default <U> U fold(Function<? super L, ? extends U> ifLeft, Function<? super R, ? extends U> ifRight) Collapses both arms into a single result.- Type Parameters:
U- the result type- Parameters:
ifLeft- mapping applied when this is a left valueifRight- mapping applied when this is a right value- Returns:
- the mapped result
- Since:
- 3.1.0
-
left
-
right
-
leftOverride
Returns a left value holdingvalue, discarding any right value this either currently holds.Useful when an
Eitherbacks a mutable field (e.g. a builder): a "left" setter can force the value onto its left arm regardless of which arm it currently holds. The caller supplies the already-merged left value, sinceEithercannot know how to combine two lefts.- Parameters:
value- the new left value- Returns:
- a left value holding
value - Since:
- 3.1.0
-
rightOverride
-
leftOrElse
-
rightOrElse
-
consumeLeft
-
consumeRight
-
empty
Checks if this either is empty.- Returns:
- true if this either is empty
- Since:
- 3.1.0
-