2003/05/12 05:10:30
[org.ibex.core.git] / src / org / mozilla / javascript / ListenerArray.java
1 /* -*- Mode: java; tab-width: 4; indent-tabs-mode: 1; c-basic-offset: 4 -*-\r
2  *\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
7  *\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
12  *\r
13  * The Original Code is Rhino code, released\r
14  * May 6, 1999.\r
15  *\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
19  * Rights Reserved.\r
20  *\r
21  * Contributor(s):\r
22  * Igor Bukanov\r
23  *\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
34  */\r
35 \r
36 /* Helper class to add/remove listeners from Object array */\r
37 \r
38 package org.mozilla.javascript;\r
39 \r
40 /**\r
41  * Utility class to manage listeners array.\r
42  * A possible usage would be:\r
43 <pre>\r
44     private Object[] listeners;\r
45     ...\r
46     void addListener(ListenerType listener) {\r
47         synchronized (this) {\r
48             listeners = ListenerArray.add(listeners, listener);\r
49         }\r
50     }\r
51 \r
52     void removeListener(ListenerType listener) {\r
53         synchronized (this) {\r
54             listeners = ListenerArray.remove(listeners, listener);\r
55         }\r
56     }\r
57 </pre>\r
58   * Here is a thread safe while synchronization free example of event firing\r
59 <pre>\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
65             }\r
66         }\r
67 \r
68     }\r
69 </pre>\r
70 \r
71  * or if listeners of different types can present in listeners array:\r
72 <pre>\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
80                 }\r
81             }\r
82         }\r
83 \r
84     }\r
85 </pre>\r
86  */\r
87 public class ListenerArray {\r
88 \r
89     /** Return newly allocated array that contains listener and all elements\r
90      ** from data array.\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
94         if (data == null) {\r
95             data = new Object[1];\r
96         }\r
97         else {\r
98             int N = data.length;\r
99             Object[] tmp = new Object[N + 1];\r
100             System.arraycopy(data, 0, tmp, 1, N);\r
101             data = tmp;\r
102         }\r
103         data[0] = listener;\r
104         return data;\r
105     }\r
106 \r
107     /** Return a copy of data array with the first occurrence of listener\r
108      ** removed.\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
111      ** of data. */\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
118                     else {\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
122                         data = tmp;\r
123                     }\r
124                     break;\r
125                 }\r
126             }\r
127         }\r
128         return data;\r
129     }\r
130 \r
131 }\r