AbstractResolvableFuture

public abstract class AbstractResolvableFuture
extends Object implements ListenableFuture<V>

java.lang.Object
   ↳ androidx.concurrent.futures.AbstractResolvableFuture<V>


An AndroidX version of Guava's AbstractFuture.

An abstract implementation of ListenableFuture, intended for advanced users only. A more common ways to create a ListenableFuture is to instantiate ResolvableFuture.

This class implements all methods in ListenableFuture. Subclasses should provide a way to set the result of the computation through the protected methods set(Object), setFuture(ListenableFuture) and setException(Throwable). Subclasses may also override afterDone(), which will be invoked automatically when the future completes. Subclasses should rarely override other methods.

Summary

Protected constructors

AbstractResolvableFuture()

Constructor for use by subclasses.

Public methods

final void addListener(Runnable listener, Executor executor)

final boolean cancel(boolean mayInterruptIfRunning)

If a cancellation attempt succeeds on a Future that had previously been set asynchronously, then the cancellation will also be propagated to the delegate Future that was supplied in the setFuture call.

final V get(long timeout, TimeUnit unit)

The default AbstractResolvableFuture implementation throws InterruptedException if the current thread is interrupted during the call, even if the value is already available.

final V get()

The default AbstractResolvableFuture implementation throws InterruptedException if the current thread is interrupted during the call, even if the value is already available.

final boolean isCancelled()
final boolean isDone()
String toString()

Protected methods

void afterDone()

Callback method that is called exactly once after the future is completed.

void interruptTask()

Subclasses can override this method to implement interruption of the future's computation.

String pendingToString()

Provide a human-readable explanation of why this future has not yet completed.

boolean set(V value)

Sets the result of this Future unless this Future has already been cancelled or set (including set asynchronously).

boolean setException(Throwable throwable)

Sets the failed result of this Future unless this Future has already been cancelled or set (including set asynchronously).

boolean setFuture(ListenableFuture<? extends V> future)

Sets the result of this Future to match the supplied input Future once the supplied Future is done, unless this Future has already been cancelled or set (including "set asynchronously," defined below).

final boolean wasInterrupted()

Returns true if this future was cancelled with mayInterruptIfRunning set to true.

Inherited methods

Protected constructors

AbstractResolvableFuture

protected AbstractResolvableFuture ()

Constructor for use by subclasses.

Public methods

addListener

public final void addListener (Runnable listener, 
                Executor executor)

Parameters
listener Runnable

executor Executor

cancel

public final boolean cancel (boolean mayInterruptIfRunning)

If a cancellation attempt succeeds on a Future that had previously been set asynchronously, then the cancellation will also be propagated to the delegate Future that was supplied in the setFuture call.

Rather than override this method to perform additional cancellation work or cleanup, subclasses should override afterDone(), consulting isCancelled() and wasInterrupted() as necessary. This ensures that the work is done even if the future is cancelled without a call to cancel, such as by calling setFuture(cancelledFuture).

Parameters
mayInterruptIfRunning boolean

Returns
boolean

get

public final V get (long timeout, 
                TimeUnit unit)

The default AbstractResolvableFuture implementation throws InterruptedException if the current thread is interrupted during the call, even if the value is already available.

Parameters
timeout long

unit TimeUnit

Returns
V

Throws
CancellationException
InterruptedException
TimeoutException
ExecutionException

get

public final V get ()

The default AbstractResolvableFuture implementation throws InterruptedException if the current thread is interrupted during the call, even if the value is already available.

Returns
V

Throws
CancellationException
InterruptedException
ExecutionException

isCancelled

public final boolean isCancelled ()

Returns
boolean

isDone

public final boolean isDone ()

Returns
boolean

toString

public String toString ()

Returns
String

Protected methods

afterDone

protected void afterDone ()

Callback method that is called exactly once after the future is completed.

If interruptTask() is also run during completion, afterDone() runs after it.

The default implementation of this method in AbstractFuture does nothing. This is intended for very lightweight cleanup work, for example, timing statistics or clearing fields. If your task does anything heavier consider, just using a listener with an executor.

interruptTask

protected void interruptTask ()

Subclasses can override this method to implement interruption of the future's computation. The method is invoked automatically by a successful call to cancel(true).

The default implementation does nothing.

This method is likely to be deprecated. Prefer to override afterDone(), checking wasInterrupted() to decide whether to interrupt your task.

pendingToString

protected String pendingToString ()

Provide a human-readable explanation of why this future has not yet completed.

Returns
String null if an explanation cannot be provided because the future is done.

set

protected boolean set (V value)

Sets the result of this Future unless this Future has already been cancelled or set (including set asynchronously). When a call to this method returns, the Future is guaranteed to be done only if the call was accepted (in which case it returns true). If it returns false, the Future may have previously been set asynchronously, in which case its result may not be known yet. That result, though not yet known, cannot be overridden by a call to a set* method, only by a call to cancel(boolean).

Parameters
value V: the value to be used as the result

Returns
boolean true if the attempt was accepted, completing the Future

setException

protected boolean setException (Throwable throwable)

Sets the failed result of this Future unless this Future has already been cancelled or set (including set asynchronously). When a call to this method returns, the Future is guaranteed to be done only if the call was accepted (in which case it returns true). If it returns false, the Future may have previously been set asynchronously, in which case its result may not be known yet. That result, though not yet known, cannot be overridden by a call to a set* method, only by a call to cancel(boolean).

Parameters
throwable Throwable: the exception to be used as the failed result

Returns
boolean true if the attempt was accepted, completing the Future

setFuture

protected boolean setFuture (ListenableFuture<? extends V> future)

Sets the result of this Future to match the supplied input Future once the supplied Future is done, unless this Future has already been cancelled or set (including "set asynchronously," defined below).

If the supplied future is done when this method is called and the call is accepted, then this future is guaranteed to have been completed with the supplied future by the time this method returns. If the supplied future is not done and the call is accepted, then the future will be set asynchronously. Note that such a result, though not yet known, cannot be overridden by a call to a set* method, only by a call to cancel(boolean).

If the call setFuture(delegate) is accepted and this Future is later cancelled, cancellation will be propagated to delegate. Additionally, any call to setFuture after any cancellation will propagate cancellation to the supplied Future.

Note that, even if the supplied future is cancelled and it causes this future to complete, it will never trigger interruption behavior. In particular, it will not cause this future to invoke the interruptTask() method, and the wasInterrupted() method will not return true.

Parameters
future ListenableFuture: the future to delegate to

Returns
boolean true if the attempt was accepted, indicating that the Future was not previously cancelled or set.

wasInterrupted

protected final boolean wasInterrupted ()

Returns true if this future was cancelled with mayInterruptIfRunning set to true.

Returns
boolean