-/*******************************************************************************\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
-package fi.vtt.simantics.procore.internal;\r
-\r
-import java.lang.reflect.Array;\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
- * @author Toni Kalajainen\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
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package fi.vtt.simantics.procore.internal;
+
+import java.lang.reflect.Array;
+
+/**
+ * ListenerList is an efficient and synchronized list of listeners.
+ * The class is optimized for quick getListeners and slow add/remove.
+ *
+ * @author Toni Kalajainen
+ */
+public class ListenerList<T> {
+
+ /**
+ * Array of listeners
+ */
+ private volatile T [] array;
+
+ /**
+ * The class of T
+ */
+ private final Class<T> componentType;
+
+ /**
+ * Construct new Listener List
+ * @param componentType the class of the listener type
+ */
+ public ListenerList(Class<T> componentType)
+ {
+ this.componentType = componentType;
+ array = createArray(0);
+ }
+
+ public T[] getListeners()
+ {
+ return array;
+ }
+
+ public synchronized void add(T listener)
+ {
+ int oldLength = array.length;
+ int newLength = oldLength + 1;
+ T newArray[] = createArray(newLength);
+ System.arraycopy(array, 0, newArray, 0, oldLength);
+ newArray[oldLength] = listener;
+ array = newArray;
+ }
+
+ /**
+ * Removes the first occurance of listener.
+ * If the listener is added multiple times, then it must be removed
+ * as many times.
+ *
+ * @param listener a listener
+ * @return the listener that was removed from the list
+ */
+ public synchronized boolean remove(T listener)
+ {
+ int pos = getPos(listener);
+ if (pos<0) return false;
+
+ int oldLength = array.length;
+ int newLength = oldLength -1;
+ T newArray[] = createArray(newLength);
+
+ // Copy beginning
+ if (pos>0)
+ System.arraycopy(array, 0, newArray, 0, pos);
+
+ // Copy ending
+ if (pos<newLength)
+ System.arraycopy(array, pos+1, newArray, pos, newLength-pos);
+
+ array = newArray;
+ return true;
+ }
+
+ private synchronized int getPos(T listener)
+ {
+ for (int i=0; i<array.length; i++)
+ if (array[i] == listener)
+ return i;
+ return -1;
+ }
+
+ public int size()
+ {
+ return array.length;
+ }
+
+ public boolean isEmpty()
+ {
+ return array.length == 0;
+ }
+
+ public void clear()
+ {
+ array = createArray(0);
+ }
+
+ @SuppressWarnings("unchecked")
+ private T[] createArray(int size)
+ {
+ return (T[]) Array.newInstance(componentType, size);
+ }
+
+}