public class AsyncResource<V> extends Observable
Modifier and Type | Class and Description |
---|---|
static class |
AsyncResource.AsyncExecutionException
Exception to wrap exceptions that are thrown during asynchronous execution.
|
static class |
AsyncResource.CancellationException
Exception thrown when the AsyncResource is cancelled.
|
Constructor and Description |
---|
AsyncResource() |
Modifier and Type | Method and Description |
---|---|
void |
addListener(AsyncResource<V> resource)
Adds another AsyncResource as a listener to this async resource.
|
static AsyncResource<Boolean> |
all(AsyncResource<?>... resources)
Creates a single AsyncResource that will fire its ready() only when all of the provided resources
are ready.
|
static AsyncResource<Boolean> |
all(Collection<AsyncResource<?>> resources)
Creates a single AsyncResource that will fire its ready() only when all of the provided resources
are ready.
|
Promise<V> |
asPromise()
Wraps this AsyncResource object as a
Promise |
void |
await()
Waits and blocks until this AsyncResource is done.
|
static void |
await(AsyncResource<?>... resources)
Waits for a set of AsyncResources to be complete.
|
static void |
await(Collection<AsyncResource<?>> resources)
Waits for a set of AsyncResources to be complete.
|
boolean |
cancel(boolean mayInterruptIfRunning)
Cancels loading the resource.
|
void |
complete(V value)
Sets the resource value.
|
void |
error(Throwable t)
Sets the error for this resource in the case that it could not be loaded.
|
AsyncResource<V> |
except(SuccessCallback<Throwable> callback)
Sets callback to run if an error occurs.
|
AsyncResource<V> |
except(SuccessCallback<Throwable> callback,
EasyThread t)
Sets callback to run if an error occurs.
|
V |
get()
Gets the resource synchronously.
|
V |
get(int timeout)
Gets the resource synchronously.
|
V |
get(V defaultVal)
Gets the resource if it is ready.
|
boolean |
isCancelled()
Checks if the resource loading was cancelled.
|
static boolean |
isCancelled(Throwable t)
Returns true if the provided throwable was caused by a cancellation of an AsyncResource.
|
boolean |
isDone()
Checks if the resource loading is done.
|
boolean |
isReady()
Checks if the resource is ready.
|
void |
onResult(AsyncResult<V> onResult)
Combines ready() and except() into a single callback with 2 parameters.
|
AsyncResource<V> |
ready(SuccessCallback<V> callback)
Runs the provided callback when the resource is ready.
|
AsyncResource<V> |
ready(SuccessCallback<V> callback,
EasyThread t)
Runs the provided callback when the resource is ready.
|
void |
waitFor()
Wait for loading to complete.
|
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
public boolean cancel(boolean mayInterruptIfRunning)
mayInterruptIfRunning
- public void waitFor()
public V get()
If on edt, this uses invokeAndBlock to block safely.
AsyncResource.AsyncExecutionException
- if the resource failed with an error. To get the actual error, use Throwable.getCause()
.public V get(int timeout) throws InterruptedException
If on edt, this uses invokeAndBlock to block safely.
timeout
- TimeoutAsyncResource.AsyncExecutionException
- if the resource failed with an error. To get the actual error, use Throwable.getCause()
.InterruptedException
- if timeout occurs.public static boolean isCancelled(Throwable t)
t
- The exception to check for a cancellation.public V get(V defaultVal)
defaultVal
- public boolean isCancelled()
public boolean isDone()
public boolean isReady()
public AsyncResource<V> ready(SuccessCallback<V> callback, EasyThread t)
If an EasyThread
is provided, then the callback will be run on that
thread. If an EasyThread is not provided, and this call is made on the EDT, then
the callback will be run on the EDT. Otherwise, the callback will occur on
whatever thread the complete(java.lang.Object)
call is called on.
callback
- Callback to run when the resource is ready.t
- Optional EasyThread on which the callback should be run.public AsyncResource<V> ready(SuccessCallback<V> callback)
If this call is made on the EDT, then the callback will be run on the EDT. Otherwise, it will be run on whatever thread the complete() methdo is invoked on.
callback
- The callback to be run when the resource is ready.public AsyncResource<V> except(SuccessCallback<Throwable> callback, EasyThread t)
If an EasyThread
is provided, then the callback will be run on that
thread. If an EasyThread is not provided, and this call is made on the EDT, then
the callback will be run on the EDT. Otherwise, the callback will occur on
whatever thread the complete(java.lang.Object)
call is called on.
callback
- Callback to run on error.t
- Optional EasyThread to run callback on.public AsyncResource<V> except(SuccessCallback<Throwable> callback)
callback
- The callback to run in case of error.public void complete(V value)
value
- The value to set for the resource.public void error(Throwable t)
t
- public static AsyncResource<Boolean> all(AsyncResource<?>... resources)
resources
- One ore more resources to wrap.public static AsyncResource<Boolean> all(Collection<AsyncResource<?>> resources)
resources
- One ore more resources to wrap.public static void await(Collection<AsyncResource<?>> resources) throws AsyncResource.AsyncExecutionException
resources
- The resources to wait for.AsyncResource.AsyncExecutionException
public void await() throws AsyncResource.AsyncExecutionException
public static void await(AsyncResource<?>... resources) throws AsyncResource.AsyncExecutionException
resources
- The resources to wait for.AsyncResource.AsyncExecutionException
public void addListener(AsyncResource<V> resource)
resource
- public void onResult(AsyncResult<V> onResult)
onResult
- A callback that handles both the ready() case and the except() case. Use isCancelled(java.lang.Throwable)
to test the error parameter of AsyncResult.onReady(java.lang.Object, java.lang.Throwable)
to see if
if was caused by a cancellation.