/** * Tests the existence of a listener in the list. * * @param listener * * @return * <tt>true</tt> if the listener exists in the list; <tt>false</tt>, * otherwise. */ public boolean contains(T listener) { return indexOf(listener) >= 0; }
/** * Adds a listener to the list, if it has not previously been added. * * @param listener */ public void add(T listener) { if (indexOf(listener) >= 0) { System.err.println("Duplicate listener " + listener + " added to " + this); return; } // If no slot is available, increase the size of the array if (last >= list.length) { list = Arrays.copyOf(list, list.length + DEFAULT_SIZE); } list[last++] = listener; }
/** * Removes a listener from the list, if it has previously been added. * * @param listener */ public void remove(T listener) { int index = indexOf(listener); if (index < 0) { System.err.println("Nonexistent listener " + listener + " removed from " + this); return; } // Once we find the entry in the list, copy the rest of the // existing entries down by one position if (index < last - 1) { System.arraycopy(list, index + 1, list, index, last - 1 - index); } list[--last] = null; }