public interface Result<T> extends Async<T>
A success Result contains a value of type T
.
A failure Result contains an exception of type Exception
See static methods
Result.success(value)
and Result.failure(exception)
to create Result objects.
Every Result
is also an Async
that's immediately completed.
A Result
object can be used wherever an Async
is expected.
Note: A success value could be null
;
however, a failure exception must be non-null.
Note: In failure, the type of the exception is Exception
,
not the more general Throwable
.
We consider that Error
indicates something catastrophic,
which is not a usual result of any action.
Therefore we do not handle Error
in Result
.
Abstract Methods | |
---|---|
boolean |
isSuccess()
Whether this result is a success.
|
boolean |
isFailure()
Whether this result is a failure.
|
T |
getValue()
Return the value if this result is a success; otherwise return null.
|
Exception |
getException()
Return the exception if this result is a failure; otherwise return null.
|
Default Methods | |
T |
getOrThrow()
Return the value if this result is a success; otherwise throw the exception.
|
Result<T> |
pollResult()
Implements
Async.pollResult() ; by default, return `this` . |
boolean |
isCompleted()
return `true`
|
void |
onCompletion(Consumer<Result<T>> callback)
Implements
Async.onCompletion(Consumer) . |
void |
cancel(Exception reason)
|
Result<T> |
timeout(Duration duration)
|
Static Methods | |
<T> Result<T> |
success(T value)
Create a success Result with the
value . |
<T> Result<T> |
failure(Exception exception)
Create a failure Result with the
exception . |
<T> Result<T> |
call(Callable<T> action)
Invoke
action.call() , convert the outcome to a Result . |
boolean isSuccess()
boolean isFailure()
T getValue()
Note that null
could be legitimate success value.
Exception getException()
Note that if this method returns null
,
this result must be a success.
default T getOrThrow() throws Exception
Exception
default Result<T> pollResult()
Async.pollResult()
; by default, return `this`
.pollResult
in interface Async<T>
default boolean isCompleted()
isCompleted
in interface Async<T>
default void onCompletion(Consumer<Result<T>> callback)
Async.onCompletion(Consumer)
.
Note that `this` is already completed.onCompletion
in interface Async<T>
default Result<T> timeout(Duration duration)
Async.timeout(Duration)
;
by default, do nothing, because a Result
is a completed Async
.
Returns `this`.static <T> Result<T> success(T value)
value
.
Example Usage:
Result<Integer> result = Result.success(42);
value
- the success value. Can be null.static <T> Result<T> failure(Exception exception)
exception
.
Example Usage:
Result<Integer> result = Result.failure(new IOException("cannot read file"));
exception
- the failure exception. Must be non-null.static <T> Result<T> call(Callable<T> action)
action.call()
, convert the outcome to a Result
.
If action.call()
returns a value, this method returns a success Result with the same value.
If action.call()
throws an Exception,
this method returns a failure Result with the same exception.
Example Usage:
Result<Integer> result = Result.call( ()->Integer.parseInt("0.1") ); // result is a failure with a NumberFormatException
See Callable_Void
in case the `action`
argument is a void-returning lambda expression or
method reference.
Note: Result does not handle Error
.
If action.call()
throws an Error, this method throws the same error.