1 11 package org.eclipse.core.runtime; 12 13 43 public class ListenerList { 44 45 48 private static final Object [] EmptyArray = new Object [0]; 49 50 54 public static final int EQUALITY = 0; 55 56 60 public static final int IDENTITY = 1; 61 62 66 private final boolean identity; 67 68 73 private volatile Object [] listeners = EmptyArray; 74 75 78 public ListenerList() { 79 this(EQUALITY); 80 } 81 82 87 public ListenerList(int mode) { 88 if (mode != EQUALITY && mode != IDENTITY) 89 throw new IllegalArgumentException (); 90 this.identity = mode == IDENTITY; 91 } 92 93 99 public synchronized void add(Object listener) { 100 if (listener == null) 103 throw new IllegalArgumentException (); 104 final int oldSize = listeners.length; 106 for (int i = 0; i < oldSize; ++i) { 107 Object listener2 = listeners[i]; 108 if (identity ? listener == listener2 : listener.equals(listener2)) 109 return; 110 } 111 Object [] newListeners = new Object [oldSize + 1]; 113 System.arraycopy(listeners, 0, newListeners, 0, oldSize); 114 newListeners[oldSize] = listener; 115 this.listeners = newListeners; 117 } 118 119 131 public Object [] getListeners() { 132 return listeners; 133 } 134 135 141 public boolean isEmpty() { 142 return listeners.length == 0; 143 } 144 145 151 public synchronized void remove(Object listener) { 152 if (listener == null) 155 throw new IllegalArgumentException (); 156 int oldSize = listeners.length; 157 for (int i = 0; i < oldSize; ++i) { 158 Object listener2 = listeners[i]; 159 if (identity ? listener == listener2 : listener.equals(listener2)) { 160 if (oldSize == 1) { 161 listeners = EmptyArray; 162 } else { 163 Object [] newListeners = new Object [oldSize - 1]; 165 System.arraycopy(listeners, 0, newListeners, 0, i); 166 System.arraycopy(listeners, i + 1, newListeners, i, oldSize - i - 1); 167 this.listeners = newListeners; 169 } 170 return; 171 } 172 } 173 } 174 175 180 public int size() { 181 return listeners.length; 182 } 183 184 187 public synchronized void clear() { 188 listeners = EmptyArray; 189 } 190 } 191 | Popular Tags |