R
- the result value typepublic interface Result<R> extends Serializable
Result instances are created using the factory methods ok(Object)
and error(String)
, denoting success and failure respectively.
Unless otherwise specified, Result
method arguments cannot be null.
Modifier and Type | Method and Description |
---|---|
static <R> Result<R> |
error(String message)
Returns a failure result wrapping the given error message.
|
<S> Result<S> |
flatMap(SerializableFunction<R,Result<S>> mapper)
If this Result has a value, applies the given Result-returning function
to the value.
|
Optional<String> |
getMessage()
Returns an Optional of the result message, or an empty Optional if none.
|
<X extends Throwable> |
getOrThrow(SerializableFunction<String,? extends X> exceptionProvider)
Return the value, if the result denotes success, otherwise throw an
exception to be created by the provided supplier.
|
void |
handle(SerializableConsumer<R> ifOk,
SerializableConsumer<String> ifError)
Invokes either the first callback or the second one, depending on whether
this Result denotes a success or a failure, respectively.
|
default void |
ifError(SerializableConsumer<String> consumer)
Applies the
consumer if result is an error. |
default void |
ifOk(SerializableConsumer<R> consumer)
Applies the
consumer if result is not an error. |
boolean |
isError()
Checks if the result denotes an error.
|
default <S> Result<S> |
map(SerializableFunction<R,S> mapper)
If this Result has a value, returns a Result of applying the given
function to the value.
|
static <R> Result<R> |
of(SerializableSupplier<R> supplier,
SerializableFunction<Exception,String> onError)
Returns a Result representing the result of invoking the given supplier.
|
static <R> Result<R> |
ok(R value)
Returns a successful result wrapping the given value.
|
static <R> Result<R> ok(R value)
R
- the result value typevalue
- the result value, can be nullstatic <R> Result<R> error(String message)
R
- the result value typemessage
- the error messagestatic <R> Result<R> of(SerializableSupplier<R> supplier, SerializableFunction<Exception,String> onError)
Result.ok
of the
value; if an exception is thrown, returns the message in a
Result.error
.R
- the result value typesupplier
- the supplier to runonError
- the function to provide the error messagedefault <S> Result<S> map(SerializableFunction<R,S> mapper)
S
- the type of the mapped valuemapper
- the mapping function<S> Result<S> flatMap(SerializableFunction<R,Result<S>> mapper)
S
- the type of the mapped valuemapper
- the mapping functionvoid handle(SerializableConsumer<R> ifOk, SerializableConsumer<String> ifError)
ifOk
- the function to call if successifError
- the function to call if failuredefault void ifOk(SerializableConsumer<R> consumer)
consumer
if result is not an error.consumer
- consumer to apply in case it's not an errordefault void ifError(SerializableConsumer<String> consumer)
consumer
if result is an error.consumer
- consumer to apply in case it's an errorboolean isError()
true
if the result denotes an error,
false
otherwiseOptional<String> getMessage()
<X extends Throwable> R getOrThrow(SerializableFunction<String,? extends X> exceptionProvider) throws X extends Throwable
X
- Type of the exception to be thrownexceptionProvider
- The provider which will return the exception to be thrown
based on the given error messageX
- if this result denotes an errorX extends Throwable
Copyright © 2019. All rights reserved.