List of usage examples for com.google.common.util.concurrent AsyncFunction AsyncFunction
AsyncFunction
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());/* ww w . j ava 2 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.util.examples.FunctionComposerExample.java
AsyncFunction<List<URL>, List<File>> downloadFiles() { return new AsyncFunction<List<URL>, List<File>>() { @Override/* www. j a va 2 s . c o m*/ public ListenableFuture<List<File>> apply(List<URL> input) throws Exception { System.out.println("3"); ImmutableList.Builder futures = ImmutableList.builder(); for (URL file : input) { futures.add(downloadFile(file)); } return allAsList(futures.build()); } }; }
From source file:dagger.producers.internal.MapProducer.java
@Override public ListenableFuture<Map<K, V>> compute() { return Futures.transformAsync(mapProducerProducer.get(), new AsyncFunction<Map<K, Producer<V>>, Map<K, V>>() { @Override//from ww w . j av a 2 s .co m public ListenableFuture<Map<K, V>> apply(final Map<K, Producer<V>> map) { // TODO(beder): Use Futures.whenAllComplete when Guava 20 is released. return transform( Futures.allAsList( Iterables.transform(map.entrySet(), MapProducer.<K, V>entryUnwrapper())), new Function<List<Map.Entry<K, V>>, Map<K, V>>() { @Override public Map<K, V> apply(List<Map.Entry<K, V>> entries) { return ImmutableMap.copyOf(entries); } }, directExecutor()); } }, directExecutor()); }
From source file:org.robotninjas.util.examples.ChainingExample.java
AsyncFunction<List<File>, File> mergeFiles() { return new AsyncFunction<List<File>, File>() { @Override/*from w w w . ja v a2 s .c om*/ public ListenableFuture<File> apply(List<File> input) throws Exception { return immediateFuture(new File("")); } }; }
From source file:com.android.camera.async.Futures2.java
/** * Create a new joined future from two existing futures and a joining function * that combines the resulting outputs of the previous functions into a single * result. The resulting future will fail if any of the dependent futures also * fail./*w w w.j av a2 s. c o m*/ */ public static <T1, T2, TResult> ListenableFuture<TResult> joinAll(final ListenableFuture<T1> f1, final ListenableFuture<T2> f2, final AsyncFunction2<T1, T2, TResult> fn) { ListenableFuture<?>[] futures = new ListenableFuture<?>[2]; futures[0] = f1; futures[1] = f2; // Futures.allAsList is used instead of Futures.successfulAsList because // allAsList will propagate the failures instead of null values to the // parameters of the supplied function. ListenableFuture<List<Object>> result = Futures.allAsList(futures); return Futures.transform(result, new AsyncFunction<List<Object>, TResult>() { @Override public ListenableFuture<TResult> apply(@Nullable List<Object> list) throws Exception { T1 value1 = (T1) list.get(0); T2 value2 = (T2) list.get(1); return fn.apply(value1, value2); } }); }
From source file:com.facebook.buck.util.network.LocalProcessScribeLogger.java
@Override public ListenableFuture<Void> log(final String category, final Iterable<String> lines) { ListenableFuture<Void> localCatSubmitResult = executorService.submit(new Callable<Void>() { @Override//from w w w . ja v a 2 s.c o m public Void call() throws Exception { sendToProcess(category, lines); return null; } }); return Futures.catchingAsync(localCatSubmitResult, Exception.class, new AsyncFunction<Throwable, Void>() { @Override public ListenableFuture<Void> apply(Throwable input) throws Exception { LOG.info(input, "Submitting to local process failed, falling back to http."); return fallback.log(category, lines); } }, executorService); }
From source file:org.opendaylight.toaster.impl.KitchenProvider.java
@Override public Future<RpcResult<Void>> makeBreakfast(MakeBreakfastInput input) { ListenableFuture<RpcResult<Void>> makeEggFuture = makeEgg(input); ListenableFuture<RpcResult<Void>> makeToastFuture = makeToast(input); ListenableFuture<List<RpcResult<Void>>> combinedFutures = Futures .allAsList(ImmutableList.of(makeEggFuture, makeToastFuture)); return Futures.transform(combinedFutures, new AsyncFunction<List<RpcResult<Void>>, RpcResult<Void>>() { @Override/* w ww. j a v a 2s . c o m*/ public ListenableFuture<RpcResult<Void>> apply(List<RpcResult<Void>> input) throws Exception { boolean atLeastOneSucceed = false; ImmutableList.Builder<RpcError> errorList = ImmutableList.builder(); for (RpcResult<Void> result : input) { if (result.isSuccessful()) { atLeastOneSucceed = true; } if (result.getErrors() != null) { errorList.addAll(result.getErrors()); } } return Futures.immediateFuture(Rpcs.<Void>getRpcResult(atLeastOneSucceed, errorList.build())); } }); }
From source file:me.j360.trace.storage.elasticsearch.ElasticsearchSpanConsumer.java
@Override public ListenableFuture<Void> accept(List<Span> spans) { if (spans.isEmpty()) return Futures.immediateFuture(null); // Create a bulk request when there is more than one span to store ListenableFuture<?> future;/*from w ww .ja v a 2s .c om*/ if (spans.size() == 1) { future = toGuava(createSpanIndexRequest(spans.get(0)).execute()); } else { BulkRequestBuilder request = client.prepareBulk(); for (Span span : spans) { request.add(createSpanIndexRequest(span)); } future = toGuava(request.execute()); } if (ElasticsearchStorage.FLUSH_ON_WRITES) { future = transform(future, new AsyncFunction() { @Override public ListenableFuture apply(Object input) { return toGuava(client.admin().indices().prepareFlush(indexNameFormatter.catchAll()).execute()); } }); } return transform(future, TO_VOID); }
From source file:dagger.producers.internal.MapOfProducedProducer.java
@Override public ListenableFuture<Map<K, Produced<V>>> compute() { return Futures.transformAsync(mapProducerProducer.get(), new AsyncFunction<Map<K, Producer<V>>, Map<K, Produced<V>>>() { @Override// w ww.j a v a 2 s . c o m public ListenableFuture<Map<K, Produced<V>>> apply(final Map<K, Producer<V>> map) { // TODO(beder): Use Futures.whenAllComplete when Guava 20 is released. return transform( Futures.allAsList(Iterables.transform(map.entrySet(), MapOfProducedProducer.<K, V>entryUnwrapper())), new Function<List<Map.Entry<K, Produced<V>>>, Map<K, Produced<V>>>() { @Override public Map<K, Produced<V>> apply(List<Map.Entry<K, Produced<V>>> entries) { return ImmutableMap.copyOf(entries); } }, directExecutor()); } }, directExecutor()); }
From source file:com.microsoft.services.orc.core.OrcMediaEntityFetcher.java
public ListenableFuture<InputStream> getStreamedContent() { Request request = getResolver().createRequest(); request.setVerb(HttpVerb.GET);/* w ww. j a va 2 s . co m*/ request.addOption(Request.MUST_STREAM_RESPONSE_CONTENT, "true"); OrcURL url = request.getUrl(); url.appendPathComponent("$value"); ListenableFuture<OrcResponse> future = oDataExecute(request); return Futures.transform(future, new AsyncFunction<OrcResponse, InputStream>() { @Override public ListenableFuture<InputStream> apply(OrcResponse response) throws Exception { SettableFuture<InputStream> result = SettableFuture.create(); result.set(new MediaEntityInputStream(response.openStreamedResponse(), response)); return result; } }); }