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, Executor executor) 

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:org.robotninjas.concurrent.FluentFutureTask.java

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

From source file:com.skcraft.plume.common.util.concurrent.DeferredImpl.java

@Override
public Deferred<Void> then(Callback<I> task, ListeningExecutorService executor) {
    return new DeferredImpl<>(Futures.transform(future, (AsyncFunction<I, Void>) input -> {
        task.handle(input);/* ww w.  j ava  2 s  . c  om*/
        return Futures.immediateFuture(null);
    }, executor), defaultExecutor);
}

From source file:org.robotninjas.concurrent.FluentDecorator.java

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

From source file:io.prestosql.split.SampledSplitSource.java

@Override
public ListenableFuture<SplitBatch> getNextBatch(ConnectorPartitionHandle partitionHandle, Lifespan lifespan,
        int maxSize) {
    ListenableFuture<SplitBatch> batch = splitSource.getNextBatch(partitionHandle, lifespan, maxSize);
    return Futures.transform(batch,
            splitBatch -> new SplitBatch(splitBatch.getSplits().stream()
                    .filter(input -> ThreadLocalRandom.current().nextDouble() < sampleRatio)
                    .collect(toImmutableList()), splitBatch.isLastBatch()),
            directExecutor());//from   ww w  . j a  v a2 s  . c  o  m
}

From source file:org.robotninjas.concurrent.FluentFutureTask.java

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

From source file:com.noorq.casser.core.operation.AbstractOperation.java

public ListenableFuture<E> async() {

    ResultSetFuture resultSetFuture = sessionOps.executeAsync(options(buildStatement()), showValues);

    ListenableFuture<E> future = Futures.transform(resultSetFuture, new Function<ResultSet, E>() {

        @Override/*from w w w. j a va  2s.  co m*/
        public E apply(ResultSet resultSet) {
            return transform(resultSet);
        }

    }, sessionOps.getExecutor());

    return future;
}

From source file:io.prestosql.split.ConnectorAwareSplitSource.java

@Override
public ListenableFuture<SplitBatch> getNextBatch(ConnectorPartitionHandle partitionHandle, Lifespan lifespan,
        int maxSize) {
    ListenableFuture<ConnectorSplitBatch> nextBatch = toListenableFuture(
            source.getNextBatch(partitionHandle, maxSize));
    return Futures.transform(nextBatch, splitBatch -> {
        ImmutableList.Builder<Split> result = ImmutableList.builder();
        for (ConnectorSplit connectorSplit : splitBatch.getSplits()) {
            result.add(new Split(connectorId, transactionHandle, connectorSplit, lifespan));
        }//w ww .  j  a v  a 2s  .  co  m
        return new SplitBatch(result.build(), splitBatch.isNoMoreSplits());
    }, directExecutor());
}

From source file:com.facebook.buck.remoteexecution.grpc.GrpcAsyncBlobFetcher.java

@Override
public ListenableFuture<ByteBuffer> fetch(Protocol.Digest digest) {
    /** Payload received on a fetch request. */
    class Data {
        ByteString data = ByteString.EMPTY;

        public ByteBuffer get() {
            return data.asReadOnlyByteBuffer();
        }/*from w  ww .jav  a2s .c o  m*/

        public void concat(ByteString bytes) {
            data = data.concat(bytes);
        }
    }

    Data data = new Data();
    return closeScopeWhenFutureCompletes(CasBlobDownloadEvent.sendEvent(buckEventBus, 1, digest.getSize()),
            Futures.transform(GrpcRemoteExecutionClients.readByteStream(instanceName, digest, byteStreamStub,
                    data::concat), ignored -> data.get(), MoreExecutors.directExecutor()));
}

From source file:com.facebook.buck.distributed.LocalFsContentsProvider.java

@Override
public ListenableFuture<Boolean> materializeFileContentsAsync(BuildJobStateFileHashEntry entry,
        Path targetAbsPath) {/*from  w  w w.  j a  v  a2s .  c  o m*/
    RuleKey key = new RuleKey(entry.getSha1());
    return Futures.transform(dirCache.fetchAsync(null, key, LazyPath.ofInstance(targetAbsPath)),
            (CacheResult result) -> result.getType() == CacheResultType.HIT, MoreExecutors.directExecutor());
}

From source file:org.thingsboard.server.dao.attributes.BaseAttributesDao.java

@Override
public ListenableFuture<Optional<AttributeKvEntry>> find(EntityId entityId, String attributeType,
        String attributeKey) {//w  ww .  j a  v a  2  s  .co  m
    Select.Where select = select().from(ATTRIBUTES_KV_CF)
            .where(eq(ENTITY_TYPE_COLUMN, entityId.getEntityType())).and(eq(ENTITY_ID_COLUMN, entityId.getId()))
            .and(eq(ATTRIBUTE_TYPE_COLUMN, attributeType)).and(eq(ATTRIBUTE_KEY_COLUMN, attributeKey));
    log.trace("Generated query [{}] for entityId {} and key {}", select, entityId, attributeKey);
    return Futures.transform(executeAsyncRead(select),
            (Function<? super ResultSet, ? extends Optional<AttributeKvEntry>>) input -> Optional
                    .ofNullable(convertResultToAttributesKvEntry(attributeKey, input.one())),
            readResultsProcessingExecutor);
}