Example usage for com.google.common.util.concurrent Futures transform

List of usage examples for com.google.common.util.concurrent Futures transform

Introduction

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

Prototype

public static <I, O> ListenableFuture<O> transform(ListenableFuture<I> input,
        Function<? super I, ? extends O> function) 

Source Link

Document

Returns a new ListenableFuture whose result is the product of applying the given Function to the result of the given Future .

Usage

From source file:com.facebook.buck.util.network.HttpPutLogger.java

@Override
protected ListenableFuture<Void> logMultiple(ImmutableCollection<BatchEntry> data) {
    StringWriter stringWriter = new StringWriter();
    try {// w ww  . ja  v a2s . c  o m
        JsonGenerator jsonGenerator = objectMapper.getFactory().createGenerator(stringWriter);
        jsonGenerator.writeStartArray();
        for (BatchEntry entry : data) {
            jsonGenerator.writeRawValue(entry.getLine());
        }
        jsonGenerator.writeEndArray();
        jsonGenerator.close();
        return Futures.transform(endpoint.post(stringWriter.toString()), new Function<HttpResponse, Void>() {
            @Nullable
            @Override
            public Void apply(HttpResponse input) {
                return null;
            }
        });
    } catch (IOException e) {
        return Futures.immediateFailedFuture(e);
    }
}

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

@Override
public ListenableFuture<Map<String, SyncgroupSpec>> getSpec(VContext ctx) {
    return Futures.transform(dbClient.getSyncgroupSpec(ctx, name),
            new Function<SyncgroupManagerClient.GetSyncgroupSpecOut, Map<String, SyncgroupSpec>>() {
                @Override//from  w  ww. j av a  2  s . c o  m
                public Map<String, SyncgroupSpec> apply(SyncgroupManagerClient.GetSyncgroupSpecOut spec) {
                    return ImmutableMap.of(spec.version, spec.spec);
                }
            });
}

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/*from ww  w .j  av a  2 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.facebook.presto.operator.ParallelLookupSourceSupplier.java

public ParallelLookupSourceSupplier(List<Type> types, List<Integer> hashChannels,
        List<? extends ListenableFuture<SharedLookupSource>> partitions) {
    this.types = ImmutableList.copyOf(requireNonNull(types, "types is null"));
    this.partitions = requireNonNull(partitions, "partitions is null");

    hashChannelTypes = hashChannels.stream().map(types::get).collect(toImmutableList());

    checkArgument(Integer.bitCount(partitions.size()) == 1, "partitions must be a power of 2");
    lookupSourceFuture = Futures.transform(Futures.allAsList(partitions), (List<SharedLookupSource> input) -> {
        return new PartitionedLookupSource(input, hashChannelTypes);
    });//from www  .  ja  v  a2 s.co m

}

From source file:us.physion.ovation.ui.browser.UnTrashEntityAction.java

@Override
protected ListenableFuture<Iterable<URI>> call(DataContext c, OvationEntity entity) {
    return Futures.transform(c.restoreFromTrash(entity), new Function<Iterable<UUID>, Iterable<URI>>() {
        @Override//ww  w.  ja  v a2  s. c  o  m
        public Iterable<URI> apply(Iterable<UUID> input) {
            return input == null ? null : Iterables.transform(input, new Function<UUID, URI>() {

                @Override
                public URI apply(UUID input) {
                    return input == null ? null : URIs.create(input);
                }
            });
        }
    });
}

From source file:com.google.gapid.models.Timeline.java

@Override
protected ListenableFuture<List<Event>> doLoad(Path.Any path) {
    return Futures.transform(client.get(path), v -> v.getEvents().getListList());
}

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

@Override
public ListenableFuture<Map<String, Permissions>> getPermissions(VContext ctx) {
    ListenableFuture<ServiceClient.GetPermissionsOut> perms = client.getPermissions(ctx);
    return VFutures.withUserLandChecks(ctx,
            Futures.transform(perms, new Function<ServiceClient.GetPermissionsOut, Map<String, Permissions>>() {
                @Override/*ww  w.  j  a v  a 2s  .co  m*/
                public Map<String, Permissions> apply(ServiceClient.GetPermissionsOut perms) {
                    return ImmutableMap.of(perms.version, perms.perms);
                }
            }));
}

From source file:com.facebook.presto.split.BufferingSplitSource.java

@Override
public ListenableFuture<List<Split>> getNextBatch(int maxSize) {
    checkArgument(maxSize > 0, "Cannot fetch a batch of zero size");
    List<Split> result = synchronizedList(new ArrayList<>(maxSize));
    ListenableFuture<?> future = fetchSplits(Math.min(bufferSize, maxSize), maxSize, result);
    return Futures.transform(future, ignored -> ImmutableList.copyOf(result));
}

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

public ListenableFuture<byte[]> getContent() {

    Request request = getResolver().createRequest();
    request.setVerb(HttpVerb.GET);/*  w w  w. ja  va 2s.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.concurrent.FluentFutureTask.java

@Override
public <Y> FluentFuture<Y> transform(AsyncFunction<V, Y> func) {
    return new FluentDecorator<>(Futures.transform(this, func));
}