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

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

Introduction

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

Prototype

@CheckReturnValue
public static <V> ListenableFuture<V> immediateFuture(@Nullable V value) 

Source Link

Document

Creates a ListenableFuture which has its value set immediately upon construction.

Usage

From source file:com.facebook.buck.artifact_cache.InMemoryArtifactCache.java

@Override
public ListenableFuture<Void> store(ArtifactInfo info, BorrowablePath output) {
    try (InputStream inputStream = Files.newInputStream(output.getPath())) {
        store(info, ByteStreams.toByteArray(inputStream));
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }/*from  ww  w  . java 2s  .  co m*/

    return Futures.immediateFuture(null);
}

From source file:com.google.devtools.build.lib.remote.ByteStreamBuildEventArtifactUploader.java

@Override
public ListenableFuture<PathConverter> upload(Map<Path, LocalFile> files) {
    if (files.isEmpty()) {
        return Futures.immediateFuture(PathConverter.NO_CONVERSION);
    }/*ww w .j  ava2 s . c o  m*/
    List<ListenableFuture<PathDigestPair>> uploads = new ArrayList<>(files.size());

    Context prevCtx = ctx.attach();
    try {
        for (Path file : files.keySet()) {
            Chunker chunker = new Chunker(file);
            Digest digest = chunker.digest();
            ListenableFuture<PathDigestPair> upload = Futures.transform(
                    uploader.uploadBlobAsync(chunker, /*forceUpload=*/false),
                    unused -> new PathDigestPair(file, digest), MoreExecutors.directExecutor());
            uploads.add(upload);
        }

        return Futures.transform(Futures.allAsList(uploads),
                (uploadsDone) -> new PathConverterImpl(remoteServerInstanceName, uploadsDone),
                MoreExecutors.directExecutor());
    } catch (IOException e) {
        return Futures.immediateFailedFuture(e);
    } finally {
        ctx.detach(prevCtx);
    }
}

From source file:com.spotify.folsom.FakeRawMemcacheClient.java

@Override
public <T> ListenableFuture<T> send(Request<T> request) {
    if (!connected) {
        return Futures.immediateFailedFuture(new MemcacheClosedException("Disconnected"));
    }//from www .j a v a 2s . c  o  m

    if (request instanceof SetRequest) {
        map.put(ByteBuffer.wrap(request.getKey()), ((SetRequest) request).getValue());
        return (ListenableFuture<T>) Futures.<MemcacheStatus>immediateFuture(MemcacheStatus.OK);
    }

    if (request instanceof GetRequest) {
        byte[] value = map.get(ByteBuffer.wrap(request.getKey()));
        if (value == null) {
            return (ListenableFuture<T>) Futures.immediateFuture(null);
        }
        return (ListenableFuture<T>) Futures.immediateFuture(GetResult.success(value, 0L));
    }

    if (request instanceof MultiRequest) {
        List<GetResult<byte[]>> result = Lists.newArrayList();
        MultiRequest<?> multiRequest = (MultiRequest<?>) request;
        for (byte[] key : multiRequest.getKeys()) {
            byte[] value = map.get(ByteBuffer.wrap(key));
            if (value != null) {
                result.add(GetResult.success(value, 0));
            } else {
                result.add(null);
            }
        }
        return (ListenableFuture<T>) Futures.<List<GetResult<byte[]>>>immediateFuture(result);
    }

    // Don't actually do anything here
    if (request instanceof TouchRequest) {
        return (ListenableFuture<T>) Futures.<MemcacheStatus>immediateFuture(MemcacheStatus.OK);
    }

    if (request instanceof IncrRequest) {
        IncrRequest incrRequest = (IncrRequest) request;
        byte[] key = request.getKey();
        byte[] value = map.get(ByteBuffer.wrap(key));
        if (value == null) {
            return (ListenableFuture<T>) Futures.<Long>immediateFuture(null);
        }
        long longValue = Long.parseLong(new String(value));
        long newValue = longValue + incrRequest.multiplier() * incrRequest.getBy();
        map.put(ByteBuffer.wrap(key), Long.toString(newValue).getBytes());
        return (ListenableFuture<T>) Futures.<Long>immediateFuture(newValue);
    }

    if (request instanceof DeleteRequest) {
        map.remove(ByteBuffer.wrap(request.getKey()));
        return (ListenableFuture<T>) Futures.<MemcacheStatus>immediateFuture(MemcacheStatus.OK);
    }

    throw new RuntimeException("Unsupported operation: " + request.getClass());
}

