]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/ListenerList.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / ListenerList.java
diff --git a/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/ListenerList.java b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/ListenerList.java
new file mode 100644 (file)
index 0000000..c9be5e7
--- /dev/null
@@ -0,0 +1,125 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+/*\r
+ * 12.6.2007\r
+ */\r
+package org.simantics.utils.datastructures;\r
+\r
+import java.lang.reflect.Array;\r
+\r
+import org.simantics.utils.threads.SyncListenerList;\r
+\r
+/**\r
+ * ListenerList is an efficient and synchronized list of listeners.\r
+ * The class is optimized for quick getListeners and slow add/remove.\r
+ * \r
+ * @see SyncListenerList for thread context switching listener list impl\r
+ * \r
+ * @author Toni Kalajainen\r
+ * @param <T> \r
+ */\r
+public class ListenerList<T> {\r
+    \r
+    /**\r
+     * Array of listeners\r
+     */\r
+    private volatile T [] array;\r
+    \r
+    /** \r
+     * The class of T\r
+     */\r
+    private final Class<T> componentType;\r
+           \r
+    /**\r
+     * Construct new Listener List\r
+     * @param componentType the class of the listener type\r
+     */\r
+    public ListenerList(Class<T> componentType)\r
+    {\r
+        this.componentType = componentType;\r
+        array = createArray(0);\r
+    }\r
+    \r
+    public T[] getListeners()\r
+    {\r
+        return array;\r
+    }\r
+    \r
+    public synchronized void add(T listener)\r
+    {\r
+        int oldLength = array.length;\r
+        int newLength = oldLength + 1;\r
+        T newArray[] = createArray(newLength);\r
+        System.arraycopy(array, 0, newArray, 0, oldLength);\r
+        newArray[oldLength] = listener;\r
+        array = newArray;\r
+    }\r
+    \r
+    /**\r
+     * Removes the first occurance of listener.\r
+     * If the listener is added multiple times, then it must be removed\r
+     * as many times.\r
+     * \r
+     * @param listener a listener\r
+     * @return the listener that was removed from the list\r
+     */\r
+    public synchronized boolean remove(T listener)\r
+    {\r
+        int pos = getPos(listener);\r
+        if (pos<0) return false;\r
+        \r
+        int oldLength = array.length;\r
+        int newLength = oldLength -1;\r
+        T newArray[] = createArray(newLength);\r
+        \r
+        // Copy beginning\r
+        if (pos>0)\r
+            System.arraycopy(array, 0, newArray, 0, pos);\r
+        \r
+        // Copy ending\r
+        if (pos<newLength)\r
+            System.arraycopy(array, pos+1, newArray, pos, newLength-pos);\r
+        \r
+        array = newArray;\r
+        return true;\r
+    }        \r
+    \r
+    private synchronized int getPos(T listener)\r
+    {\r
+        for (int i=0; i<array.length; i++)\r
+            if (array[i] == listener)\r
+                return i;\r
+        return -1;\r
+    }\r
+    \r
+    public int size()\r
+    {\r
+        return array.length;\r
+    }\r
+    \r
+    public boolean isEmpty()\r
+    {\r
+        return array.length == 0;\r
+    }\r
+    \r
+    public void clear()\r
+    {\r
+        array = createArray(0);\r
+    }\r
+    \r
+    @SuppressWarnings("unchecked")\r
+    private T[] createArray(int size)\r
+    {\r
+        return (T[]) Array.newInstance(componentType, size);\r
+    }\r
+    \r
+}\r