Example usage for com.google.common.util.concurrent Futures transformAsync

List of usage examples for com.google.common.util.concurrent Futures transformAsync

Introduction

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

Prototype

public static <I, O> ListenableFuture<O> transformAsync(ListenableFuture<I> input,
        AsyncFunction<? super I, ? extends O> function, Executor executor) 

Source Link

Document

Returns a new ListenableFuture whose result is asynchronously derived from the result of the given Future .

Usage

From source file:com.facebook.buck.rules.CachingBuildRuleBuilder.java

private ListenableFuture<Optional<BuildResult>> transformBuildResultAsyncIfNotPresent(
        ListenableFuture<Optional<BuildResult>> future,
        Callable<ListenableFuture<Optional<BuildResult>>> function) {
    // Immediately (i.e. without posting a task), returns the current result if it's already present
    // or a cancelled result if we've already seen a failure.
    return Futures.transformAsync(future, (result) -> {
        if (result.isPresent()) {
            return Futures.immediateFuture(result);
        }//from   w w w. ja  va 2s . c om
        if (!buildRuleBuilderDelegate.shouldKeepGoing(buildContext)) {
            Preconditions.checkNotNull(buildRuleBuilderDelegate.getFirstFailure());
            return Futures.immediateFuture(
                    Optional.of(BuildResult.canceled(rule, buildRuleBuilderDelegate.getFirstFailure())));
        }
        return function.call();
    }, MoreExecutors.directExecutor());
}