]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ListenerList.java
Sync git svn branch with SVN repository r33308.
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ListenerList.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package fi.vtt.simantics.procore.internal;\r
13 \r
14 import java.lang.reflect.Array;\r
15 \r
16 /**\r
17  * ListenerList is an efficient and synchronized list of listeners.\r
18  * The class is optimized for quick getListeners and slow add/remove.\r
19  * \r
20  * @author Toni Kalajainen\r
21  */\r
22 public class ListenerList<T> {\r
23     \r
24     /**\r
25      * Array of listeners\r
26      */\r
27     private volatile T [] array;\r
28     \r
29     /** \r
30      * The class of T\r
31      */\r
32     private final Class<T> componentType;\r
33            \r
34     /**\r
35      * Construct new Listener List\r
36      * @param componentType the class of the listener type\r
37      */\r
38     public ListenerList(Class<T> componentType)\r
39     {\r
40         this.componentType = componentType;\r
41         array = createArray(0);\r
42     }\r
43     \r
44     public T[] getListeners()\r
45     {\r
46         return array;\r
47     }\r
48     \r
49     public synchronized void add(T listener)\r
50     {\r
51         int oldLength = array.length;\r
52         int newLength = oldLength + 1;\r
53         T newArray[] = createArray(newLength);\r
54         System.arraycopy(array, 0, newArray, 0, oldLength);\r
55         newArray[oldLength] = listener;\r
56         array = newArray;\r
57     }\r
58     \r
59     /**\r
60      * Removes the first occurance of listener.\r
61      * If the listener is added multiple times, then it must be removed\r
62      * as many times.\r
63      * \r
64      * @param listener a listener\r
65      * @return the listener that was removed from the list\r
66      */\r
67     public synchronized boolean remove(T listener)\r
68     {\r
69         int pos = getPos(listener);\r
70         if (pos<0) return false;\r
71         \r
72         int oldLength = array.length;\r
73         int newLength = oldLength -1;\r
74         T newArray[] = createArray(newLength);\r
75         \r
76         // Copy beginning\r
77         if (pos>0)\r
78             System.arraycopy(array, 0, newArray, 0, pos);\r
79         \r
80         // Copy ending\r
81         if (pos<newLength)\r
82             System.arraycopy(array, pos+1, newArray, pos, newLength-pos);\r
83         \r
84         array = newArray;\r
85         return true;\r
86     }        \r
87     \r
88     private synchronized int getPos(T listener)\r
89     {\r
90         for (int i=0; i<array.length; i++)\r
91             if (array[i] == listener)\r
92                 return i;\r
93         return -1;\r
94     }\r
95     \r
96     public int size()\r
97     {\r
98         return array.length;\r
99     }\r
100     \r
101     public boolean isEmpty()\r
102     {\r
103         return array.length == 0;\r
104     }\r
105     \r
106     public void clear()\r
107     {\r
108         array = createArray(0);\r
109     }\r
110     \r
111     @SuppressWarnings("unchecked")\r
112     private T[] createArray(int size)\r
113     {\r
114         return (T[]) Array.newInstance(componentType, size);\r
115     }\r
116     \r
117 }\r