1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.util; |
16 |
| |
17 |
| import java.util.Iterator; |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| public class EventListenerList |
30 |
| { |
31 |
| private static final int START_SIZE = 5; |
32 |
| |
33 |
| private Object[] _listeners; |
34 |
| private int _count; |
35 |
| private int _iteratorCount; |
36 |
| private int _uid; |
37 |
| |
38 |
| private class ListenerIterator implements Iterator |
39 |
| { |
40 |
| private Object[] _localListeners; |
41 |
| private int _localCount; |
42 |
| private int _localUid; |
43 |
| private int _pos; |
44 |
| |
45 |
12156
| private ListenerIterator()
|
46 |
| { |
47 |
12156
| _localListeners = _listeners;
|
48 |
12156
| _localCount = _count;
|
49 |
12156
| _localUid = _uid;
|
50 |
| } |
51 |
| |
52 |
30542
| public boolean hasNext()
|
53 |
| { |
54 |
30542
| if (_pos >= _localCount)
|
55 |
| { |
56 |
| |
57 |
| |
58 |
| |
59 |
12055
| adjustIteratorCount(_localUid);
|
60 |
| |
61 |
12055
| _localListeners = null;
|
62 |
12055
| _localCount = 0;
|
63 |
12055
| _localUid = -1;
|
64 |
12055
| _pos = 0;
|
65 |
| |
66 |
12055
| return false;
|
67 |
| } |
68 |
| |
69 |
18487
| return true;
|
70 |
| } |
71 |
| |
72 |
18502
| public Object next()
|
73 |
| { |
74 |
18502
| return _localListeners[_pos++];
|
75 |
| } |
76 |
| |
77 |
1
| public void remove()
|
78 |
| { |
79 |
1
| throw new UnsupportedOperationException();
|
80 |
| } |
81 |
| |
82 |
| } |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
12156
| public synchronized Iterator getListeners()
|
95 |
| { |
96 |
12156
| _iteratorCount++;
|
97 |
| |
98 |
12156
| return new ListenerIterator();
|
99 |
| } |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
19921
| public synchronized void addListener(Object listener)
|
106 |
| { |
107 |
19931
| copyOnWrite(_count + 1);
|
108 |
| |
109 |
19886
| _listeners[_count] = listener;
|
110 |
| |
111 |
19904
| _count++;
|
112 |
| } |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
103
| public synchronized void removeListener(Object listener)
|
121 |
| { |
122 |
103
| for (int i = 0; i < _count; i++)
|
123 |
| { |
124 |
137
| if (_listeners[i] == listener)
|
125 |
| { |
126 |
102
| removeListener(i);
|
127 |
102
| return;
|
128 |
| } |
129 |
| } |
130 |
| } |
131 |
| |
132 |
102
| private void removeListener(int index)
|
133 |
| { |
134 |
102
| copyOnWrite(_count);
|
135 |
| |
136 |
| |
137 |
| |
138 |
102
| _listeners[index] = _listeners[_count - 1];
|
139 |
| |
140 |
| |
141 |
| |
142 |
102
| _listeners[_count - 1] = null;
|
143 |
| |
144 |
102
| _count--;
|
145 |
| } |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
20030
| private void copyOnWrite(int requiredSize)
|
153 |
| { |
154 |
20024
| int size = _listeners == null ? 0 : _listeners.length;
|
155 |
| |
156 |
20027
| if (_iteratorCount > 0 || size < requiredSize)
|
157 |
| { |
158 |
12533
| int nominalSize = (size == 0) ? START_SIZE : 2 * size;
|
159 |
| |
160 |
| |
161 |
12533
| if (size >= requiredSize)
|
162 |
| { |
163 |
101
| nominalSize = size;
|
164 |
| } |
165 |
| |
166 |
12533
| int newSize = Math.max(requiredSize, nominalSize);
|
167 |
| |
168 |
12533
| Object[] newListeners = new Object[newSize];
|
169 |
| |
170 |
12534
| if (_count > 0)
|
171 |
357
| System.arraycopy(_listeners, 0, newListeners, 0, _count);
|
172 |
| |
173 |
12518
| _listeners = newListeners;
|
174 |
| |
175 |
| |
176 |
12535
| _iteratorCount = 0;
|
177 |
12535
| _uid++;
|
178 |
| } |
179 |
| } |
180 |
| |
181 |
12055
| private synchronized void adjustIteratorCount(int iteratorUid)
|
182 |
| { |
183 |
12055
| if (_uid == iteratorUid)
|
184 |
12053
| _iteratorCount--;
|
185 |
| } |
186 |
| } |