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.spotify.folsom.client.Utils.java

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

From source file:com.facebook.buck.parser.BuildTargetRawNodeParsePipeline.java

@Override
public ListenableFuture<Map<String, Object>> getNodeJob(Cell cell, UnconfiguredBuildTarget buildTarget)
        throws BuildTargetException {
    return Futures.transformAsync(
            buildFileRawNodeParsePipeline.getAllNodesJob(cell, cell.getAbsolutePathToBuildFile(buildTarget)),
            input -> {/*from   w w w .  ja v  a 2  s.c  om*/
                if (!input.getTargets().containsKey(buildTarget.getShortName())) {
                    throw NoSuchBuildTargetException.createForMissingBuildRule(buildTarget,
                            cell.getAbsolutePathToBuildFile(buildTarget));
                }
                return Futures.immediateFuture(input.getTargets().get(buildTarget.getShortName()));
            }, executorService);
}

From source file:com.orangerhymelabs.helenus.cassandra.view.ViewService.java

public ListenableFuture<View> create(View view) {
    ListenableFuture<Boolean> tableFuture = tables.exists(view.databaseName(), view.tableName());
    return Futures.transformAsync(tableFuture, new AsyncFunction<Boolean, View>() {
        @Override/* www  .j  ava  2 s . com*/
        public ListenableFuture<View> apply(Boolean exists) throws Exception {
            if (exists) {
                try {
                    ValidationEngine.validateAndThrow(view);
                    return views.create(view);
                } catch (ValidationException e) {
                    return Futures.immediateFailedFuture(e);
                }
            } else {
                return Futures.immediateFailedFuture(
                        new ItemNotFoundException("Table not found: " + view.tableName()));
            }
        }
    }, MoreExecutors.directExecutor());
}

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

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

From source file:com.facebook.buck.parser.ConvertingPipeline.java

@Override
public ListenableFuture<ImmutableSet<T>> getAllNodesJob(final Cell cell, final Path buildFile)
        throws BuildTargetException {
    // TODO(csarbora): this hits the chained pipeline before hitting the cache
    ListenableFuture<List<T>> allNodesListJob = Futures.transformAsync(getItemsToConvert(cell, buildFile),
            allToConvert -> {/*from w  w w .j av  a2  s. c  o m*/
                if (shuttingDown()) {
                    return Futures.immediateCancelledFuture();
                }

                ImmutableList.Builder<ListenableFuture<T>> allNodeJobs = ImmutableList.builder();

                for (final F from : allToConvert) {
                    if (isValid(from)) {
                        final BuildTarget target = getBuildTarget(cell.getRoot(), buildFile, from);
                        allNodeJobs.add(cache.getJobWithCacheLookup(cell, target, () -> {
                            if (shuttingDown()) {
                                return Futures.immediateCancelledFuture();
                            }
                            return dispatchComputeNode(cell, target, from);
                        }));
                    }
                }

                return Futures.allAsList(allNodeJobs.build());
            }, executorService);
    return Futures.transform(allNodesListJob, (Function<List<T>, ImmutableSet<T>>) ImmutableSet::copyOf,
            executorService);
}

From source file:com.orangerhymelabs.helenus.cassandra.table.TableService.java

public ListenableFuture<Table> create(Table table) {
    ListenableFuture<Boolean> dbFuture = databases.exists(table.databaseName());
    return Futures.transformAsync(dbFuture, new AsyncFunction<Boolean, Table>() {
        @Override//from  ww  w .j  a v a2  s.co  m
        public ListenableFuture<Table> apply(Boolean exists) throws Exception {
            if (exists) {
                try {
                    ValidationEngine.validateAndThrow(table);
                    return tables.create(table);
                } catch (ValidationException e) {
                    return Futures.immediateFailedFuture(e);
                }
            } else {
                return Futures.immediateFailedFuture(
                        new ItemNotFoundException("Database not found: " + table.databaseName()));
            }
        }
    }, MoreExecutors.directExecutor());
}

From source file:dagger.producers.internal.MapProducer.java

@Override
public ListenableFuture<Map<K, V>> compute() {
    return Futures.transformAsync(mapProducerProducer.get(),
            new AsyncFunction<Map<K, Producer<V>>, Map<K, V>>() {
                @Override/*  w w  w  . j a v  a2 s  .  c  o m*/
                public ListenableFuture<Map<K, V>> apply(final Map<K, Producer<V>> map) {
                    // TODO(beder): Use Futures.whenAllComplete when Guava 20 is released.
                    return transform(
                            Futures.allAsList(
                                    Iterables.transform(map.entrySet(), MapProducer.<K, V>entryUnwrapper())),
                            new Function<List<Map.Entry<K, V>>, Map<K, V>>() {
                                @Override
                                public Map<K, V> apply(List<Map.Entry<K, V>> entries) {
                                    return ImmutableMap.copyOf(entries);
                                }
                            }, directExecutor());
                }
            }, directExecutor());
}

From source file:dagger.producers.internal.AbstractProducesMethodProducer.java

@Override
protected final ListenableFuture<T> compute() {
    monitor = monitorProvider.get().producerMonitorFor(token);
    monitor.requested();//  w ww. j av a  2 s.  com
    ListenableFuture<T> result = Futures.transformAsync(collectDependencies(), this, this);
    monitor.addCallbackTo(result);
    return result;
}

From source file:dagger.producers.internal.MapOfProducedProducer.java

@Override
public ListenableFuture<Map<K, Produced<V>>> compute() {
    return Futures.transformAsync(mapProducerProducer.get(),
            new AsyncFunction<Map<K, Producer<V>>, Map<K, Produced<V>>>() {
                @Override//from   w w  w  . j a  v a2  s .c om
                public ListenableFuture<Map<K, Produced<V>>> apply(final Map<K, Producer<V>> map) {
                    // TODO(beder): Use Futures.whenAllComplete when Guava 20 is released.
                    return transform(
                            Futures.allAsList(Iterables.transform(map.entrySet(),
                                    MapOfProducedProducer.<K, V>entryUnwrapper())),
                            new Function<List<Map.Entry<K, Produced<V>>>, Map<K, Produced<V>>>() {
                                @Override
                                public Map<K, Produced<V>> apply(List<Map.Entry<K, Produced<V>>> entries) {
                                    return ImmutableMap.copyOf(entries);
                                }
                            }, directExecutor());
                }
            }, directExecutor());
}

From source file:com.facebook.buck.util.concurrent.WeightedListeningExecutorService.java

private <T> ListenableFuture<T> submitWithSemaphore(final Callable<T> callable, final ResourceAmounts amounts) {
    ListenableFuture<T> future = Futures.transformAsync(semaphore.acquire(amounts), input -> {
        try {/* ww  w. jav  a 2  s. c o  m*/
            return Futures.immediateFuture(callable.call());
        } catch (Throwable thrown) {
            return Futures.immediateFailedFuture(thrown);
        }
    }, delegate);
    future.addListener(() -> semaphore.release(amounts),
            com.google.common.util.concurrent.MoreExecutors.directExecutor());
    return future;
}