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

CallbackToFutureAdapter

public final class CallbackToFutureAdapter
extends Object

java.lang.Object
   ↳ androidx.concurrent.futures.CallbackToFutureAdapter


A utility that provides safety checks as an alternative to ResolvableFuture, failing the future if it will never complete. Useful for adapting interfaces that take callbacks into interfaces that return ListenableFuture.

Example:

return CallbackToFutureAdapter.getFuture(
    completer -> {
      Callback myCallback = foo -> completer.set(foo);
      someObject.getFoo(myCallback);
      return myCallback;
    });
 
Try to avoid creating references from listeners on the returned Future to the CallbackToFutureAdapter.Completer or the passed-in tag object, as this will defeat the best-effort early failure detection based on garbage collection.

Summary

Nested classes

class CallbackToFutureAdapter.Completer<T>

Used to complete the future returned by CallbackToFutureAdapter.getFuture(CallbackToFutureAdapter.Resolver)  

interface CallbackToFutureAdapter.Resolver<T>

Called by CallbackToFutureAdapter.getFuture(CallbackToFutureAdapter.Resolver)

Public methods

static <T> ListenableFuture<T> getFuture(Resolver<T> callback)

Inherited methods

Public methods

getFuture

public static ListenableFuture<T> getFuture (Resolver<T> callback)

Parameters
callback Resolver

Returns
ListenableFuture<T>