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

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

Introduction

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

Prototype

AsyncFunction

Source Link

Usage

From source file:io.v.android.impl.google.services.beam.VBeamServer.java

@Override
public synchronized ListenableFuture<GetIntentOut> getIntent(VContext ctx, ServerCall call, String secret) {
    evictStaleRequests();/*from  ww  w .ja va2s  .c  om*/
    if (requestMap.remove(secret) != null) {
        try {
            return Futures.transform(callback.createIntent(ctx, call),
                    new AsyncFunction<Pair<String, byte[]>, GetIntentOut>() {

                        @Override
                        public ListenableFuture<GetIntentOut> apply(Pair<String, byte[]> input)
                                throws Exception {
                            return convertIntent(input);
                        }
                    });
        } catch (Throwable t) {
            return Futures.immediateFailedFuture(t);
        }
    }
    return Futures.immediateFailedFuture(new VException("Bad request"));
}

From source file:org.opendaylight.openflowplugin.impl.util.BarrierUtil.java

/**
 * chain a barrier message - regardless of previous result and use given {@link Function} to combine
 * original result and barrier result/*from   w  w  w.  ja  v  a2 s . co  m*/
 *
 * @param <T>                type of input future
 * @param input              future to chain barrier to
 * @param nodeRef            target device
 * @param transactionService barrier service
 * @param compositeTransform
 * @return future holding both results (input and of the barrier)
 */
public static <T> ListenableFuture<RpcResult<T>> chainBarrier(final ListenableFuture<RpcResult<T>> input,
        final NodeRef nodeRef, final FlowCapableTransactionService transactionService,
        final Function<Pair<RpcResult<T>, RpcResult<Void>>, RpcResult<T>> compositeTransform) {
    final MutablePair<RpcResult<T>, RpcResult<Void>> resultPair = new MutablePair<>();

    // store input result and append barrier
    final ListenableFuture<RpcResult<Void>> barrierResult = Futures.transform(input,
            new AsyncFunction<RpcResult<T>, RpcResult<Void>>() {
                @Override
                public ListenableFuture<RpcResult<Void>> apply(@Nullable final RpcResult<T> interInput)
                        throws Exception {
                    resultPair.setLeft(interInput);
                    final SendBarrierInput barrierInput = createSendBarrierInput(nodeRef);
                    return JdkFutureAdapters.listenInPoolThread(transactionService.sendBarrier(barrierInput));
                }
            });
    // store barrier result and return initiated pair
    final ListenableFuture<Pair<RpcResult<T>, RpcResult<Void>>> compositeResult = Futures
            .transform(barrierResult, new Function<RpcResult<Void>, Pair<RpcResult<T>, RpcResult<Void>>>() {
                @Nullable
                @Override
                public Pair<RpcResult<T>, RpcResult<Void>> apply(@Nullable final RpcResult<Void> input) {
                    resultPair.setRight(input);
                    return resultPair;
                }
            });
    // append assembling transform to barrier result
    return Futures.transform(compositeResult, compositeTransform);
}

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

/**
 * Converts a {@link Function} into {@link AsyncFunction} by performing the operation in the same thread.
 *
 * @param function Function to apply// ww w. j a  v  a2 s .c  om
 * @param <I> Input type
 * @param <O> Output type
 * @return A {@link AsyncFunction} that will call the function in the same thread as the caller thread.
 */
public static <I, O> AsyncFunction<I, O> asyncWrap(final Function<I, O> function) {
    return new AsyncFunction<I, O>() {
        @Override
        public ListenableFuture<O> apply(I input) throws Exception {
            return Futures.immediateFuture(function.apply(input));
        }
    };
}

From source file:io.v.impl.google.rpc.StreamImpl.java

@Override
public ListenableFuture<Object> recv(final Type type) {
    ListenableFutureCallback<byte[]> callback = new ListenableFutureCallback<>();
    nativeRecv(nativeRef, callback);/*from w  ww . j  a v  a 2 s  . co  m*/
    return VFutures.withUserLandChecks(ctx,
            Futures.transform(callback.getVanillaFuture(), new AsyncFunction<byte[], Object>() {
                @Override
                public ListenableFuture<Object> apply(byte[] result) throws Exception {
                    return Futures.immediateFuture(VomUtil.decode(result, type));
                }
            }));
}

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

AsyncFunction<String, List<String>> lookupFiles() {
    return new AsyncFunction<String, List<String>>() {
        @Override// ww  w. ja  v a2s .com
        public ListenableFuture<List<String>> apply(String input) throws Exception {
            System.out.println("1");
            return immediateFuture((List<String>) newArrayList("1", "2", "3"));
        }
    };
}

From source file:io.v.v23.syncbase.nosql.RowImpl.java

@Override
public ListenableFuture<Object> get(VContext ctx, final Type type) {
    return Futures.transform(client.get(ctx, schemaVersion), new AsyncFunction<byte[], Object>() {
        @Override//from   w w w .  j av  a 2 s  .  co  m
        public ListenableFuture<Object> apply(byte[] data) throws Exception {
            return Futures.immediateFuture(VomUtil.decode(data, type));
        }
    });
}

From source file:com.orangerhymelabs.helenus.cassandra.table.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//from w  ww .ja  va2  s. c  o m
        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()));
            }
        }
    });
}

From source file:io.v.v23.V23TestUtil.java

/**
 * Asserts that the given future is executed on the provided thread.
 * <p>//from www  . java2  s . co m
 * Note that this check is inherently racey: if the future has already completed, there is
 * no way to check on which thread it got executed.  Therefore, this method should be executed
 * as close possible to the future's creation, to reduce the chance of the race happening.
 * Additionally, the future could be created with an executor that delays execution for a time
 * period that sufficient to all but prohibit races (e.g., 100ms).
 */
public static <T> void assertRunsOnThread(ListenableFuture<T> future, final Thread thread) {
    try {
        assertThat(future.isDone()).isFalse();
        Futures.transform(future, new AsyncFunction<T, Void>() {
            @Override
            public ListenableFuture<Void> apply(T input) throws Exception {
                assertThat(Thread.currentThread()).isEqualTo(thread);
                return Futures.immediateFuture(null);
            }
        }).get();
    } catch (Exception e) {
        Truth.assertWithMessage("error asserting executor").that(e.getMessage()).isEmpty();
    }
}

From source file:io.v.v23.syncbase.RowImpl.java

@Override
public <T> ListenableFuture<T> get(VContext ctx, final Class<T> clazz) {
    return VFutures.withUserLandChecks(ctx,
            Futures.transform(client.get(ctx, this.batchHandle), new AsyncFunction<VdlAny, T>() {
                @Override/*from w  w  w . j  a v a  2 s  .c  om*/
                public ListenableFuture<T> apply(VdlAny vdlAny) throws Exception {
                    final byte[] encodedBytes = VomUtil.encode(vdlAny, VdlAny.VDL_TYPE);
                    final Object decodedObject = VomUtil.decode(encodedBytes, clazz);
                    return Futures.immediateFuture((T) decodedObject);
                }
            }));
}

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

AsyncFunction<List<String>, List<URL>> locateFiles() {
    return new AsyncFunction<List<String>, List<URL>>() {
        @Override/*from w  ww  . j a v a  2  s  . c o  m*/
        public ListenableFuture<List<URL>> apply(List<String> input) throws Exception {
            return immediateFuture((List<URL>) Lists.<URL>newArrayList());
        }
    };
}