A
- first type of valueB
- second type of valuepublic class Either<A,B> extends Object
<A>
and <B>
.
The value can be accessed in a type-safe way using getFirst()
and
getSecond()
.
Additional methods to process the value based on its type and resulting in a
fixed typed instance are provided, such as map(Function, Function)
.
This is a value-based class;
use of identity-sensitive operations (including reference equality
(==
), identity hash code, or synchronization) on instances of
Optional
may have unpredictable results and should be avoided.
Constructor and Description |
---|
Either(Optional<A> first,
Optional<B> second)
Creates a new
Either instance. |
Modifier and Type | Method and Description |
---|---|
protected boolean |
canEqual(Object other) |
boolean |
equals(Object o) |
Optional<A> |
getFirst()
Value of first type
|
Optional<B> |
getSecond()
Value of second type
|
int |
hashCode() |
void |
ifFirstIsPresent(Consumer<? super A> consumer)
If the first value is present, invoke the specified consumer with the value,
otherwise do nothing.
|
void |
ifPresent(Consumer<? super A> firstConsumer,
Consumer<? super B> secondConsumer)
Invoke either consumer depending on which value is present..
|
void |
ifSecondIsPresent(Consumer<? super B> consumer)
If the second value is present, invoke the specified consumer with the value,
otherwise do nothing.
|
<T> T |
map(Function<? super A,? extends T> firstFunction,
Function<? super B,? extends T> secondFunction)
If value is of first type
functionA is applied, else value is of
second type and functionB is applied. |
B |
mapFirst(Function<? super A,? extends B> function)
If the value is of second type
function is applied and the resulting
value is returned else the value is of first type and returned as-is. |
A |
mapSecond(Function<? super B,? extends A> function)
If the value is of first type
function is applied and the resulting
value is returned else the value is of second type and returned as-is. |
static <A,B> Either<A,B> |
of(A first,
B second)
Deprecated.
This is a convenience method for special cases only. Use
ofFirst(A) and ofSecond(B) where possible. |
static <A,B> Either<A,B> |
ofFirst(A value)
Returns an
Either object describing the given value of the
first type. |
static <A,B> Either<A,B> |
ofSecond(B value)
Returns an
Either object describing the given value of the
second type. |
String |
toString() |
@Deprecated public static <A,B> Either<A,B> of(@Nullable A first, @Nullable B second)
ofFirst(A)
and ofSecond(B)
where possible.Either
object describing the given value. One of both
parameters has to be non null (means being set), the other one has to be null
(means being not set).A
- type of first valueB
- type of second valuefirst
- value of first type or null
second
- value of second type or null
Either
object with the given valueNullPointerException
- if both parameters are null
IllegalArgumentException
- if both parameters are non-null
public static <A,B> Either<A,B> ofFirst(A value)
Either
object describing the given value of the
first type.A
- first type of valueB
- second type of valuevalue
- value to describeEither
object describing the given valuepublic static <A,B> Either<A,B> ofSecond(B value)
Either
object describing the given value of the
second type.A
- first type of valueB
- second type of valuevalue
- value to describeEither
object describing the given valuepublic void ifPresent(Consumer<? super A> firstConsumer, Consumer<? super B> secondConsumer)
firstConsumer
- block to be executed if the first value is presentsecondConsumer
- block to be executed if the second value is presentpublic void ifFirstIsPresent(Consumer<? super A> consumer)
consumer
- block to be executed if the first value is presentpublic void ifSecondIsPresent(Consumer<? super B> consumer)
consumer
- block to be executed if the second value is present@Nullable public <T> T map(Function<? super A,? extends T> firstFunction, Function<? super B,? extends T> secondFunction)
functionA
is applied, else value is of
second type and functionB
is applied. The resulting value is
returned.
Example: The first type is somehow used for user input, while the second one is used for system default values.
Either<String, Number> either = Either.ofFirst("123"); String text = either.map(s -> "Changed", n -> "System default"); long value = either.map(Long::parseLong, Number::longValue);
T
- resulting typefirstFunction
- function to map value, if it is of first typesecondFunction
- function to map value, if it is of second typefunctionA
or
functionB
@Nullable public B mapFirst(Function<? super A,? extends B> function)
function
is applied and the resulting
value is returned else the value is of first type and returned as-is.
Example: The first type is somehow used for user input, while the second one is used for system default values.
Either<String, Number> either = Either.ofFirst("123"); String text = either.map(s -> "Changed", n -> "System default"); Number value = either.mapFirst(Long::parseLong);
function
- function to map value, if it is of first typefunction
@Nullable public A mapSecond(Function<? super B,? extends A> function)
function
is applied and the resulting
value is returned else the value is of second type and returned as-is.
Example: The first type is somehow used for user input, while the second one is used for system default values.
Either<String, Number> either = Either.ofFirst(123); String text = either.map(s -> "Changed", n -> "System default"); String displayValue = either.mapSecond(Number::toString);
function
- function to map value, if it is of second typefunction
protected boolean canEqual(@Nullable Object other)
Copyright © 2025. All rights reserved.