1 29 30 package nextapp.echo2.app.event; 31 32 import java.io.IOException ; 33 import java.io.ObjectInputStream ; 34 import java.io.ObjectOutputStream ; 35 import java.io.Serializable ; 36 import java.util.EventListener ; 37 38 41 public class EventListenerList 42 implements Serializable { 43 44 private static final Object [] EMPTY = new Object [0]; 45 private static final EventListener [] NO_LISTENERS = new EventListener [0]; 46 47 51 private static final int INCREMENT = 3; 52 53 57 private static final int DECREMENT = 5; 58 59 private transient Object [] listeners = EMPTY; 60 private transient int size = 0; 61 62 65 public EventListenerList() { 66 super(); 67 } 68 69 75 public void addListener(Class listenerClass, EventListener l) { 76 if (size * 2 == listeners.length) { 77 Object [] newListeners = new Object [listeners.length + INCREMENT * 2]; 79 System.arraycopy(listeners, 0, newListeners, 0, listeners.length); 80 listeners = newListeners; 81 } 82 83 listeners[size * 2] = listenerClass; 84 listeners[size * 2 + 1] = l; 85 ++size; 86 } 87 88 96 public int getListenerCount(Class listenerClass) { 97 int count = 0; 98 for (int index = 0; index < size; ++index) { 99 if (listeners[index * 2] == listenerClass) { 100 ++count; 101 } 102 } 103 return count; 104 } 105 106 114 public EventListener [] getListeners(Class listenerClass) { 115 int listenerCount = getListenerCount(listenerClass); 116 if (listenerCount == 0) { 117 return NO_LISTENERS; 118 } else { 119 EventListener [] matchingListeners = new EventListener [listenerCount]; 120 int matchIndex = 0; 121 for (int index = 0; index < size; ++index) { 122 if (listeners[index * 2] == listenerClass) { 123 matchingListeners[matchIndex++] = (EventListener ) listeners[index * 2 + 1]; 124 } 125 } 126 return matchingListeners; 127 } 128 } 129 130 133 private void readObject(ObjectInputStream in) 134 throws ClassNotFoundException , IOException { 135 in.defaultReadObject(); 136 listeners = EMPTY; 137 size = 0; 138 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 139 Object listenerClassName = in.readObject(); 140 while (listenerClassName != null) { 141 EventListener listener = (EventListener ) in.readObject(); 142 addListener(Class.forName((String ) listenerClassName, true, classLoader), listener); 143 listenerClassName = in.readObject(); 144 } 145 } 146 147 154 public void removeListener(Class listenerClass, EventListener l) { 155 for (int index = 0; index < size; ++index) { 156 if (listeners[index * 2] == listenerClass && listeners[index * 2 + 1] == l) { 157 listeners[index * 2] = listeners[(size - 1) * 2]; 159 listeners[index * 2 + 1] = listeners[(size - 1) * 2 + 1]; 160 --size; 161 break; 162 } 163 } 164 165 if (size == 0) { 167 listeners = EMPTY; 168 } else if (listeners.length / 2 - size > DECREMENT) { 169 Object [] newListeners = new Object [size * 2]; 171 System.arraycopy(listeners, 0, newListeners, 0, newListeners.length); 172 listeners = newListeners; 173 } 174 } 175 176 179 private void writeObject(ObjectOutputStream out) 180 throws IOException { 181 out.defaultWriteObject(); 182 for (int index = 0; index < size; ++index) { 183 String listenerClassName = ((Class ) listeners[index * 2]).getName(); 184 EventListener listener = (EventListener ) listeners[index * 2 + 1]; 185 if (listener instanceof Serializable ) { 186 out.writeObject(listenerClassName); 187 out.writeObject(listener); 188 } 189 } 190 out.writeObject(null); } 192 } 193 | Popular Tags |