public interface AutoAsync<T> extends Async<T>
Often we need to convert a `Foo` object to an `Async<Foo>` object. This can be done by `Async.success(foo)`; but it can become tedious if it happens a lot.
By simply `extends/implements AutoAsync<Foo>`, we can make an `interface/class Bar` a subtype of `Async<Foo>`, then a `Bar` object can be used anywhere an `Async<Foo>` is needed. Note that `AutoAsync` is an interface with no abstract method.
Requirement: if `interface/class Bar extends/implements AutoAsync<Foo>`, it must be true that `Bar` is `Foo` or a subtype of `Foo`.
Caution: Bar inherits all instance methods from Async<Foo>, which can create confusions along with Bar's own methods.
One usage example is `HttpResponseImpl`:
public class HttpResponseImpl implements HttpResponse, AutoAsync<HttpResponse>
this means that an HttpResponseImpl object can be used any where an Async<HttpResponse> is needed.
Default Methods | |
---|---|
Result<T> |
pollResult()
Implements
Async.pollResult() . |
boolean |
isCompleted()
return `true`
|
void |
onCompletion(Consumer<Result<T>> callback)
Implements
Async.onCompletion(Consumer) . |
void |
cancel(Exception reason)
Implements
Async.cancel(Exception) ;
by default, do nothing. |
Async<T> |
timeout(Duration duration)
Implements
Async.timeout(Duration) ;
by default, do nothing. |
default Result<T> pollResult()
Async.pollResult()
.
Equivalent to `Result.success( (T)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 void cancel(Exception reason)
Async.cancel(Exception)
;
by default, do nothing.