Example usage for com.google.common.util.concurrent SettableFuture create

List of usage examples for com.google.common.util.concurrent SettableFuture create

Introduction

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

Prototype

public static <V> SettableFuture<V> create() 

Source Link

Document

Creates a new SettableFuture that can be completed or cancelled by a later method call.

Usage

From source file:org.opendaylight.netconf.topology.pipeline.tx.ProxyReadOnlyTransaction.java

@Override
public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final LogicalDatastoreType store,
        final YangInstanceIdentifier path) {
    final Future<Optional<NormalizedNodeMessage>> future = delegate.read(store, path);
    final SettableFuture<Optional<NormalizedNode<?, ?>>> settableFuture = SettableFuture.create();
    final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> checkedFuture = Futures
            .makeChecked(settableFuture, new Function<Exception, ReadFailedException>() {
                @Nullable//from  w ww.j a  v  a 2s.  c  o  m
                @Override
                public ReadFailedException apply(Exception cause) {
                    return new ReadFailedException("Read from transaction failed", cause);
                }
            });
    future.onComplete(new OnComplete<Optional<NormalizedNodeMessage>>() {
        @Override
        public void onComplete(Throwable throwable, Optional<NormalizedNodeMessage> normalizedNodeMessage)
                throws Throwable {
            if (throwable == null) {
                if (normalizedNodeMessage.isPresent()) {
                    settableFuture.set(normalizedNodeMessage
                            .transform(new Function<NormalizedNodeMessage, NormalizedNode<?, ?>>() {
                                @Nullable
                                @Override
                                public NormalizedNode<?, ?> apply(NormalizedNodeMessage input) {
                                    return input.getNode();
                                }
                            }));
                } else {
                    settableFuture.set(Optional.absent());
                }
            } else {
                settableFuture.setException(throwable);
            }
        }
    }, actorSystem.dispatcher());
    return checkedFuture;
}

From source file:org.waveprotocol.box.server.waveletstate.segment.BlockWaveletStateStub.java

@Override
public ListenableFuture<Map<String, Block>> readBlocks(Set<String> blockIds) throws WaveletStateException {
    readBlocksCalls++;/* w w  w.j  a va 2 s .  c  o  m*/
    SettableFuture<Map<String, Block>> ret = SettableFuture.create();
    ImmutableMap.Builder<String, Block> builder = ImmutableMap.builder();
    for (String blockId : blockIds) {
        Block block = blocks.get(blockId);
        if (block != null) {
            builder.put(blockId, block);
        }
    }
    ret.set(builder.build());
    return ret;
}

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 .ja  v  a 2 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:com.mirantis.opendaylight.Retrier.java

public ListenableFuture<V> retry() {
    SettableFuture<V> promise = SettableFuture.create();
    int attempt = attempts;
    executor.execute(() -> doRetry(promise, attempt));
    return promise;
}

From source file:org.opendaylight.controller.cluster.datastore.SingleCommitCohortProxy.java

@Override
public ListenableFuture<Boolean> canCommit() {
    LOG.debug("Tx {} canCommit", transactionId);

    final SettableFuture<Boolean> returnFuture = SettableFuture.create();

    cohortFuture.onComplete(new OnComplete<Object>() {
        @Override//ww  w.  j  a v  a 2s  .co  m
        public void onComplete(Throwable failure, Object cohortResponse) {
            if (failure != null) {
                operationCallbackRef.get().failure();
                returnFuture.setException(failure);
                return;
            }

            operationCallbackRef.get().success();

            LOG.debug("Tx {} successfully completed direct commit", transactionId);

            // The Future was the result of a direct commit to the shard, essentially eliding the
            // front-end 3PC coordination. We don't really care about the specific Future
            // response object, only that it completed successfully. At this point the Tx is complete
            // so return true. The subsequent preCommit and commit phases will be no-ops, ie return
            // immediate success, to complete the 3PC for the front-end.
            returnFuture.set(Boolean.TRUE);
        }
    }, actorContext.getClientDispatcher());

    return returnFuture;
}

From source file:com.google.devtools.kythe.extractors.shared.ExtractorUtils.java

public static List<FileData> processRequiredInputs(Iterable<String> files) throws ExtractionException {
    final SettableFuture<ExtractionException> exception = SettableFuture.create();

    List<FileData> result = Lists.newArrayList(Iterables.transform(files, new Function<String, FileData>() {
        @Override//  w  w w. ja v  a2 s .  co  m
        public FileData apply(String path) {
            byte[] content = new byte[0];
            try {
                content = Files.toByteArray(new File(path));
            } catch (IOException e) {
                exception.set(new ExtractionException(e, false));
            }
            if (content == null) {
                exception.set(new ExtractionException(String.format("Unable to locate required input %s", path),
                        false));
                return null;
            }
            String digest = getContentDigest(content);
            return createFileData(path, digest, content);
        }
    }));
    if (exception.isDone()) {
        try {
            throw exception.get();
        } catch (InterruptedException e) {
            throw new ExtractionException(e, true);
        } catch (ExecutionException e) {
            throw new ExtractionException(e, false);
        }
    }
    return result;
}

