Class OptionalResult<T,E>
- Type Parameters:
T- the type of the success valueE- the type of the error value
null success value or be empty, or it may be in error state
containing a non-null error value.
A variable whose type is OptionalResult should never itself be
null, it should always point to an OptionalResult instance.
-
Method Summary
Modifier and TypeMethodDescriptionIf in success state, applies the optional success value to the given consumer, otherwise does nothing.consumeEither(Consumer<? super T> valueConsumer, Runnable emptyRunnable, Consumer<? super E> errorConsumer) If in success state with a success value, applies the success value to the given value consumer.If in success state, applies the optional success value to the given success consumer.consumeError(Consumer<? super E> errorConsumer) If in error state, applies the error value to the given consumer, otherwise does nothing.consumeValue(Consumer<T> consumer) If in success state with a success value, applies the success value to the given consumer, otherwise does nothing.static <T,E> OptionalResult<T, E> empty()Returns anOptionalResultin success state, which is empty with no success value.booleanstatic <T,E> OptionalResult<T, E> error(E value) Returns anOptionalResultin error state containing the given non-nullvalue as error value.flatConsume(Function<Optional<T>, ? extends VoidResult<? extends E>> function) If in success state, applies the optional success value to the given function.flatConsumeValue(Function<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 optional success value, otherwise returns aResultcontaining the error value of thisOptionalResult.flatMapToBooleanResult(Function<Optional<? extends T>, BooleanResult<? extends E>> function) If in success state, returns theBooleanResultfrom applying the given mapping function to the optional success value, otherwise returns aBooleanResultcontaining the error value of thisOptionalResult.<N> OptionalResult<N,E> flatMapToOptionalResult(Function<Optional<T>, OptionalResult<? extends N, ? extends E>> function) If in success state, returns theOptionalResultfrom applying the given mapping function to the optional success value, otherwise returns the unalteredOptionalResultin error state.flatMapToVoidResult(Function<Optional<? extends T>, VoidResult<? extends E>> function) If in success state, returns theVoidResultfrom applying the given mapping function to the optional success value, otherwise returns aVoidResultcontaining the error value of thisOptionalResult.flatMapValueWithBooleanResult(Function<? super T, BooleanResult<? extends E>> function) If in success state with a success value, returns anOptionalResultfrom applying the given mapping function to the success value, otherwise returns the unalteredOptionalResultwhich may be empty or in error state.<N> OptionalResult<N,E> flatMapValueWithOptionalResult(Function<? super T, OptionalResult<? extends N, ? extends E>> function) If in success state with a success value, returns theOptionalResultfrom applying the given mapping function to the success value, otherwise returns the unalteredOptionalResultwhich may be empty or in error state.<N> OptionalResult<N,E> flatMapValueWithResult(Function<? super T, Result<? extends N, ? extends E>> function) If in success state with a success value, returns anOptionalResultfrom applying the given mapping function to the success value, otherwise returns the unalteredOptionalResultwhich may be empty or in error state.<N> OptionalResult<N,E> flatRecover(Function<E, OptionalResult<? extends N, ? extends E>> function) If in error state, returns theOptionalResultfrom applying the given mapping function to the error value, otherwise returns the unalteredOptionalResultin success state.<N> OptionalResult<N,E> flatReplaceEmpty(Supplier<OptionalResult<N, E>> supplier) If in empty success state, returns theOptionalResultfrom applying the given supplier, otherwise returns the unalteredOptionalResultin success state with value or error state.flatReplaceEmptyWithResult(Supplier<Result<N, E>> supplier) If in empty success state, returns theResultfrom applying the given supplier, otherwise returns aResultwith the existing success value or error value.flatRunIfSuccess(Supplier<? extends VoidResult<? extends E>> supplier) If in success state, runs the given supplier.flatRunIfValue(Supplier<? extends VoidResult<? extends E>> supplier) If in success state with a success value, runs the given supplier.<N> Nfold(Function<? super T, ? extends N> valueFunction, Supplier<? extends N> emptySupplier, Function<? super E, ? extends N> errorFunction) Retrieve a value from thisOptionalResultby folding the states.<N> Nfold(Function<Optional<T>, ? extends N> successFunction, Function<? super E, ? extends N> errorFunction) Retrieve a value from thisOptionalResultby folding the states.static <T> OptionalResult<T,Exception> Handle the givenCallable.static <T,E> OptionalResult<T, E> Handle the givenCallable.inthashCode()If in success state, returns aResultcontaining the result of applying the given mapping function to the optional success value of thisOptionalResult, otherwise returns aResultcontaining the error value of thisOptionalResult.<N> OptionalResult<T,N> If in error state, returns aOptionalResultcontaining the result of applying the given mapping function to the error value, otherwise returns the unalteredOptionalResultin success state.mapToBoolean(Function<Optional<T>, Boolean> function) If in success state, returns aBooleanResultcontaining the result of applying the given mapping function to the optional success value, otherwise returns aBooleanResultcontaining the error value of thisOptionalResult.<N> OptionalResult<N,E> mapToOptional(Function<Optional<T>, ? extends Optional<? extends N>> function) If in success state, returns aOptionalResultcontaining the result of applying the given mapping function to the optional success value, otherwise returns the unalteredOptionalResultin error state.<N> OptionalResult<N,E> If in success state with a success value, returns anOptionalResultcontaining the result of applying the given mapping function to the success value, otherwise returns the unalteredOptionalResultwhich may be empty or in error state.<N> OptionalResult<N,E> mapValueToOptional(Function<? super T, Optional<N>> function) If in success state with a success value, returns anOptionalResultcontaining the result of applying the given mapping function to the success value, otherwise returns the unalteredOptionalResultwhich may be empty or in error state.If in success state, returns the optional success value, otherwise returnsother.If in success state, returns the optional success value, otherwise returns the value returned from the given function.orElseThrow(Function<? super E, ? extends X> function) If in success state, returns the optional success value, otherwise throws the exception returned by the given function.If in error state, returns aOptionalResultwith the success value from applying the given mapping function to the error value, otherwise returns the unalteredOptionalResultin 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, runs the given value runnable.runIfEmpty(Runnable runnable) If in empty success state with no success value, runs the given runnable, otherwise does nothing.runIfError(Runnable runnable) If in error state, runs the given runnable, otherwise does nothing.runIfNoValue(Runnable runnable) If in empty success state or error state, runs the given runnable, otherwise does nothing.runIfSuccess(Runnable runnable) If in success state, runs the given runnable, otherwise does nothing.runIfValue(Runnable runnable) If in success state with a success value, runs the given runnable, otherwise does nothing.static <T,E> OptionalResult<T, E> Returns anOptionalResultin success state containing the given non-nullvalue as success value.static <T,E> OptionalResult<T, E> success(T value) Returns anOptionalResultin success state containing the given non-nullvalue as success value.static <T,E> OptionalResult<T, E> successNullable(T value) Returns anOptionalResultin success state either containing the given value as success value, or empty if the given value is null.Transforms thisOptionalResultto aResult.toString()Transforms thisOptionalResultto aVoidResult.valueOrElse(T other) If in success state with a success value, returns the success value, otherwise returnsother.valueOrElseGet(Supplier<? extends T> supplier) If in success state with a success value, returns the success value, otherwise returns the value returned from the given function.valueOrElseThrow(Supplier<? extends X> supplier) If in success state with a success value, returns the success value, otherwise throws the exception returned by the given function.If in success state, verifies the success value of thisOptionalResultby mapping it to aVoidResult.If in success state, verifies the optional success value of thisOptionalResultby testing it with the given predicate.verifyValue(Function<? super T, ? extends VoidResult<? extends E>> function) If in non-empty success state, verifies the success value of thisOptionalResultby mapping it to aVoidResult.verifyValue(Predicate<? super T> predicate, Supplier<? extends E> errorSupplier) If in success state with a success value, verifies the success value of thisOptionalResultby testing it with the given predicate.
-
Method Details
-
success
Returns anOptionalResultin success state containing the given non-nullvalue as success value.- Type Parameters:
T- the type of the success value which may be present in the givenOptionalE- the type of the error value- Parameters:
maybeValue- anOptionalwhich may contain an success value, or may be empty- Returns:
- an
OptionalResultin success state which either contains a success value or is empty - Throws:
NullPointerException- if givenOptionalisnull
-
success
Returns anOptionalResultin 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:
- an
OptionalResultin success state containing the given success value - Throws:
NullPointerException- if given success value isnull
-
successNullable
Returns anOptionalResultin success state either containing the given value as success value, or empty if the given value is null.- Type Parameters:
T- the type of the success valueE- the type of the error value- Parameters:
value- the success value, which may benull- Returns:
- an
OptionalResultin success state containing the given success value if not null, otherwise an emptyOptionalResult
-
empty
Returns anOptionalResultin success state, which is empty with no success value.- Type Parameters:
T- the type of the success valueE- the type of the error value- Returns:
- an empty
OptionalResultin success state
-
error
Returns anOptionalResultin 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:
- an
OptionalResultin 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 optional success value of thisOptionalResult, otherwise returns aResultcontaining the error value of thisOptionalResult.- Type Parameters:
N- the type of the value returned from the mapping function- Parameters:
function- the mapping function to apply to the optional success value, if success state- Returns:
- a
Resultcontaining the result of applying the mapping function to the optional success value of thisOptionalResult, if in success state, otherwise aResultcontaining the error value of thisOptionalResult - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
mapToOptional
public <N> OptionalResult<N,E> mapToOptional(Function<Optional<T>, ? extends Optional<? extends N>> function) If in success state, returns aOptionalResultcontaining the result of applying the given mapping function to the optional success value, otherwise returns the unalteredOptionalResultin error state.- 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 optional success value, if success state- Returns:
- a
OptionalResultcontaining the result of applying the mapping function to the optional success value of thisOptionalResult, if in success state, otherwise the unalteredOptionalResultin error state - 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 optional success value, otherwise returns aBooleanResultcontaining the error value of thisOptionalResult.- Parameters:
function- the mapping function to apply to the optional success value, if success state- Returns:
- a
BooleanResultcontaining the result of applying the mapping function to the optional success value of thisOptionalResult, if in success state, otherwise aBooleanResultcontaining the error value of thisOptionalResult - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
mapError
If in error state, returns aOptionalResultcontaining the result of applying the given mapping function to the error value, otherwise returns the unalteredOptionalResultin 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
OptionalResultcontaining the result of applying the mapping function to the error value of thisOptionalResult, if in error state, otherwise the unalteredOptionalResultin success state - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
mapValue
If in success state with a success value, returns anOptionalResultcontaining the result of applying the given mapping function to the success value, otherwise returns the unalteredOptionalResultwhich may be empty or in error state.If the given mapping function returns null, then the returned
OptionalResultwill be empty.- 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 with a success value- Returns:
- an
OptionalResultcontaining the result of applying the mapping function to the success value of thisOptionalResult, if in success state with a success value, otherwise the unalteredOptionalResultwhich may be empty or in error state - Throws:
NullPointerException- if the given mapping function isnull
-
mapValueToOptional
If in success state with a success value, returns anOptionalResultcontaining the result of applying the given mapping function to the success value, otherwise returns the unalteredOptionalResultwhich may be empty or in error state.If the given mapping function returns a non-empty
Optional, then the returnedOptionalResultwill contain theOptionalcontent as success value. If the given mapping function returns an emptyOptional, then the returnedOptionalResultwill also be empty.- 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 with a success value- Returns:
- an
OptionalResultcontaining the result of applying the mapping function to the success value of thisOptionalResult, if in success state with a success value, otherwise the unalteredOptionalResultwhich may be empty or in error state - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
flatMap
If in success state, returns theResultfrom applying the given mapping function to the optional success value, otherwise returns aResultcontaining the error value of thisOptionalResult.- 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 optional success value, if success state- Returns:
- the
Resultreturned from the mapping function, if in success state, otherwise aResultcontaining the error value of thisOptionalResult - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
flatMapToOptionalResult
public <N> OptionalResult<N,E> flatMapToOptionalResult(Function<Optional<T>, OptionalResult<? extends N, ? extends E>> function) If in success state, returns theOptionalResultfrom applying the given mapping function to the optional success value, otherwise returns the unalteredOptionalResultin error state.- 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 optional success value, if success state- Returns:
- the
OptionalResultreturned from the mapping function, if in success state, otherwise the unalteredOptionalResultin error state - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
flatReplaceEmpty
If in empty success state, returns theOptionalResultfrom applying the given supplier, otherwise returns the unalteredOptionalResultin success state with value or error state.- Type Parameters:
N- the type of success value which may be present in theOptionalResultreturned by this function.- Parameters:
supplier- the supplier to call if in empty success state.- Returns:
- the
OptionalResultreturned from the supplier, if in empty state, otherwise the unalteredOptionalResultin success state with value or error state - Throws:
NullPointerException- if the given supplier isnullor returnsnull
-
flatReplaceEmptyWithResult
If in empty success state, returns theResultfrom applying the given supplier, otherwise returns aResultwith the existing success value or error value.- Type Parameters:
N- the type of success value which may be present in theResultreturned by this function.- Parameters:
supplier- the supplier to call if in empty success state.- Returns:
- the
Resultreturned from the supplier, if in empty state, otherwise aResultwith the existing success value or error value. - Throws:
NullPointerException- if the given supplier isnullor returnsnull
-
flatMapToBooleanResult
public BooleanResult<E> flatMapToBooleanResult(Function<Optional<? extends T>, BooleanResult<? extends E>> function) If in success state, returns theBooleanResultfrom applying the given mapping function to the optional success value, otherwise returns aBooleanResultcontaining the error value of thisOptionalResult.- Parameters:
function- the mapping function to apply to the optional success value, if success state- Returns:
- the
BooleanResultreturned from the mapping function, if in success state, otherwise aBooleanResultcontaining the error value of thisOptionalResult - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
flatMapToVoidResult
public VoidResult<E> flatMapToVoidResult(Function<Optional<? extends T>, VoidResult<? extends E>> function) If in success state, returns theVoidResultfrom applying the given mapping function to the optional success value, otherwise returns aVoidResultcontaining the error value of thisOptionalResult.- Parameters:
function- the mapping function to apply to the optional success value, if success state- Returns:
- the
VoidResultreturned from the mapping function, if in success state, otherwise aVoidResultcontaining the error value of thisOptionalResult - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
flatMapValueWithResult
public <N> OptionalResult<N,E> flatMapValueWithResult(Function<? super T, Result<? extends N, ? extends E>> function) If in success state with a success value, returns anOptionalResultfrom applying the given mapping function to the success value, otherwise returns the unalteredOptionalResultwhich may be empty or in error state.If the
Resultreturned from the mapping function is in success state. the returnedOptionalResultwill contain the success value from theResult. If theResultis in error state, the returnedOptionalResultwill contain the error value from theResult.- Type Parameters:
N- the type of success value which may be present in theOptionalResultafter applying the mapping function- Parameters:
function- the mapping function to apply to the success value, if success state with a success value- Returns:
- an
OptionalResultafter applying the mapping function, if in success state with a success value, otherwise the unalteredOptionalResultwhich may be empty or in error state - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
flatMapValueWithOptionalResult
public <N> OptionalResult<N,E> flatMapValueWithOptionalResult(Function<? super T, OptionalResult<? extends N, ? extends E>> function) If in success state with a success value, returns theOptionalResultfrom applying the given mapping function to the success value, otherwise returns the unalteredOptionalResultwhich may be empty or in error state.- 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 with a success value- Returns:
- the
OptionalResultreturned from the mapping function, if in success state with a success value, otherwise the unalteredOptionalResultwhich may be empty or in error state - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
flatMapValueWithBooleanResult
public OptionalResult<Boolean,E> flatMapValueWithBooleanResult(Function<? super T, BooleanResult<? extends E>> function) If in success state with a success value, returns anOptionalResultfrom applying the given mapping function to the success value, otherwise returns the unalteredOptionalResultwhich may be empty or in error state.If the
BooleanResultreturned from the mapping function is in success state. the returnedOptionalResultwill contain the boolean success value from theBooleanResult. If theBooleanResultis in error state, the returnedOptionalResultwill contain the error value from theBooleanResult.- Parameters:
function- the mapping function to apply to the success value, if success state with a success value- Returns:
- an
OptionalResultafter applying the mapping function, if in success state with a success value, otherwise the unalteredOptionalResultwhich may be empty or in error state - Throws:
NullPointerException- if the given mapping function isnullor returnsnull
-
recover
If in error state, returns aOptionalResultwith the success value from applying the given mapping function to the error value, otherwise returns the unalteredOptionalResultin 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
OptionalResultcontaining the value from the mapping function, if in error state, otherwise the unalteredOptionalResultin success state
-
flatRecover
public <N> OptionalResult<N,E> flatRecover(Function<E, OptionalResult<? extends N, ? extends E>> function) If in error state, returns theOptionalResultfrom applying the given mapping function to the error value, otherwise returns the unalteredOptionalResultin success state.- 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 error value to convert to a newOptionalResult, if error state- Returns:
- the
OptionalResultreturned from the mapping function, if in error state, otherwise the unalteredOptionalResultin success state
-
consume
If in success state, applies the optional success value to the given consumer, otherwise does nothing.- Parameters:
consumer- the consumer which accepts the optional success value- Returns:
- the original
OptionalResultunaltered - Throws:
NullPointerException- if the given consumer isnull
-
consumeValue
If in success state with a success value, applies the success value to the given consumer, otherwise does nothing.- Parameters:
consumer- the consumer which accepts the success value- Returns:
- the original
OptionalResultunaltered - 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
OptionalResultunaltered - Throws:
NullPointerException- if the given consumer isnull
-
consumeEither
public OptionalResult<T,E> consumeEither(Consumer<Optional<T>> successConsumer, Consumer<? super E> errorConsumer) If in success state, applies the optional success value to the given success consumer. If in error state, applies the error value to the given error consumer.- Parameters:
successConsumer- the consumer which accepts the optional success valueerrorConsumer- the consumer which accepts the error value- Returns:
- the original
OptionalResultunaltered - Throws:
NullPointerException- if one of the given consumers isnull
-
consumeEither
public OptionalResult<T,E> consumeEither(Consumer<? super T> valueConsumer, Runnable emptyRunnable, Consumer<? super E> errorConsumer) If in success state with a success value, applies the success value to the given value consumer. If empty, run the given empty runnable. If in error state, applies the error value to the given error consumer.- Parameters:
valueConsumer- the consumer which accepts the optional success valueemptyRunnable- the runnable to run if emptyerrorConsumer- the consumer which accepts the error value- Returns:
- the original
OptionalResultunaltered - Throws:
NullPointerException- if one of the given consumers or runnable isnull
-
flatConsume
public OptionalResult<T,E> flatConsume(Function<Optional<T>, ? extends VoidResult<? extends E>> function) If in success state, applies the optional success value to the given function. If the function returns aVoidResultin success state, the originalOptionalResultis returned unaltered. If the function returns aVoidResultin error state, aOptionalResultcontaining the error value is returned. If in error state, the originalOptionalResultis returned unaltered.- Parameters:
function- the function which accepts the optional success value- Returns:
- the original
OptionalResultunaltered if the given function returns success or the originalOptionalResultis in error state, otherwise aOptionalResultcontaining the error value from the function result - Throws:
NullPointerException- if the given function isnullor returnsnull
-
flatConsumeValue
If in success state, applies the success value to the given function. If the function returns aVoidResultin success state, the originalOptionalResultis returned unaltered. If the function returns aVoidResultin error state, aOptionalResultcontaining the error value is returned. If in empty or error state, the originalOptionalResultis returned unaltered.- Parameters:
function- the function which accepts the success value- Returns:
- the original
OptionalResultunaltered if the given function returns success or the originalOptionalResultis in empty or error state, otherwise aOptionalResultcontaining 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
OptionalResultunaltered - Throws:
NullPointerException- if the given runnable isnull
-
runIfValue
If in success state with a success value, runs the given runnable, otherwise does nothing.- Parameters:
runnable- the runnable to run if success state with a success value- Returns:
- the original
OptionalResultunaltered - Throws:
NullPointerException- if the given runnable isnull
-
runIfNoValue
If in empty success state or error state, runs the given runnable, otherwise does nothing.- Parameters:
runnable- the runnable to run if empty success state or error state- Returns:
- the original
OptionalResultunaltered - Throws:
NullPointerException- if the given runnable isnull
-
runIfEmpty
If in empty success state with no success value, runs the given runnable, otherwise does nothing.- Parameters:
runnable- the runnable to run if empty success state- Returns:
- the original
OptionalResultunaltered - 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
OptionalResultunaltered - 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
OptionalResultunaltered - Throws:
NullPointerException- if one of the given runnables isnull
-
runEither
public OptionalResult<T,E> runEither(Runnable valueRunnable, Runnable emptyRunnable, Runnable errorRunnable) If in success state with a success value, runs the given value runnable. If empty, runs the given empty runnable. If in error state, runs the given error runnable.- Parameters:
valueRunnable- the runnable to run if success state with a success valueemptyRunnable- the runnable to run if emptyerrorRunnable- the runnable to run if error state- Returns:
- the original
OptionalResultunaltered - 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
OptionalResultunaltered - 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
OptionalResultunaltered - Throws:
NullPointerException- if the given runnable isnull
-
flatRunIfSuccess
If in success state, runs the given supplier. If the supplier returns aVoidResultin success state, the originalOptionalResultis returned unaltered. If the supplier returns aVoidResultin error state, aOptionalResultcontaining the error value is returned. If in error state, the originalOptionalResultis returned unaltered.- Parameters:
supplier- the supplier to run- Returns:
- the original
OptionalResultunaltered if the given supplier returns success or the originalOptionalResultis in error state, otherwise aOptionalResultcontaining the error value from the supplier result - Throws:
NullPointerException- if the given supplier isnullor returnsnull
-
flatRunIfValue
If in success state with a success value, runs the given supplier. If the supplier returns aVoidResultin success state, the originalOptionalResultis returned unaltered. If the supplier returns aVoidResultin error state, aOptionalResultcontaining the error value is returned. If in error state or empty, the originalOptionalResultis returned unaltered.- Parameters:
supplier- the supplier to run- Returns:
- the original
OptionalResultunaltered if the given supplier returns success or the originalOptionalResultis in empty or error state, otherwise aOptionalResultcontaining the error value from the supplier result - Throws:
NullPointerException- if the given supplier isnullor returnsnull
-
verify
public OptionalResult<T,E> verify(Predicate<Optional<T>> predicate, Supplier<? extends E> errorSupplier) If in success state, verifies the optional success value of thisOptionalResultby testing it with the given predicate. If the predicate evaluates to false, a newOptionalResultis returned containing the error value provided by the given error supplier. If the predicate evaluates to true, or theOptionalResultalready was in error state, the originalOptionalResultis returned unaltered.- Parameters:
predicate- the predicate used to verify the optional success value, if success stateerrorSupplier- supplier providing the error if predicate evaluates to false- Returns:
- the original
OptionalResultunaltered, unless the predicate evaluates to false, then a newOptionalResultin 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 thisOptionalResultby mapping it to aVoidResult. If the returnedVoidResultis in error state, a newOptionalResultis returned containing the error value of theVoidResult. If theVoidResultis in success state, or theOptionalResultalready was in error state, the originalOptionalResultis returned unaltered.- Parameters:
function- the function applied to the success value, if success state- Returns:
- the original
OptionalResultunaltered, unless theVoidResultreturned by the mapping function is in error state, then a newOptionalResultin error state is returned containing the error value from theVoidResult - Throws:
NullPointerException- if the given function isnullor returnsnull
-
verifyValue
public OptionalResult<T,E> verifyValue(Predicate<? super T> predicate, Supplier<? extends E> errorSupplier) If in success state with a success value, verifies the success value of thisOptionalResultby testing it with the given predicate. If the predicate evaluates to false, a newOptionalResultis returned containing the error value provided by the given error supplier. If the predicate evaluates to true, or theOptionalResultalready was empty or in error state, the originalOptionalResultis returned unaltered.- Parameters:
predicate- the predicate used to verify the success value, if success state with a success valueerrorSupplier- supplier providing the error if predicate evaluates to false- Returns:
- the original
OptionalResultunaltered, unless the predicate evaluates to false, then a newOptionalResultin error state is returned containing the supplied error value - Throws:
NullPointerException- if the given predicate isnullor returnsnull, or the given error supplier isnullor returnsnull
-
verifyValue
public OptionalResult<T,E> verifyValue(Function<? super T, ? extends VoidResult<? extends E>> function) If in non-empty success state, verifies the success value of thisOptionalResultby mapping it to aVoidResult. If the returnedVoidResultis in error state, a newOptionalResultis returned containing the error value of theVoidResult. If theVoidResultis in success state, or theOptionalResultalready was empty or in error state, the originalOptionalResultis returned unaltered.- Parameters:
function- the function applied to the success value, if non-empty success state- Returns:
- the original
OptionalResultunaltered, unless theVoidResultreturned by the mapping function is in error state, then a newOptionalResultin error state is returned containing the error value from theVoidResult - Throws:
NullPointerException- if the given function isnullor returnsnull
-
fold
public <N> N fold(Function<Optional<T>, ? extends N> successFunction, Function<? super E, ? extends N> errorFunction) Retrieve a value from thisOptionalResultby folding the states. If in success state, return the value of applying the success function to the optional 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:
successFunction- the mapping function to apply to the optional 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(Function<? super T, ? extends N> valueFunction, Supplier<? extends N> emptySupplier, Function<? super E, ? extends N> errorFunction) Retrieve a value from thisOptionalResultby folding the states. If in success state with a success value, return the value of applying the value function to the success value. If empty, return the value provided by the empty-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:
valueFunction- the mapping function to apply to the success value, if success state, may returnnullemptySupplier- the supplier to provide the value if empty, 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 optional success value, otherwise returnsother.- Parameters:
other- the value to be returned, if not in success state, may not benull- Returns:
- the optional success value, if success state, otherwise
other - Throws:
NullPointerException- if the other value isnull
-
valueOrElse
If in success state with a success value, 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 with a success value,
otherwise
other
-
orElseGet
If in success state, returns the optional 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 not returnnull- Returns:
- the optional success value, if success state, otherwise the result returned from the given function
- Throws:
NullPointerException- if the given function isnullor returnsnull
-
valueOrElseGet
If in success state with a success value, returns the success value, otherwise returns the value returned from the given function.- Parameters:
supplier- the supplier providing the return value, if not in success state with a success value, it may returnnull- Returns:
- the success value, if success state with a success value, otherwise the result returned from the given function
- Throws:
NullPointerException- if the given function isnull
-
orElseThrow
public <X extends Throwable> Optional<T> orElseThrow(Function<? super E, ? extends X> function) throws XIf in success state, returns the optional 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 optional success value, if success state
- Throws:
X- if in error stateNullPointerException- if the given function isnullor returnsnull
-
valueOrElseThrow
If in success state with a success value, returns the success value, otherwise throws the exception returned by the given function.- Type Parameters:
X- type of the exception to be thrown- Parameters:
supplier- the supplier providing the return value, if not in success state with a success value- Returns:
- the success value, if success state with success value
- Throws:
X- if empty or in error stateNullPointerException- if the given function isnullor returnsnull
-
toResult
Transforms thisOptionalResultto aResult. If in non-empty succes state, theResultwill be in success state containing the success value of thisOptionalResult. If empty success state, theResultwill be in error state, containing the error value supplied by the given error supplier. If in error state, theResultwill be in error state containing the error value of thisOptionalResult.- Parameters:
errorSupplier- supplier providing the error value if empty success state- Returns:
- a
Resulteither in success state containing the success value from thisOptionalResult, or in error state containing either the error value from the given error supplier or the error value present in thisOptionalResult - Throws:
NullPointerException- if the given supplier isnullor returnsnull
-
toVoidResult
Transforms thisOptionalResultto aVoidResult. If in success state, theVoidResultwill be in success state. If in error state, theVoidResultwill be in error state containing the error value from thisOptionalResult.- Returns:
- a
VoidResulteither in success state or in error state containing the error value from thisOptionalResult
-
handle
Handle the givenCallable. If theCallableexecutes successfully, theOptionalResultwill be in success state containing the returned value. If theCallablethrows an exception, theOptionalResultwill be in error state containing the thrown exception.- Type Parameters:
T- type of the return value of theCallable- Parameters:
callable- theCallableto handle- Returns:
- a
OptionalResulteither 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> OptionalResult<T,E> handle(Callable<Optional<T>> callable, Function<Exception, E> exceptionMapper) Handle the givenCallable. If theCallableexecutes successfully, theOptionalResultwill be in success state containing the returned value. If theCallablethrows an exception, theOptionalResultwill 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
OptionalResulteither 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