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