List of usage examples for com.google.common.util.concurrent Futures transform
public static <I, O> ListenableFuture<O> transform(ListenableFuture<I> input, Function<? super I, ? extends O> function)
From source file:io.v.impl.google.rpc.ServerRPCHelper.java
static ListenableFuture<byte[][]> invoke(final Invoker invoker, final VContext ctx, final StreamServerCall call, final String method, final byte[][] vomArgs) { return Futures.transform(invoker.getArgumentTypes(ctx, method), new AsyncFunction<Type[], byte[][]>() { @Override/*w w w .j ava 2 s . c om*/ public ListenableFuture<byte[][]> apply(Type[] argTypes) throws Exception { if (argTypes.length != vomArgs.length) { throw new VException(String.format("Wrong number of args, want %d, got %d", argTypes.length, vomArgs.length)); } final Object[] args = new Object[argTypes.length]; for (int i = 0; i < argTypes.length; ++i) { args[i] = VomUtil.decode(vomArgs[i], argTypes[i]); } return Futures.transform( Futures.<Object>allAsList(invoker.getResultTypes(ctx, method), invoker.invoke(ctx, call, method, args)), new AsyncFunction<List<Object>, byte[][]>() { @Override public ListenableFuture<byte[][]> apply(List<Object> input) throws Exception { Type[] resultTypes = (Type[]) input.get(0); Object[] results = (Object[]) input.get(1); if (resultTypes.length != results.length) { throw new VException(String.format("Wrong number of results, want %d, got %d", resultTypes.length, results.length)); } byte[][] vomResults = new byte[resultTypes.length][]; for (int i = 0; i < resultTypes.length; ++i) { vomResults[i] = VomUtil.encode(results[i], resultTypes[i]); } return Futures.immediateFuture(vomResults); } }); } }); }
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 .j a va 2s.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:com.google.gapid.models.ConstantSets.java
public ConstantSets(Client client) { this.cache = FutureCache.hardCache( path -> Futures.transform(client.get(Paths.any(path)), Service.Value::getConstantSet), result -> result.getConstantsCount() != 0); }
From source file:dagger2.producers.internal.Producers.java
/** * Returns a future of {@link Produced} that represents the completion (either success or failure) * of the given future. If the input future succeeds, then the resulting future also succeeds with * a successful {@code Produced}; if the input future fails, then the resulting future succeeds * with a failing {@code Produced}./*from www .j ava2s .c o m*/ * * <p>Cancelling the resulting future will propagate the cancellation to the input future; but * cancelling the input future will trigger the resulting future to succeed with a failing * {@code Produced}. */ // TODO(user): Document what happens with an InterruptedException after you figure out how to // trigger one in a test. public static <T> ListenableFuture<Produced<T>> createFutureProduced(ListenableFuture<T> future) { return Futures.withFallback(Futures.transform(future, new Function<T, Produced<T>>() { @Override public Produced<T> apply(final T value) { return new Produced<T>() { @Override public T get() { return value; } }; } }), Producers.<T>futureFallbackForProduced()); }
From source file:com.spotify.asyncdatastoreclient.example.ExampleAsync.java
private static ListenableFuture<MutationResult> addDataInTransaction(final Datastore datastore) { final ListenableFuture<TransactionResult> txn = datastore.transactionAsync(); final KeyQuery get = QueryBuilder.query("employee", 2345678L); return Futures.transform(datastore.executeAsync(get, txn), (QueryResult result) -> { if (result.getEntity() == null) { datastore.rollbackAsync(txn); // fire and forget return Futures.immediateFuture(MutationResult.build()); }//from w ww.j a va 2 s.co m final Insert insert = QueryBuilder.insert("employee", 2345678L).value("fullname", "Fred Blinge") .value("inserted", new Date()).value("age", 40); return datastore.executeAsync(insert); }); }
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//w w w . j a v a 2 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:com.google.gapid.image.FetchedImage.java
public static ListenableFuture<FetchedImage> load(Client client, Path.ImageInfo imagePath) { return Futures.transformAsync(client.get(imageInfo(imagePath)), value -> { Images.Format format = getFormat(value.getImageInfo2D()); return Futures.transform(client.get(imageData(imagePath, format.format)), pixelValue -> new FetchedImage(client, format, pixelValue.getImageInfo2D())); });/* ww w.ja va 2 s . co m*/ }
From source file:com.skcraft.plume.common.util.concurrent.DeferredImpl.java
@Override public Deferred<Void> thenRun(final Runnable task, ListeningExecutorService executor) { return new DeferredImpl<Void>(Futures.transform(future, new Function<I, Void>() { @Override/* w w w .j a v a2 s.co m*/ public Void apply(I input) { task.run(); return null; } }), defaultExecutor); }
From source file:io.v.v23.syncbase.nosql.NoSql.java
private static AsyncFunction<Boolean, Void> getRetryFn(final VContext ctx, final Database db, final BatchOptions opts, final BatchOperation op, final int round) { return new AsyncFunction<Boolean, Void>() { @Override//from w w w . java2 s . com public ListenableFuture<Void> apply(Boolean success) throws Exception { if (success) { return Futures.immediateFuture(null); } if (round >= 3) { throw new ConcurrentBatchException(ctx); } return Futures.transform(tryBatch(ctx, db, opts, op), getRetryFn(ctx, db, opts, op, round + 1)); } }; }
From source file:com.noorq.casser.core.operation.AbstractOperation.java
public ListenableFuture<PreparedOperation<E>> prepareAsync() { final O _this = (O) this; return Futures.transform(prepareStatementAsync(), new Function<PreparedStatement, PreparedOperation<E>>() { @Override/*from w w w . j a v a2s . c o m*/ public PreparedOperation<E> apply(PreparedStatement preparedStatement) { return new PreparedOperation<E>(preparedStatement, _this); } }); }