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: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/*  ww  w  .j av  a  2s .co  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()));
            }
        }
    }, MoreExecutors.directExecutor());
}

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

@Override
public ListenableFuture<Object[]> finish(final Type[] types) {
    ListenableFutureCallback<byte[][]> callback = new ListenableFutureCallback<>();
    nativeFinish(nativeRef, types.length, callback);
    return VFutures.withUserLandChecks(ctx,
            Futures.transform(callback.getVanillaFuture(), new AsyncFunction<byte[][], Object[]>() {
                @Override/* w ww.ja v  a2  s  . c  o m*/
                public ListenableFuture<Object[]> apply(byte[][] vomResults) throws Exception {
                    if (vomResults.length != types.length) {
                        throw new VException(String.format("Mismatch in number of results, want %s, have %s",
                                types.length, vomResults.length));
                    }
                    // VOM-decode results.
                    Object[] ret = new Object[types.length];
                    for (int i = 0; i < types.length; i++) {
                        ret[i] = VomUtil.decode(vomResults[i], types[i]);
                    }
                    return Futures.immediateFuture(ret);
                }
            }));
}

From source file:com.spotify.folsom.retry.RetryingClient.java

@Override
public <T> ListenableFuture<T> send(final Request<T> request) {
    final ListenableFuture<T> future = delegate.send(request);
    return Futures.catchingAsync(future, MemcacheClosedException.class,
            new AsyncFunction<MemcacheClosedException, T>() {
                @Override//from   www  .j ava  2s.  c o m
                public ListenableFuture<T> apply(final MemcacheClosedException e) {
                    if (delegate.isConnected()) {
                        return delegate.send(request);
                    } else {
                        return Futures.immediateFailedFuture(e);
                    }
                }
            });
}

From source file:com.microsoft.services.orc.core.OrcMediaEntityFetcher.java

public ListenableFuture<byte[]> getContent() {

    Request request = getResolver().createRequest();
    request.setVerb(HttpVerb.GET);/*from  ww  w  .j a  v a2  s .  c  o m*/
    OrcURL url = request.getUrl();
    url.appendPathComponent("$value");

    ListenableFuture<OrcResponse> future = oDataExecute(request);

    return Futures.transform(future, new AsyncFunction<OrcResponse, byte[]>() {
        @Override
        public ListenableFuture<byte[]> apply(OrcResponse response) throws Exception {
            SettableFuture<byte[]> result = SettableFuture.create();
            result.set(response.getPayload());
            return result;
        }
    });
}

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

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

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

AsyncFunction<List<URL>, List<File>> downloadFiles() {
    return new AsyncFunction<List<URL>, List<File>>() {
        @Override//  w  ww .  j  a va 2 s  . co  m
        public ListenableFuture<List<File>> apply(List<URL> input) throws Exception {
            return immediateFuture((List<File>) Collections.EMPTY_LIST);
        }
    };
}

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/*  w  w w .  ja  v  a  2  s  . com*/
        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:io.v.impl.google.rt.VRuntimeImpl.java

/**
 * Returns a new runtime instance.// w  w w  . j a  v  a 2 s  .  c  o m
 */
public static VRuntimeImpl create(Options opts) throws VException {
    VContext ctx = nativeInit();
    ctx = ctx.withValue(new RuntimeExecutorKey(), Executors.newCachedThreadPool());
    final VContext ctxC = ctx.withCancel();
    Futures.transform(ctxC.onDone(), new AsyncFunction<VContext.DoneReason, Void>() {
        @Override
        public ListenableFuture<Void> apply(VContext.DoneReason reason) {
            ListenableFutureCallback<Void> callback = new ListenableFutureCallback<>();
            nativeShutdown(ctxC, callback);
            return callback.getVanillaFuture();
        }
    });
    return new VRuntimeImpl(ctxC);
}

From source file:org.robotninjas.protobuf.netty.client.NettyRpcChannel.java

private ListenableFuture<Message> doCallMethod(MethodDescriptor method, final RpcController controller,
        Message request, final Message responsePrototype, boolean blocking) {

    ListenableFuture<NettyRpcProto.RpcResponse> result = new RpcCall(buildRequest(blocking, method, request));
    channel.writeAndFlush(result);/*from w w  w .j a  va  2  s.  co  m*/
    return Futures.transform(result, new AsyncFunction<NettyRpcProto.RpcResponse, Message>() {
        public ListenableFuture<Message> apply(NettyRpcProto.RpcResponse input) {
            SettableFuture<Message> response = SettableFuture.create();
            try {
                final Message.Builder builder = responsePrototype.newBuilderForType();
                Message result = builder.mergeFrom(input.getResponseMessage()).build();
                response.set(result);
            } catch (InvalidProtocolBufferException e) {
                controller.setFailed(nullToEmpty(e.getMessage()));
                response.setException(e);
            }
            return response;
        }
    });

}

From source file:zipkin.elasticsearch.ElasticsearchSpanConsumer.java

@Override
public ListenableFuture<Void> accept(List<Span> spans) {
    BulkRequestBuilder request = client.prepareBulk();
    for (Span span : spans) {
        request.add(createSpanIndexRequest(ApplyTimestampAndDuration.apply(span)));
    }/*w ww  . j  a va 2s .  c o m*/
    ListenableFuture<?> future = toGuava(request.execute());
    if (FLUSH_ON_WRITES) {
        future = transform(future, new AsyncFunction() {
            @Override
            public ListenableFuture apply(Object input) {
                return toGuava(client.admin().indices().prepareFlush(indexNameFormatter.catchAll()).execute());
            }
        });
    }
    return transform(future, TO_VOID);
}