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.AbstractList ; 27 import java.util.ArrayList ; 28 import java.util.EventListener ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 38 39 public final class ListenerList<T extends EventListener > implements Serializable { 40 41 static final long serialVersionUID = 0L; 42 43 44 private static final EventListener [] EMPTY_LISTENER_ARRAY = new EventListener [0]; 45 46 47 private transient ImmutableList<T> listenersList; 48 49 public ListenerList() { 50 listenersList = new ImmutableList<T>(EMPTY_LISTENER_ARRAY); 51 } 52 53 70 public synchronized List <T> getListeners() { 71 return listenersList; 72 } 73 74 77 public synchronized int getListenerCount() { 78 return listenersList.size(); 79 } 80 81 86 public synchronized void add(T listener) { 87 if (listener == null) 88 return; 89 90 EventListener [] arr = new EventListener [listenersList.getArray().length + 1]; 91 if (arr.length > 1) { 92 System.arraycopy(listenersList.getArray(), 0, arr, 0, arr.length - 1); 93 } 94 arr[arr.length - 1] = listener; 95 96 listenersList = new ImmutableList<T>(arr); 97 } 98 99 104 public synchronized void remove(T listener) { 105 if (listener == null) 106 return; 107 108 int idx = listenersList.indexOf(listener); 109 if (idx == -1) { 110 return; 111 } 112 113 EventListener [] arr = new EventListener [listenersList.getArray().length - 1]; 114 if (arr.length > 0) { 115 System.arraycopy(listenersList.getArray(), 0, arr, 0, idx); 116 } 117 if (arr.length > idx) { 118 System.arraycopy(listenersList.getArray(), idx + 1, arr, idx, listenersList.getArray().length - idx - 1); 119 } 120 121 listenersList = new ImmutableList<T>(arr); 122 } 123 124 private void writeObject(ObjectOutputStream s) throws IOException { 126 s.defaultWriteObject(); 127 128 for (Iterator <T> i = listenersList.iterator(); i.hasNext(); ) { 130 T l = i.next(); 131 if (l instanceof Serializable ) { 133 s.writeObject(l); 134 } 135 } 136 137 s.writeObject(null); 138 } 139 140 private void readObject(ObjectInputStream s) throws IOException , ClassNotFoundException { 141 s.defaultReadObject(); 142 List <T> lList = new ArrayList <T>(); 143 Object listenerOrNull; 144 while (null != (listenerOrNull = s.readObject())) { 145 @SuppressWarnings ("unchecked") 146 T l = (T)listenerOrNull; 147 lList.add(l); 148 } 149 this.listenersList = new ImmutableList<T>((EventListener [])lList.toArray(new EventListener [lList.size()])); 150 } 151 152 public String toString() { 153 return listenersList.toString(); 154 } 155 156 private static final class ImmutableList<E extends EventListener > extends AbstractList <E> { 157 158 private EventListener [] array; 159 160 public ImmutableList(EventListener [] array) { 161 super(); 162 163 assert array != null : "The array can't be null"; this.array = array; 165 } 166 167 public E get(int index) { 168 if (index >= 0 && index < array.length) { 169 @SuppressWarnings ("unchecked") 170 E element = (E) array[index]; 171 return element; 172 } else { 173 throw new IndexOutOfBoundsException ("index = " + index + ", size = " + array.length); } 175 } 176 177 public int size() { 178 return array.length; 179 } 180 181 public EventListener [] getArray() { 182 return array; 183 } 184 } } 186 | Popular Tags |