Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

AbstractResolvableFuture

abstract class AbstractResolvableFuture<V : Any!> : ListenableFuture<V>
kotlin.Any
   ↳ androidx.concurrent.futures.AbstractResolvableFuture

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), 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

Constructor for use by subclasses.

Public methods
Unit
addListener(listener: Runnable!, executor: Executor!)

Boolean
cancel(mayInterruptIfRunning: Boolean)

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.

V
get(timeout: Long, unit: TimeUnit!)

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

V
get()

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

Boolean

open String

Boolean

Protected methods
open String?

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

open Boolean
setException(throwable: Throwable!)

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

open Boolean
setFuture(future: ListenableFuture<out V>!)

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).

open Boolean
set(@Nullable value: V?)

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

Boolean

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

open Unit

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

open Unit

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

Protected constructors

<init>

protected AbstractResolvableFuture()

Constructor for use by subclasses.

Public methods

addListener

fun addListener(listener: Runnable!, executor: Executor!): Unit

cancel

fun cancel(mayInterruptIfRunning: Boolean): Boolean

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 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).

get

fun get(timeout: Long, unit: TimeUnit!): V

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

Exceptions
CancellationException

get

fun get(): V

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

Exceptions
CancellationException

isCancelled

fun isCancelled(): Boolean

toString

open fun toString(): String

isDone

fun isDone(): Boolean

Protected methods

pendingToString

@Nullable protected open fun pendingToString(): String?

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

Return
String?: null if an explanation cannot be provided because the future is done.

setException

protected open fun setException(throwable: Throwable!): Boolean

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.

Parameters
throwable Throwable!: the exception to be used as the failed result
Return
Boolean: true if the attempt was accepted, completing the Future

setFuture

protected open fun setFuture(future: ListenableFuture<out V>!): Boolean

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.

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<out V>!: the future to delegate to
Return
Boolean: true if the attempt was accepted, indicating that the Future was not previously cancelled or set.

set

protected open fun set(@Nullable value: V?): Boolean

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.

Parameters
value V?: the value to be used as the result
Return
Boolean: true if the attempt was accepted, completing the Future

wasInterrupted

protected fun wasInterrupted(): Boolean

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

afterDone

protected open fun afterDone(): Unit

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 open fun interruptTask(): Unit

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.