1 19 20 package org.netbeans.lib.editor.util; 21 22 import java.io.IOException ; 23 import java.io.ObjectInputStream ; 24 import java.io.ObjectOutputStream ; 25 import java.io.Serializable ; 26 import java.util.ArrayList ; 27 import java.util.EventListener ; 28 import java.util.List ; 29 30 40 41 public class PriorityListenerList<T extends EventListener > implements Serializable { 42 43 static final long serialVersionUID = 0L; 44 45 private static final EventListener [] EMPTY_LISTENER_ARRAY = new EventListener [0]; 46 47 private static final EventListener [][] EMPTY_LISTENER_ARRAY_ARRAY = new EventListener [0][]; 48 49 private transient T[][] listenersArray; 50 51 public PriorityListenerList() { 52 listenersArray = emptyTArrayArray(); 53 } 54 55 71 public synchronized void add(T listener, int priority) { 72 if (listener == null) 73 return; 74 75 if (priority >= listenersArray.length) { 76 T[][] newListenersArray = allocateTArrayArray(priority + 1); 77 System.arraycopy(listenersArray, 0, newListenersArray, 0, listenersArray.length); 78 for (int i = listenersArray.length; i < priority; i++) { 79 newListenersArray[i] = emptyTArray(); 80 } 81 T[] arr = allocateTArray(1); 82 arr[0] = listener; 83 newListenersArray[priority] = arr; 84 listenersArray = newListenersArray; 85 86 } else { @SuppressWarnings ("unchecked") 88 T[][] newListenersArray = (T[][])listenersArray.clone(); 89 T[] listeners = listenersArray[priority]; 90 T[] newListeners = allocateTArray(listeners.length + 1); 91 System.arraycopy(listeners, 0, newListeners, 1, listeners.length); 92 newListeners[0] = listener; 93 newListenersArray[priority] = newListeners; 94 listenersArray = newListenersArray; 95 } 96 } 97 98 109 public synchronized void remove(T listener, int priority) { 110 if (listener == null) 111 return; 112 113 if (priority < listenersArray.length) { 114 T[] listeners = listenersArray[priority]; 115 int index = 0; 117 while (index < listeners.length && listeners[index] != listener) { 118 index++; 119 } 120 if (index < listeners.length) { 121 T[] newListeners; 122 boolean removeHighestPriorityLevel; 123 if (listeners.length == 1) { 124 newListeners = emptyTArray(); 125 removeHighestPriorityLevel = (priority == listenersArray.length - 1); 126 } else { 127 newListeners = allocateTArray(listeners.length - 1); 128 System.arraycopy(listeners, 0, newListeners, 0, index); 129 System.arraycopy(listeners, index + 1, newListeners, index, 130 newListeners.length - index); 131 removeHighestPriorityLevel = false; 132 } 133 134 135 if (removeHighestPriorityLevel) { 136 T[][] newListenersArray = allocateTArrayArray(listenersArray.length - 1); 137 System.arraycopy(listenersArray, 0, newListenersArray, 0, newListenersArray.length); 138 listenersArray = newListenersArray; 139 } else { @SuppressWarnings ("unchecked") 141 T[][] newListenersArray = (T[][])listenersArray.clone(); 142 newListenersArray[priority] = newListeners; 143 listenersArray = newListenersArray; 144 } 145 } 146 } 147 } 148 149 173 public T[][] getListenersArray() { 174 return listenersArray; 175 } 176 177 private void writeObject(ObjectOutputStream s) throws IOException { 179 s.defaultWriteObject(); 180 181 int priority = listenersArray.length - 1; s.writeInt(priority); for (; priority >= 0; priority--) { 185 T[] listeners = listenersArray[priority]; 186 for (int i = 0; i < listeners.length; i++) { 188 T listener = listeners[i]; 189 if (listener instanceof Serializable ) { 191 s.writeObject(listener); 192 } 193 } 194 s.writeObject(null); 195 } 196 } 197 198 private void readObject(ObjectInputStream s) 199 throws IOException , ClassNotFoundException { 200 s.defaultReadObject(); 201 int priority = s.readInt(); 202 listenersArray = (priority != -1) 203 ? allocateTArrayArray(priority + 1) 204 : emptyTArrayArray(); 205 206 for (; priority >= 0; priority--) { 207 List <T> lList = new ArrayList <T>(); 208 Object listenerOrNull; 209 while (null != (listenerOrNull = s.readObject())) { 210 @SuppressWarnings ("unchecked") 211 T l = (T)listenerOrNull; 212 lList.add(l); 213 } 214 @SuppressWarnings ("unchecked") 215 T[] lArr = (T[])lList.toArray(new EventListener [lList.size()]); 216 listenersArray[priority] = lArr; 217 } 218 } 219 220 @SuppressWarnings ("unchecked") 221 private T[] emptyTArray() { 222 return (T[])EMPTY_LISTENER_ARRAY; 223 } 224 225 @SuppressWarnings ("unchecked") 226 private T[][] emptyTArrayArray() { 227 return (T[][])EMPTY_LISTENER_ARRAY_ARRAY; 228 } 229 230 @SuppressWarnings ("unchecked") 231 private T[] allocateTArray(int length) { 232 return (T[])new EventListener [length]; 233 } 234 235 @SuppressWarnings ("unchecked") 236 private T[][] allocateTArrayArray(int length) { 237 return (T[][])new EventListener [length][]; 238 } 239 240 } 241 | Popular Tags |