summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/common
diff options
context:
space:
mode:
authorzachh <zachh@google.com>2017-11-01 17:23:24 -0700
committerzachh <zachh@google.com>2017-11-10 23:41:09 +0000
commit2f38c65ba17d94b187591ed917a2dfe3bbc3b772 (patch)
tree3f676b2aafe35d58ffdd86e45258cbf4eaeb7e84 /java/com/android/dialer/common
parent711512175cb047b5ea7c9b15013f3b1fc5c2cae7 (diff)
Implemented CompositePhoneLookup#isDirty.
IsDirty is implemented by (possibly in parallel) executing all child lookups, and completing as soon as the first lookup reports itself as dirty, cancelling other lookups upon completion. If a lookup fails for some reason, it is treated as not being dirty. This required adding a new method DialerFutures#firstMatching. Bug: 34672501 Test: yes PiperOrigin-RevId: 174261470 Change-Id: Icb4f7b5d9926094fc446542411d15d02a4b873a3
Diffstat (limited to 'java/com/android/dialer/common')
-rw-r--r--java/com/android/dialer/common/concurrent/DialerFutures.java138
1 files changed, 138 insertions, 0 deletions
diff --git a/java/com/android/dialer/common/concurrent/DialerFutures.java b/java/com/android/dialer/common/concurrent/DialerFutures.java
new file mode 100644
index 000000000..98299823b
--- /dev/null
+++ b/java/com/android/dialer/common/concurrent/DialerFutures.java
@@ -0,0 +1,138 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.dialer.common.concurrent;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.ImmutableList;
+import com.google.common.util.concurrent.AbstractFuture;
+import com.google.common.util.concurrent.Atomics;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.MoreExecutors;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+
+/** Static utility methods related to futures. */
+public class DialerFutures {
+
+ /**
+ * Returns a future that will complete with the same value as the first matching the supplied
+ * predicate, cancelling all inputs upon completion. If none match, {@code defaultValue} is
+ * returned.
+ *
+ * <p>If an input fails, it is treated as if the predicate did not match.
+ *
+ * <p>Cancellation of the output future will cause cancellation of all input futures.
+ *
+ * @throws IllegalArgumentException if {@code futures} is empty.
+ */
+ public static <T> ListenableFuture<T> firstMatching(
+ Iterable<? extends ListenableFuture<? extends T>> futures,
+ Predicate<T> predicate,
+ T defaultValue) {
+ return firstMatchingImpl(futures, predicate, defaultValue);
+ }
+
+ private static <T> ListenableFuture<T> firstMatchingImpl(
+ Iterable<? extends ListenableFuture<? extends T>> futures,
+ Predicate<T> predicate,
+ T defaultValue) {
+ AggregateFuture<T> output = new AnyOfFuture<>(futures);
+ // Use an atomic reference to ensure that late listeners don't pin the FirstOfFuture in memory.
+ final AtomicReference<AggregateFuture<T>> ref = Atomics.newReference(output);
+ final AtomicInteger pending = new AtomicInteger(output.futures.size());
+ for (final ListenableFuture<? extends T> future : output.futures) {
+ future.addListener(
+ new Runnable() {
+ @Override
+ public void run() {
+ // Call get() and then set() instead of getAndSet() because a volatile read/write is
+ // cheaper than a CAS and atomicity is guaranteed by setFuture.
+ AggregateFuture<T> output = ref.get();
+ if (output != null) {
+ boolean threw = false;
+ T value = null;
+ try {
+ value = Futures.getDone(future);
+ } catch (ExecutionException e) {
+ threw = true;
+ }
+ if (threw || !predicate.apply(value)) {
+ if (pending.decrementAndGet() == 0) {
+ // we are the last future (and every other future hasn't matched or failed).
+ output.set(defaultValue);
+ // no point in clearing the ref, every other listener has already run
+ }
+ } else {
+ ref.set(null); // unpin
+ output.set(value);
+ }
+ }
+ }
+ },
+ MoreExecutors.directExecutor());
+ }
+ return output;
+ }
+
+ private static class AggregateFuture<T> extends AbstractFuture<T> {
+ ImmutableList<ListenableFuture<? extends T>> futures;
+
+ AggregateFuture(Iterable<? extends ListenableFuture<? extends T>> futures) {
+ ImmutableList<ListenableFuture<? extends T>> futuresCopy = ImmutableList.copyOf(futures);
+ if (futuresCopy.isEmpty()) {
+ throw new IllegalArgumentException("Expected at least one future, got 0.");
+ }
+ this.futures = futuresCopy;
+ }
+
+ // increase visibility
+ @Override
+ protected boolean set(T t) {
+ return super.set(t);
+ }
+
+ @Override
+ protected boolean setFuture(ListenableFuture<? extends T> t) {
+ return super.setFuture(t);
+ }
+ }
+
+ // Propagates cancellation to all inputs cancels all inputs upon completion
+ private static final class AnyOfFuture<T> extends AggregateFuture<T> {
+ AnyOfFuture(Iterable<? extends ListenableFuture<? extends T>> futures) {
+ super(futures);
+ }
+
+ @SuppressWarnings("ShortCircuitBoolean")
+ @Override
+ protected void afterDone() {
+ ImmutableList<ListenableFuture<? extends T>> localFutures = futures;
+ futures = null; // unpin
+ // even though afterDone is only called once, it is possible that the 'futures' field is null
+ // because it isn't final and thus the write might not be visible if the future instance was
+ // unsafely published. See the comment at the top of Futures.java on memory visibility.
+ if (localFutures != null) {
+ boolean interrupt = !isCancelled() | wasInterrupted();
+ for (ListenableFuture<? extends T> future : localFutures) {
+ future.cancel(interrupt);
+ }
+ }
+ }
+ }
+}