From source file:org.eclipse.osee.cache.admin.internal.CacheFactory.java

public <K, V> Cache<K, V> createLoadingCache(final CacheConfiguration config,
        final CacheDataLoader<K, V> dataLoader, final CacheKeysLoader<K> keyLoader) throws Exception {
    Preconditions.checkNotNull(config, "cacheConfiguration");
    Preconditions.checkNotNull(dataLoader, "cacheDataLoader");
    Preconditions.checkNotNull(keyLoader, "cacheKeysLoader");

    final LoadingCache<K, V> loadingCache = createCacheBuilder(config).build(new CacheLoader<K, V>() {

        @Override//from  w  ww  .j a  v a 2  s  .c  om
        public Map<K, V> loadAll(Iterable<? extends K> keys) throws Exception {
            return dataLoader.load(keys);
        }

        @Override
        public V load(K key) throws Exception {
            return dataLoader.load(key);
        }

        @Override
        public ListenableFuture<V> reload(K key, V oldValue) throws Exception {
            V newValue = dataLoader.reload(key, oldValue);
            return Futures.immediateFuture(newValue);
        }
    });
    Cache<K, V> toReturn = new LoadingCacheProxy<K, V>(loadingCache, keyLoader);
    return toReturn;
}

From source file:org.opendaylight.controller.clustering.it.provider.BasicRpcTestProvider.java

@Override
public Future<RpcResult<Void>> basicGlobal() {
    LOG.info("Basic test global rpc invoked");

    return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
}

From source file:com.google.pubsub.clients.experimental.CPSSubscriberTask.java

@Override
public ListenableFuture<AckReply> receiveMessage(PubsubMessage message) {
    recordMessageLatency(Integer.parseInt(message.getAttributesMap().get("clientId")),
            Integer.parseInt(message.getAttributesMap().get("sequenceNumber")),
            System.currentTimeMillis() - Long.parseLong(message.getAttributesMap().get("sendTime")));
    return Futures.immediateFuture(AckReply.ACK);
}

From source file:org.esbtools.eventhandler.lightblue.testing.MultiStringNotification.java

@Override
public Future<Collection<DocumentEvent>> toDocumentEvents() {
    return Futures.immediateFuture(Arrays.asList(new MultiStringDocumentEvent(entity.get_id(), values, clock)));
}

From source file:com.google.api.server.spi.config.datastore.testing.FakeAsyncMemcacheService.java

@Override
public Future<Boolean> delete(Object key) {
    return Futures.immediateFuture(memcacheService.delete(key));
}

From source file:org.opendaylight.didm.ovs.OpenFlowDeviceDriver.java

@Override
public Future<RpcResult<AdjustFlowOutput>> adjustFlow(AdjustFlowInput input) {
    List<Flow> adjustedFlows = new ArrayList<Flow>();

    LOG.info("Mininet adjustFlow");

    // TODO: finish this method, but for now just return the same flow that was received
    org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.didm.drivers.openflow.rev150211.adjust.flow.input.Flow flow = input
            .getFlow();//w  w  w. j a v  a2 s .co m

    adjustedFlows.add(new FlowBuilder(flow).build());
    AdjustFlowOutputBuilder outputBuilder = new AdjustFlowOutputBuilder();
    outputBuilder.setFlow(adjustedFlows);
    AdjustFlowOutput rpcResultType = outputBuilder.build();
    return Futures
            .immediateFuture(RpcResultBuilder.<AdjustFlowOutput>status(true).withResult(rpcResultType).build());

}

From source file:org.thingsboard.rule.engine.metadata.TbGetOriginatorFieldsNode.java

private ListenableFuture<Void> putEntityFields(TbContext ctx, EntityId entityId, TbMsg msg) {
    if (config.getFieldsMapping().isEmpty()) {
        return Futures.immediateFuture(null);
    } else {/*ww w .java 2 s.  c om*/
        return Futures.transform(EntitiesFieldsAsyncLoader.findAsync(ctx, entityId), data -> {
            config.getFieldsMapping().forEach((field, metaKey) -> {
                String val = data.getFieldValue(field);
                if (val != null) {
                    msg.getMetaData().putValue(metaKey, val);
                }
            });
            return null;
        });
    }
}