# CallbackToFutureAdapter.Completer

Artifact: [androidx.concurrent:concurrent-futures](https://developer.android.com/jetpack/androidx/releases/concurrent) [View Source](https://cs.android.com/search?q=file:androidx/concurrent/futures/CallbackToFutureAdapter.java+class:androidx.concurrent.futures.CallbackToFutureAdapter.Completer) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/concurrent#1.0.0)

*** ** * ** ***


```
public final class CallbackToFutureAdapter.Completer<T>
```

<br />

*** ** * ** ***

Used to complete the future returned by `https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter#getFuture(androidx.concurrent.futures.CallbackToFutureAdapter.Resolver<T>)`

## Summary

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter.Completer#addCancellationListener(java.lang.Runnable,java.util.concurrent.Executor)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/Runnable.html runnable, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/concurrent/Executor.html executor )` Use to propagate cancellation from the future to whatever operation is using this Completer. |
| `boolean` | `https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter.Completer#set(T)(T value)` Sets the result of the `Future` unless the `Future` has already been cancelled or set. |
| `boolean` | `https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter.Completer#setCancelled()()` Cancels `Future` unless the `Future` has already been cancelled or set. |
| `boolean` | `https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter.Completer#setException(java.lang.Throwable)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/Throwable.html t)` Sets the failed result of the `Future` unless the `Future` has already been cancelled or set. |

| ### Protected methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter.Completer#finalize()()` |

## Public methods

### addCancellationListener

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/concurrent#1.0.0)

```
public void addCancellationListener(
    @NonNull Runnable runnable,
    @NonNull Executor executor
)
```

Use to propagate cancellation from the future to whatever operation is using this Completer.

Will be called when the returned Future is cancelled by `https://developer.android.com/reference/java/util/concurrent/Future.html#cancel(boolean)` or this `Completer` object is garbage collected before the future completes. Not triggered by `https://developer.android.com/reference/androidx/concurrent/futures/CallbackToFutureAdapter.Completer#setCancelled()`.

### set

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/concurrent#1.0.0)

```
public boolean set(T value)
```

Sets the result of the `Future` unless the `Future` has already been cancelled or set. When a call to this method returns, the `Future` is guaranteed to be done.

| Parameters |
|---|---|
| `T value` | the value to be used as the result |

| Returns |
|---|---|
| `boolean` | true if this attempt completed the `Future`, false if it was already complete |

### setCancelled

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/concurrent#1.0.0)

```
public boolean setCancelled()
```

Cancels `Future` unless the `Future` has already been cancelled or set. When a call to this method returns, the `Future` is guaranteed to be done.

| Returns |
|---|---|
| `boolean` | true if this attempt completed the `Future`, false if it was already complete |

### setException

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/concurrent#1.0.0)

```
public boolean setException(@NonNull Throwable t)
```

Sets the failed result of the `Future` unless the `Future` has already been cancelled or set. When a call to this method returns, the `Future` is guaranteed to be done.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/Throwable.html t` | the exception to be used as the failed result |

| Returns |
|---|---|
| `boolean` | true if this attempt completed the `Future`, false if it was already complete |

## Protected methods

### finalize

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/concurrent#1.0.0)

```
protected void finalize()
```