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.continuuity.loom.common.zookeeper.lib.ZKMap.java

public ListenableFuture<T> getOrWait(String key) {
    if (currentView.get().containsKey(key)) {
        return Futures.immediateFuture(currentView.get().get(key));
    }//from  ww w  .j a va 2  s.c  o m
    SettableFuture<T> future = SettableFuture.create();
    waitingForElements.put(key, future);
    return future;
}

From source file:io.joynr.dispatching.MessageReceiverMock.java

@Override
public Future<Void> start(MessageArrivedListener messageArrivedListener,
        ReceiverStatusListener... statusListeners) {
    this.messageArrivedListener = messageArrivedListener;
    started = true;/*from  ww  w  .  ja v  a  2s  . co m*/

    if (isBlockInitialisation()) {
        // Added to check if Dispatcher blocks on addReplyCaller
        try {
            Thread.sleep(Long.MAX_VALUE);
        } catch (InterruptedException e) {

        }
    }

    for (ReceiverStatusListener statusListener : statusListeners) {
        statusListener.receiverStarted();
    }

    return Futures.immediateFuture(null);

}

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

@Override
public <T> Future<Map<T, Object>> getAll(Collection<T> keys) {
    return Futures.immediateFuture(memcacheService.getAll(keys));
}

From source file:com.orangerhymelabs.helenus.cassandra.AbstractCassandraRepository.java

public ListenableFuture<T> create(T entity) {
    ListenableFuture<ResultSet> future = submitCreate(entity);
    return Futures.transformAsync(future, new AsyncFunction<ResultSet, T>() {
        @Override//from w  ww . java 2s .  c o  m
        public ListenableFuture<T> apply(ResultSet result) throws Exception {
            if (result.wasApplied()) {
                return Futures.immediateFuture(entity);
            }

            return Futures.immediateFailedFuture(new DuplicateItemException(entity.toString()));
        }
    }, MoreExecutors.directExecutor());
}

From source file:com.proofpoint.event.monitor.test.SerialScheduledExecutorService.java

@Override
public <T> Future<T> submit(Runnable runnable, T t) {
    try {/*from w  ww  .  jav a  2 s  .  co  m*/
        runnable.run();
        return Futures.immediateFuture(t);
    } catch (Exception e) {
        return Futures.immediateFailedFuture(e);
    }
}

From source file:io.v.rx.syncbase.RxTable.java

private <T> Observable<T> getInitial(final BatchDatabase db, final String tableName, final String key,
        final TypeToken<T> tt, final T defaultValue) {
    @SuppressWarnings("unchecked")
    final ListenableFuture<T> fromGet = (ListenableFuture<T>) db.getTable(tableName).get(mVContext, key,
            tt == null ? Object.class : tt.getType());
    return toObservable(Futures.withFallback(fromGet,
            t -> t instanceof NoExistException ? Futures.immediateFuture(defaultValue)
                    : Futures.immediateFailedFuture(t)));
}

From source file:org.dswarm.tools.apiclients.DswarmGraphExtensionAPIClient.java

private static Observable<Tuple2<String, String>> generateReadDataModelRequest(
        final Tuple2<String, String> dataModelRequestInputTuple) {

    final String dataModelId = dataModelRequestInputTuple._1;
    final String recordClassURI = dataModelRequestInputTuple._2;

    final String dataModelURI = String.format(DswarmToolsStatics.DATA_MODEL_URI_TEMPLATE, dataModelId);

    final ObjectNode requestJSON = DswarmToolsStatics.MAPPER.createObjectNode();

    requestJSON.put(DswarmToolsStatics.DATA_MODEL_URI_IDENTIFIER, dataModelURI)
            .put(DswarmToolsStatics.RECORD_CLASS_URI_IDENTIFIER, recordClassURI);

    return Observable.from(Futures.immediateFuture(DswarmToolUtils.serialize(requestJSON,
            "something went wrong while serializing the request JSON for the read-data-model-content-request")))
            .map(requestJSONString -> Tuple.of(dataModelId, requestJSONString));
}

From source file:ch.raffael.util.swing.SwingUtil.java

public static ListenableFuture<Void> invokeInEventQueue(Runnable runnable) {
    if (SwingUtilities.isEventDispatchThread()) {
        return Futures.immediateFuture(null);
    } else {//w  w  w  . j  a  v  a2  s.  c  om
        ListenableFutureTask<Void> future = ListenableFutureTask.create(runnable, null);
        SwingUtilities.invokeLater(future);
        return future;
    }
}

From source file:com.orangerhymelabs.helenus.cassandra.document.DocumentService.java

public ListenableFuture<Document> create(String database, String table, Document document) {
    ListenableFuture<AbstractDocumentRepository> docs = acquireRepositoryFor(database, table);
    return Futures.transformAsync(docs, new AsyncFunction<AbstractDocumentRepository, Document>() {
        @Override/*from  w w  w . jav a 2  s.  co  m*/
        public ListenableFuture<Document> apply(AbstractDocumentRepository docRepo) throws Exception {
            try {
                ValidationEngine.validateAndThrow(document);
                Document newDoc = docRepo.create(document).get();
                updateViews(newDoc);
                return Futures.immediateFuture(newDoc);
            } catch (ValidationException e) {
                return Futures.immediateFailedFuture(e);
            }
        }

        private void updateViews(Document document) throws InterruptedException, ExecutionException {
            List<View> tableViews = getTableViews(database, table).get();
            tableViews.forEach(new Consumer<View>() {
                @Override
                public void accept(View v) {
                    try {
                        Identifier id = v.identifierFrom(document);

                        if (id != null) {
                            Document viewDoc = new Document(document.object());
                            viewDoc.identifier(id);
                            Document newView = createViewDocument(v, viewDoc).get();
                            System.out.println(newView.createdAt());
                        }
                    } catch (KeyDefinitionException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
        }

        //         private ListenableFuture<List<Document>> updateViews(Document document)
        //         {
        //            ListenableFuture<List<View>> tableViews = getTableViews(database, table);
        //            return Futures.transformAsync(tableViews, new AsyncFunction<List<View>, List<Document>>()
        //            {
        //               @Override
        //               public ListenableFuture<List<Document>> apply(List<View> input)
        //               throws Exception
        //               {
        //                  List<ListenableFuture<Document>> newDocs = new ArrayList<>(input.size());
        //                  input.parallelStream().forEach(new Consumer<View>()
        //                  {
        //                     @Override
        //                     public void accept(View v)
        //                     {
        //                        try
        //                        {
        //                            Identifier id = v.identifierFrom(document);
        //
        //                           if (id != null)
        //                           {
        //                              Document viewDoc = new Document(document.object());
        //                              viewDoc.identifier(id);
        //                              newDocs.add(createViewDocument(v, document));
        //                           }
        //                        }
        //                        catch (KeyDefinitionException e)
        //                        {
        //                           e.printStackTrace();
        //                        }
        //                     }
        //                  });
        //
        //                  return null;
        //               }
        //            });
        //         }

    }, MoreExecutors.directExecutor());
}

From source file:io.v.x.jni.test.fortune.FortuneServerImpl.java

@Override
public ListenableFuture<Map<String, String>> parameterizedGet(VContext context, ServerCall call) {
    if (lastAddedFortune == null) {
        return Futures.immediateFailedFuture(new NoFortunesException(context));
    }/* w w w.j  av  a  2  s .c  o m*/
    return Futures.immediateFuture((Map<String, String>) ImmutableMap.of(lastAddedFortune, lastAddedFortune));
}