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.booleanstatic <E> VoidResult<E>error(E value) Returns aVoidResultin error state containing the given non-nullvalue as error value.flatRecover(Function<E, VoidResult<? extends E>> function) If in error state, returns theVoidResultfrom applying the given mapping function to the error value, otherwise returns the unalteredVoidResultin success state.flatReplace(Supplier<Result<? extends N, ? extends E>> supplier) If in success state, returns theResultprovided by the given supplier, otherwise returns aResultcontaining the error value of thisVoidResult.flatReplaceToBooleanResult(Supplier<BooleanResult<? extends E>> supplier) If in success state, returns theBooleanResultprovided by the given supplier, otherwise returns aBooleanResultcontaining the error value of thisVoidResult.<N> OptionalResult<N,E> flatReplaceToOptionalResult(Supplier<OptionalResult<? extends N, ? extends E>> supplier) If in success state, returns theOptionalResultprovided by the given supplier, otherwise returns anOptionalResultcontaining the error value of thisVoidResult.flatReplaceToVoidResult(Supplier<VoidResult<? extends E>> supplier) If in success state, returns theVoidResultprovided by the given supplier, otherwise returns the unalteredVoidResultin error state.flatRunIfSuccess(Supplier<? extends VoidResult<? extends E>> supplier) If in success state, runs the given supplier.<N> NRetrieve a value from thisVoidResultby 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.inthashCode()<N> VoidResult<N>If in error state, returns aVoidResultcontaining the result of applying the given mapping function to the error value, otherwise returns the unalteredVoidResultin 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 newVoidResultin success state, otherwise returns the unalteredVoidResultin success state.If in success state, returns aResultcontaining the value provided by the given supplier, otherwise returns aResultcontaining the error value of thisVoidResult.replaceWithBoolean(Supplier<Boolean> supplier) If in success state, returns aBooleanResultcontaining the boolean value provided by the given supplier, otherwise returns aBooleanResultcontaining the error value of thisVoidResult.<N> OptionalResult<N,E> replaceWithOptional(Supplier<Optional<? extends N>> supplier) If in success state, returns aOptionalResultcontaining the optional value provided by given supplier, otherwise returns aOptionalResultcontaining 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 aVoidResultin success state.<N> OptionalResult<N,E> Transforms thisVoidResultto anOptionalResult.toString()
-
Method Details
-
success
Returns aVoidResultin success state.- Type Parameters:
E- the type of the error value- Returns:
- a
VoidResultin success state
-
error
Returns aVoidResultin error state containing the given non-nullvalue as error value.- Type Parameters:
E- the type of the error value- Parameters:
value- the error value, which must be non-null- Returns:
- a
VoidResultin error state containing the given error value - Throws:
NullPointerException- if given error value isnull
-
mapError
If in error state, returns aVoidResultcontaining the result of applying the given mapping function to the error value, otherwise returns the unalteredVoidResultin 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
VoidResultcontaining the result of applying the mapping function to the error value of thisVoidResult, if in error state, otherwise the unalteredVoidResultin success state - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
replace
If in success state, returns aResultcontaining the value provided by the given supplier, otherwise returns aResultcontaining 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
Resultcontaining the value provided by the given supplier, if in success state, otherwise aResultcontaining the error value of thisVoidResult - Throws:
NullPointerException- if the given supplier isnullor returnsnull
-
replaceWithOptional
If in success state, returns aOptionalResultcontaining the optional value provided by given supplier, otherwise returns aOptionalResultcontaining the error value of thisVoidResult.- Type Parameters:
N- the type of the value which may be present in theOptionalprovided by the supplier- Parameters:
supplier- the supplier to provide the optional value if success state, may not benull- Returns:
- a
OptionalResultcontaining the optional value provided by the given supplier, if in success state, otherwise aOptionalResultcontaining the error value of thisVoidResult - Throws:
NullPointerException- if the given supplier isnullor returnsnull
-
replaceWithBoolean
If in success state, returns aBooleanResultcontaining the boolean value provided by the given supplier, otherwise returns aBooleanResultcontaining the error value of thisVoidResult.- Parameters:
supplier- the supplier to provide the boolean value if success state, may not benull- Returns:
- a
BooleanResultcontaining the boolean value provided by the given supplier, if in success state, otherwise aBooleanResultcontaining the error value of thisVoidResult - Throws:
NullPointerException- if the given supplier isnullor returnsnull
-
flatReplace
If in success state, returns theResultprovided by the given supplier, otherwise returns aResultcontaining the error value of thisVoidResult.- Type Parameters:
N- the type of success value which may be present in theResultprovided by the supplier- Parameters:
supplier- the supplier to provide theResult, if success state- Returns:
- the
Resultprovided by the supplier, if in success state, otherwise aResultcontaining the error value of thisVoidResult - Throws:
NullPointerException- if the given supplier isnullor returnsnull
-
flatReplaceToOptionalResult
public <N> OptionalResult<N,E> flatReplaceToOptionalResult(Supplier<OptionalResult<? extends N, ? extends E>> supplier) If in success state, returns theOptionalResultprovided by the given supplier, otherwise returns anOptionalResultcontaining the error value of thisVoidResult.- Type Parameters:
N- the type of success value which may be present in theOptionalResultprovided by the supplier- Parameters:
supplier- the supplier to provide theOptionalResult, if success state- Returns:
- the
OptionalResultprovided by the supplier, if in success state, otherwise anOptionalResultcontaining the error value of thisVoidResult - Throws:
NullPointerException- if the given supplier isnullor returnsnull
-
flatReplaceToBooleanResult
If in success state, returns theBooleanResultprovided by the given supplier, otherwise returns aBooleanResultcontaining the error value of thisVoidResult.- Parameters:
supplier- the supplier to provide theBooleanResult, if success state- Returns:
- the
BooleanResultprovided by the supplier, if in success state, otherwise aBooleanResultcontaining the error value of thisVoidResult - Throws:
NullPointerException- if the given supplier isnullor returnsnull
-
flatReplaceToVoidResult
If in success state, returns theVoidResultprovided by the given supplier, otherwise returns the unalteredVoidResultin error state.- Parameters:
supplier- the supplier to provide theVoidResult, if success state- Returns:
- the
VoidResultprovided by the supplier, if in success state, otherwise the unalteredVoidResultin error state - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
recover
If in error state, returns a newVoidResultin success state, otherwise returns the unalteredVoidResultin success state.- Returns:
- a
VoidResultin success state
-
flatRecover
If in error state, returns theVoidResultfrom applying the given mapping function to the error value, otherwise returns the unalteredVoidResultin success state.- Parameters:
function- the mapping function to apply to the error value to convert to a newVoidResult, if error state- Returns:
- the
VoidResultreturned from the mapping function, if in error state, otherwise the unalteredVoidResultin 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
VoidResultunaltered - 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
VoidResultunaltered - 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
VoidResultunaltered - 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
VoidResultunaltered - 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
VoidResultunaltered - 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
VoidResultunaltered - 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
VoidResultunaltered - Throws:
NullPointerException- if the given runnable isnull
-
flatRunIfSuccess
If in success state, runs the given supplier. If the supplier returns aVoidResultin success state, the originalVoidResultis returned unaltered. If the supplier returns aVoidResultin error state, aVoidResultcontaining the error value is returned. If in error state, the originalVoidResultis returned unaltered.- Parameters:
supplier- the supplier to run- Returns:
- the original
VoidResultunaltered if the given supplier returns success or the originalVoidResultis in error state, otherwise aVoidResultcontaining the error value from the supplier result - Throws:
NullPointerException- if the given supplier isnullor returnsnull
-
fold
public <N> N fold(Supplier<? extends N> valueSupplier, Function<? super E, ? extends N> errorFunction) Retrieve a value from thisVoidResultby 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 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 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 isnullor returnsnull
-
toOptionalResult
Transforms thisVoidResultto anOptionalResult. If in success state, theOptionalResultwill be in empty success state. If in error state, theOptionalResultwill be in error state containing the error value from thisVoidResult.The returned
OptionalResultwill never have a value.- Type Parameters:
N- the type of the success value in the returnedOptionalResult, inferred from the variable- Returns:
- an
OptionalResultin empty success state or in error state containing the error value from thisVoidResult
-
handle
Handle the givenCheckedRunnable. If theCheckedRunnableexecutes successfully, theVoidResultwill be in success state. If theCheckedRunnablethrows an exception, theVoidResultwill be in error state containing the thrown exception. Note! A customCheckedRunnableis used here instead ofRunnableto allow handling of checked exceptions.- Parameters:
runnable- theCheckedRunnableto handle- Returns:
- a
VoidResulteither 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 theCheckedRunnableexecutes successfully, theVoidResultwill be in success state. If theCheckedRunnablethrows an exception, theVoidResultwill be in error state containing the result after mapping the exception with the given exception mapper function. Note! A customCheckedRunnableis used here instead ofRunnableto allow handling of checked exceptions.- Type Parameters:
E- type of the error value after mapping a thrown exception- Parameters:
runnable- theCheckedRunnableto handle- Returns:
- a
VoidResulteither in success state, or in error state containing the result after mapping the exception thrown by theCheckedRunnable - Throws:
NullPointerException- if the given runnable isnullor the given exception mapper function isnullor returnsnull
-
equals
-
hashCode
public int hashCode() -
toString
-
runAlways(java.lang.Runnable)instead for clarity