KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > util > AbstractListenerContainer


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool.util;
7
8
9 /**
10  * Implementation of {@link ListenerContainerIF} that uses a reads/write lock to handle concurrency in a safe and
11  * fast way.
12  * <p>
13  * The registered listeners are offered to subclasses through the protected {@link #getListeners} method. This
14  * method returns a reference to an array containing the registered listeners. A new array holding the listeners
15  * is created everytime a modification on the registration list is required (add/remove listener). Therefore,
16  * subclasses can safely iterate over the received array.
17  *
18  * Your code sould look like this:
19  * <code>
20  * <pre>
21      Object[] listeners = getListeners();
22      for(int i=0; i<listeners.length; i++) {
23          // do something
24      }
25  </pre>
26  </code>
27  * </p>
28  *
29  * @version $Revision: 1.8 $, $Date: 2004/03/16 08:48:33 $
30  * @author Christian Nedregaard (christian_nedregaard@email.com)
31  * @author $Author: brenuart $ (current maintainer)
32  * @since Proxool 0.7
33  */

34 public abstract class AbstractListenerContainer implements ListenerContainerIF {
35         
36     private Object JavaDoc[] listeners = EMPTY_LISTENERS;
37     private static final Object JavaDoc[] EMPTY_LISTENERS = new Object JavaDoc[]{};
38
39     
40     /**
41      * @see ListenerContainerIF#addListener(Object)
42      */

43     public synchronized void addListener(Object JavaDoc listener)
44     {
45         if(listener==null)
46             throw new NullPointerException JavaDoc("Unexpected NULL listener argument received");
47         
48         // create a new array
49
Object JavaDoc[] newListeners = new Object JavaDoc[listeners.length+1];
50         
51         // copy listeners currently registered
52
System.arraycopy(listeners, 0, newListeners, 0, listeners.length);
53         
54         // add the new one
55
newListeners[listeners.length] = listener;
56         
57         // commit changes
58
listeners = newListeners;
59     }
60     
61     
62     /**
63      * @see ListenerContainerIF#removeListener(Object)
64      */

65     public synchronized boolean removeListener(Object JavaDoc listener)
66     {
67         if(listener==null)
68             throw new NullPointerException JavaDoc("Unexpected NULL listener argument received");
69         
70         // find listener to remove in the list
71
int index=-1;
72         for(int i=0; i<listeners.length; i++) {
73             if( listeners[i]==listener ) {
74                 index = i;
75                 break;
76             }
77         }
78         
79         // not found ?
80
if( index==-1 )
81             return false;
82         
83         // create a new array of the right size
84
Object JavaDoc[] newListeners = new Object JavaDoc[listeners.length-1];
85         
86         // copy registered listeners minus the one to remove
87
if( index > 0 )
88             System.arraycopy(listeners, 0, newListeners, 0, index);
89         
90         if( index < listeners.length-1 )
91             System.arraycopy(listeners, index+1, newListeners, index, listeners.length-index-1);
92         
93         // commit
94
listeners = newListeners;
95         return true;
96     }
97
98     
99     /**
100      * Get a reference to the array of registered listeners.
101      *
102      * @return reference to the array containing registered listeners (always not NULL)
103      */

104     protected Object JavaDoc[] getListeners() {
105         return listeners;
106     }
107
108
109     /**
110      * @see ListenerContainerIF#isEmpty()
111      */

112     public boolean isEmpty() {
113         return listeners.length==0;
114     }
115 }
116
117 /*
118  Revision history:
119  $Log: AbstractListenerContainer.java,v $
120  Revision 1.8 2004/03/16 08:48:33 brenuart
121  Changes in the AbstractListenerContainer:
122  - provide more efficient concurrent handling;
123  - better handling of RuntimeException thrown by external listeners.
124
125  Revision 1.7 2003/03/11 00:12:11 billhorsman
126  switch to concurrent package
127
128  Revision 1.6 2003/03/10 15:26:55 billhorsman
129  refactoringn of concurrency stuff (and some import
130  optimisation)
131
132  Revision 1.5 2003/03/03 11:12:01 billhorsman
133  fixed licence
134
135  Revision 1.4 2003/02/19 19:35:21 chr32
136  Formated code in javadoc.
137
138  Revision 1.3 2003/02/07 17:20:18 billhorsman
139  checkstyle
140
141  Revision 1.2 2003/02/07 15:06:43 billhorsman
142  fixed isEmpty bug
143
144  Revision 1.1 2003/02/07 01:46:31 chr32
145  Initial revition.
146
147 */
Popular Tags