1
/* -*- Mode: java; tab-width: 4; indent-tabs-mode: 1; c-basic-offset: 4 -*-
3
* The contents of this file are subject to the Netscape Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/NPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is Rhino code, released
16
* The Initial Developer of the Original Code is Netscape
17
* Communications Corporation. Portions created by Netscape are
18
* Copyright (C) 1997-1999 Netscape Communications Corporation. All
24
* Alternatively, the contents of this file may be used under the
25
* terms of the GNU Public License (the "GPL"), in which case the
26
* provisions of the GPL are applicable instead of those above.
27
* If you wish to allow use of your version of this file only
28
* under the terms of the GPL and not to allow others to use your
29
* version of this file under the NPL, indicate your decision by
30
* deleting the provisions above and replace them with the notice
31
* and other provisions required by the GPL. If you do not delete
32
* the provisions above, a recipient may use your version of this
33
* file under either the NPL or the GPL.
36
/* Helper class to add/remove listeners from Object array */
38
package org.mozilla.javascript;
41
* Utility class to manage listeners array.
42
* A possible usage would be:
44
private Object[] listeners;
46
void addListener(ListenerType listener) {
48
listeners = ListenerArray.add(listeners, listener);
52
void removeListener(ListenerType listener) {
54
listeners = ListenerArray.remove(listeners, listener);
58
* Here is a thread safe while synchronization free example of event firing
60
void fireEvent(EventType event) {
61
Object[] array = listeners;
63
for (int i = array.length; i-- != 0;) {
64
((ListenerType)array[i]).onEvent(event);
71
* or if listeners of different types can present in listeners array:
73
void fireEvent(EventType event) {
74
Object[] array = listeners;
76
for (int i = array.length; i-- != 0;) {
77
Object obj = array[i];
78
if (obj instanceof ListenerType) {
79
((ListenerType)obj).onEvent(event);
87
public class ListenerArray {
89
/** Return newly allocated array that contains listener and all elements
91
** Note: listener is added to resulting array even if it is already
93
public static Object[] add(Object[] data, Object listener) {
99
Object[] tmp = new Object[N + 1];
100
System.arraycopy(data, 0, tmp, 1, N);
107
/** Return a copy of data array with the first occurrence of listener
109
** If listener is not present in data, simply return data.
110
** Note: return <code>null</code> if listener is the single element
112
public static Object[] remove(Object[] data, Object listener) {
115
for (int i = 0; i != N; ++i) {
116
if (data[i] == listener) {
117
if (N == 1) { data = null; }
119
Object[] tmp = new Object[N - 1];
120
System.arraycopy(data, 0, tmp, 0, i);
121
System.arraycopy(data, i + 1, tmp, i, N - 1 - i);