Package no.gorandalum.fluentresult
Class Result<T,E>
java.lang.Object
no.gorandalum.fluentresult.Result<T,E>
- Type Parameters:
T- the type of the success valueE- the type of the error value
A result object which either is in success state containing a
non-
null success value, or in error state containing a
non-null error value.
A variable whose type is Result should never itself be null,
it should always point to an Result instance.
-
Method Summary
Modifier and TypeMethodDescriptionIf in success state, applies the success value to the given consumer, otherwise does nothing.consumeEither(Consumer<? super T> valueConsumer, Consumer<? super E> errorConsumer) If in success state, applies the success value to the given value consumer.consumeError(Consumer<? super E> errorConsumer) If in error state, applies the error value to the given consumer, otherwise does nothing.booleanstatic <T,E> Result<T, E> error(E value) Returns aResultin error state containing the given non-nullvalue as error value.flatConsume(Function<? super T, ? extends VoidResult<? extends E>> function) If in success state, applies the success value to the given function.If in success state, returns theResultfrom applying the given mapping function to the success value, otherwise returns the unalteredResultin error state.flatMapToBooleanResult(Function<? super T, BooleanResult<E>> function) If in success state, returns theBooleanResultfrom applying the given mapping function to the success value, otherwise returns aBooleanResultcontaining the error value of thisResult.<N> OptionalResult<N,E> flatMapToOptionalResult(Function<? super T, OptionalResult<N, E>> function) If in success state, returns theOptionalResultfrom applying the given mapping function to the success value, otherwise returns aOptionalResultcontaining the error value of thisResult.flatMapToVoidResult(Function<? super T, VoidResult<E>> function) If in success state, returns theVoidResultfrom applying the given mapping function to the success value, otherwise returns aVoidResultcontaining the error value of thisResult.flatRecover(Function<E, Result<? extends N, ? extends E>> function) If in error state, returns theResultfrom applying the given mapping function to the error value, otherwise returns the unalteredResultin success state.flatRunIfSuccess(Supplier<? extends VoidResult<? extends E>> supplier) If in success state, runs the given supplier.<N> NRetrieve a value from thisResultby folding the states.Handle the givenCallable.static <T,E> Result<T, E> Handle the givenCallable.inthashCode()If in success state, returns aResultcontaining the result of applying the given mapping function to the success value, otherwise returns the unalteredResultin error state.If in error state, returns aResultcontaining the result of applying the given mapping function to the error value, otherwise returns the unalteredResultin success state.mapToBoolean(Function<? super T, Boolean> function) If in success state, returns aBooleanResultcontaining the result of applying the given mapping function to the success value, otherwise returns aBooleanResultcontaining the error value of thisResult.<N> OptionalResult<N,E> mapToOptional(Function<? super T, Optional<N>> function) If in success state, returns aOptionalResultcontaining the result of applying the given mapping function to the success value, otherwise returns aOptionalResultcontaining the error value of thisResult.If in success state, returns the success value, otherwise returnsother.If in success state, returns the success value, otherwise returns the value returned from the given function.orElseThrow(Function<? super E, ? extends X> function) If in success state, returns the success value, otherwise throws the exception returned by the given function.If in error state, returns aResultwith the success value from applying the given mapping function to the error value, otherwise returns the unalteredResultin success state.Deprecated.Runs the given runnable, no matter the state.If in success state, runs the given success runnable.runIfError(Runnable runnable) If in error state, runs the given runnable, otherwise does nothing.runIfSuccess(Runnable runnable) If in success state, runs the given runnable, otherwise does nothing.static <T,E> Result<T, E> success(T value) Returns aResultin success state containing the given non-nullvalue as success value.Transforms thisResultto anOptionalResult.toString()Transforms thisResultto aVoidResult.verify(Function<? super T, ? extends VoidResult<? extends E>> function) If in success state, verifies the success value of thisResultby mapping it to aVoidResult.If in success state, verifies the success value of thisResultby testing it with the given predicate.
-
Method Details
-
success
Returns aResultin success state containing the given non-nullvalue as success value.- Type Parameters:
T- the type of the success valueE- the type of the error value- Parameters:
value- the success value, which must be non-null- Returns:
- a
Resultin success state containing the given success value - Throws:
NullPointerException- if given success value isnull
-
error
Returns aResultin error state containing the given non-nullvalue as error value.- Type Parameters:
T- the type of the success valueE- the type of the error value- Parameters:
value- the error value, which must be non-null- Returns:
- a
Resultin error state containing the given error value - Throws:
NullPointerException- if given error value isnull
-
map
If in success state, returns aResultcontaining the result of applying the given mapping function to the success value, otherwise returns the unalteredResultin error state.- Type Parameters:
N- the type of the value returned from the mapping function- Parameters:
function- the mapping function to apply to the success value, if success state- Returns:
- a
Resultcontaining the result of applying the mapping function to the success value of thisResult, if in success state, otherwise the unalteredResultin error state - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
mapToOptional
If in success state, returns aOptionalResultcontaining the result of applying the given mapping function to the success value, otherwise returns aOptionalResultcontaining the error value of thisResult.- Type Parameters:
N- the type of the success value which may be present in theOptionalreturned from the mapping function- Parameters:
function- the mapping function to apply to the success value, if success state- Returns:
- a
OptionalResultcontaining the result of applying the mapping function to the success value of thisResult, if in success state, otherwise aOptionalResultcontaining the error value of thisResult - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
mapToBoolean
If in success state, returns aBooleanResultcontaining the result of applying the given mapping function to the success value, otherwise returns aBooleanResultcontaining the error value of thisResult.- Parameters:
function- the mapping function to apply to the success value, if success state- Returns:
- a
BooleanResultcontaining the result of applying the mapping function to the success value of thisResult, if in success state, otherwise aBooleanResultcontaining the error value of thisResult - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
mapError
If in error state, returns aResultcontaining the result of applying the given mapping function to the error value, otherwise returns the unalteredResultin success state.- Type Parameters:
N- the type of the value returned from the mapping function- Parameters:
function- the mapping function to apply to the error value, if error state- Returns:
- a
Resultcontaining the result of applying the mapping function to the error value of thisResult, if in error state, otherwise the unalteredResultin success state - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
flatMap
public <N> Result<N,E> flatMap(Function<? super T, ? extends Result<? extends N, ? extends E>> function) If in success state, returns theResultfrom applying the given mapping function to the success value, otherwise returns the unalteredResultin error state.- Type Parameters:
N- the type of success value which may be present in theResultreturned by the mapping function- Parameters:
function- the mapping function to apply to the success value, if success state- Returns:
- the
Resultreturned from the mapping function, if in success state, otherwise the unalteredResultin error state - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
flatMapToOptionalResult
public <N> OptionalResult<N,E> flatMapToOptionalResult(Function<? super T, OptionalResult<N, E>> function) If in success state, returns theOptionalResultfrom applying the given mapping function to the success value, otherwise returns aOptionalResultcontaining the error value of thisResult.- Type Parameters:
N- the type of success value which may be present in theOptionalResultreturned by the mapping function- Parameters:
function- the mapping function to apply to the success value, if success state- Returns:
- the
OptionalResultreturned from the mapping function, if in success state, otherwise aOptionalResultcontaining the error value of thisResult - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
flatMapToBooleanResult
If in success state, returns theBooleanResultfrom applying the given mapping function to the success value, otherwise returns aBooleanResultcontaining the error value of thisResult.- Parameters:
function- the mapping function to apply to the success value, if success state- Returns:
- the
BooleanResultreturned from the mapping function, if in success state, otherwise aBooleanResultcontaining the error value of thisResult - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
flatMapToVoidResult
If in success state, returns theVoidResultfrom applying the given mapping function to the success value, otherwise returns aVoidResultcontaining the error value of thisResult.- Parameters:
function- the mapping function to apply to the success value, if success state- Returns:
- the
VoidResultreturned from the mapping function, if in success state, otherwise aVoidResultcontaining the error value of thisResult - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
recover
If in error state, returns aResultwith the success value from applying the given mapping function to the error value, otherwise returns the unalteredResultin success state.- Parameters:
function- the mapping function to apply to the error value to convert to a new success value, if error state- Returns:
- A
Resultcontaining the value from the mapping function, if in error state, otherwise the unalteredResultin success state
-
flatRecover
If in error state, returns theResultfrom applying the given mapping function to the error value, otherwise returns the unalteredResultin success state.- Type Parameters:
N- the type of success value which may be present in theResultreturned by the mapping function- Parameters:
function- the mapping function to apply to the error value to convert to a newResult, if error state- Returns:
- the
Resultreturned from the mapping function, if in error state, otherwise the unalteredResultin success state
-
consume
If in success state, applies the success value to the given consumer, otherwise does nothing.- Parameters:
consumer- the consumer which accepts the success value- Returns:
- the original
Resultunaltered - Throws:
NullPointerException- if the given consumer isnull
-
consumeError
If in error state, applies the error value to the given consumer, otherwise does nothing.- Parameters:
errorConsumer- the consumer which accepts the error value- Returns:
- the original
Resultunaltered - Throws:
NullPointerException- if the given consumer isnull
-
consumeEither
public Result<T,E> consumeEither(Consumer<? super T> valueConsumer, Consumer<? super E> errorConsumer) If in success state, applies the success value to the given value consumer. If in error state, applies the error value to the given error consumer.- Parameters:
valueConsumer- the consumer which accepts the success valueerrorConsumer- the consumer which accepts the error value- Returns:
- the original
Resultunaltered - Throws:
NullPointerException- if one of the given consumers isnull
-
flatConsume
If in success state, applies the success value to the given function. If the function returns aVoidResultin success state, the originalResultis returned unaltered. If the function returns aVoidResultin error state, aResultcontaining the error value is returned. If in error state, the originalResultis returned unaltered.- Parameters:
function- the function which accepts the success value- Returns:
- the original
Resultunaltered if the given function returns success or the originalResultis in error state, otherwise aResultcontaining the error value from the function result - Throws:
NullPointerException- if the given function isnullor returnsnull
-
runIfSuccess
If in success state, runs the given runnable, otherwise does nothing.- Parameters:
runnable- the runnable to run if success state- Returns:
- the original
Resultunaltered - Throws:
NullPointerException- if the given runnable isnull
-
runIfError
If in error state, runs the given runnable, otherwise does nothing.- Parameters:
runnable- the runnable to run if error state- Returns:
- the original
Resultunaltered - Throws:
NullPointerException- if the given runnable isnull
-
runEither
If in success state, runs the given success runnable. If in error state, runs the given error runnable.- Parameters:
successRunnable- the runnable to run if success stateerrorRunnable- the runnable to run if error state- Returns:
- the original
Resultunaltered - Throws:
NullPointerException- if one of the given runnables isnull
-
run
Deprecated.userunAlways(java.lang.Runnable)instead for clarityRuns the given runnable, no matter the state.- Parameters:
runnable- the runnable to run- Returns:
- the original
Resultunaltered - Throws:
NullPointerException- if the given runnable isnull
-
runAlways
Runs the given runnable, no matter the state.- Parameters:
runnable- the runnable to run- Returns:
- the original
Resultunaltered - Throws:
NullPointerException- if the given runnable isnull
-
flatRunIfSuccess
If in success state, runs the given supplier. If the supplier returns aVoidResultin success state, the originalResultis returned unaltered. If the supplier returns aVoidResultin error state, aResultcontaining the error value is returned. If in error state, the originalResultis returned unaltered.- Parameters:
supplier- the supplier to run- Returns:
- the original
Resultunaltered if the given supplier returns success or the originalResultis in error state, otherwise aResultcontaining the error value from the supplier result - Throws:
NullPointerException- if the given supplier isnullor returnsnull
-
verify
If in success state, verifies the success value of thisResultby testing it with the given predicate. If the predicate evaluates to false, a newResultis returned containing the error value provided by the given error supplier. If the predicate evaluates to true, or theResultalready was in error state, the originalResultis returned unaltered.- Parameters:
predicate- the predicate used to verify the success value, if success stateerrorSupplier- supplier providing the error if predicate evaluates to false- Returns:
- the original
Resultunaltered, unless the predicate evaluates to false, then a newResultin error state is returned containing the supplied error value - Throws:
NullPointerException- if the given predicate isnullor returnsnull, or the given error supplier isnullor returnsnull
-
verify
If in success state, verifies the success value of thisResultby mapping it to aVoidResult. If the returnedVoidResultis in error state, a newResultis returned containing the error value of theVoidResult. If theVoidResultis in success state, or theResultalready was in error state, the originalResultis returned unaltered.- Parameters:
function- the function applied to the success value, if success state- Returns:
- the original
Resultunaltered, unless theVoidResultreturned by the mapping function is in error state, then a newResultin error state is returned containing the error value from theVoidResult - Throws:
NullPointerException- if the given function isnullor returnsnull
-
fold
public <N> N fold(Function<? super T, ? extends N> valueFunction, Function<? super E, ? extends N> errorFunction) Retrieve a value from thisResultby folding the states. If in success state, return the value of applying the value function to the success value. If in error state, return the value of applying the error function to the error value.- Type Parameters:
N- the type of retrieved value- Parameters:
valueFunction- the mapping function to apply to the success value, if success state, may returnnullerrorFunction- the mapping function to apply to the error value, if error state, may returnnull- Returns:
- the folded value mapped from either the success value or error
value, may be
null - Throws:
NullPointerException- if one of the given functions isnull
-
orElse
If in success state, returns the success value, otherwise returnsother.- Parameters:
other- the value to be returned, if not in success state, may benull- Returns:
- the success value, if success state, otherwise
other
-
orElseGet
If in success state, returns the success value, otherwise returns the value returned from the given function.- Parameters:
function- the mapping function to apply to the error value, if not in success state, it may returnnull- Returns:
- the success value, if success state, otherwise the result returned from the given function
- Throws:
NullPointerException- if the given function isnull
-
orElseThrow
If in success state, returns the success value, otherwise throws the exception returned by the given function.- Type Parameters:
X- type of the exception to be thrown- Parameters:
function- the mapping function producing an exception by applying the error value, if not in success state- Returns:
- the success value, if success state
- Throws:
X- if in error stateNullPointerException- if the given function isnullor returnsnull
-
toOptionalResult
Transforms thisResultto anOptionalResult. If in success state, theOptionalResultwill be in success state containing the success value from thisResult. If in error state, theOptionalResultwill be in error state containing the error value from thisResult.The returned
OptionalResultwill never be empty.- Returns:
- an
OptionalResultin success state containing the success value from thisResultor in error state containing the error value from thisResult
-
toVoidResult
Transforms thisResultto aVoidResult. If in success state, theVoidResultwill be in success state. If in error state, theVoidResultwill be in error state containing the error value from thisResult.- Returns:
- a
VoidResulteither in success state or in error state containing the error value from thisResult
-
handle
Handle the givenCallable. If theCallableexecutes successfully, theResultwill be in success state containing the returned value. If theCallablethrows an exception, theResultwill be in error state containing the thrown exception.- Type Parameters:
T- type of the return value of theCallable- Parameters:
callable- theCallableto handle- Returns:
- a
Resulteither in success state containing the value from theCallable, or in error state containing the exception thrown by theCallable - Throws:
NullPointerException- if the given callable isnullor returnsnull
-
handle
public static <T,E> Result<T,E> handle(Callable<T> callable, Function<Exception, E> exceptionMapper) Handle the givenCallable. If theCallableexecutes successfully, theResultwill be in success state containing the returned value. If theCallablethrows an exception, theResultwill be in error state containing the result after mapping the exception with the given exception mapper function.- Type Parameters:
T- type of the return value of theCallableE- type of the error value after mapping a thrown exception- Parameters:
callable- theCallableto handle- Returns:
- a
Resulteither in success state containing the value from theCallable, or in error state containing the result after mapping the exception thrown by theCallable - Throws:
NullPointerException- if the given callable isnullor returnsnull, or if the given exception mapper function isnullor returnsnull
-
equals
-
hashCode
public int hashCode() -
toString
-
runAlways(java.lang.Runnable)instead for clarity