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 anOptionalResult
in success state, which is empty with no success value.boolean
static <T,
E> OptionalResult<T, E> error
(E value) Returns anOptionalResult
in error state containing the given non-null
value 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 theResult
from applying the given mapping function to the optional success value, otherwise returns aResult
containing the error value of thisOptionalResult
.flatMapToBooleanResult
(Function<Optional<? extends T>, BooleanResult<? extends E>> function) If in success state, returns theBooleanResult
from applying the given mapping function to the optional success value, otherwise returns aBooleanResult
containing the error value of thisOptionalResult
.<N> OptionalResult<N,
E> flatMapToOptionalResult
(Function<Optional<T>, OptionalResult<? extends N, ? extends E>> function) If in success state, returns theOptionalResult
from applying the given mapping function to the optional success value, otherwise returns the unalteredOptionalResult
in error state.flatMapToVoidResult
(Function<Optional<? extends T>, VoidResult<? extends E>> function) If in success state, returns theVoidResult
from applying the given mapping function to the optional success value, otherwise returns aVoidResult
containing the error value of thisOptionalResult
.flatMapValueWithBooleanResult
(Function<? super T, BooleanResult<? extends E>> function) If in success state with a success value, returns anOptionalResult
from applying the given mapping function to the success value, otherwise returns the unalteredOptionalResult
which 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 theOptionalResult
from applying the given mapping function to the success value, otherwise returns the unalteredOptionalResult
which 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 anOptionalResult
from applying the given mapping function to the success value, otherwise returns the unalteredOptionalResult
which 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 theOptionalResult
from applying the given mapping function to the error value, otherwise returns the unalteredOptionalResult
in success state.<N> OptionalResult<N,
E> flatReplaceEmpty
(Supplier<OptionalResult<N, E>> supplier) If in empty success state, returns theOptionalResult
from applying the given supplier, otherwise returns the unalteredOptionalResult
in success state with value or error state.flatReplaceEmptyWithResult
(Supplier<Result<N, E>> supplier) If in empty success state, returns theResult
from applying the given supplier, otherwise returns aResult
with 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> N
fold
(Function<? super T, ? extends N> valueFunction, Supplier<? extends N> emptySupplier, Function<? super E, ? extends N> errorFunction) Retrieve a value from thisOptionalResult
by folding the states.<N> N
fold
(Function<Optional<T>, ? extends N> successFunction, Function<? super E, ? extends N> errorFunction) Retrieve a value from thisOptionalResult
by folding the states.static <T> OptionalResult<T,
Exception> Handle the givenCallable
.static <T,
E> OptionalResult<T, E> Handle the givenCallable
.int
hashCode()
If in success state, returns aResult
containing the result of applying the given mapping function to the optional success value of thisOptionalResult
, otherwise returns aResult
containing the error value of thisOptionalResult
.<N> OptionalResult<T,
N> If in error state, returns aOptionalResult
containing the result of applying the given mapping function to the error value, otherwise returns the unalteredOptionalResult
in success state.mapToBoolean
(Function<Optional<T>, Boolean> function) If in success state, returns aBooleanResult
containing the result of applying the given mapping function to the optional success value, otherwise returns aBooleanResult
containing the error value of thisOptionalResult
.<N> OptionalResult<N,
E> mapToOptional
(Function<Optional<T>, ? extends Optional<? extends N>> function) If in success state, returns aOptionalResult
containing the result of applying the given mapping function to the optional success value, otherwise returns the unalteredOptionalResult
in error state.<N> OptionalResult<N,
E> If in success state with a success value, returns anOptionalResult
containing the result of applying the given mapping function to the success value, otherwise returns the unalteredOptionalResult
which 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 anOptionalResult
containing the result of applying the given mapping function to the success value, otherwise returns the unalteredOptionalResult
which 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 aOptionalResult
with the success value from applying the given mapping function to the error value, otherwise returns the unalteredOptionalResult
in 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 anOptionalResult
in success state containing the given non-null
value as success value.static <T,
E> OptionalResult<T, E> success
(T value) Returns anOptionalResult
in success state containing the given non-null
value as success value.static <T,
E> OptionalResult<T, E> successNullable
(T value) Returns anOptionalResult
in success state either containing the given value as success value, or empty if the given value is null.Transforms thisOptionalResult
to aResult
.toString()
Transforms thisOptionalResult
to 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 thisOptionalResult
by mapping it to aVoidResult
.If in success state, verifies the optional success value of thisOptionalResult
by 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 thisOptionalResult
by 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 thisOptionalResult
by testing it with the given predicate.
-
Method Details
-
success
Returns anOptionalResult
in success state containing the given non-null
value as success value.- Type Parameters:
T
- the type of the success value which may be present in the givenOptional
E
- the type of the error value- Parameters:
maybeValue
- anOptional
which may contain an success value, or may be empty- Returns:
- an
OptionalResult
in success state which either contains a success value or is empty - Throws:
NullPointerException
- if givenOptional
isnull
-
success
Returns anOptionalResult
in success state containing the given non-null
value as success value.- Type Parameters:
T
- the type of the success valueE
- the type of the error value- Parameters:
value
- the success value, which must be non-null
- Returns:
- an
OptionalResult
in success state containing the given success value - Throws:
NullPointerException
- if given success value isnull
-
successNullable
Returns anOptionalResult
in 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
OptionalResult
in success state containing the given success value if not null, otherwise an emptyOptionalResult
-
empty
Returns anOptionalResult
in 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
OptionalResult
in success state
-
error
Returns anOptionalResult
in error state containing the given non-null
value as error value.- Type Parameters:
T
- the type of the success valueE
- the type of the error value- Parameters:
value
- the error value, which must be non-null
- Returns:
- an
OptionalResult
in error state containing the given error value - Throws:
NullPointerException
- if given error value isnull
-
map
If in success state, returns aResult
containing the result of applying the given mapping function to the optional success value of thisOptionalResult
, otherwise returns aResult
containing 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
Result
containing the result of applying the mapping function to the optional success value of thisOptionalResult
, if in success state, otherwise aResult
containing the error value of thisOptionalResult
- Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
mapToOptional
public <N> OptionalResult<N,E> mapToOptional(Function<Optional<T>, ? extends Optional<? extends N>> function) If in success state, returns aOptionalResult
containing the result of applying the given mapping function to the optional success value, otherwise returns the unalteredOptionalResult
in error state.- Type Parameters:
N
- the type of the success value which may be present in theOptional
returned from the mapping function- Parameters:
function
- the mapping function to apply to the optional success value, if success state- Returns:
- a
OptionalResult
containing the result of applying the mapping function to the optional success value of thisOptionalResult
, if in success state, otherwise the unalteredOptionalResult
in error state - Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
mapToBoolean
If in success state, returns aBooleanResult
containing the result of applying the given mapping function to the optional success value, otherwise returns aBooleanResult
containing the error value of thisOptionalResult
.- Parameters:
function
- the mapping function to apply to the optional success value, if success state- Returns:
- a
BooleanResult
containing the result of applying the mapping function to the optional success value of thisOptionalResult
, if in success state, otherwise aBooleanResult
containing the error value of thisOptionalResult
- Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
mapError
If in error state, returns aOptionalResult
containing the result of applying the given mapping function to the error value, otherwise returns the unalteredOptionalResult
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
OptionalResult
containing the result of applying the mapping function to the error value of thisOptionalResult
, if in error state, otherwise the unalteredOptionalResult
in success state - Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
mapValue
If in success state with a success value, returns anOptionalResult
containing the result of applying the given mapping function to the success value, otherwise returns the unalteredOptionalResult
which may be empty or in error state.If the given mapping function returns null, then the returned
OptionalResult
will 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
OptionalResult
containing the result of applying the mapping function to the success value of thisOptionalResult
, if in success state with a success value, otherwise the unalteredOptionalResult
which 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 anOptionalResult
containing the result of applying the given mapping function to the success value, otherwise returns the unalteredOptionalResult
which may be empty or in error state.If the given mapping function returns a non-empty
Optional
, then the returnedOptionalResult
will contain theOptional
content as success value. If the given mapping function returns an emptyOptional
, then the returnedOptionalResult
will also be empty.- Type Parameters:
N
- the type of the success value which may be present in theOptional
returned from the mapping function- Parameters:
function
- the mapping function to apply to the success value, if success state with a success value- Returns:
- an
OptionalResult
containing the result of applying the mapping function to the success value of thisOptionalResult
, if in success state with a success value, otherwise the unalteredOptionalResult
which may be empty or in error state - Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
flatMap
If in success state, returns theResult
from applying the given mapping function to the optional success value, otherwise returns aResult
containing the error value of thisOptionalResult
.- Type Parameters:
N
- the type of success value which may be present in theResult
returned by the mapping function- Parameters:
function
- the mapping function to apply to the optional success value, if success state- Returns:
- the
Result
returned from the mapping function, if in success state, otherwise aResult
containing the error value of thisOptionalResult
- Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
flatMapToOptionalResult
public <N> OptionalResult<N,E> flatMapToOptionalResult(Function<Optional<T>, OptionalResult<? extends N, ? extends E>> function) If in success state, returns theOptionalResult
from applying the given mapping function to the optional success value, otherwise returns the unalteredOptionalResult
in error state.- Type Parameters:
N
- the type of success value which may be present in theOptionalResult
returned by the mapping function- Parameters:
function
- the mapping function to apply to the optional success value, if success state- Returns:
- the
OptionalResult
returned from the mapping function, if in success state, otherwise the unalteredOptionalResult
in error state - Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
flatReplaceEmpty
If in empty success state, returns theOptionalResult
from applying the given supplier, otherwise returns the unalteredOptionalResult
in success state with value or error state.- Type Parameters:
N
- the type of success value which may be present in theOptionalResult
returned by this function.- Parameters:
supplier
- the supplier to call if in empty success state.- Returns:
- the
OptionalResult
returned from the supplier, if in empty state, otherwise the unalteredOptionalResult
in success state with value or error state - Throws:
NullPointerException
- if the given supplier isnull
or returnsnull
-
flatReplaceEmptyWithResult
If in empty success state, returns theResult
from applying the given supplier, otherwise returns aResult
with the existing success value or error value.- Type Parameters:
N
- the type of success value which may be present in theResult
returned by this function.- Parameters:
supplier
- the supplier to call if in empty success state.- Returns:
- the
Result
returned from the supplier, if in empty state, otherwise aResult
with the existing success value or error value. - Throws:
NullPointerException
- if the given supplier isnull
or returnsnull
-
flatMapToBooleanResult
public BooleanResult<E> flatMapToBooleanResult(Function<Optional<? extends T>, BooleanResult<? extends E>> function) If in success state, returns theBooleanResult
from applying the given mapping function to the optional success value, otherwise returns aBooleanResult
containing the error value of thisOptionalResult
.- Parameters:
function
- the mapping function to apply to the optional success value, if success state- Returns:
- the
BooleanResult
returned from the mapping function, if in success state, otherwise aBooleanResult
containing the error value of thisOptionalResult
- Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
flatMapToVoidResult
public VoidResult<E> flatMapToVoidResult(Function<Optional<? extends T>, VoidResult<? extends E>> function) If in success state, returns theVoidResult
from applying the given mapping function to the optional success value, otherwise returns aVoidResult
containing the error value of thisOptionalResult
.- Parameters:
function
- the mapping function to apply to the optional success value, if success state- Returns:
- the
VoidResult
returned from the mapping function, if in success state, otherwise aVoidResult
containing the error value of thisOptionalResult
- Throws:
NullPointerException
- if the given mapping function isnull
or 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 anOptionalResult
from applying the given mapping function to the success value, otherwise returns the unalteredOptionalResult
which may be empty or in error state.If the
Result
returned from the mapping function is in success state. the returnedOptionalResult
will contain the success value from theResult
. If theResult
is in error state, the returnedOptionalResult
will contain the error value from theResult
.- Type Parameters:
N
- the type of success value which may be present in theOptionalResult
after applying the mapping function- Parameters:
function
- the mapping function to apply to the success value, if success state with a success value- Returns:
- an
OptionalResult
after applying the mapping function, if in success state with a success value, otherwise the unalteredOptionalResult
which may be empty or in error state - Throws:
NullPointerException
- if the given mapping function isnull
or 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 theOptionalResult
from applying the given mapping function to the success value, otherwise returns the unalteredOptionalResult
which may be empty or in error state.- Type Parameters:
N
- the type of success value which may be present in theOptionalResult
returned by the mapping function- Parameters:
function
- the mapping function to apply to the success value, if success state with a success value- Returns:
- the
OptionalResult
returned from the mapping function, if in success state with a success value, otherwise the unalteredOptionalResult
which may be empty or in error state - Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
flatMapValueWithBooleanResult
public OptionalResult<Boolean,E> flatMapValueWithBooleanResult(Function<? super T, BooleanResult<? extends E>> function) If in success state with a success value, returns anOptionalResult
from applying the given mapping function to the success value, otherwise returns the unalteredOptionalResult
which may be empty or in error state.If the
BooleanResult
returned from the mapping function is in success state. the returnedOptionalResult
will contain the boolean success value from theBooleanResult
. If theBooleanResult
is in error state, the returnedOptionalResult
will 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
OptionalResult
after applying the mapping function, if in success state with a success value, otherwise the unalteredOptionalResult
which may be empty or in error state - Throws:
NullPointerException
- if the given mapping function isnull
or returnsnull
-
recover
If in error state, returns aOptionalResult
with the success value from applying the given mapping function to the error value, otherwise returns the unalteredOptionalResult
in success state.- Parameters:
function
- the mapping function to apply to the error value to convert to a new success value, if error state- Returns:
- A
OptionalResult
containing the value from the mapping function, if in error state, otherwise the unalteredOptionalResult
in success state
-
flatRecover
public <N> OptionalResult<N,E> flatRecover(Function<E, OptionalResult<? extends N, ? extends E>> function) If in error state, returns theOptionalResult
from applying the given mapping function to the error value, otherwise returns the unalteredOptionalResult
in success state.- Type Parameters:
N
- the type of success value which may be present in theOptionalResult
returned by the mapping function- Parameters:
function
- the mapping function to apply to the error value to convert to a newOptionalResult
, if error state- Returns:
- the
OptionalResult
returned from the mapping function, if in error state, otherwise the unalteredOptionalResult
in 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
OptionalResult
unaltered - 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
OptionalResult
unaltered - Throws:
NullPointerException
- if the given consumer isnull
-
consumeError
If in error state, applies the error value to the given consumer, otherwise does nothing.- Parameters:
errorConsumer
- the consumer which accepts the error value- Returns:
- the original
OptionalResult
unaltered - 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
OptionalResult
unaltered - 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
OptionalResult
unaltered - 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 aVoidResult
in success state, the originalOptionalResult
is returned unaltered. If the function returns aVoidResult
in error state, aOptionalResult
containing the error value is returned. If in error state, the originalOptionalResult
is returned unaltered.- Parameters:
function
- the function which accepts the optional success value- Returns:
- the original
OptionalResult
unaltered if the given function returns success or the originalOptionalResult
is in error state, otherwise aOptionalResult
containing the error value from the function result - Throws:
NullPointerException
- if the given function isnull
or returnsnull
-
flatConsumeValue
If in success state, applies the success value to the given function. If the function returns aVoidResult
in success state, the originalOptionalResult
is returned unaltered. If the function returns aVoidResult
in error state, aOptionalResult
containing the error value is returned. If in empty or error state, the originalOptionalResult
is returned unaltered.- Parameters:
function
- the function which accepts the success value- Returns:
- the original
OptionalResult
unaltered if the given function returns success or the originalOptionalResult
is in empty or error state, otherwise aOptionalResult
containing the error value from the function result - Throws:
NullPointerException
- if the given function isnull
or returnsnull
-
runIfSuccess
If in success state, runs the given runnable, otherwise does nothing.- Parameters:
runnable
- the runnable to run if success state- Returns:
- the original
OptionalResult
unaltered - 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
OptionalResult
unaltered - 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
OptionalResult
unaltered - 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
OptionalResult
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
OptionalResult
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
OptionalResult
unaltered - 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
OptionalResult
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
OptionalResult
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
OptionalResult
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 originalOptionalResult
is returned unaltered. If the supplier returns aVoidResult
in error state, aOptionalResult
containing the error value is returned. If in error state, the originalOptionalResult
is returned unaltered.- Parameters:
supplier
- the supplier to run- Returns:
- the original
OptionalResult
unaltered if the given supplier returns success or the originalOptionalResult
is in error state, otherwise aOptionalResult
containing the error value from the supplier result - Throws:
NullPointerException
- if the given supplier isnull
or returnsnull
-
flatRunIfValue
If in success state with a success value, runs the given supplier. If the supplier returns aVoidResult
in success state, the originalOptionalResult
is returned unaltered. If the supplier returns aVoidResult
in error state, aOptionalResult
containing the error value is returned. If in error state or empty, the originalOptionalResult
is returned unaltered.- Parameters:
supplier
- the supplier to run- Returns:
- the original
OptionalResult
unaltered if the given supplier returns success or the originalOptionalResult
is in empty or error state, otherwise aOptionalResult
containing the error value from the supplier result - Throws:
NullPointerException
- if the given supplier isnull
or 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 thisOptionalResult
by testing it with the given predicate. If the predicate evaluates to false, a newOptionalResult
is returned containing the error value provided by the given error supplier. If the predicate evaluates to true, or theOptionalResult
already was in error state, the originalOptionalResult
is 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
OptionalResult
unaltered, unless the predicate evaluates to false, then a newOptionalResult
in error state is returned containing the supplied error value - Throws:
NullPointerException
- if the given predicate isnull
or returnsnull
, or the given error supplier isnull
or returnsnull
-
verify
If in success state, verifies the success value of thisOptionalResult
by mapping it to aVoidResult
. If the returnedVoidResult
is in error state, a newOptionalResult
is returned containing the error value of theVoidResult
. If theVoidResult
is in success state, or theOptionalResult
already was in error state, the originalOptionalResult
is returned unaltered.- Parameters:
function
- the function applied to the success value, if success state- Returns:
- the original
OptionalResult
unaltered, unless theVoidResult
returned by the mapping function is in error state, then a newOptionalResult
in error state is returned containing the error value from theVoidResult
- Throws:
NullPointerException
- if the given function isnull
or 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 thisOptionalResult
by testing it with the given predicate. If the predicate evaluates to false, a newOptionalResult
is returned containing the error value provided by the given error supplier. If the predicate evaluates to true, or theOptionalResult
already was empty or in error state, the originalOptionalResult
is 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
OptionalResult
unaltered, unless the predicate evaluates to false, then a newOptionalResult
in error state is returned containing the supplied error value - Throws:
NullPointerException
- if the given predicate isnull
or returnsnull
, or the given error supplier isnull
or returnsnull
-
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 thisOptionalResult
by mapping it to aVoidResult
. If the returnedVoidResult
is in error state, a newOptionalResult
is returned containing the error value of theVoidResult
. If theVoidResult
is in success state, or theOptionalResult
already was empty or in error state, the originalOptionalResult
is returned unaltered.- Parameters:
function
- the function applied to the success value, if non-empty success state- Returns:
- the original
OptionalResult
unaltered, unless theVoidResult
returned by the mapping function is in error state, then a newOptionalResult
in error state is returned containing the error value from theVoidResult
- Throws:
NullPointerException
- if the given function isnull
or returnsnull
-
fold
public <N> N fold(Function<Optional<T>, ? extends N> successFunction, Function<? super E, ? extends N> errorFunction) Retrieve a value from thisOptionalResult
by 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 returnnull
errorFunction
- the mapping function to apply to the error value, if error state, may returnnull
- Returns:
- the folded value mapped from either the success value or error
value, may be
null
- Throws:
NullPointerException
- if one of the given functions isnull
-
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 thisOptionalResult
by 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 returnnull
emptySupplier
- the supplier to provide the value if empty, may returnnull
errorFunction
- the mapping function to apply to the error value, if error state, may returnnull
- Returns:
- the folded value mapped from either the success value or error
value, may be
null
- Throws:
NullPointerException
- if one of the given functions 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 isnull
or 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 isnull
or 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 isnull
or returnsnull
-
toResult
Transforms thisOptionalResult
to aResult
. If in non-empty succes state, theResult
will be in success state containing the success value of thisOptionalResult
. If empty success state, theResult
will be in error state, containing the error value supplied by the given error supplier. If in error state, theResult
will be in error state containing the error value of thisOptionalResult
.- Parameters:
errorSupplier
- supplier providing the error value if empty success state- Returns:
- a
Result
either 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 isnull
or returnsnull
-
toVoidResult
Transforms thisOptionalResult
to aVoidResult
. If in success state, theVoidResult
will be in success state. If in error state, theVoidResult
will be in error state containing the error value from thisOptionalResult
.- Returns:
- a
VoidResult
either in success state or in error state containing the error value from thisOptionalResult
-
handle
Handle the givenCallable
. If theCallable
executes successfully, theOptionalResult
will be in success state containing the returned value. If theCallable
throws an exception, theOptionalResult
will be in error state containing the thrown exception.- Type Parameters:
T
- type of the return value of theCallable
- Parameters:
callable
- theCallable
to handle- Returns:
- a
OptionalResult
either in success state containing the value from theCallable
, or in error state containing the exception thrown by theCallable
- Throws:
NullPointerException
- if the given callable isnull
or returnsnull
-
handle
public static <T,E> OptionalResult<T,E> handle(Callable<Optional<T>> callable, Function<Exception, E> exceptionMapper) Handle the givenCallable
. If theCallable
executes successfully, theOptionalResult
will be in success state containing the returned value. If theCallable
throws an exception, theOptionalResult
will be in error state containing the result after mapping the exception with the given exception mapper function.- Type Parameters:
T
- type of the return value of theCallable
E
- type of the error value after mapping a thrown exception- Parameters:
callable
- theCallable
to handle- Returns:
- a
OptionalResult
either in success state containing the value from theCallable
, or in error state containing the result after mapping the exception thrown by theCallable
- Throws:
NullPointerException
- if the given callable isnull
or returnsnull
, or if the given exception mapper function isnull
or returnsnull
-
equals
-
hashCode
public int hashCode() -
toString
-
runAlways(java.lang.Runnable)
instead for clarity