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:io.v.v23.syncbase.SyncbaseAppImpl.java

@Override
public ListenableFuture<Map<String, Permissions>> getPermissions(VContext ctx) {
    return Futures.transform(client.getPermissions(ctx),
            new Function<AppClient.GetPermissionsOut, Map<String, Permissions>>() {
                @Override//from w  ww . ja va2s  . c  om
                public Map<String, Permissions> apply(AppClient.GetPermissionsOut perms) {
                    return ImmutableMap.of(perms.version, perms.perms);
                }
            });
}

From source file:com.google.gapid.image.FetchedImage.java

public static ListenableFuture<FetchedImage> load(Client client, Path.ResourceData imagePath,
        Images.Format format) {/*from w  ww.  j ava 2  s .  c o m*/
    return Futures.transform(client.get(imageData(imagePath, format.format)), value -> {
        switch (value.getValCase()) {
        case TEXTURE_2D:
            return new FetchedImage(client, format, value.getTexture2D());
        case CUBEMAP:
            return new FetchedImage(client, format, value.getCubemap());
        default:
            throw new UnsupportedOperationException("Unexpected resource type: " + value);
        }
    });
}

From source file:org.opendaylight.openflowplugin.applications.frsync.impl.SyncReactorRetryDecorator.java

public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
        final SyncupEntry syncupEntry) throws InterruptedException {

    final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
    LOG.trace("syncup retry decorator: {}", nodeId.getValue());

    if (syncupEntry.isOptimizedConfigDelta() && reconciliationRegistry.isRegistered(nodeId)) {
        LOG.debug("Config change ignored because {} is in reconcile.", nodeId.getValue());
        return Futures.immediateFuture(Boolean.FALSE);
    }//from   ww  w .  j a  v a  2 s . c om

    ListenableFuture<Boolean> syncupResult = delegate.syncup(flowcapableNodePath, syncupEntry);

    return Futures.transform(syncupResult, new Function<Boolean, Boolean>() {
        @Override
        public Boolean apply(Boolean result) {
            LOG.trace("syncup return in retry decorator: {} [{}]", nodeId.getValue(), result);
            if (result) {
                reconciliationRegistry.unregisterIfRegistered(nodeId);
                return true;
            } else {
                reconciliationRegistry.register(nodeId);
                return false;
            }
        }
    });
}

From source file:io.v.impl.google.rt.VRuntimeImpl.java

/**
 * Returns a new runtime instance./*from   ww  w.jav  a2 s  .  co 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:com.google.gapid.server.GapisProcess.java

public GapisProcess(Listener listener) {
    super("gapis");
    this.listener = (listener == null) ? Listener.NULL : listener;
    connection = Futures.transform(start(), port -> {
        LOG.log(INFO, "Established a new client connection to " + port);
        return GapisConnection.create(SERVER_HOST + ":" + port, authToken, con -> {
            shutdown();/*from   w  ww  .  j  a  va2s.  c o  m*/
        });
    });
}

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);/*w  ww. ja  va 2 s  .  c  o  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:org.opendaylight.protocol.bgp.openconfig.impl.moduleconfig.RibInstanceFunction.java

@Override
public ListenableFuture<T> apply(final String instanceName) {
    final ListenableFuture<Optional<Service>> readFuture = configModuleOp
            .readConfigService(new ServiceKey(RibInstance.class), rTx);
    return Futures.transform(readFuture, new Function<Optional<Service>, T>() {
        @Override//  w w w . ja v  a2s.  c  om
        public T apply(final Optional<Service> maybeService) {
            if (maybeService.isPresent()) {
                final Optional<Instance> maybeInstance = Iterables.tryFind(maybeService.get().getInstance(),
                        new Predicate<Instance>() {
                            @Override
                            public boolean apply(final Instance instance) {
                                final String moduleName = OpenConfigUtil.getModuleName(instance.getProvider());
                                if (moduleName.equals(instanceName)) {
                                    return true;
                                }
                                return false;
                            }
                        });
                if (maybeInstance.isPresent()) {
                    return function.apply(maybeInstance.get().getName());
                }
            }
            return null;
        }

    });
}

From source file:es.udc.pfc.gameroom.GameComponent.java

private final ListenableFuture<String> getUniqueRoomName() {
    final IQ unique = new IQ(IQ.Type.get);
    unique.setFrom(getJID());//  w  w w.  jav  a2  s.co m
    unique.setTo(JID.jid(getMUCServiceName()));
    unique.addExtension("unique", XMPPNamespaces.MUC_UNIQUE);

    return Futures.transform(sendIQ(unique), new AsyncFunction<IQ, String>() {
        @Override
        public ListenableFuture<String> apply(IQ input) throws Exception {
            final XMLElement unique = input.getExtension("unique", XMPPNamespaces.MUC_UNIQUE);
            if (unique == null)
                throw new Exception("No unique received");

            return Futures.immediateFuture(unique.getText());
        }
    });
}

From source file:org.robotninjas.barge.rpc.ProtoRpcRaftClient.java

@Nonnull
public ListenableFuture<RequestVoteResponse> requestVote(@Nonnull RequestVote request) {
    checkNotNull(request);/*from ww w .jav  a2  s  . co m*/
    return Futures.transform(call(RpcCall.requestVote(ProtoUtils.convert(request))),
            ProtoUtils.convertVoteResponse);
}

From source file:c5db.replication.C5GeneralizedReplicationService.java

@Override
public ListenableFuture<GeneralizedReplicator> createReplicator(String quorumId, Collection<Long> peerIds) {
    return Futures.transform(replicationModule.createReplicator(quorumId, peerIds), (Replicator replicator) -> {
        return new C5GeneralizedReplicator(replicator, createAndStartFiber(this::notifyFailed));
    });/*from  w ww  .  j av  a  2  s.c  o  m*/
}