Example usage for com.google.common.util.concurrent SettableFuture set

List of usage examples for com.google.common.util.concurrent SettableFuture set

Introduction

In this page you can find the example usage for com.google.common.util.concurrent SettableFuture set.

Prototype

@Override
    public boolean set(@Nullable V value) 

Source Link

Usage

From source file:c5db.ConcurrencyTestUtil.java

private static ListenableFuture<Boolean> runAndReturnCompletionFuture(ExecutorService executor,
        IndexedExceptionThrowingRunnable runnable, int invocationIndex) {
    final SettableFuture<Boolean> setWhenFinished = SettableFuture.create();

    executor.execute(() -> {//  w  ww .  j  a  va  2s .  c  om
        try {
            runnable.run(invocationIndex);
            setWhenFinished.set(true);
        } catch (Throwable t) {
            setWhenFinished.setException(t);
        }
    });
    return setWhenFinished;
}

From source file:org.apache.bookkeeper.statelib.testing.executors.MockExecutorController.java

private static Answer<Future<?>> answerNow(MockExecutorController controller) {
    return invocationOnMock -> {

        Runnable task = invocationOnMock.getArgument(0);
        controller.runTask(task);/*from   w  w  w . ja  v  a  2 s.co  m*/
        SettableFuture<Void> future = SettableFuture.create();
        future.set(null);
        return future;
    };
}

From source file:com.spotify.apollo.concurrent.Util.java

public static <T> ListenableFuture<T> asFuture(CompletionStage<T> stage) {
    SettableFuture<T> future = SettableFuture.create();

    stage.whenComplete((result, throwable) -> {
        if (throwable != null) {
            future.setException(throwable);
        } else {/*from   w  w  w .j  a v  a2 s. co m*/
            future.set(result);
        }
    });

    return future;
}

From source file:co.cask.cdap.common.async.AsyncFunctions.java

/**
 * Converts a {@link Function} into {@link AsyncFunction} by performing the operation in the given executor.
 *
 * @param function Function to apply//from w w  w  . j  av a 2 s. c  om
 * @param executor Executor for the function to execute in
 * @param <I> Input type
 * @param <O> Output type
 * @return A {@link AsyncFunction} that will call the function in the given executor.
 */
public static <I, O> AsyncFunction<I, O> asyncWrap(final Function<I, O> function, final Executor executor) {
    return new AsyncFunction<I, O>() {
        @Override
        public ListenableFuture<O> apply(final I input) throws Exception {
            final SettableFuture<O> resultFuture = SettableFuture.create();
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    try {
                        resultFuture.set(function.apply(input));
                    } catch (Throwable t) {
                        resultFuture.setException(t);
                    }
                }
            });
            return resultFuture;
        }
    };
}

From source file:org.apache.heron.statemgr.FileSystemStateManager.java

protected static <V> void safeSetFuture(SettableFuture<V> future, V result) {
    if (!future.set(result)) {
        LOG.warning("Unexpected - a local settable future is set twice!");
    }/*from  www.  ja  v a  2s . c  om*/
}

From source file:io.airlift.drift.client.DriftInvocationHandler.java

private static ListenableFuture<Object> unwrapUserException(ListenableFuture<Object> future) {
    SettableFuture<Object> result = SettableFuture.create();
    Futures.addCallback(future, new FutureCallback<Object>() {
        @Override//from  www.  ja  v a 2 s . c  o  m
        public void onSuccess(Object value) {
            result.set(value);
        }

        @Override
        public void onFailure(Throwable t) {
            result.setException(unwrapUserException(t));
        }
    }, directExecutor());
    return result;
}

From source file:org.apache.twill.internal.Services.java

/**
 * Returns a {@link ListenableFuture} that will be completed when the given service is stopped. If the service
 * stopped due to error, the failure cause would be reflected in the future.
 *
 * @param service The {@link Service} to block on.
 * @return A {@link ListenableFuture} that will be completed when the service is stopped.
 */// w ww . ja  v a 2s .c o m
public static ListenableFuture<Service.State> getCompletionFuture(Service service) {
    final SettableFuture<Service.State> resultFuture = SettableFuture.create();

    service.addListener(new ServiceListenerAdapter() {
        @Override
        public void terminated(Service.State from) {
            resultFuture.set(Service.State.TERMINATED);
        }

        @Override
        public void failed(Service.State from, Throwable failure) {
            resultFuture.setException(failure);
        }
    }, Threads.SAME_THREAD_EXECUTOR);

    Service.State state = service.state();
    if (state == Service.State.TERMINATED) {
        return Futures.immediateFuture(state);
    } else if (state == Service.State.FAILED) {
        return Futures
                .immediateFailedFuture(new IllegalStateException("Service failed with unknown exception."));
    }

    return resultFuture;
}

From source file:org.testfx.util.WaitForAsyncUtils.java

private static <T> void callCallableAndSetFuture(Callable<T> callable, SettableFuture<T> future) {
    try {//from   w  w w. j  a  v  a  2  s  . c  om
        future.set(callable.call());
    } catch (Throwable exception) {
        future.setException(exception);
    }
}

From source file:org.loadui.testfx.utils.FXTestUtils.java

/**
 * Runs the given Callable in the JavaFX thread, waiting for it to complete before returning.
 * Also attempts to wait for any other JavaFX events that may have been queued in the Callable
 * to complete. If any Exception is thrown during execution of the Callable, that exception
 * will be re-thrown from invokeAndWait.
 *
 * @param task//from   ww  w.j  a v  a2s. c  o  m
 * @param timeoutInSeconds
 * @throws Exception
 */
public static void invokeAndWait(final Callable<?> task, int timeoutInSeconds) throws Exception {
    final SettableFuture<Void> future = SettableFuture.create();

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            try {
                task.call();
                future.set(null);
            } catch (Throwable e) {
                future.setException(e);
            }
        }
    });

    try {
        future.get(timeoutInSeconds, TimeUnit.SECONDS);
        awaitEvents();
    } catch (ExecutionException exception) {
        if (exception.getCause() instanceof Exception) {
            throw (Exception) exception.getCause();
        } else {
            throw exception;
        }
    }
}

From source file:org.glowroot.central.util.MoreFutures.java

public static <V> ListenableFuture<V> onFailure(ListenableFuture<V> future, Runnable onFailure) {
    SettableFuture<V> outerFuture = SettableFuture.create();
    Futures.addCallback(future, new FutureCallback<V>() {
        @Override//from   w ww.ja  va  2 s. c o  m
        public void onSuccess(V result) {
            outerFuture.set(result);
        }

        @Override
        public void onFailure(Throwable t) {
            logger.debug(t.getMessage(), t);
            onFailure.run();
            outerFuture.setException(t);
        }
    }, MoreExecutors.directExecutor());
    return outerFuture;
}