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.boolean
static <T,
E> Result<T, E> error
(E value) Returns aResult
in error state containing the given non-null
value 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 theResult
from applying the given mapping function to the success value, otherwise returns the unalteredResult
in error state.flatMapToBooleanResult
(Function<? super T, BooleanResult<E>> function) If in success state, returns theBooleanResult
from applying the given mapping function to the success value, otherwise returns aBooleanResult
containing the error value of thisResult
.<N> OptionalResult<N,
E> flatMapToOptionalResult
(Function<? super T, OptionalResult<N, E>> function) If in success state, returns theOptionalResult
from applying the given mapping function to the success value, otherwise returns aOptionalResult
containing the error value of thisResult
.flatMapToVoidResult
(Function<? super T, VoidResult<E>> function) If in success state, returns theVoidResult
from applying the given mapping function to the success value, otherwise returns aVoidResult
containing the error value of thisResult
.flatRecover
(Function<E, Result<? extends N, ? extends E>> function) If in error state, returns theResult
from applying the given mapping function to the error value, otherwise returns the unalteredResult
in success state.flatRunIfSuccess
(Supplier<? extends VoidResult<? extends E>> supplier) If in success state, runs the given supplier.<N> N
Retrieve a value from thisResult
by folding the states.Handle the givenCallable
.static <T,
E> Result<T, E> Handle the givenCallable
.int
hashCode()
If in success state, returns aResult
containing the result of applying the given mapping function to the success value, otherwise returns the unalteredResult
in error state.If in error state, returns aResult
containing the result of applying the given mapping function to the error value, otherwise returns the unalteredResult
in success state.mapToBoolean
(Function<? super T, Boolean> function) If in success state, returns aBooleanResult
containing the result of applying the given mapping function to the success value, otherwise returns aBooleanResult
containing the error value of thisResult
.<N> OptionalResult<N,
E> mapToOptional
(Function<? super T, Optional<N>> function) If in success state, returns aOptionalResult
containing the result of applying the given mapping function to the success value, otherwise returns aOptionalResult
containing 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 aResult
with the success value from applying the given mapping function to the error value, otherwise returns the unalteredResult
in 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 aResult
in success state containing the given non-null
value as success value.Transforms thisResult
to anOptionalResult
.toString()
Transforms thisResult
to aVoidResult
.verify
(Function<? super T, ? extends VoidResult<? extends E>> function) If in success state, verifies the success value of thisResult
by mapping it to aVoidResult
.If in success state, verifies the success value of thisResult
by testing it with the given predicate.
-
Method Details
-
success
Returns aResult
in success state containing the given non-null
value 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
Result
in success state containing the given success value - Throws:
NullPointerException
- if given success value isnull
-
error
Returns aResult
in error state containing the given non-null
value 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
Result
in error state containing the given error value - Throws:
NullPointerException
- if given error value isnull
-
map
If in success state, returns aResult
containing the result of applying the given mapping function to the success value, otherwise returns the unalteredResult
in 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
Result
containing the result of applying the mapping function to the success value of thisResult
, if in success state, otherwise the unalteredResult
in error state - Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
mapToOptional
If in success state, returns aOptionalResult
containing the result of applying the given mapping function to the success value, otherwise returns aOptionalResult
containing the error value of thisResult
.- Type Parameters:
N
- the type of the success value which may be present in theOptional
returned from the mapping function- Parameters:
function
- the mapping function to apply to the success value, if success state- Returns:
- a
OptionalResult
containing the result of applying the mapping function to the success value of thisResult
, if in success state, otherwise aOptionalResult
containing the error value of thisResult
- Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
mapToBoolean
If in success state, returns aBooleanResult
containing the result of applying the given mapping function to the success value, otherwise returns aBooleanResult
containing the error value of thisResult
.- Parameters:
function
- the mapping function to apply to the success value, if success state- Returns:
- a
BooleanResult
containing the result of applying the mapping function to the success value of thisResult
, if in success state, otherwise aBooleanResult
containing the error value of thisResult
- Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
mapError
If in error state, returns aResult
containing the result of applying the given mapping function to the error value, otherwise returns the unalteredResult
in 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
Result
containing the result of applying the mapping function to the error value of thisResult
, if in error state, otherwise the unalteredResult
in success state - Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
flatMap
public <N> Result<N,E> flatMap(Function<? super T, ? extends Result<? extends N, ? extends E>> function) If in success state, returns theResult
from applying the given mapping function to the success value, otherwise returns the unalteredResult
in error state.- Type Parameters:
N
- the type of success value which may be present in theResult
returned by the mapping function- Parameters:
function
- the mapping function to apply to the success value, if success state- Returns:
- the
Result
returned from the mapping function, if in success state, otherwise the unalteredResult
in error state - Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
flatMapToOptionalResult
public <N> OptionalResult<N,E> flatMapToOptionalResult(Function<? super T, OptionalResult<N, E>> function) If in success state, returns theOptionalResult
from applying the given mapping function to the success value, otherwise returns aOptionalResult
containing the error value of thisResult
.- Type Parameters:
N
- the type of success value which may be present in theOptionalResult
returned by the mapping function- Parameters:
function
- the mapping function to apply to the success value, if success state- Returns:
- the
OptionalResult
returned from the mapping function, if in success state, otherwise aOptionalResult
containing the error value of thisResult
- Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
flatMapToBooleanResult
If in success state, returns theBooleanResult
from applying the given mapping function to the success value, otherwise returns aBooleanResult
containing the error value of thisResult
.- Parameters:
function
- the mapping function to apply to the success value, if success state- Returns:
- the
BooleanResult
returned from the mapping function, if in success state, otherwise aBooleanResult
containing the error value of thisResult
- Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
flatMapToVoidResult
If in success state, returns theVoidResult
from applying the given mapping function to the success value, otherwise returns aVoidResult
containing the error value of thisResult
.- Parameters:
function
- the mapping function to apply to the success value, if success state- Returns:
- the
VoidResult
returned from the mapping function, if in success state, otherwise aVoidResult
containing the error value of thisResult
- Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
recover
If in error state, returns aResult
with the success value from applying the given mapping function to the error value, otherwise returns the unalteredResult
in 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
Result
containing the value from the mapping function, if in error state, otherwise the unalteredResult
in success state
-
flatRecover
If in error state, returns theResult
from applying the given mapping function to the error value, otherwise returns the unalteredResult
in success state.- Type Parameters:
N
- the type of success value which may be present in theResult
returned 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
Result
returned from the mapping function, if in error state, otherwise the unalteredResult
in 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
Result
unaltered - 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
Result
unaltered - 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
Result
unaltered - 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 aVoidResult
in success state, the originalResult
is returned unaltered. If the function returns aVoidResult
in error state, aResult
containing the error value is returned. If in error state, the originalResult
is returned unaltered.- Parameters:
function
- the function which accepts the success value- Returns:
- the original
Result
unaltered if the given function returns success or the originalResult
is in error state, otherwise aResult
containing the error value from the function result - Throws:
NullPointerException
- if the given function isnull
or 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
Result
unaltered - 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
Result
unaltered - 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
Result
unaltered - 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
Result
unaltered - 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
Result
unaltered - Throws:
NullPointerException
- if the given runnable isnull
-
flatRunIfSuccess
If in success state, runs the given supplier. If the supplier returns aVoidResult
in success state, the originalResult
is returned unaltered. If the supplier returns aVoidResult
in error state, aResult
containing the error value is returned. If in error state, the originalResult
is returned unaltered.- Parameters:
supplier
- the supplier to run- Returns:
- the original
Result
unaltered if the given supplier returns success or the originalResult
is in error state, otherwise aResult
containing the error value from the supplier result - Throws:
NullPointerException
- if the given supplier isnull
or returnsnull
-
verify
If in success state, verifies the success value of thisResult
by testing it with the given predicate. If the predicate evaluates to false, a newResult
is returned containing the error value provided by the given error supplier. If the predicate evaluates to true, or theResult
already was in error state, the originalResult
is 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
Result
unaltered, unless the predicate evaluates to false, then a newResult
in error state is returned containing the supplied error value - Throws:
NullPointerException
- if the given predicate isnull
or returnsnull
, or the given error supplier isnull
or returnsnull
-
verify
If in success state, verifies the success value of thisResult
by mapping it to aVoidResult
. If the returnedVoidResult
is in error state, a newResult
is returned containing the error value of theVoidResult
. If theVoidResult
is in success state, or theResult
already was in error state, the originalResult
is returned unaltered.- Parameters:
function
- the function applied to the success value, if success state- Returns:
- the original
Result
unaltered, unless theVoidResult
returned by the mapping function is in error state, then a newResult
in error state is returned containing the error value from theVoidResult
- Throws:
NullPointerException
- if the given function isnull
or returnsnull
-
fold
public <N> N fold(Function<? super T, ? extends N> valueFunction, Function<? super E, ? extends N> errorFunction) Retrieve a value from thisResult
by 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 returnnull
errorFunction
- 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 isnull
or returnsnull
-
toOptionalResult
Transforms thisResult
to anOptionalResult
. If in success state, theOptionalResult
will be in success state containing the success value from thisResult
. If in error state, theOptionalResult
will be in error state containing the error value from thisResult
.The returned
OptionalResult
will never be empty.- Returns:
- an
OptionalResult
in success state containing the success value from thisResult
or in error state containing the error value from thisResult
-
toVoidResult
Transforms thisResult
to aVoidResult
. If in success state, theVoidResult
will be in success state. If in error state, theVoidResult
will be in error state containing the error value from thisResult
.- Returns:
- a
VoidResult
either in success state or in error state containing the error value from thisResult
-
handle
Handle the givenCallable
. If theCallable
executes successfully, theResult
will be in success state containing the returned value. If theCallable
throws an exception, theResult
will be in error state containing the thrown exception.- Type Parameters:
T
- type of the return value of theCallable
- Parameters:
callable
- theCallable
to handle- Returns:
- a
Result
either in success state containing the value from theCallable
, or in error state containing the exception thrown by theCallable
- Throws:
NullPointerException
- if the given callable isnull
or returnsnull
-
handle
public static <T,E> Result<T,E> handle(Callable<T> callable, Function<Exception, E> exceptionMapper) Handle the givenCallable
. If theCallable
executes successfully, theResult
will be in success state containing the returned value. If theCallable
throws an exception, theResult
will 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 theCallable
E
- type of the error value after mapping a thrown exception- Parameters:
callable
- theCallable
to handle- Returns:
- a
Result
either 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 isnull
or returnsnull
, or if the given exception mapper function isnull
or returnsnull
-
equals
-
hashCode
public int hashCode() -
toString
-
runAlways(java.lang.Runnable)
instead for clarity