Package no.gorandalum.fluentresult
Class VoidResult<E>
java.lang.Object
no.gorandalum.fluentresult.VoidResult<E>
- Type Parameters:
E
- the type of the error value
A result object which either is in success state with no value, or in error
state containing a non-
null
error value.
A variable whose type is VoidResult
should never itself be
null
, it should always point to an VoidResult
instance.
-
Method Summary
Modifier and TypeMethodDescriptionconsumeEither
(Runnable successRunnable, Consumer<? super E> errorConsumer) If in success state, runs the success-runnable.consumeError
(Consumer<? super E> errorConsumer) If in error state, applies the error value to the given consumer, otherwise does nothing.boolean
static <E> VoidResult<E>
error
(E value) Returns aVoidResult
in error state containing the given non-null
value as error value.flatRecover
(Function<E, VoidResult<? extends E>> function) If in error state, returns theVoidResult
from applying the given mapping function to the error value, otherwise returns the unalteredVoidResult
in success state.flatReplace
(Supplier<Result<? extends N, ? extends E>> supplier) If in success state, returns theResult
provided by the given supplier, otherwise returns aResult
containing the error value of thisVoidResult
.flatReplaceToBooleanResult
(Supplier<BooleanResult<? extends E>> supplier) If in success state, returns theBooleanResult
provided by the given supplier, otherwise returns aBooleanResult
containing the error value of thisVoidResult
.<N> OptionalResult<N,
E> flatReplaceToOptionalResult
(Supplier<OptionalResult<? extends N, ? extends E>> supplier) If in success state, returns theOptionalResult
provided by the given supplier, otherwise returns anOptionalResult
containing the error value of thisVoidResult
.flatReplaceToVoidResult
(Supplier<VoidResult<? extends E>> supplier) If in success state, returns theVoidResult
provided by the given supplier, otherwise returns the unalteredVoidResult
in error state.flatRunIfSuccess
(Supplier<? extends VoidResult<? extends E>> supplier) If in success state, runs the given supplier.<N> N
Retrieve a value from thisVoidResult
by folding the states.static VoidResult<Exception>
handle
(CheckedRunnable runnable) Handle the givenCheckedRunnable
.static <E> VoidResult<E>
handle
(CheckedRunnable runnable, Function<Exception, E> exceptionMapper) Handle the givenCheckedRunnable
.int
hashCode()
<N> VoidResult<N>
If in error state, returns aVoidResult
containing the result of applying the given mapping function to the error value, otherwise returns the unalteredVoidResult
in success state.<X extends Throwable>
voidorElseThrow
(Function<? super E, ? extends X> function) If in success state, does nothing, otherwise throws the exception returned by the given function.recover()
If in error state, returns a newVoidResult
in success state, otherwise returns the unalteredVoidResult
in success state.If in success state, returns aResult
containing the value provided by the given supplier, otherwise returns aResult
containing the error value of thisVoidResult
.replaceWithBoolean
(Supplier<Boolean> supplier) If in success state, returns aBooleanResult
containing the boolean value provided by the given supplier, otherwise returns aBooleanResult
containing the error value of thisVoidResult
.<N> OptionalResult<N,
E> replaceWithOptional
(Supplier<Optional<? extends N>> supplier) If in success state, returns aOptionalResult
containing the optional value provided by given supplier, otherwise returns aOptionalResult
containing the error value of thisVoidResult
.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 <E> VoidResult<E>
success()
Returns aVoidResult
in success state.<N> OptionalResult<N,
E> Transforms thisVoidResult
to anOptionalResult
.toString()
-
Method Details
-
success
Returns aVoidResult
in success state.- Type Parameters:
E
- the type of the error value- Returns:
- a
VoidResult
in success state
-
error
Returns aVoidResult
in error state containing the given non-null
value as error value.- Type Parameters:
E
- the type of the error value- Parameters:
value
- the error value, which must be non-null
- Returns:
- a
VoidResult
in error state containing the given error value - Throws:
NullPointerException
- if given error value isnull
-
mapError
If in error state, returns aVoidResult
containing the result of applying the given mapping function to the error value, otherwise returns the unalteredVoidResult
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
VoidResult
containing the result of applying the mapping function to the error value of thisVoidResult
, if in error state, otherwise the unalteredVoidResult
in success state - Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
replace
If in success state, returns aResult
containing the value provided by the given supplier, otherwise returns aResult
containing the error value of thisVoidResult
.- Type Parameters:
N
- the type of the value provided by the supplier- Parameters:
supplier
- the supplier to provide the value if success state, may not benull
- Returns:
- a
Result
containing the value provided by the given supplier, if in success state, otherwise aResult
containing the error value of thisVoidResult
- Throws:
NullPointerException
- if the given supplier isnull
or returnsnull
-
replaceWithOptional
If in success state, returns aOptionalResult
containing the optional value provided by given supplier, otherwise returns aOptionalResult
containing the error value of thisVoidResult
.- Type Parameters:
N
- the type of the value which may be present in theOptional
provided by the supplier- Parameters:
supplier
- the supplier to provide the optional value if success state, may not benull
- Returns:
- a
OptionalResult
containing the optional value provided by the given supplier, if in success state, otherwise aOptionalResult
containing the error value of thisVoidResult
- Throws:
NullPointerException
- if the given supplier isnull
or returnsnull
-
replaceWithBoolean
If in success state, returns aBooleanResult
containing the boolean value provided by the given supplier, otherwise returns aBooleanResult
containing the error value of thisVoidResult
.- Parameters:
supplier
- the supplier to provide the boolean value if success state, may not benull
- Returns:
- a
BooleanResult
containing the boolean value provided by the given supplier, if in success state, otherwise aBooleanResult
containing the error value of thisVoidResult
- Throws:
NullPointerException
- if the given supplier isnull
or returnsnull
-
flatReplace
If in success state, returns theResult
provided by the given supplier, otherwise returns aResult
containing the error value of thisVoidResult
.- Type Parameters:
N
- the type of success value which may be present in theResult
provided by the supplier- Parameters:
supplier
- the supplier to provide theResult
, if success state- Returns:
- the
Result
provided by the supplier, if in success state, otherwise aResult
containing the error value of thisVoidResult
- Throws:
NullPointerException
- if the given supplier isnull
or returnsnull
-
flatReplaceToOptionalResult
public <N> OptionalResult<N,E> flatReplaceToOptionalResult(Supplier<OptionalResult<? extends N, ? extends E>> supplier) If in success state, returns theOptionalResult
provided by the given supplier, otherwise returns anOptionalResult
containing the error value of thisVoidResult
.- Type Parameters:
N
- the type of success value which may be present in theOptionalResult
provided by the supplier- Parameters:
supplier
- the supplier to provide theOptionalResult
, if success state- Returns:
- the
OptionalResult
provided by the supplier, if in success state, otherwise anOptionalResult
containing the error value of thisVoidResult
- Throws:
NullPointerException
- if the given supplier isnull
or returnsnull
-
flatReplaceToBooleanResult
If in success state, returns theBooleanResult
provided by the given supplier, otherwise returns aBooleanResult
containing the error value of thisVoidResult
.- Parameters:
supplier
- the supplier to provide theBooleanResult
, if success state- Returns:
- the
BooleanResult
provided by the supplier, if in success state, otherwise aBooleanResult
containing the error value of thisVoidResult
- Throws:
NullPointerException
- if the given supplier isnull
or returnsnull
-
flatReplaceToVoidResult
If in success state, returns theVoidResult
provided by the given supplier, otherwise returns the unalteredVoidResult
in error state.- Parameters:
supplier
- the supplier to provide theVoidResult
, if success state- Returns:
- the
VoidResult
provided by the supplier, if in success state, otherwise the unalteredVoidResult
in error state - Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
recover
If in error state, returns a newVoidResult
in success state, otherwise returns the unalteredVoidResult
in success state.- Returns:
- a
VoidResult
in success state
-
flatRecover
If in error state, returns theVoidResult
from applying the given mapping function to the error value, otherwise returns the unalteredVoidResult
in success state.- Parameters:
function
- the mapping function to apply to the error value to convert to a newVoidResult
, if error state- Returns:
- the
VoidResult
returned from the mapping function, if in error state, otherwise the unalteredVoidResult
in success state
-
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
VoidResult
unaltered - Throws:
NullPointerException
- if the given consumer isnull
-
consumeEither
If in success state, runs the success-runnable. If in error state, applies the error value to the given error-consumer.- Parameters:
successRunnable
- the runnable to run if success stateerrorConsumer
- the consumer which accepts the error value- Returns:
- the original
VoidResult
unaltered - Throws:
NullPointerException
- if either the given runnable or consumer isnull
-
runIfSuccess
If in success state, runs the given runnable, otherwise does nothing.- Parameters:
runnable
- the runnable to run if success state- Returns:
- the original
VoidResult
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
VoidResult
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
VoidResult
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
VoidResult
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
VoidResult
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 originalVoidResult
is returned unaltered. If the supplier returns aVoidResult
in error state, aVoidResult
containing the error value is returned. If in error state, the originalVoidResult
is returned unaltered.- Parameters:
supplier
- the supplier to run- Returns:
- the original
VoidResult
unaltered if the given supplier returns success or the originalVoidResult
is in error state, otherwise aVoidResult
containing the error value from the supplier result - Throws:
NullPointerException
- if the given supplier isnull
or returnsnull
-
fold
public <N> N fold(Supplier<? extends N> valueSupplier, Function<? super E, ? extends N> errorFunction) Retrieve a value from thisVoidResult
by folding the states. If in success state, return the value provided by the value-supplier. 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:
valueSupplier
- supplier to provide the 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 either the given supplier or function isnull
-
orElseThrow
If in success state, does nothing, 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- Throws:
X
- if in error stateNullPointerException
- if the given function isnull
or returnsnull
-
toOptionalResult
Transforms thisVoidResult
to anOptionalResult
. If in success state, theOptionalResult
will be in empty success state. If in error state, theOptionalResult
will be in error state containing the error value from thisVoidResult
.The returned
OptionalResult
will never have a value.- Type Parameters:
N
- the type of the success value in the returnedOptionalResult
, inferred from the variable- Returns:
- an
OptionalResult
in empty success state or in error state containing the error value from thisVoidResult
-
handle
Handle the givenCheckedRunnable
. If theCheckedRunnable
executes successfully, theVoidResult
will be in success state. If theCheckedRunnable
throws an exception, theVoidResult
will be in error state containing the thrown exception. Note! A customCheckedRunnable
is used here instead ofRunnable
to allow handling of checked exceptions.- Parameters:
runnable
- theCheckedRunnable
to handle- Returns:
- a
VoidResult
either in success state, or in error state containing the exception thrown by theCheckedRunnable
- Throws:
NullPointerException
- if the given runnable isnull
-
handle
public static <E> VoidResult<E> handle(CheckedRunnable runnable, Function<Exception, E> exceptionMapper) Handle the givenCheckedRunnable
. If theCheckedRunnable
executes successfully, theVoidResult
will be in success state. If theCheckedRunnable
throws an exception, theVoidResult
will be in error state containing the result after mapping the exception with the given exception mapper function. Note! A customCheckedRunnable
is used here instead ofRunnable
to allow handling of checked exceptions.- Type Parameters:
E
- type of the error value after mapping a thrown exception- Parameters:
runnable
- theCheckedRunnable
to handle- Returns:
- a
VoidResult
either in success state, or in error state containing the result after mapping the exception thrown by theCheckedRunnable
- Throws:
NullPointerException
- if the given runnable isnull
or the given exception mapper function isnull
or returnsnull
-
equals
-
hashCode
public int hashCode() -
toString
-
runAlways(java.lang.Runnable)
instead for clarity