Class Ok<T,E>
-
Method Summary
Modifier and TypeMethodDescriptionadaptErr()
adaptOk()
<R> R
boolean
boolean
containsErr
(E err) boolean
boolean
errMatches
(java.util.function.Predicate<E> errPredicate) LikeResult.errOrNullable(E)
, but does not allow null input or output.LikeResult.errOrNullable(Supplier)
, but does not allow null input or output.errOrNullable
(E alternative) errOrNullable
(java.util.function.Supplier<E> alternativeSupplier) flatMapErr
(java.util.function.Function<E, Result<T, F>> converter) get()
Returns the value contained by thisOk
.Deprecated.getErrOrThrow
(String exceptionMessage) Deprecated.Marked as deprecated because callinggetErrOrThrow(String)
onOk
always fails.getErrOrThrow
(java.util.function.Supplier<RuntimeException> exceptionSupplier) Deprecated.Marked as deprecated because callinggetErrOrThrow(Supplier)
onOk
always fails.getOrThrow
(String exceptionMessage) If theResult
isOk
, return its content, otherwise throwWrongResultVariantException
with the given message.getOrThrow
(java.util.function.Supplier<RuntimeException> exceptionSupplier) int
hashCode()
void
void
Run an action on the value ofErr
.void
Run an action on the value ofOk
.boolean
isOk()
iterator()
Returns an iterator containing a single value - the content of thisOk
.Map theOk
value to a new value of a different type.Map theErr
value to a new value of a different type.boolean
static <T,
E> Ok<T, E> of
(T value) Create a new, successfulResult
.LikeResult.okOrNullable(Supplier)
, but does not allow null input or output.LikeResult.okOrNullable(T)
, but does not allow null input or output.okOrNull()
okOrNullable
(java.util.function.Supplier<T> alternativeSupplier) okOrNullable
(T alternative) java.util.stream.Stream<T>
stream()
Returns a stream containing a single value - the content of thisOk
.toString()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
of
Create a new, successfulResult
. Same asResult.ok(Object)
. -
isOk
public boolean isOk() -
get
-
getOrThrow
If theResult
isOk
, return its content. Otherwise, throwWrongResultVariantException
.Note that it may be better to get the okay value without chance of exceptions:
if (result instanceof Ok<List<Integer>, String> ok) { var value = ok.get(); }
- Specified by:
getOrThrow
in interfaceResult<T,
E> - See Also:
-
getOrThrow
If theResult
isOk
, return its content, otherwise throwWrongResultVariantException
with the given message.- Specified by:
getOrThrow
in interfaceResult<T,
E> - See Also:
-
getOrThrow
@Nonnull public T getOrThrow(@Nonnull java.util.function.Supplier<RuntimeException> exceptionSupplier) - Specified by:
getOrThrow
in interfaceResult<T,
E> - See Also:
-
getErrOrThrow
Deprecated.Marked as deprecated because callinggetErrOrThrow()
onOk
always fails. It is not deprecated to call this onResult
, but onOk
useget()
.If theResult
isErr
, return its content. Otherwise, throwWrongResultVariantException
.This is the dual of
Result.getOrThrow()
, which describes more details.- Specified by:
getErrOrThrow
in interfaceResult<T,
E> - See Also:
-
getErrOrThrow
Deprecated.Marked as deprecated because callinggetErrOrThrow(String)
onOk
always fails. It is not deprecated to call this onResult
, but onOk
useget()
.If theResult
isErr
, return its content, otherwise throwWrongResultVariantException
with the given message.- Specified by:
getErrOrThrow
in interfaceResult<T,
E> - See Also:
-
getErrOrThrow
@Deprecated @Nonnull public E getErrOrThrow(@Nonnull java.util.function.Supplier<RuntimeException> exceptionSupplier) Deprecated.Marked as deprecated because callinggetErrOrThrow(Supplier)
onOk
always fails. It is not deprecated to call this onResult
, but onOk
useget()
.- Specified by:
getErrOrThrow
in interfaceResult<T,
E> - See Also:
-
map
- Specified by:
map
in interfaceResult<T,
E> - See Also:
-
Result.mapErr(Function)
Result.branch(Function, Function)
Stream.map(Function)
-
flatMap
@Nonnull public <U> Result<U,E> flatMap(@Nonnull java.util.function.Function<T, Result<U, E>> converter) Map theOk
value to a newResult
value, flattening the two results to a single one. Does nothing onErr
.- Specified by:
flatMap
in interfaceResult<T,
E> - See Also:
-
Result.map(Function)
Result.flatMapErr(Function)
Result.flatten(Result)
Stream.flatMap(Function)
-
mapErr
- Specified by:
mapErr
in interfaceResult<T,
E> - See Also:
-
Result.map(Function)
Result.branch(Function, Function)
Stream.map(Function)
-
flatMapErr
@Nonnull public <F> Result<T,F> flatMapErr(@Nonnull java.util.function.Function<E, Result<T, F>> converter) Map theErr
value to a newResult
value, flattening the two results to a single one. Does nothing onOk
.- Specified by:
flatMapErr
in interfaceResult<T,
E> - See Also:
-
Result.mapErr(Function)
Result.flatMap(Function)
Result.flatten(Result)
Stream.flatMap(Function)
-
ifOk
Run an action on the value ofOk
. Does nothing onErr
.If the action is a transformation,
Result.map(Function)
should be preferred, which can take lambdas without side effects. -
ifErr
Run an action on the value ofErr
. Does nothing onOk
.If the action is a transformation,
Result.mapErr(Function)
should be preferred, which can take lambdas without side effects. -
ifEither
public void ifEither(@Nonnull java.util.function.Consumer<T> okAction, @Nonnull java.util.function.Consumer<E> errAction) -
branch
@Nonnull public <R> R branch(@Nonnull java.util.function.Function<T, R> okConverter, @Nonnull java.util.function.Function<E, R> errConverter) Call on of the functions, depending onOk
orErr
. Both should return the same type.If the functions return nothing, use
Result.ifEither(Consumer, Consumer)
instead.Also called: fold
-
recover
If thisResult
isOk
, return the value. If it is not, then map the error to something of the same type asResult
, and return that.If the content of
Err
is not needed to produce an alternative value, useResult.okOr(Supplier)
instead.Also called: recover
-
okOr
LikeResult.okOrNullable(T)
, but does not allow null input or output. -
okOr
LikeResult.okOrNullable(Supplier)
, but does not allow null input or output. -
okOrNullable
If thisResult
isOk
, return the value. If it is not, return the given alternative.If the result is heavy to compute, use
Result.okOr(Supplier)
orResult.recover(Function)
instead.- Specified by:
okOrNullable
in interfaceResult<T,
E> - Parameters:
alternative
- The value that will replaceErr
.- See Also:
-
okOrNullable
If thisResult
isOk
, return the value. If it is not, produce an alternative using the given supplier. -
okOrNull
-
errOr
LikeResult.errOrNullable(E)
, but does not allow null input or output. -
errOr
LikeResult.errOrNullable(Supplier)
, but does not allow null input or output. -
errOrNullable
If thisResult
isErr
, return the value. If it is not, return the given alternative.If the result is heavy to compute, use
Result.errOrNullable(Supplier)
instead.- Specified by:
errOrNullable
in interfaceResult<T,
E> - Parameters:
alternative
- The value that will replaceOk
.- See Also:
-
errOrNullable
If thisResult
isErr
, return the value. If it is not, produce an alternative using the given supplier. -
errOrNull
-
adaptOk
-
adaptErr
-
withoutErr
- Specified by:
withoutErr
in interfaceResult<T,
E>
-
withoutOk
-
and
Returns the currentResult
if it isErr
, and the next one otherwise.See
Result.and(Supplier)
for more details. -
and
Returns the currentResult
if it isErr
, and produces the next result (given by the argument) otherwise.This simulates '
&&
' in the sense that the result isOk
if and only if both inputs areOk
.This returns the last object that had to be evaluated, like '
and
' in Python. I.e. if the currentResult
isErr
, the result must beErr
, and the supplier is not called. -
or
Returns the currentResult
if it isOk
, and the next one otherwise.See
Result.or(Supplier)
for more details. -
or
Returns the currentResult
if it isOk
, and produces the next result (given by the argument) otherwise.This simulates '
||
' in the sense that the result isOk
if at least one of the inputs isOk
.This returns the last object that had to be evaluated, like '
and
' in Python. I.e. if the currentResult
isOk
, the result must beOk
, and the supplier is not called. -
contains
-
containsErr
- Specified by:
containsErr
in interfaceResult<T,
E> - Parameters:
err
- The object that will be compared againstErr
usingObject.equals(Object)
. The comparison will only be done if thisResult
isErr
.- See Also:
-
matches
If thisResult
isOk
, the predicate will be tested against its content. If this is anErr
, returnsfalse
without invoking the predicate. -
errMatches
If thisResult
isErr
, the predicate will be tested against its content. If this is anOk
, returnsfalse
without invoking the predicate.- Specified by:
errMatches
in interfaceResult<T,
E> - See Also:
-
getUnified
Get the content of the result, whether it is insideOk
orErr
.Since success and failure types are generally different, type information is lost.
- Specified by:
getUnified
in interfaceResult<T,
E>
-
equals
-
hashCode
public int hashCode() -
toString
-
iterator
Returns an iterator containing a single value - the content of thisOk
. -
stream
Returns a stream containing a single value - the content of thisOk
.
-
getErrOrThrow()
onOk
always fails.