From source file:io.prestosql.operator.exchange.LocalExchangeMemoryManager.java

public synchronized ListenableFuture<?> getNotFullFuture() {
    // if we are full and the current not full future is already complete, create a new one
    if (bufferedBytes.get() > maxBufferedBytes && notFullFuture.isDone()) {
        notFullFuture = SettableFuture.create();
    }//from w  ww  . j  a  v  a  2  s .c o m
    return notFullFuture;
}

From source file:org.opendaylight.messenger.impl.MessengerService.java

@Override
public Future<RpcResult<SendMessageOutput>> sendMessage(SendMessageInput input) {
    final SettableFuture<RpcResult<SendMessageOutput>> futureResult = SettableFuture.create();
    if (messProv.sendMessage(input.getMessId(), input.getMessageSource(), input.getMessageDest(),
            input.getText())) {/*from   w  ww .  j  a v  a  2  s  .  com*/
        final SendMessageOutput messOutput = new SendMessageOutputBuilder().setMessageId(input.getMessId())
                .build();
        futureResult.set(RpcResultBuilder.<SendMessageOutput>success(messOutput).build());
    } else {
        futureResult.set(RpcResultBuilder.<SendMessageOutput>failed().build());
    }
    return futureResult;
}

From source file:org.opendaylight.netconf.topology.impl.NetconfNodeOperationalDataAggregator.java

@Override
public ListenableFuture<Node> combineCreateAttempts(final List<ListenableFuture<Node>> stateFutures) {
    final SettableFuture<Node> future = SettableFuture.create();
    final ListenableFuture<List<Node>> allAsList = Futures.allAsList(stateFutures);
    Futures.addCallback(allAsList, new FutureCallback<List<Node>>() {
        @Override//from   w  ww .j a v  a2  s .co m
        public void onSuccess(final List<Node> result) {
            Node base = null;
            NetconfNode baseAugmentation = null;
            AvailableCapabilities masterCaps = null;
            UnavailableCapabilities unavailableMasterCaps = null;
            final ArrayList<NodeStatus> statusList = new ArrayList<>();
            for (final Node node : result) {
                final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
                if (base == null && netconfNode.getConnectionStatus().equals(ConnectionStatus.Connected)) {
                    base = node;
                    baseAugmentation = netconfNode;
                }
                // we need to pull out caps from master, since slave does not go through resolution
                if (masterCaps == null) {
                    masterCaps = netconfNode.getAvailableCapabilities();
                    unavailableMasterCaps = netconfNode.getUnavailableCapabilities();
                }
                if (netconfNode.getAvailableCapabilities().getAvailableCapability().size() > masterCaps
                        .getAvailableCapability().size()) {
                    masterCaps = netconfNode.getAvailableCapabilities();
                    unavailableMasterCaps = netconfNode.getUnavailableCapabilities();
                }
                LOG.debug(netconfNode.toString());
                statusList.addAll(netconfNode.getClusteredConnectionStatus().getNodeStatus());
            }

            if (base == null) {
                base = result.get(0);
                baseAugmentation = result.get(0).getAugmentation(NetconfNode.class);
                LOG.debug("All results {}", result.toString());
            }

            final Node aggregatedNode = new NodeBuilder(base).addAugmentation(NetconfNode.class,
                    new NetconfNodeBuilder(baseAugmentation)
                            .setClusteredConnectionStatus(
                                    new ClusteredConnectionStatusBuilder().setNodeStatus(statusList).build())
                            .setAvailableCapabilities(masterCaps)
                            .setUnavailableCapabilities(unavailableMasterCaps).build())
                    .build();

            future.set(aggregatedNode);
        }

        @Override
        public void onFailure(final Throwable t) {
            LOG.error("One of the combined create attempts failed {}", t);
            future.setException(t);
        }
    });
    return future;
}

From source file:com.android.camera.one.v2.autofocus.AFTriggerResult.java

public AFTriggerResult() {
    mFutureResult = SettableFuture.create();
    mStateMachine = new TriggerStateMachine(CaptureRequest.CONTROL_AF_TRIGGER_START, TRIGGER_DONE_STATES);
}