Package no.gorandalum.fluentresult
Class BooleanResult<E>
java.lang.Object
no.gorandalum.fluentresult.BooleanResult<E>
- Type Parameters:
E- the type of the error value
A result object which either is in success state containing a
non-
null boolean value, or in error state containing a
non-null error value.
A variable whose type is BooleanResult should never itself be
null, it should always point to an BooleanResult instance.
-
Method Summary
Modifier and TypeMethodDescriptionIf in success state, applies the boolean success value to the given consumer, otherwise does nothing.consumeEither(Runnable trueRunnable, Runnable falseRunnable, Consumer<? super E> errorConsumer) If in success state with a success value oftrue, runs the given true-runnable.consumeEither(Consumer<Boolean> valueConsumer, Consumer<? super E> errorConsumer) If in success state, applies the boolean 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 <E> BooleanResult<E>error(E value) Returns aBooleanResultin error state containing the given non-nullvalue as error value.flatConsume(Function<Boolean, ? extends VoidResult<? extends E>> function) If in success state, applies the boolean success value to the given function.If in success state, returns theResultfrom applying the given mapping function to the boolean success value, otherwise returns aResultcontaining the error value of thisBooleanResult.flatMapToBooleanResult(Function<Boolean, BooleanResult<? extends E>> function) If in success state, returns theBooleanResultfrom applying the given mapping function to the boolean success value, otherwise returns the unalteredBooleanResultin error state.<N> OptionalResult<N,E> flatMapToOptionalResult(Function<Boolean, OptionalResult<? extends N, ? extends E>> function) If in success state, returns theOptionalResultfrom applying the given mapping function to the boolean success value, otherwise returns aOptionalResultcontaining the error value of thisBooleanResult.flatMapToVoidResult(Function<Boolean, VoidResult<? extends E>> function) If in success state, returns theVoidResultfrom applying the given mapping function to the boolean success value, otherwise returns aVoidResultcontaining the error value of thisBooleanResult.flatRecover(Function<E, BooleanResult<? extends E>> function) If in error state, returns theBooleanResultfrom applying the given mapping function to the error value, otherwise returns the unalteredBooleanResultin success state.flatRunIfSuccess(Supplier<? extends VoidResult<? extends E>> supplier) If in success state, runs the given supplier.<N> NRetrieve a value from thisBooleanResultby folding the states.<N> Nfold(Supplier<? extends N> trueSupplier, Supplier<? extends N> falseSupplier, Function<? super E, ? extends N> errorFunction) Retrieve a value from thisBooleanResultby folding the states.static BooleanResult<Exception>Handle the givenCallable.static <E> BooleanResult<E>Handle the givenCallable.inthashCode()If in success state, returns aResultcontaining the result of applying the given mapping function to the boolean success value, otherwise returns aResultcontaining the error value of thisBooleanResult.<N> BooleanResult<N>If in error state, returns aBooleanResultcontaining the result of applying the given mapping function to the error value, otherwise returns the unalteredBooleanResultin success state.mapToBoolean(Function<Boolean, Boolean> function) If in success state, returns aBooleanResultcontaining the result of applying the given mapping function to the success value, otherwise returns the unalteredBooleanResultin error state.<N> OptionalResult<N,E> mapToOptional(Function<Boolean, ? extends Optional<? extends N>> function) If in success state, returns aOptionalResultcontaining the result of applying the given mapping function to the boolean success value, otherwise returns aOptionalResultcontaining the error value of thisBooleanResult.If in success state, returns the boolean success value, otherwise returnsother.booleanIf in success state, returns the boolean success value, otherwise returnsfalse.If in success state, returns the boolean success value, otherwise returns the value returned from the given function.orElseThrow(Function<? super E, ? extends X> function) If in success state, returns the boolean success value, otherwise throws the exception returned by the given function.booleanIf in success state, returns the boolean success value, otherwise returnstrue.If in error state, returns aBooleanResultwith the success value from applying the given mapping function to the error value, otherwise returns the unalteredBooleanResultin success state.Deprecated.Runs the given runnable, no matter the state.If in success state, runs the given success runnable.If in success state with a success value oftrue, runs the given true-runnable.runIfError(Runnable runnable) If in error state, runs the given runnable, otherwise does nothing.runIfFalse(Runnable runnable) If in success state with a success value offalse, runs the given runnable, otherwise does nothing.runIfSuccess(Runnable runnable) If in success state, runs the given runnable, otherwise does nothing.If in success state with a success value oftrue, runs the given runnable, otherwise does nothing.static <E> BooleanResult<E>success(boolean value) Returns aBooleanResultin success state containing the given non-nullboolean value as success value.static <E> BooleanResult<E>Returns aBooleanResultin success state containingfalseas the boolean success value.static <E> BooleanResult<E>Returns aBooleanResultin success state containingtrueas the boolean success value.Transforms thisBooleanResultto anOptionalResult.toString()Transforms thisBooleanResultto aVoidResult.verify(Function<Boolean, ? extends VoidResult<? extends E>> function) If in success state, verifies the success value of thisBooleanResultby mapping it to aVoidResult.If in success state, verifies the boolean success value of thisBooleanResultby testing it with the given predicate.
-
Method Details
-
success
Returns aBooleanResultin success state containing the given non-nullboolean value as success value.- Type Parameters:
E- the type of the error value- Parameters:
value- the boolean success value, which must be non-null- Returns:
- a
BooleanResultin success state containing the given boolean success value - Throws:
NullPointerException- if given success value isnull
-
successTrue
Returns aBooleanResultin success state containingtrueas the boolean success value.- Type Parameters:
E- the type of the error value- Returns:
- a
BooleanResultin success state containingtrueas the boolean success value.
-
successFalse
Returns aBooleanResultin success state containingfalseas the boolean success value.- Type Parameters:
E- the type of the error value- Returns:
- a
BooleanResultin success state containingfalseas the boolean success value.
-
error
Returns aBooleanResultin 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
BooleanResultin 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 boolean success value, otherwise returns aResultcontaining the error value of thisBooleanResult.- Type Parameters:
N- the type of the value returned from the mapping function- Parameters:
function- the mapping function to apply to the boolean success value, if success state- Returns:
- a
Resultcontaining the result of applying the mapping function to the boolean success value of thisBooleanResult, if in success state, otherwise aResultcontaining the error value of thisBooleanResult - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
mapToOptional
public <N> OptionalResult<N,E> mapToOptional(Function<Boolean, ? extends Optional<? extends N>> function) If in success state, returns aOptionalResultcontaining the result of applying the given mapping function to the boolean success value, otherwise returns aOptionalResultcontaining the error value of thisBooleanResult.- 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 boolean success value, if success state- Returns:
- a
OptionalResultcontaining the result of applying the mapping function to the boolean success value of thisBooleanResult, if in success state, otherwise aOptionalResultcontaining the error value of thisBooleanResult - 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 the unalteredBooleanResultin error state.- Parameters:
function- the mapping function to apply to the boolean success value, if success state- Returns:
- a
BooleanResultcontaining the result of applying the mapping function to the boolean success value of thisBooleanResult, if in success state, otherwise the unalteredBooleanResultin error state - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
mapError
If in error state, returns aBooleanResultcontaining the result of applying the given mapping function to the error value, otherwise returns the unalteredBooleanResultin 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
BooleanResultcontaining the result of applying the mapping function to the error value of thisBooleanResult, if in error state, otherwise the unalteredBooleanResultin success state - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
flatMap
If in success state, returns theResultfrom applying the given mapping function to the boolean success value, otherwise returns aResultcontaining the error value of thisBooleanResult.- 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 boolean success value, if success state- Returns:
- the
Resultreturned from the mapping function, if in success state, otherwise aResultcontaining the error value of thisBooleanResult - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
flatMapToOptionalResult
public <N> OptionalResult<N,E> flatMapToOptionalResult(Function<Boolean, OptionalResult<? extends N, ? extends E>> function) If in success state, returns theOptionalResultfrom applying the given mapping function to the boolean success value, otherwise returns aOptionalResultcontaining the error value of thisBooleanResult.- 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 boolean success value, if success state- Returns:
- the
OptionalResultreturned from the mapping function, if in success state, otherwise aOptionalResultcontaining the error value of thisBooleanResult - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
flatMapToBooleanResult
public BooleanResult<E> flatMapToBooleanResult(Function<Boolean, BooleanResult<? extends E>> function) If in success state, returns theBooleanResultfrom applying the given mapping function to the boolean success value, otherwise returns the unalteredBooleanResultin error state.- Parameters:
function- the mapping function to apply to the boolean success value, if success state- Returns:
- the
BooleanResultreturned from the mapping function, if in success state, otherwise the unalteredBooleanResultin error state - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
flatMapToVoidResult
If in success state, returns theVoidResultfrom applying the given mapping function to the boolean success value, otherwise returns aVoidResultcontaining the error value of thisBooleanResult.- Parameters:
function- the mapping function to apply to the boolean success value, if success state- Returns:
- the
VoidResultreturned from the mapping function, if in success state, otherwise aVoidResultcontaining the error value of thisBooleanResult - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
recover
If in error state, returns aBooleanResultwith the success value from applying the given mapping function to the error value, otherwise returns the unalteredBooleanResultin 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
BooleanResultcontaining the value from the mapping function, if in error state, otherwise the unalteredBooleanResultin success state
-
flatRecover
If in error state, returns theBooleanResultfrom applying the given mapping function to the error value, otherwise returns the unalteredBooleanResultin success state.- Parameters:
function- the mapping function to apply to the error value to convert to a newBooleanResult, if error stateBooleanResultreturned by the mapping function- Returns:
- the
BooleanResultreturned from the mapping function, if in error state, otherwise the unalteredBooleanResultin success state
-
consume
If in success state, applies the boolean success value to the given consumer, otherwise does nothing.- Parameters:
consumer- the consumer which accepts the boolean success value- Returns:
- the original
BooleanResultunaltered - 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
BooleanResultunaltered - Throws:
NullPointerException- if the given consumer isnull
-
consumeEither
public BooleanResult<E> consumeEither(Consumer<Boolean> valueConsumer, Consumer<? super E> errorConsumer) If in success state, applies the boolean 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 boolean success valueerrorConsumer- the consumer which accepts the error value- Returns:
- the original
BooleanResultunaltered - Throws:
NullPointerException- if one of the given consumers isnull
-
consumeEither
public BooleanResult<E> consumeEither(Runnable trueRunnable, Runnable falseRunnable, Consumer<? super E> errorConsumer) If in success state with a success value oftrue, runs the given true-runnable. If in success state with a success value offalse, runs the given false-runnable. If in error state, applies the error value to the given error consumer.- Parameters:
trueRunnable- the runnable to run if a success value oftruefalseRunnable- the runnable to run if a success value offalseerrorConsumer- the consumer which accepts the error value- Returns:
- the original
BooleanResultunaltered - Throws:
NullPointerException- if one of the given runnables or consumer isnull
-
flatConsume
If in success state, applies the boolean success value to the given function. If the function returns aVoidResultin success state, the originalBooleanResultis returned unaltered. If the function returns aVoidResultin error state, aBooleanResultcontaining the error value is returned. If in error state, the originalBooleanResultis returned unaltered.- Parameters:
function- the function which accepts the boolean success value- Returns:
- the original
BooleanResultunaltered if the given function returns success or the originalBooleanResultis in error state, otherwise aBooleanResultcontaining 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
BooleanResultunaltered - Throws:
NullPointerException- if the given runnable isnull
-
runIfTrue
If in success state with a success value oftrue, runs the given runnable, otherwise does nothing.- Parameters:
runnable- the runnable to run if a success value oftrue- Returns:
- the original
BooleanResultunaltered - Throws:
NullPointerException- if the given runnable isnull
-
runIfFalse
If in success state with a success value offalse, runs the given runnable, otherwise does nothing.- Parameters:
runnable- the runnable to run if a success value offalse- Returns:
- the original
BooleanResultunaltered - 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
BooleanResultunaltered - 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
BooleanResultunaltered - Throws:
NullPointerException- if one of the given runnables isnull
-
runEither
public BooleanResult<E> runEither(Runnable trueRunnable, Runnable falseRunnable, Runnable errorRunnable) If in success state with a success value oftrue, runs the given true-runnable. If in success state with a success value offalse, runs the given false-runnable. If in error state, runs the given error-runnable.- Parameters:
trueRunnable- the runnable to run if a success value oftruefalseRunnable- the runnable to run if a success value offalseerrorRunnable- the runnable to run if error state- Returns:
- the original
BooleanResultunaltered - 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
BooleanResultunaltered - 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
BooleanResultunaltered - Throws:
NullPointerException- if the given runnable isnull
-
flatRunIfSuccess
If in success state, runs the given supplier. If the supplier returns aVoidResultin success state, the originalBooleanResultis returned unaltered. If the supplier returns aVoidResultin error state, aBooleanResultcontaining the error value is returned. If in error state, the originalBooleanResultis returned unaltered.- Parameters:
supplier- the supplier to run- Returns:
- the original
BooleanResultunaltered if the given supplier returns success or the originalBooleanResultis in error state, otherwise aBooleanResultcontaining the error value from the supplier result - Throws:
NullPointerException- if the given supplier isnullor returnsnull
-
verify
If in success state, verifies the boolean success value of thisBooleanResultby testing it with the given predicate. If the predicate evaluates to false, a newBooleanResultis returned containing the error value provided by the given error supplier. If the predicate evaluates to true, or theBooleanResultalready was in error state, the originalBooleanResultis returned unaltered.- Parameters:
predicate- the predicate used to verify the boolean success value, if success stateerrorSupplier- supplier providing the error if predicate evaluates to false- Returns:
- the original
BooleanResultunaltered, unless the predicate evaluates to false, then a newBooleanResultin 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 thisBooleanResultby mapping it to aVoidResult. If the returnedVoidResultis in error state, a newBooleanResultis returned containing the error value of theVoidResult. If theVoidResultis in success state, or theBooleanResultalready was in error state, the originalBooleanResultis returned unaltered.- Parameters:
function- the function applied to the success value, if success state- Returns:
- the original
BooleanResultunaltered, unless theVoidResultreturned by the mapping function is in error state, then a newBooleanResultin error state is returned containing the error value from theVoidResult - Throws:
NullPointerException- if the given function isnullor returnsnull
-
fold
public <N> N fold(Function<Boolean, ? extends N> valueFunction, Function<? super E, ? extends N> errorFunction) Retrieve a value from thisBooleanResultby folding the states. If in success state, return the value of applying the value function to the boolean success value. If in error state, return the value of applying the error function to the error value.- Type Parameters:
N- the type of the retrieved value- Parameters:
valueFunction- the mapping function to apply to the boolean 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
-
fold
public <N> N fold(Supplier<? extends N> trueSupplier, Supplier<? extends N> falseSupplier, Function<? super E, ? extends N> errorFunction) Retrieve a value from thisBooleanResultby folding the states. If in success state with a success value oftrue, return the value provided by the true-supplier. If in success state with a success value offalse, return the value provided by the false-supplier. If in error state, return the value of applying the error function to the error value.- Type Parameters:
N- the type of the retrieved value- Parameters:
trueSupplier- the supplier to provide the value if a success value oftrue, may returnnullfalseSupplier- the supplier to provide the value if a success value offalse, 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 or the supplier isnull
-
orElse
If in success state, returns the boolean success value, otherwise returnsother.- Parameters:
other- the value to be returned, if not in success state, may benull- Returns:
- the boolean success value, if success state, otherwise
other
-
orElseTrue
public boolean orElseTrue()If in success state, returns the boolean success value, otherwise returnstrue.- Returns:
- the boolean success value, if success state, otherwise
true
-
orElseFalse
public boolean orElseFalse()If in success state, returns the boolean success value, otherwise returnsfalse.- Returns:
- the boolean success value, if success state, otherwise
false
-
orElseGet
If in success state, returns the boolean 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 boolean 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 boolean 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 boolean success value, if success state
- Throws:
X- if in error stateNullPointerException- if the given function isnullor returnsnull
-
toOptionalResult
Transforms thisBooleanResultto anOptionalResult. If in success state, theOptionalResultwill be in success state containing the boolean success value from thisBooleanResult. If in error state, theOptionalResultwill be in error state containing the error value from thisBooleanResult.The returned
OptionalResultwill never be empty.- Returns:
- an
OptionalResultin success state containing the boolean success value from thisBooleanResultor in error state containing the error value from thisBooleanResult
-
toVoidResult
Transforms thisBooleanResultto aVoidResult. If in success state, theVoidResultwill be in success state. If in error state, theVoidResultwill be in error state containing the error value from thisBooleanResult.- Returns:
- a
VoidResulteither in success state or in error state containing the error value from thisBooleanResult
-
handle
Handle the givenCallable. If theCallableexecutes successfully, theBooleanResultwill be in success state containing the returned value. If theCallablethrows an exception, theBooleanResultwill be in error state containing the thrown exception.- Parameters:
callable- theCallableto handle- Returns:
- a
BooleanResulteither 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 <E> BooleanResult<E> handle(Callable<Boolean> callable, Function<Exception, E> exceptionMapper) Handle the givenCallable. If theCallableexecutes successfully, theBooleanResultwill be in success state containing the returned value. If theCallablethrows an exception, theBooleanResultwill be in error state containing the result after mapping the exception with the given exception mapper function.- Type Parameters:
E- type of the error value after mapping a thrown exception- Parameters:
callable- theCallableto handle- Returns:
- a
BooleanResulteither 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