Example usage for com.google.common.util.concurrent AsyncFunction apply

List of usage examples for com.google.common.util.concurrent AsyncFunction apply

Introduction

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

Prototype

ListenableFuture<O> apply(@Nullable I input) throws Exception;

Source Link

Document

Returns an output Future to use in place of the given input .

Usage

From source file:com.tinspx.util.net.Requests.java

/**
 * Wraps {@code delegate} in a {@code RequestContext} that will convert
 * successful responses that satisfy {@code failPredicate} to an error. Note
 * that converting the response to a failure only affects the status of the
 * response {@code ListenableFuture}, and has <i>no</i> effect on the
 * {@link RequestCallback} callback methods.
 *//*from w w  w .j  a va 2 s.  c  o m*/
public static RequestContext toFailure(final @NonNull AsyncFunction<? super Request, Response> delegate,
        final @NonNull Predicate<? super Response> failPredicate) {
    return new RequestContext() {
        @Override
        public ListenableFuture<Response> apply(Request request) throws Exception {
            ListenableFuture<Response> delegateFuture = delegate.apply(request);
            final SettableFuture<Response> future = SettableFuture.create();
            Futures.addCallback(delegateFuture, new FutureCallback<Response>() {
                @Override
                public void onSuccess(Response result) {
                    try {
                        if (failPredicate.apply(result)) {
                            result.onError(Errors.message("predicate failure: %s", failPredicate));
                            future.setException(new RequestException(result));
                        } else {
                            future.set(result);
                        }
                    } catch (Throwable t) {
                        //have to ensure the returned future always completes
                        //even if the predicate or onError throws
                        future.setException(new RequestException(result, t));
                        //guava Futures should log this
                        throw Throwables.propagate(t);
                    }
                }

                @Override
                public void onFailure(Throwable t) {
                    future.setException(t);
                }
            });
            FutureUtils.linkFailure(future, delegateFuture, false);
            return future;
        }
    };
}

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

public ListenableFuture<List<File>> getMergedFileForUser(String user) throws Exception {

    AsyncFunction<String, List<File>> f = FunctionComposition.<String>builder(mainPool).transform(lookupFiles())
            .transform(locateFiles()).transform(downloadFiles()).fork(backupFiles())
            .transform(mergeFiles(), ioPool)
            .allAsList(copyFile("1", "tmp1"), copyFile("2", "tmp2"), copyFile("3", "tmp3"))
            .buildAsyncFunction();// w  w w.  j  a  v a2 s. c om

    return f.apply(user);
}

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

/**
 * @param cell the cell in which we're parsing
 * @param withParser the function that performs the interaction with the parser
 * @param executorService where to apply the async function
 * @param <T> type of result/*from   ww w . j a  v  a2s . co  m*/
 * @return a {@link ListenableFuture} that will run the supplied AsyncFunction when a parser
 *         is available.
 */
public <T> ListenableFuture<T> leaseParser(final Cell cell, final AsyncFunction<P, T> withParser,
        ListeningExecutorService executorService) {
    Preconditions.checkState(!closed.get());

    final ListenableFuture<P> obtainedParser = obtainParser(cell);
    ListenableFuture<T> futureWork = Futures.transformAsync(obtainedParser, new AsyncFunction<P, T>() {
        @Override
        public ListenableFuture<T> apply(P input) throws Exception {
            try {
                return withParser.apply(input);
            } finally {
                returnParser(cell, input);
            }
        }
    }, executorService);
    return futureWork;
}

From source file:com.facebook.buck.core.build.engine.impl.CachingBuildRuleBuilder.java

private <F, T> AsyncFunction<F, T> ruleAsyncFunction(AsyncFunction<F, T> delegate) {
    return input -> {
        try (Scope ignored = buildRuleScope()) {
            return delegate.apply(input);
        }//from w  w  w . j a va2 s.c o  m
    };
}

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

private <F, T> AsyncFunction<F, T> ruleAsyncFunction(final BuckEventBus eventBus,
        final AsyncFunction<F, T> delegate) {
    return input -> {
        try (Scope event = BuildRuleEvent.resumeSuspendScope(eventBus, rule, buildRuleDurationTracker,
                ruleKeyFactories.getDefaultRuleKeyFactory())) {
            return delegate.apply(input);
        }/*from w w w.  j av a 2  s. c om*/
    };
}