Class Futures

java.lang.Object
org.eclipse.hono.util.Futures

public final class Futures extends Object
Helper class working with Java and vert.x futures.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Interface representing blocking code to be executed.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> io.vertx.core.Future<T>
    create(Supplier<CompletionStage<T>> futureSupplier)
    Create a completion stage and map the outcome to a vert.x future.
    static <T> io.vertx.core.Future<T>
    executeBlocking(io.vertx.core.Vertx vertx, Futures.BlockingCode<T> blocking)
    Use Vertx.executeBlocking(Handler, Handler) with Futures.
    static <T> io.vertx.core.Future<T>
    executeOnContextWithSameRoot(io.vertx.core.Context requiredContextOrContextRoot, io.vertx.core.Handler<io.vertx.core.Promise<T>> codeToRun)
    Executes some code, ensuring it is run on the given context or a duplicated context of the given context.
    static <T> io.vertx.core.Handler<io.vertx.core.AsyncResult<T>>
    onCurrentContextCompletionHandler(io.vertx.core.Handler<io.vertx.core.AsyncResult<T>> handlerToComplete)
    Gets a handler that completes the given handler on the current Vert.x context (if set).
    static <T> void
    tryHandleResult(io.vertx.core.Promise<T> promise, io.vertx.core.AsyncResult<T> asyncResult)
    Applies the given result on the given promise using Promise.tryComplete(Object) or Promise.tryFail(Throwable).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • create

      public static <T> io.vertx.core.Future<T> create(Supplier<CompletionStage<T>> futureSupplier)
      Create a completion stage and map the outcome to a vert.x future.

      If the supplier fails providing a future, then the failure will be reported on the returned future as well.

      Type Parameters:
      T - The result data type.
      Parameters:
      futureSupplier - The supplier of the future.
      Returns:
      A vert.x future, never returns null.
      Throws:
      NullPointerException - if futureSupplier is null.
    • executeBlocking

      public static <T> io.vertx.core.Future<T> executeBlocking(io.vertx.core.Vertx vertx, Futures.BlockingCode<T> blocking)
      Use Vertx.executeBlocking(Handler, Handler) with Futures.
      Type Parameters:
      T - The type of the result.
      Parameters:
      vertx - The vertx context.
      blocking - The blocking code.
      Returns:
      The future, reporting the result.
      Throws:
      NullPointerException - if any of the parameters is null.
    • executeOnContextWithSameRoot

      public static <T> io.vertx.core.Future<T> executeOnContextWithSameRoot(io.vertx.core.Context requiredContextOrContextRoot, io.vertx.core.Handler<io.vertx.core.Promise<T>> codeToRun)
      Executes some code, ensuring it is run on the given context or a duplicated context of the given context.

      If invoked from a context that is either equal to the given context or a duplicated context of it, the code is run directly. Otherwise it is scheduled to be run asynchronously on the given context.

      Type Parameters:
      T - The type of the result that the code produces.
      Parameters:
      requiredContextOrContextRoot - The context to run the code on.
      codeToRun - The code to execute. The code is required to either complete or fail the promise that is passed into the handler.
      Returns:
      The future containing the result of the promise passed in to the handler for executing the code. The future thus indicates the outcome of executing the code.
      Throws:
      NullPointerException - If any of the parameters is null.
    • tryHandleResult

      public static <T> void tryHandleResult(io.vertx.core.Promise<T> promise, io.vertx.core.AsyncResult<T> asyncResult)
      Applies the given result on the given promise using Promise.tryComplete(Object) or Promise.tryFail(Throwable).

      Similar to Promise.handle(AsyncResult) but does nothing if the promise is already complete.

      Type Parameters:
      T - The promise and result type.
      Parameters:
      promise - The promise to complete or fail.
      asyncResult - The result to apply.
      Throws:
      NullPointerException - if any of the parameters is null.
    • onCurrentContextCompletionHandler

      public static <T> io.vertx.core.Handler<io.vertx.core.AsyncResult<T>> onCurrentContextCompletionHandler(io.vertx.core.Handler<io.vertx.core.AsyncResult<T>> handlerToComplete)
      Gets a handler that completes the given handler on the current Vert.x context (if set).
      Type Parameters:
      T - The type of the result.
      Parameters:
      handlerToComplete - The handler to apply the result of the returned handler on.
      Returns:
      The handler that will wrap the given handler so that it is handled on the original context.
      Throws:
      NullPointerException - if handlerToComplete is null.