Interface Either<L,R>
- Type Parameters:
L- The left type, by convention the error type if being used to represent possible errorsR- The right type, by convention the success type if being used to represent possible errors
public interface Either<L,R>
Copy of com.ibm.asyncutil.util.Either from com.ibm.async:asyncutil:0.1.0
without all the methods and imports we don't need for Hibernate Reactive.
A container type that contains either an L or an R type object. An Either indicating a left value
can be constructed with
Either.left(value), a right with Either.right(value).
By convention, if using this class to represent a result that may be an error or a success, the
error type should be in the left position and the success type should be in the right position
(mnemonic: "right" also means correct). As a result, this class implements monadic methods
(similar to those on Optional, Stream, etc) for working on the R
type.
Either<Exception, String> tryGetString();
Either<Exception, Integer> tryParse(String s);
Double convert(Integer i);
// if any intermediate step fails, will just be an Either.left(exception).
Either<Exception, Double> eitherDouble = tryGetString() // Either<Exception, String>
.flatMap(tryParse) // Either<Exception, Integer>
.map(convert); // Either<Exception, Double>
-
Method Summary
Modifier and TypeMethodDescriptionTransforms the right type ofthis, producing a right Either of typeVifthiswas right andfproduced a right Either, or a left Either otherwise.<V> VApplies exactly one of the two provided functions to produce a value of typeV.default voidCalls exactly one of the two provided functions with an L or an Rdefault booleanisLeft()Whether this container holds an L typedefault booleanisRight()Whether this container holds an R typeleft()Optionally gets the left value ofthisif it existsstatic <A,B> Either<A, B> left(A a) Constructs an Either with a left valuedefault <A,B> Either<A, B> Creates a new Either possibly of two new and distinct types, by applying the provided transformation functions.Transforms the right type ofthis, producing an Either of the transformed value ifthiscontained right, or an Either of the original left value otherwise.right()Optionally gets the right value ofthisif it existsstatic <A,B> Either<A, B> right(B b) Constructs an Either with a right value
-
Method Details
-
isLeft
default boolean isLeft()Whether this container holds an L type- Returns:
- true if this is a Left, false otherwise
-
isRight
default boolean isRight()Whether this container holds an R type- Returns:
- true if this is a Right, false otherwise
-
fold
Applies exactly one of the two provided functions to produce a value of typeV. For example, applying an int function or defaulting to zero on error:{ Either<Exception, Integer> either = tryGetInteger(); int halvedOrZero = either.fold(e -> 0, i -> i / 2); }- Type Parameters:
V- the return type- Parameters:
leftFn- a function the produces a V from an L, only applied ifthiscontains an L typerightFn- a function the produces a V from an R, only applied ifthiscontains an R type- Returns:
- a
Vvalue produced byleftFnifthiscontained an L, produced byrightFnotherwise. - Throws:
NullPointerException- if the function to be applied is null
-
forEach
Calls exactly one of the two provided functions with an L or an R- Parameters:
leftConsumer- a function that consumes an L, only applied ifthiscontains an L typerightConsumer- a function the consumes an R, only applied ifthiscontains an R type- Throws:
NullPointerException- if the function to be applied is null
-
map
default <A,B> Either<A,B> map(Function<? super L, ? extends A> leftFn, Function<? super R, ? extends B> rightFn) Creates a new Either possibly of two new and distinct types, by applying the provided transformation functions.Either<Double, Integer> result; Either<String, Boolean> x = result.map(d -> d.toString(), i -> i % 2 == 0)- Type Parameters:
A- the left type of the returned EitherB- the right type of the returned Either- Parameters:
leftFn- a function that takes an L and produces an A, to be applied ifthiscontains an LrightFn- a function that takes an R and produces and B, to be applied ifthiscontains an R- Returns:
- a new Either, containing an A resulting from the application of leftFn if
thiscontained an L, or containing a B resulting from the application of rightFn otherwise - Throws:
NullPointerException- if the function to be applied is null
-
map
Transforms the right type ofthis, producing an Either of the transformed value ifthiscontained right, or an Either of the original left value otherwise. For example, if we have either a Double or an error, convert it into an Integer if it's a Double, then convert it to a String if it's an Integer:Either<Exception, Double> eitherDouble; Either<Exception, String> eitherString = eitherDouble.map(Double::intValue).map(Integer::toString)- Type Parameters:
V- the right type of the returned Either- Parameters:
fn- function that takes an R value and produces a V value, to be applied ifthiscontains an R- Returns:
- an Either containing the transformed value produced by
fnifthiscontained an R, or the original L value otherwise.
-
flatMap
Transforms the right type ofthis, producing a right Either of typeVifthiswas right andfproduced a right Either, or a left Either otherwise. For example, if we have either a String or an error, attempt to parse into an Integer (which could potentially itself produce an error):Either<Exception, Integer> tryParse(String s); Either<Exception, String> eitherString; // will be an exception if eitherString was an exception // or if tryParse failed, otherwise will be an Integer Either<Exception, Integer> e = eitherString.flatMap(tryParse);- Type Parameters:
V- the right type of the returned Either- Parameters:
f- a function that takes an R value and produces an Either of a L value or a V value- Returns:
- an Either containing the right value produced by
fif boththisand the produced Either were right, the left value of the produced Either ifthiswas right but the produced value wasn't, or the original left value ifthiswas left.
-
left
Optionally gets the left value ofthisif it exists- Returns:
- a present
Optionalof an L ifthiscontains an L, an empty one otherwise. - Throws:
NullPointerException- if the left value ofthisis present and null
-
right
Optionally gets the right value ofthisif it exists- Returns:
- a present
Optionalof an R ifthiscontains an R, an empty one otherwise. - Throws:
NullPointerException- if the right value ofthisis present and null
-
left
Constructs an Either with a left value- Parameters:
a- the left element of the new Either- Returns:
- an Either with a left value
-
right
Constructs an Either with a right value- Parameters:
b- the right element of the new Either- Returns:
- An Either with a right value
-