aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/collections/ListenableArrayList.java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /vespajlib/src/main/java/com/yahoo/collections/ListenableArrayList.java
Publish
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/collections/ListenableArrayList.java')
-rw-r--r--vespajlib/src/main/java/com/yahoo/collections/ListenableArrayList.java75
1 files changed, 75 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/collections/ListenableArrayList.java b/vespajlib/src/main/java/com/yahoo/collections/ListenableArrayList.java
new file mode 100644
index 00000000000..1b77e97d159
--- /dev/null
+++ b/vespajlib/src/main/java/com/yahoo/collections/ListenableArrayList.java
@@ -0,0 +1,75 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.collections;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * An array list which notifies listeners after one or more items are added
+ *
+ * @author <a href="mailto:bratseth@yahoo-inc.com">Jon Bratseth</a>
+ * @since 5.1.17
+ */
+@SuppressWarnings("serial")
+public class ListenableArrayList<ITEM> extends ArrayList<ITEM> {
+
+ private List<Runnable> listeners = null;
+
+ public ListenableArrayList() {}
+
+ public ListenableArrayList(int initialCapacity) {
+ super(initialCapacity);
+ }
+
+ @Override
+ public boolean add(ITEM e) {
+ boolean result = super.add(e);
+ notifyListeners();
+ return result;
+ }
+
+ @Override
+ public void add(int index, ITEM e) {
+ super.add(index, e);
+ notifyListeners();
+ }
+
+ @Override
+ public boolean addAll(Collection<? extends ITEM> a) {
+ boolean result = super.addAll(a);
+ notifyListeners();
+ return result;
+ }
+
+ @Override
+ public boolean addAll(int index, Collection<? extends ITEM> a) {
+ boolean result = super.addAll(index, a);
+ notifyListeners();
+ return result;
+ }
+
+ @Override
+ public ITEM set(int index, ITEM e) {
+ ITEM result = super.set(index, e);
+ notifyListeners();
+ return result;
+ }
+
+ /**
+ * Adds a listener which is invoked whenever elements are added to this.
+ * This may not be invoked once for each added element.
+ */
+ public void addListener(Runnable listener) {
+ if (listeners == null)
+ listeners = new ArrayList<>();
+ listeners.add(listener);
+ }
+
+ private void notifyListeners() {
+ if (listeners == null) return;
+ for (Runnable listener : listeners)
+ listener.run();
+ }
+
+}