]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/ListenerGroup.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / ListenerGroup.java
1 /*****************************************************/\r
2 /*          This java file is a part of the          */\r
3 /*                                                   */\r
4 /*           -  Plouf's Java IRC Client  -           */\r
5 /*                                                   */\r
6 /*   Copyright (C)  2002 - 2004 Philippe Detournay   */\r
7 /*                                                   */\r
8 /*         All contacts : theplouf@yahoo.com         */\r
9 /*                                                   */\r
10 /*  PJIRC is free software; you can redistribute     */\r
11 /*  it and/or modify it under the terms of the GNU   */\r
12 /*  General Public License as published by the       */\r
13 /*  Free Software Foundation; version 2 or later of  */\r
14 /*  the License.                                     */\r
15 /*                                                   */\r
16 /*  PJIRC is distributed in the hope that it will    */\r
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */\r
18 /*  even the implied warranty of MERCHANTABILITY or  */\r
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */\r
20 /*  General Public License for more details.         */\r
21 /*                                                   */\r
22 /*  You should have received a copy of the GNU       */\r
23 /*  General Public License along with PJIRC; if      */\r
24 /*  not, write to the Free Software Foundation,      */\r
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */\r
26 /*  MA  02111-1307  USA                              */\r
27 /*                                                   */\r
28 /*****************************************************/\r
29 \r
30 package irc;\r
31 \r
32 import java.util.Enumeration;\r
33 import java.util.Vector;\r
34 \r
35 /**\r
36  * Handles a group of listeners.\r
37  */\r
38 public class ListenerGroup {\r
39         private Vector _listeners;\r
40 \r
41         /**\r
42          * Create a new ListenerGroup.\r
43          */\r
44         public ListenerGroup() {\r
45                 _listeners = new Vector();\r
46         }\r
47 \r
48         /**\r
49          * Add a listener to the group.\r
50          * \r
51          * @param listener\r
52          *          listener to add.\r
53          */\r
54         public synchronized void addListener(Object listener) {\r
55                 _listeners.insertElementAt(listener, _listeners.size());\r
56         }\r
57 \r
58         /**\r
59          * Remove a listener from the group.\r
60          * \r
61          * @param listener\r
62          *          listener to remove.\r
63          */\r
64         public synchronized void removeListener(Object listener) {\r
65                 for (int i = 0; i < _listeners.size(); i++) {\r
66                         if (_listeners.elementAt(i) == listener) {\r
67                                 _listeners.removeElementAt(i);\r
68                                 return;\r
69                         }\r
70                 }\r
71         }\r
72 \r
73         /**\r
74          * Send an event to all listeners in the group, in the event thread.\r
75          * \r
76          * @param method\r
77          *          method to call on each listener.\r
78          * @param params\r
79          *          parameters to pass to the called method.\r
80          */\r
81         public synchronized void sendEventAsync(String method, Object[] params) {\r
82                 Enumeration e = _listeners.elements();\r
83                 while (e.hasMoreElements())\r
84                         EventDispatcher.dispatchEventAsync(e.nextElement(), method, params);\r
85         }\r
86 \r
87         /**\r
88          * Send an event to all listeners in the group, in the event thread.\r
89          * \r
90          * @param method\r
91          *          method to call on each listener.\r
92          */\r
93         public synchronized void sendEventAsync(String method) {\r
94                 sendEventAsync(method, new Object[0]);\r
95         }\r
96 \r
97         /**\r
98          * Send an event to all listeners in the group, in the event thread.\r
99          * \r
100          * @param method\r
101          *          method to call on each listener.\r
102          * @param param1\r
103          *          first parameter to pass to the called method.\r
104          */\r
105         public synchronized void sendEventAsync(String method, Object param1) {\r
106                 Object[] p = { param1 };\r
107                 sendEventAsync(method, p);\r
108         }\r
109 \r
110         /**\r
111          * Send an event to all listeners in the group, in the event thread.\r
112          * \r
113          * @param method\r
114          *          method to call on each listener.\r
115          * @param param1\r
116          *          first parameter to pass to the called method.\r
117          * @param param2\r
118          *          second parameter to pass to the called method.\r
119          */\r
120         public synchronized void sendEventAsync(String method, Object param1, Object param2) {\r
121                 Object[] p = { param1, param2 };\r
122                 sendEventAsync(method, p);\r
123         }\r
124 \r
125         /**\r
126          * Send an event to all listeners in the group, in the event thread.\r
127          * \r
128          * @param method\r
129          *          method to call on each listener.\r
130          * @param param1\r
131          *          first parameter to pass to the called method.\r
132          * @param param2\r
133          *          second parameter to pass to the called method.\r
134          * @param param3\r
135          *          third parameter to pass to the called method.\r
136          */\r
137         public synchronized void sendEventAsync(String method, Object param1, Object param2, Object param3) {\r
138                 Object[] p = { param1, param2, param3 };\r
139                 sendEventAsync(method, p);\r
140         }\r
141 \r
142         /**\r
143          * Send an event to all listeners in the group. The event will be processed\r
144          * synchrounsly in the current thread.\r
145          * \r
146          * @param method\r
147          *          method to call on each listener.\r
148          * @param params\r
149          *          parameters to pass to the called method.\r
150          * @return array of returned resuluts for each method call.\r
151          * @deprecated use sendEventEx instead.\r
152          */\r
153         @Deprecated\r
154         public synchronized Object[] sendEvent(String method, Object[] params) {\r
155                 try {\r
156                         return sendEventEx(method, params);\r
157                 } catch (Throwable ex) {\r
158                         ex.printStackTrace();\r
159                         return null;\r
160                 }\r
161         }\r
162 \r
163         /**\r
164          * Send an event to all listeners in the group. The event will be processed\r
165          * synchrounsly in the current thread.\r
166          * \r
167          * @param method\r
168          *          method to call on each listener.\r
169          * @param params\r
170          *          parameters to pass to the called method.\r
171          * @return array of returned resuluts for each method call.\r
172          * @throws Throwable\r
173          */\r
174         public synchronized Object[] sendEventEx(String method, Object[] params) throws Throwable {\r
175                 Object[] res = new Object[_listeners.size()];\r
176                 int i = 0;\r
177                 Enumeration e = _listeners.elements();\r
178                 while (e.hasMoreElements()) {\r
179                         try {\r
180                                 res[i++] = EventDispatcher.dispatchEventSyncEx(e.nextElement(), method, params);\r
181                         } catch (Throwable ex) {\r
182                                 ex.printStackTrace();\r
183                         }\r
184                 }\r
185                 return res;\r
186         }\r
187 \r
188         /**\r
189          * Send an event to all listeners in the group in the current thread.\r
190          * \r
191          * @param method\r
192          *          method to call on each listener.\r
193          * @return array of returned resuluts for each method call.\r
194          */\r
195         public synchronized Object[] sendEvent(String method) {\r
196                 return sendEvent(method, new Object[0]);\r
197         }\r
198 \r
199         /**\r
200          * Send an event to all listeners in the group in the current thread.\r
201          * \r
202          * @param method\r
203          *          method to call on each listener.\r
204          * @param param1\r
205          *          first parameter to pass to the called method.\r
206          * @return array of returned resuluts for each method call.\r
207          */\r
208         public synchronized Object[] sendEvent(String method, Object param1) {\r
209                 Object[] p = { param1 };\r
210                 return sendEvent(method, p);\r
211         }\r
212 \r
213         /**\r
214          * Send an event to all listeners in the group in the current thread.\r
215          * \r
216          * @param method\r
217          *          method to call on each listener.\r
218          * @param param1\r
219          *          first parameter to pass to the called method.\r
220          * @param param2\r
221          *          second parameter to pass to the called method.\r
222          * @return array of returned resuluts for each method call.\r
223          */\r
224         public synchronized Object[] sendEvent(String method, Object param1, Object param2) {\r
225                 Object[] p = { param1, param2 };\r
226                 return sendEvent(method, p);\r
227         }\r
228 \r
229         /**\r
230          * Send an event to all listeners in the group in the current thread.\r
231          * \r
232          * @param method\r
233          *          method to call on each listener.\r
234          * @param param1\r
235          *          first parameter to pass to the called method.\r
236          * @param param2\r
237          *          second parameter to pass to the called method.\r
238          * @param param3\r
239          *          third parameter to pass to the called method.\r
240          * @return array of returned resuluts for each method call.\r
241          */\r
242         public synchronized Object[] sendEvent(String method, Object param1, Object param2, Object param3) {\r
243                 Object[] p = { param1, param2, param3 };\r
244                 return sendEvent(method, p);\r
245         }\r
246 \r
247         /**\r
248          * Send an event to all listeners in the group in the current thread.\r
249          * \r
250          * @param method\r
251          *          method to call on each listener.\r
252          * @return array of returned resuluts for each method call.\r
253          * @throws Throwable\r
254          */\r
255         public synchronized Object[] sendEventEx(String method) throws Throwable {\r
256                 return sendEventEx(method, new Object[0]);\r
257         }\r
258 \r
259         /**\r
260          * Send an event to all listeners in the group in the current thread.\r
261          * \r
262          * @param method\r
263          *          method to call on each listener.\r
264          * @param param1\r
265          *          first parameter to pass to the called method.\r
266          * @return array of returned resuluts for each method call.\r
267          * @throws Throwable\r
268          */\r
269         public synchronized Object[] sendEventEx(String method, Object param1) throws Throwable {\r
270                 Object[] p = { param1 };\r
271                 return sendEventEx(method, p);\r
272         }\r
273 \r
274         /**\r
275          * Send an event to all listeners in the group in the current thread.\r
276          * \r
277          * @param method\r
278          *          method to call on each listener.\r
279          * @param param1\r
280          *          first parameter to pass to the called method.\r
281          * @param param2\r
282          *          second parameter to pass to the called method.\r
283          * @return array of returned resuluts for each method call.\r
284          * @throws Throwable\r
285          */\r
286         public synchronized Object[] sendEventEx(String method, Object param1, Object param2) throws Throwable {\r
287                 Object[] p = { param1, param2 };\r
288                 return sendEventEx(method, p);\r
289         }\r
290 \r
291         /**\r
292          * Send an event to all listeners in the group in the current thread.\r
293          * \r
294          * @param method\r
295          *          method to call on each listener.\r
296          * @param param1\r
297          *          first parameter to pass to the called method.\r
298          * @param param2\r
299          *          second parameter to pass to the called method.\r
300          * @param param3\r
301          *          third parameter to pass to the called method.\r
302          * @return array of returned resuluts for each method call.\r
303          * @throws Throwable\r
304          */\r
305         public synchronized Object[] sendEventEx(String method, Object param1, Object param2, Object param3) throws Throwable {\r
306                 Object[] p = { param1, param2, param3 };\r
307                 return sendEventEx(method, p);\r
308         }\r
309 \r
310 }\r