E - {code left} type parameter, typically used for errors.T - right type parameter, typically used for the value if there is no error.public final class Either<E,T> extends Object
Either are either an instance of left or right.
This implementation of Either also allows for an empty value, where neither left or @code{right} are
set.
A common use of Either is as an alternative to exception handling. Convention in other languages,
such as Scala, is that left is used for the exception and right for the success.| Modifier and Type | Method and Description |
|---|---|
static <E1,T1> Either<E1,T1> |
empty()
Creates an empty
Either. |
boolean |
equals(Object obj) |
Optional<E> |
filterLeft(Predicate<? super E> predicate)
If a
left value is present,
and the value matches the given predicate,
return an Optional describing the value,
otherwise return an Optional.empty. |
Optional<T> |
filterRight(Predicate<? super T> predicate)
If a
right value is present,
and the value matches the given predicate,
return an Optional describing the value,
otherwise return an Optional.empty. |
E |
getLeft()
Gets the
left value. |
T |
getRight()
Gets the right value.
|
int |
hashCode() |
E |
ifLeftNotPresent(E other)
Returns provided
other value if left is not present. |
E |
ifLeftNotPresentGet(Supplier<? extends E> other)
Returns value provided by a
Provider if left is not present. |
<X extends Throwable> |
ifLeftNotPresentThrow(Supplier<? extends X> other)
Throw a
Throwable of type X provided by a Provider if {code left} is not present. |
void |
ifLeftPresent(Consumer<? super E> consumer)
If {code left} has a value apply the
Consumer to the {code left} value. |
T |
ifRightNotPresent(T other)
Returns provided
other value if right is not present. |
T |
ifRightNotPresentGet(Supplier<? extends T> other)
Returns value provided by a
Provider if right is not present. |
<X extends Throwable> |
ifRightNotPresentThrow(Supplier<? extends X> other)
Throw a
Throwable of type X provided by a Provider if {code right} is not present. |
void |
ifRightPresent(Consumer<? super T> consumer)
If {code right} has a value apply the
Consumer to the {code right} value. |
boolean |
isEmpty()
True if neither
left or right are present. |
boolean |
isLeftPresent()
True if the
left value is present. |
boolean |
isRightPresent()
True if the
right value is present. |
<U> Optional<? extends U> |
mapLeft(Function<? super E,? extends U> mapper)
If a
left value is present, apply the provided mapping function to it,
and if the result is non-null, return an Optional describing the result. |
<U> Optional<? extends U> |
mapRight(Function<? super T,? extends U> mapper)
If a
right value is present, apply the provided mapping function to it,
and if the result is non-null, return an Optional describing the result. |
static <E1,T1> Either<E1,T1> |
ofLeft(E1 left)
Creates an
Either with the left value set. |
static <E1,T1> Either<E1,T1> |
ofNullableLeft(E1 left)
Creates an
Either with the left value set.The left value may be null. |
static <E1,T1> Either<E1,T1> |
ofNullableRight(T1 right)
Creates an
Either with the right value set. |
static <E1,T1> Either<E1,T1> |
ofRight(T1 right)
Creates an
Either with the right value set. |
Either<T,E> |
swap()
Gets a new
Either with left and right swapped. |
String |
toString() |
public static <E1,T1> Either<E1,T1> empty()
Either.E1 - left type parameterT1 - right type parameterpublic static <E1,T1> Either<E1,T1> ofLeft(E1 left)
Either with the left value set. The left value may not be null.E1 - left type parameterT1 - right type parameterleft - left valueEither with left value setNullPointerException - if left is nullpublic static <E1,T1> Either<E1,T1> ofNullableLeft(E1 left)
Either with the left value set.The left value may be null.E1 - left type parameter.T1 - right type parameter.left - left valueEither with left value setpublic static <E1,T1> Either<E1,T1> ofNullableRight(T1 right)
Either with the right value set. Right may be null.E1 - left type parameterT1 - right type parameterright - right valueEither with right value setpublic static <E1,T1> Either<E1,T1> ofRight(T1 right)
Either with the right value set. The right value may not be null.E1 - left type parameterT1 - right type parameterright - right valueEither with right value setNullPointerException - if right is nullpublic Optional<E> filterLeft(Predicate<? super E> predicate)
left value is present,
and the value matches the given predicate,
return an Optional describing the value,
otherwise return an Optional.empty.predicate - Predicate to apply to the left value, if presentOptional describing filtered left value,
if a left value is present and the left value matches the given predicate,
otherwise an empty OptionalNullPointerException - if the predicate is null throw a NullPointerExceptionpublic Optional<T> filterRight(Predicate<? super T> predicate)
right value is present,
and the value matches the given predicate,
return an Optional describing the value,
otherwise return an Optional.empty.predicate - to apply to the right value, if presentOptional describing filtered right value,
if a right value is present and the right value matches the given predicate,
otherwise an empty OptionalNullPointerException - if the predicate is null throw a NullPointerExceptionpublic E getLeft()
left value.left valueNoSuchElementException - a NoSuchElementException is thrown when left is not presentpublic T getRight()
right valueNoSuchElementException - a NoSuchElementException is thrown when right is not presentpublic E ifLeftNotPresent(E other)
other value if left is not present.other - value to return if left is not presentleft is present else otherpublic E ifLeftNotPresentGet(Supplier<? extends E> other)
Provider if left is not present.other - provider to provide a value to return if left is not presentleft is present else otherNullPointerException - throws a NullPointerException if other is nullpublic <X extends Throwable> E ifLeftNotPresentThrow(Supplier<? extends X> other) throws X extends Throwable
Throwable of type X provided by a Provider if {code left} is not present.X - type of the Throwable to throwother - provider to provide exception to throw if {code left} is not presentleft is presentX - Throwable that will be thrown if {code left} is not presentNullPointerException - throws a NullPointerException if other is nullX extends Throwablepublic void ifLeftPresent(Consumer<? super E> consumer)
Consumer to the {code left} value.consumer - left to apply if left is presentNullPointerException - throws a NullPointerException if consumer is nullpublic T ifRightNotPresent(T other)
other value if right is not present.other - value to return if right is not presentright is present else otherpublic T ifRightNotPresentGet(Supplier<? extends T> other)
Provider if right is not present.other - provider to provide a value to return if right is not presentright is present else otherNullPointerException - throws a NullPointerException if other is nullpublic <X extends Throwable> T ifRightNotPresentThrow(Supplier<? extends X> other) throws X extends Throwable
Throwable of type X provided by a Provider if {code right} is not present.X - type of the Throwable to throwother - provider to provide exception to throw if {code right} is not presentright is presentX - Throwable that will be thrown if {code right} is not presentNullPointerException - throws a NullPointerException if other is nullX extends Throwablepublic void ifRightPresent(Consumer<? super T> consumer)
Consumer to the {code right} value.consumer - left to apply if right is presentNullPointerException - throws a NullPointerException if consumer is nullpublic boolean isEmpty()
left or right are present.left or right are presentpublic boolean isLeftPresent()
left value is present.left value is presentpublic boolean isRightPresent()
right value is present.right value is presentpublic <U> Optional<? extends U> mapLeft(Function<? super E,? extends U> mapper)
left value is present, apply the provided mapping function to it,
and if the result is non-null, return an Optional describing the result.U - the type of the result of the mapping functionmapper - a mapping function to apply to the left value,
if present left value of this Either, if a
left value is present, otherwise an empty OptionalOptional describing mapped left value,
if a left value is present,
otherwise an empty OptionalNullPointerException - a NullPointerException if mapper is nullpublic <U> Optional<? extends U> mapRight(Function<? super T,? extends U> mapper)
right value is present, apply the provided mapping function to it,
and if the result is non-null, return an Optional describing the result.U - the type of the result of the mapping functionmapper - a mapping function to apply to the right value,
if present right value of this Either, if a
right value is present, otherwise an empty OptionalOptional describing mapped right value,
if a right value is present,
otherwise an empty OptionalNullPointerException - a NullPointerException if mapper is nullpublic Either<T,E> swap()
Either with left and right swapped.EitherCopyright © 2021. All rights reserved.