Class AsyncResource<V>

java.lang.Object
java.util.Observable
com.codename1.util.AsyncResource<V>
Direct Known Subclasses:
AsyncMedia.PauseRequest, AsyncMedia.PlayRequest, BrowserWindow.EvalRequest, MessageEvent.PromptPromise, Oauth2.RefreshTokenRequest

public class AsyncResource<V> extends Observable
A wrapper for an object that needs to be loaded asynchronously. This can serve as a handle for the object to be passed around irrespective of whether the object has finished loading. Conceptually this is very similar to Futures and Promises.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Exception to wrap exceptions that are thrown during asynchronous execution.
    static class 
    Exception thrown when the AsyncResource is cancelled.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds another AsyncResource as a listener to this async resource.
    all(AsyncResource<?>... resources)
    Creates a single AsyncResource that will fire its ready() only when all of the provided resources are ready.
    all(Collection<AsyncResource<?>> resources)
    Creates a single AsyncResource that will fire its ready() only when all of the provided resources are ready.
    Wraps this AsyncResource object as a Promise
    void
    Waits and blocks until this AsyncResource is done.
    static void
    await(AsyncResource<?>... resources)
    Waits for a set of AsyncResources to be complete.
    static void
    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
    Sets the error for this resource in the case that it could not be loaded.
    Sets callback to run if an error occurs.
    Sets callback to run if an error occurs.
    get()
    Gets the resource synchronously.
    get(int timeout)
    Gets the resource synchronously.
    get(V defaultVal)
    Gets the resource if it is ready.
    boolean
    Checks if the resource loading was cancelled.
    static boolean
    Returns true if the provided throwable was caused by a cancellation of an AsyncResource.
    boolean
    Checks if the resource loading is done.
    boolean
    Checks if the resource is ready.
    void
    onResult(AsyncResult<V> onResult)
    Combines ready() and except() into a single callback with 2 parameters.
    ready(SuccessCallback<V> callback)
    Runs the provided callback when the resource is ready.
    Runs the provided callback when the resource is ready.
    void
    Wait for loading to complete.

    Methods inherited from class Observable

    addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
    Modifier and Type
    Method
    Description
    void
    Adds the specified observer to the list of observers.
    protected void
    Clears the changed flag for this Observable.
    int
    Returns the number of observers registered to this Observable.
    void
    Removes the specified observer from the list of observers.
    void
    Removes all observers from the list of observers.
    boolean
    Returns the changed flag for this Observable.
    void
    If hasChanged() returns true, calls the update() method for every observer in the list of observers using null as the argument.
    void
    If hasChanged() returns true, calls the update() method for every Observer in the list of observers using the specified argument.
    protected void
    Sets the changed flag for this Observable.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    Modifier and Type
    Method
    Description
    protected Object
     
    boolean
    Indicates whether some other object is "equal to" this one.
    final Class
    Returns the runtime class of an object.
    int
    Returns a hash code value for the object.
    final void
    Wakes up a single thread that is waiting on this object's monitor.
    final void
    Wakes up all threads that are waiting on this object's monitor.
    Returns a string representation of the object.
    final void
    Causes current thread to wait until another thread invokes the method or the method for this object.
    final void
    wait(long timeout)
    Causes current thread to wait until either another thread invokes the method or the method for this object, or a specified amount of time has elapsed.
    final void
    wait(long timeout, int nanos)
    Causes current thread to wait until another thread invokes the method or the method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
  • Constructor Details

    • AsyncResource

      public AsyncResource()
  • Method Details

    • isCancelled

      public static boolean isCancelled(Throwable t)

      Returns true if the provided throwable was caused by a cancellation of an AsyncResource.

      Parameters
      • t: The exception to check for a cancellation.
      Returns

      True if the exception was caused by cancelling an AsyncResource.

      Since

      7.0

    • all

      public static AsyncResource<Boolean> all(AsyncResource<?>... resources)

      Creates a single AsyncResource that will fire its ready() only when all of the provided resources are ready. And will fire an exception if any of the provided resources fires an exception.

      Parameters
      • resources: One ore more resources to wrap.
      Returns

      A combined AsyncResource.

      Since

      7.0

    • all

      public 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. And will fire an exception if any of the provided resources fires an exception.

      Parameters
      • resources: One ore more resources to wrap.
      Returns

      A combined AsyncResource.

      Since

      7.0

    • await

      public static void await(Collection<AsyncResource<?>> resources) throws AsyncResource.AsyncExecutionException

      Waits for a set of AsyncResources to be complete. If any of them fires an exception, then this method will throw a RuntimeException with that exception as the cause.

      Parameters
      • resources: The resources to wait for.
      Since

      7.0

      Throws:
      AsyncResource.AsyncExecutionException
    • await

      public static void await(AsyncResource<?>... resources) throws AsyncResource.AsyncExecutionException

      Waits for a set of AsyncResources to be complete. If any of them fires an exception, then this method will throw a RuntimeException with that exception as the cause.

      Parameters
      • resources: The resources to wait for.
      Since

      7.0

      Throws:
      AsyncResource.AsyncExecutionException
    • cancel

      public boolean cancel(boolean mayInterruptIfRunning)

      Cancels loading the resource.

      Parameters
      • mayInterruptIfRunning
      Returns

      True if the resource loading was cancelled. False if the loading was already done.

    • waitFor

      public void waitFor()
      Wait for loading to complete. If on EDT, this will use invokeAndBlock to safely block until loading is complete.
    • get

      public V get()

      Gets the resource synchronously. This will wait until either the resource failed with an exception, or the loading was canceled, or was done without error.

      If on edt, this uses invokeAndBlock to block safely.

      Returns

      The wrapped resource.

      Throws
      • AsyncExecutionException: if the resource failed with an error. To get the actual error, use Throwable#getCause().
    • get

      public V get(int timeout) throws InterruptedException

      Gets the resource synchronously. This will wait until either the resource failed with an exception, or the loading was canceled, or was done without error.

      If on edt, this uses invokeAndBlock to block safely.

      Parameters
      • timeout: Timeout
      Returns

      The wrapped resource.

      Throws
      • AsyncExecutionException: if the resource failed with an error. To get the actual error, use Throwable#getCause().

      • InterruptedException: if timeout occurs.

      Throws:
      InterruptedException
    • get

      public V get(V defaultVal)

      Gets the resource if it is ready. If it is not ready, then it will simply return the provided defaultVal.

      Parameters
      • defaultVal
      Returns

      Either the resource value, or the provided default.

    • isCancelled

      public boolean isCancelled()
      Checks if the resource loading was cancelled.
    • isDone

      public boolean isDone()
      Checks if the resource loading is done. This will be true even if the resource loading failed with an error.
    • isReady

      public boolean isReady()
      Checks if the resource is ready.
    • ready

      public AsyncResource<V> ready(SuccessCallback<V> callback, EasyThread t)

      Runs the provided callback when the resource is ready.

      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.

      Parameters
      • callback: Callback to run when the resource is ready.

      • t: Optional EasyThread on which the callback should be run.

      Returns

      Self for chaining

    • ready

      public AsyncResource<V> ready(SuccessCallback<V> callback)

      Runs the provided callback when the resource is ready.

      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.

      Parameters
      • callback: The callback to be run when the resource is ready.
      Returns

      Self for chaining.

    • except

      public AsyncResource<V> except(SuccessCallback<Throwable> callback, EasyThread t)

      Sets callback to run if an error occurs.

      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.

      Parameters
      • callback: Callback to run on error.

      • t: Optional EasyThread to run callback on.

      Returns

      Self for chaining.

    • except

      public AsyncResource<V> except(SuccessCallback<Throwable> callback)

      Sets callback to run if an error occurs. 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 error() method is invoked on.

      Parameters
      • callback: The callback to run in case of error.
    • complete

      public void complete(V value)

      Sets the resource value. This will trigger the ready callbacks to be run.

      Parameters
      • value: The value to set for the resource.
    • error

      public void error(Throwable t)

      Sets the error for this resource in the case that it could not be loaded. This will trigger the error callbacks.

      Parameters
      • t
    • await

      public void await() throws AsyncResource.AsyncExecutionException

      Waits and blocks until this AsyncResource is done.

      Throws
      • com.codename1.util.AsyncResource.AsyncExecutionException
      Throws:
      AsyncResource.AsyncExecutionException
    • addListener

      public void addListener(AsyncResource<V> resource)

      Adds another AsyncResource as a listener to this async resource.

      Parameters
      • resource
      Since

      7.0

    • onResult

      public void onResult(AsyncResult<V> onResult)

      Combines ready() and except() into a single callback with 2 parameters.

      Parameters
      • onResult: @param onResult A callback that handles both the ready() case and the except() case. Use #isCancelled(java.lang.Throwable) to test the error parameter of java.lang.Throwable) to see if if was caused by a cancellation.
      Since

      7.0

    • asPromise

      public Promise<V> asPromise()

      Wraps this AsyncResource object as a Promise

      Returns

      A Promise wrapping this AsyncResource.

      Since

      8.0