Commit 38127d1744bbe793cfc345d692fed643852d12a4
1 parent
181d76f7
implement remaining LinkedList methods in HandlerList
Showing
1 changed file
with
216 additions
and
37 deletions
java/common/com/mumfrey/liteloader/core/event/HandlerList.java
@@ -62,19 +62,29 @@ public class HandlerList<T> extends LinkedList<T> | @@ -62,19 +62,29 @@ public class HandlerList<T> extends LinkedList<T> | ||
62 | this.type = type; | 62 | this.type = type; |
63 | } | 63 | } |
64 | 64 | ||
65 | - /* (non-Javadoc) | ||
66 | - * @see java.util.LinkedList#add(java.lang.Object) | 65 | + /** |
66 | + * Returns the baked list of all listeners | ||
67 | + * | ||
68 | + * @return | ||
67 | */ | 69 | */ |
68 | - @Override | ||
69 | - public boolean add(T listener) | 70 | + public T all() |
70 | { | 71 | { |
71 | - if (!this.contains(listener)) | 72 | + if (this.bakedHandler == null) |
72 | { | 73 | { |
73 | - super.add(listener); | ||
74 | - this.invalidate(); | 74 | + this.bake(); |
75 | } | 75 | } |
76 | 76 | ||
77 | - return true; | 77 | + return this.bakedHandler.get(); |
78 | + } | ||
79 | + | ||
80 | + /** | ||
81 | + * Bake the current handler list | ||
82 | + */ | ||
83 | + protected void bake() | ||
84 | + { | ||
85 | + HandlerListClassLoader<T> classLoader = new HandlerListClassLoader<T>(this.type, this.size()); | ||
86 | + this.bakedHandler = classLoader.newHandler(); | ||
87 | + this.bakedHandler.populate(this); | ||
78 | } | 88 | } |
79 | 89 | ||
80 | /** | 90 | /** |
@@ -82,6 +92,11 @@ public class HandlerList<T> extends LinkedList<T> | @@ -82,6 +92,11 @@ public class HandlerList<T> extends LinkedList<T> | ||
82 | */ | 92 | */ |
83 | public void invalidate() | 93 | public void invalidate() |
84 | { | 94 | { |
95 | + if (this.bakedHandler == null) | ||
96 | + { | ||
97 | + return; | ||
98 | + } | ||
99 | + | ||
85 | this.bakedHandler = null; | 100 | this.bakedHandler = null; |
86 | HandlerList.uncollectedHandlerLists++; | 101 | HandlerList.uncollectedHandlerLists++; |
87 | if (HandlerList.uncollectedHandlerLists > HandlerList.MAX_UNCOLLECTED_CLASSES) | 102 | if (HandlerList.uncollectedHandlerLists > HandlerList.MAX_UNCOLLECTED_CLASSES) |
@@ -90,58 +105,137 @@ public class HandlerList<T> extends LinkedList<T> | @@ -90,58 +105,137 @@ public class HandlerList<T> extends LinkedList<T> | ||
90 | HandlerList.uncollectedHandlerLists = 0; | 105 | HandlerList.uncollectedHandlerLists = 0; |
91 | } | 106 | } |
92 | } | 107 | } |
93 | - | ||
94 | - /** | ||
95 | - * Returns the baked list of all listeners | ||
96 | - * | ||
97 | - * @return | 108 | + |
109 | + /* (non-Javadoc) | ||
110 | + * @see java.util.LinkedList#add(java.lang.Object) | ||
98 | */ | 111 | */ |
99 | - public T all() | 112 | + @Override |
113 | + public boolean add(T listener) | ||
100 | { | 114 | { |
101 | - if (this.bakedHandler == null) | 115 | + if (!this.contains(listener)) |
102 | { | 116 | { |
103 | - HandlerListClassLoader<T> classLoader = new HandlerListClassLoader<T>(this.type, this.size()); | ||
104 | - this.bakedHandler = classLoader.newHandler(); | ||
105 | - this.bakedHandler.populate(this); | 117 | + super.add(listener); |
118 | + this.invalidate(); | ||
106 | } | 119 | } |
107 | 120 | ||
108 | - return this.bakedHandler.get(); | 121 | + return true; |
109 | } | 122 | } |
110 | 123 | ||
111 | /* (non-Javadoc) | 124 | /* (non-Javadoc) |
112 | - * @see java.util.LinkedList#remove() | 125 | + * @see java.util.LinkedList#offer(java.lang.Object) |
113 | */ | 126 | */ |
114 | @Override | 127 | @Override |
115 | - public T remove() | 128 | + public boolean offer(T listener) |
116 | { | 129 | { |
117 | - throw new UnsupportedOperationException("'remove' is not supported for HandlerList"); | 130 | + return this.add(listener); |
118 | } | 131 | } |
119 | 132 | ||
120 | /* (non-Javadoc) | 133 | /* (non-Javadoc) |
121 | - * @see java.util.LinkedList#remove(int) | 134 | + * @see java.util.LinkedList#offerFirst(java.lang.Object) |
122 | */ | 135 | */ |
123 | @Override | 136 | @Override |
124 | - public T remove(int index) | 137 | + public boolean offerFirst(T listener) |
125 | { | 138 | { |
126 | - throw new UnsupportedOperationException("'remove' is not supported for HandlerList"); | 139 | + this.addFirst(listener); |
140 | + return true; | ||
127 | } | 141 | } |
128 | 142 | ||
129 | /* (non-Javadoc) | 143 | /* (non-Javadoc) |
130 | - * @see java.util.LinkedList#remove(java.lang.Object) | 144 | + * @see java.util.LinkedList#offerLast(java.lang.Object) |
131 | */ | 145 | */ |
132 | @Override | 146 | @Override |
133 | - public boolean remove(Object o) | 147 | + public boolean offerLast(T listener) |
134 | { | 148 | { |
135 | - throw new UnsupportedOperationException("'remove' is not supported for HandlerList"); | 149 | + this.addLast(listener); |
150 | + return true; | ||
136 | } | 151 | } |
137 | 152 | ||
138 | /* (non-Javadoc) | 153 | /* (non-Javadoc) |
139 | - * @see java.util.AbstractCollection#removeAll(java.util.Collection) | 154 | + * @see java.util.LinkedList#add(int, java.lang.Object) |
155 | + */ | ||
156 | + @Override | ||
157 | + public void add(int index, T listener) | ||
158 | + { | ||
159 | + if (!this.contains(listener)) | ||
160 | + { | ||
161 | + super.add(index, listener); | ||
162 | + this.invalidate(); | ||
163 | + } | ||
164 | + } | ||
165 | + | ||
166 | + /* (non-Javadoc) | ||
167 | + * @see java.util.LinkedList#addFirst(java.lang.Object) | ||
168 | + */ | ||
169 | + @Override | ||
170 | + public void addFirst(T listener) | ||
171 | + { | ||
172 | + if (!this.contains(listener)) | ||
173 | + { | ||
174 | + super.addFirst(listener); | ||
175 | + this.invalidate(); | ||
176 | + } | ||
177 | + } | ||
178 | + | ||
179 | + /* (non-Javadoc) | ||
180 | + * @see java.util.LinkedList#addLast(java.lang.Object) | ||
181 | + */ | ||
182 | + @Override | ||
183 | + public void addLast(T listener) | ||
184 | + { | ||
185 | + if (!this.contains(listener)) | ||
186 | + { | ||
187 | + super.addLast(listener); | ||
188 | + this.invalidate(); | ||
189 | + } | ||
190 | + } | ||
191 | + | ||
192 | + /* (non-Javadoc) | ||
193 | + * @see java.util.LinkedList#addAll(java.util.Collection) | ||
140 | */ | 194 | */ |
141 | @Override | 195 | @Override |
142 | - public boolean removeAll(Collection<?> c) | 196 | + public boolean addAll(Collection<? extends T> listeners) |
143 | { | 197 | { |
144 | - throw new UnsupportedOperationException("'removeAll' is not supported for HandlerList"); | 198 | + throw new UnsupportedOperationException("'addAll' is not supported for HandlerList"); |
199 | + } | ||
200 | + | ||
201 | + /* (non-Javadoc) | ||
202 | + * @see java.util.LinkedList#addAll(int, java.util.Collection) | ||
203 | + */ | ||
204 | + @Override | ||
205 | + public boolean addAll(int index, Collection<? extends T> listeners) | ||
206 | + { | ||
207 | + throw new UnsupportedOperationException("'addAll' is not supported for HandlerList"); | ||
208 | + } | ||
209 | + | ||
210 | + /* (non-Javadoc) | ||
211 | + * @see java.util.LinkedList#remove() | ||
212 | + */ | ||
213 | + @Override | ||
214 | + public T remove() | ||
215 | + { | ||
216 | + return this.removeFirst(); | ||
217 | + } | ||
218 | + | ||
219 | + /* (non-Javadoc) | ||
220 | + * @see java.util.LinkedList#remove(int) | ||
221 | + */ | ||
222 | + @Override | ||
223 | + public T remove(int index) | ||
224 | + { | ||
225 | + T removed = super.remove(index); | ||
226 | + this.invalidate(); | ||
227 | + return removed; | ||
228 | + } | ||
229 | + | ||
230 | + /* (non-Javadoc) | ||
231 | + * @see java.util.LinkedList#remove(java.lang.Object) | ||
232 | + */ | ||
233 | + @Override | ||
234 | + public boolean remove(Object listener) | ||
235 | + { | ||
236 | + boolean removed = super.remove(listener); | ||
237 | + this.invalidate(); | ||
238 | + return removed; | ||
145 | } | 239 | } |
146 | 240 | ||
147 | /* (non-Javadoc) | 241 | /* (non-Javadoc) |
@@ -150,16 +244,18 @@ public class HandlerList<T> extends LinkedList<T> | @@ -150,16 +244,18 @@ public class HandlerList<T> extends LinkedList<T> | ||
150 | @Override | 244 | @Override |
151 | public T removeFirst() | 245 | public T removeFirst() |
152 | { | 246 | { |
153 | - throw new UnsupportedOperationException("'removeFirst' is not supported for HandlerList"); | 247 | + T removed = super.removeFirst(); |
248 | + this.invalidate(); | ||
249 | + return removed; | ||
154 | } | 250 | } |
155 | 251 | ||
156 | /* (non-Javadoc) | 252 | /* (non-Javadoc) |
157 | * @see java.util.LinkedList#removeFirstOccurrence(java.lang.Object) | 253 | * @see java.util.LinkedList#removeFirstOccurrence(java.lang.Object) |
158 | */ | 254 | */ |
159 | @Override | 255 | @Override |
160 | - public boolean removeFirstOccurrence(Object o) | 256 | + public boolean removeFirstOccurrence(Object listener) |
161 | { | 257 | { |
162 | - throw new UnsupportedOperationException("'removeFirstOccurrence' is not supported for HandlerList"); | 258 | + return this.remove(listener); |
163 | } | 259 | } |
164 | 260 | ||
165 | /* (non-Javadoc) | 261 | /* (non-Javadoc) |
@@ -168,16 +264,99 @@ public class HandlerList<T> extends LinkedList<T> | @@ -168,16 +264,99 @@ public class HandlerList<T> extends LinkedList<T> | ||
168 | @Override | 264 | @Override |
169 | public T removeLast() | 265 | public T removeLast() |
170 | { | 266 | { |
171 | - throw new UnsupportedOperationException("'removeLast' is not supported for HandlerList"); | 267 | + T removed = super.removeLast(); |
268 | + this.invalidate(); | ||
269 | + return removed; | ||
172 | } | 270 | } |
173 | 271 | ||
174 | /* (non-Javadoc) | 272 | /* (non-Javadoc) |
175 | * @see java.util.LinkedList#removeLastOccurrence(java.lang.Object) | 273 | * @see java.util.LinkedList#removeLastOccurrence(java.lang.Object) |
176 | */ | 274 | */ |
177 | @Override | 275 | @Override |
178 | - public boolean removeLastOccurrence(Object o) | 276 | + public boolean removeLastOccurrence(Object listener) |
277 | + { | ||
278 | + boolean removed = super.removeLastOccurrence(listener); | ||
279 | + this.invalidate(); | ||
280 | + return removed; | ||
281 | + } | ||
282 | + | ||
283 | + /* (non-Javadoc) | ||
284 | + * @see java.util.AbstractCollection#removeAll(java.util.Collection) | ||
285 | + */ | ||
286 | + @Override | ||
287 | + public boolean removeAll(Collection<?> listeners) | ||
288 | + { | ||
289 | + boolean removed = super.removeAll(listeners); | ||
290 | + this.invalidate(); | ||
291 | + return removed; | ||
292 | + } | ||
293 | + | ||
294 | + /* (non-Javadoc) | ||
295 | + * @see java.util.LinkedList#poll() | ||
296 | + */ | ||
297 | + @Override | ||
298 | + public T poll() | ||
299 | + { | ||
300 | + T polled = super.poll(); | ||
301 | + this.invalidate(); | ||
302 | + return polled; | ||
303 | + } | ||
304 | + | ||
305 | + /* (non-Javadoc) | ||
306 | + * @see java.util.LinkedList#pollFirst() | ||
307 | + */ | ||
308 | + @Override | ||
309 | + public T pollFirst() | ||
310 | + { | ||
311 | + T polled = super.pollFirst(); | ||
312 | + this.invalidate(); | ||
313 | + return polled; | ||
314 | + } | ||
315 | + | ||
316 | + /* (non-Javadoc) | ||
317 | + * @see java.util.LinkedList#pollLast() | ||
318 | + */ | ||
319 | + @Override | ||
320 | + public T pollLast() | ||
179 | { | 321 | { |
180 | - throw new UnsupportedOperationException("'removeLastOccurrence' is not supported for HandlerList"); | 322 | + T polled = super.pollLast(); |
323 | + this.invalidate(); | ||
324 | + return polled; | ||
325 | + } | ||
326 | + | ||
327 | + /* (non-Javadoc) | ||
328 | + * @see java.util.LinkedList#push(java.lang.Object) | ||
329 | + */ | ||
330 | + @Override | ||
331 | + public void push(T listener) | ||
332 | + { | ||
333 | + this.addFirst(listener); | ||
334 | + } | ||
335 | + | ||
336 | + /* (non-Javadoc) | ||
337 | + * @see java.util.LinkedList#pop() | ||
338 | + */ | ||
339 | + @Override | ||
340 | + public T pop() | ||
341 | + { | ||
342 | + return this.removeFirst(); | ||
343 | + } | ||
344 | + | ||
345 | + /* (non-Javadoc) | ||
346 | + * @see java.util.LinkedList#set(int, java.lang.Object) | ||
347 | + */ | ||
348 | + @Override | ||
349 | + public T set(int index, T listener) | ||
350 | + { | ||
351 | + T oldValue = null; | ||
352 | + | ||
353 | + if (!this.contains(listener)) | ||
354 | + { | ||
355 | + oldValue = super.set(index, listener); | ||
356 | + this.invalidate(); | ||
357 | + } | ||
358 | + | ||
359 | + return oldValue; | ||
181 | } | 360 | } |
182 | 361 | ||
183 | /** | 362 | /** |