1 /* 2 * Copyright (c) 2003-2008 by Cosylab d. d. 3 * 4 * This file is part of Java-Common. 5 * 6 * Java-Common is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * Java-Common is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with Java-Common. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package com.cosylab.util; 21 22 /** 23 * A listener list is an efficient data structure for holding listeners. Its 24 * design is based on the fact that listeners are extensively accessed for 25 * event dispatching, but the collection of listeners changes rarely. Under 26 * these circumstances (especilly rare invocations of 27 * <code>addListener()</code> and <code>removeListener</code> methods) this 28 * class is a good choice. It holds listeners in an array that is trimmed 29 * every time the listener membership changes (which is quite inefficient). On 30 * the other hand, accessing the elements of the array is quick, <b>on the 31 * condition and contract that the client does not modify the array being 32 * returned</b>. Also note that the array is of the correct run-time type 33 * specified by the constructor of this class, and can therefore be cast. For 34 * example, if you speficy <code>WindowListener</code> as the RTT of this 35 * list, you can perform <code>WindowListener[] wla = 36 * (WindowListener[])toArray()</code> without raising a 37 * <code>ClassCastException</code>. For cases where the listener membership 38 * changes often, but events are dispatched rarely, use <code>ArrayList</code> 39 * instead. <b>This class is thread safe, there is no need for additional 40 * synchronization.</b> See also swing based listener registration for an 41 * approach that is more time consuming during dispatching, but can store 42 * different classes of listeners in a single array. 43 * 44 * @author <a href="mailto:gasper.tkacik@cosylab.com">Gasper Tkacik</a> 45 * @version $id$ 46 * 47 * @see javax.swing.event.EventListenerList 48 */ 49 public class ListenerList extends ObjectList 50 { 51 /** 52 * Constructs a new instance of the listener list that will be used to hold 53 * elements of type <code>type</code>. The specified class should be a 54 * subclass of <code>java.util.EventListener</code>. 55 * 56 * @param type a run-time type of the elements in this list 57 * 58 * @throws NullPointerException DOCUMENT ME! 59 * @throws IllegalArgumentException DOCUMENT ME! 60 */ 61 public ListenerList(Class type) 62 { 63 super(type); 64 65 if (type == null) { 66 throw new NullPointerException("type"); 67 } 68 69 if (!java.util.EventListener.class.isAssignableFrom(type)) { 70 throw new IllegalArgumentException("Type '" + type 71 + "' does not extend 'java.util.EventListener'."); 72 } 73 } 74 } 75 76 /* __oOo__ */