Interface Completer<T>

Type Parameters:
T - type of the result of the computation

public interface Completer<T>
Creator and controller of a Future. An asynchronous computation that produces a future has to create a completer and return its future object. Afterwards, the completer is used to complete the future either with a value, using Completer.complete(), or with an error, using Completer.completeWithError().

If the completer is supplied a cancellation callback using onCancel(Runnable), a successful cancellation request on the future calls the cancellation callback.

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    complete(T value)
    Completes the future with a value, if pending.
    void
    Completes the future with an error, if pending.
    static <T> Completer<T>
    Creates a new completer.
    Returns the future created and controlled by this completer.
    void
    onCancel(Runnable cancellationCallback)
    Sets the cancellation callback.
  • Method Details

    • create

      static <T> Completer<T> create()
      Creates a new completer.
      Type Parameters:
      T - type of the result of the computation
      Returns:
      a new completer; never null
    • complete

      void complete(T value)
      Completes the future with a value, if pending. If the future is already complete, does nothing.
      Parameters:
      value - the value with which the future is completed; may be null
    • completeWithError

      void completeWithError(Throwable error)
      Completes the future with an error, if pending. If the future is already complete, does nothing.
      Parameters:
      error - the error with which the future is completed; must not be null
    • onCancel

      void onCancel(Runnable cancellationCallback)
      Sets the cancellation callback. Note that this method may be called at most once; subsequent calls will result in an exception.
      Parameters:
      cancellationCallback - the cancellation callback; may not be null
    • future

      Future<T> future()
      Returns the future created and controlled by this completer.
      Returns:
      the future; never null