summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-06-02 18:44:35 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-06-02 18:44:35 +0200
commit0b1c7cc35f68ce540b54eda3553a6ca6d4c3b347 (patch)
tree2a9209f27fc7f87155e3e49cf763daf61329539b
parent9e278c88e6a261f51ecdcf8f5788a9d0d6902b19 (diff)
Topic: John does things differently
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--app/src/main/java/com/wireguard/android/util/Topic.java59
1 files changed, 30 insertions, 29 deletions
diff --git a/app/src/main/java/com/wireguard/android/util/Topic.java b/app/src/main/java/com/wireguard/android/util/Topic.java
index 23f15e6e..b7aee601 100644
--- a/app/src/main/java/com/wireguard/android/util/Topic.java
+++ b/app/src/main/java/com/wireguard/android/util/Topic.java
@@ -10,6 +10,10 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
+import java.lang.ref.WeakReference;
+import java.util.ArrayList;
+import java.util.List;
+
public class Topic {
private static final int NON_INIT = 0;
@@ -20,27 +24,24 @@ public class Topic {
private List<WeakReference<Subscriber>> subscribers;
private Object[] results;
- public void subscribe(Subscriber sub) {
- if (subscribers == null) {
- subscribers = new LinkedList<>();
- }
- synchronized (subscribers) {
- subscribers.add(new WeakReference<>(sub));
- }
+ public Topic() {
+ subscribers = new SyncArrayList<>();
}
- public void unsubscribe() {
- subscribers = null;
+ public synchronized void subscribe(Subscriber sub) {
+ subscribers.add(new WeakReference<>(sub));
}
- public void unsubscribe(Subscriber sub) {
- synchronized (subscribers) {
- for (Iterator<WeakReference<Subscriber>> i = subscribers.iterator(); i.hasNext(); ) {
- WeakReference<Subscriber> subscriber = i.next();
- if (subscriber.get() == null || subscriber.get() == sub) {
- i.remove();
- }
- }
+ public synchronized void unsubscribe() {
+ subscribers = new SyncArrayList<>();
+ }
+
+ public synchronized void unsubscribe(Subscriber sub) {
+ List<WeakReference<Subscriber>> subs = subscribers;
+ subscribers = new ArrayList<>();
+ for (WeakReference<Subscriber> subscriber : subs) {
+ if (subscriber.get() != null && subscriber.get() != sub)
+ subscribers.add(subscriber);
}
}
@@ -61,15 +62,11 @@ public class Topic {
if (record)
state = PUBLISHED;
this.results = results;
- if (subscribers != null) {
- List<WeakReference<Subscriber>> subscribersCopy = new LinkedList<>();
- synchronized (subscribers) {
- subscribersCopy.addAll(subscribers);
- }
- for (WeakReference<Subscriber> subscriber : subscribersCopy) {
- if (subscriber.get() != null)
- subscriber.get().onTopicPublished(this);
- }
+ // Snapshot
+ List<WeakReference<Subscriber>> subs = subscribers;
+ for (WeakReference<Subscriber> subscriber : subs) {
+ if (subscriber != null && subscriber.get() != null)
+ subscriber.get().onTopicPublished(this);
}
}
@@ -94,15 +91,19 @@ public class Topic {
topic.subscribe(this);
}
}
-
default void unsubscribeTopics() {
for (Topic event : getSubscription()) {
event.unsubscribe(this);
}
}
-
void onTopicPublished(Topic topic);
-
Topic[] getSubscription();
}
+
+ private static class SyncArrayList<E> extends ArrayList<E> {
+ @Override
+ public synchronized boolean add(E e) {
+ return super.add(e);
+ }
+ }
}