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

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

Introduction

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

Prototype

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

Source Link

Document

Returns a new ListenableFuture whose result is the product of applying the given Function to the result of the given Future .

Usage

From source file:com.spotify.folsom.client.Utils.java

public static <I, O> ListenableFuture<O> transform(final ListenableFuture<I> input,
        final Function<? super I, ? extends O> function) {
    return Futures.transform(input, function, SAME_THREAD_EXECUTOR);
}

From source file:com.skcraft.plume.common.util.concurrent.DeferredImpl.java

@Override
public <O> Deferred<O> thenRun(final Callable<O> task, ListeningExecutorService executor) {
    return new DeferredImpl<>(Futures.transform(future,
            (AsyncFunction<I, O>) input -> Futures.immediateFuture(task.call()), executor), defaultExecutor);
}

From source file:com.skcraft.concurrency.DeferredImpl.java

@Override
public <O> Deferred<O> thenRunAsync(final Callable<O> task, ListeningExecutorService executor) {
    return new DeferredImpl<O>(Futures.transform(future, new Function<I, O>() {
        @Override/*  www  .  j a v  a 2s  .c om*/
        public O apply(I input) {
            try {
                return task.call();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }, executor), defaultExecutor);
}

From source file:com.rackspacecloud.blueflood.concurrent.AsyncChain.java

public ListenableFuture<O> apply(I input) throws Exception {
    List<AsyncFunctionWithThreadPool> copy = new ArrayList<AsyncFunctionWithThreadPool>(functions);
    AsyncFunctionWithThreadPool func;// w  ww . j  av  a 2  s .c o m
    ListenableFuture nextInput = new NoOpFuture<I>(input);

    while (copy.size() > 0) {
        func = copy.remove(0);
        nextInput = Futures.transform(nextInput, func, func.getThreadPool());
    }
    return nextInput;
}

From source file:com.facebook.buck.distributed.RuleKeyUtils.java

/**
 * Calculates default rule keys for nodes in given graph
 *
 * @return Map of rules to their keys//from   w  w w .j  av  a 2s .co  m
 */
public static ListenableFuture<List<Pair<BuildRule, RuleKey>>> calculateDefaultRuleKeys(
        ActionGraphBuilder actionGraphBuilder, ParallelRuleKeyCalculator<RuleKey> ruleKeyCalculator,
        BuckEventBus eventBus, Iterable<BuildTarget> topLevelTargets) {
    Set<BuildRule> allRulesInGraph = findAllRulesInGraph(topLevelTargets, actionGraphBuilder);

    List<ListenableFuture<Pair<BuildRule, RuleKey>>> ruleKeys = new ArrayList<>(allRulesInGraph.size());
    for (BuildRule rule : allRulesInGraph) {
        ruleKeys.add(Futures.transform(ruleKeyCalculator.calculate(eventBus, rule), ruleKey -> {
            LOG.debug("Rule key calculation: [%s] [%s]", rule.getFullyQualifiedName(), ruleKey);
            return new Pair<>(rule, ruleKey);
        }, MoreExecutors.directExecutor()));
    }

    return Futures.allAsList(ruleKeys);
}

From source file:org.robotninjas.util.examples.ChainingExample.java

public ListenableFuture<File> getMergedFileForUser(String user) throws Exception {
    ListenableFuture<List<String>> files = getFilesForUser("dave");
    ListenableFuture<List<URL>> urls = Futures.transform(files, locateFiles(), mainPool);
    ListenableFuture<List<File>> downloadedFiles = Futures.transform(urls, downloadFiles(), mainPool);
    return Futures.transform(downloadedFiles, mergeFiles(), ioPool);
}

From source file:org.robotninjas.concurrent.FluentDecorator.java

@Override
public <Y> FluentFuture<Y> transform(Executor executor, Function<V, Y> func) {
    return new FluentDecorator<>(Futures.transform(this, func, executor), this.executor);
}

From source file:org.thingsboard.server.dao.nosql.CassandraAbstractAsyncDao.java

protected <T> ListenableFuture<T> getFuture(ResultSetFuture future,
        java.util.function.Function<ResultSet, T> transformer) {
    return Futures.transform(future, new Function<ResultSet, T>() {
        @Nullable//from  www .j  av  a 2 s .c  o m
        @Override
        public T apply(@Nullable ResultSet input) {
            return transformer.apply(input);
        }
    }, readResultsProcessingExecutor);
}

From source file:org.immutables.eventual.CompletedModule.java

static ListenableFuture<Module> from(Injector injectingFutures, CompletionCriteria criteria) {
    final LinkedHashMap<Key<?>, Key<?>> keyMapping = mapUnfutureKeys(injectingFutures);
    List<ListenableFuture<?>> listOfFutures = getFutureInstances(keyMapping.keySet(), injectingFutures);

    ListenableFuture<List<Object>> futureOfList = criteria == CompletionCriteria.SUCCESSFUL
            ? Futures.successfulAsList(listOfFutures)
            : Futures.allAsList(listOfFutures);

    return Futures.transform(futureOfList, new Function<List<Object>, Module>() {
        Key<?>[] keys = Iterables.toArray(keyMapping.values(), Key.class);

        @Override/*  ww w.jav a2s  .  c om*/
        public Module apply(List<Object> instances) {
            return new CompletedModule(keys, instances.toArray());
        }
    }, MoreExecutors.directExecutor());
}

From source file:com.salesforce.grpc.contrib.FutureChain.java

/**
 * @see Futures#transform(ListenableFuture, Function, Executor)
 *///from  w  ww.  j  a  va 2 s . c  o m
public <U> FutureChain<U> transform(Function<? super T, ? extends U> function) {
    return new FutureChain<>(Futures.transform(future, function, executor), executor);
}