src/share/classes/sun/awt/EventListenerAggregate.java

Print this page




  36  * This class is thread-safe.
  37  *
  38  * @author Alexander Gerasimov
  39  *
  40  * @since 1.5
  41  */
  42 public class EventListenerAggregate {
  43 
  44     private EventListener[] listenerList;
  45 
  46     /**
  47      * Constructs an <code>EventListenerAggregate</code> object.
  48      *
  49      * @param listenerClass the type of the listeners to be managed by this object
  50      *
  51      * @throws NullPointerException if <code>listenerClass</code> is
  52      *         <code>null</code>
  53      * @throws ClassCastException if <code>listenerClass</code> is not
  54      *         assignable to <code>java.util.EventListener</code>
  55      */
  56     public EventListenerAggregate(Class listenerClass) {
  57         if (listenerClass == null) {
  58             throw new NullPointerException("listener class is null");
  59         }
  60 
  61         if (!EventListener.class.isAssignableFrom(listenerClass)) {
  62             throw new ClassCastException("listener class " + listenerClass +
  63                                          " is not assignable to EventListener");
  64         }
  65 
  66         listenerList = (EventListener[])Array.newInstance(listenerClass, 0);
  67     }
  68 
  69     private Class getListenerClass() {
  70         return listenerList.getClass().getComponentType();
  71     }
  72 
  73     /**
  74      * Adds the listener to this aggregate.
  75      *
  76      * @param listener the listener to be added
  77      *
  78      * @throws ClassCastException if <code>listener</code> is not
  79      *         an instatce of <code>listenerClass</code> specified
  80      *         in the constructor
  81      */
  82     public synchronized void add(EventListener listener) {
  83         Class listenerClass = getListenerClass();
  84 
  85         if (!listenerClass.isInstance(listener)) { // null is not an instance of any class
  86             throw new ClassCastException("listener " + listener + " is not " +
  87                     "an instance of listener class " + listenerClass);
  88         }
  89 
  90         EventListener[] tmp = (EventListener[])Array.newInstance(listenerClass, listenerList.length + 1);
  91         System.arraycopy(listenerList, 0, tmp, 0, listenerList.length);
  92         tmp[listenerList.length] = listener;
  93         listenerList = tmp;
  94     }
  95 
  96     /**
  97      * Removes a listener that is equal to the given one from this aggregate.
  98      * <code>equals()</code> method is used to compare listeners.
  99      *
 100      * @param listener the listener to be removed
 101      *
 102      * @return <code>true</code> if this aggregate contained the specified
 103      *         <code>listener</code>; <code>false</code> otherwise
 104      *
 105      * @throws ClassCastException if <code>listener</code> is not
 106      *         an instatce of <code>listenerClass</code> specified
 107      *         in the constructor
 108      */
 109     public synchronized boolean remove(EventListener listener) {
 110         Class listenerClass = getListenerClass();
 111 
 112         if (!listenerClass.isInstance(listener)) { // null is not an instance of any class
 113             throw new ClassCastException("listener " + listener + " is not " +
 114                     "an instance of listener class " + listenerClass);
 115         }
 116 
 117         for (int i = 0; i < listenerList.length; i++) {
 118             if (listenerList[i].equals(listener)) {
 119                 EventListener[] tmp = (EventListener[])Array.newInstance(listenerClass,
 120                                                                          listenerList.length - 1);
 121                 System.arraycopy(listenerList, 0, tmp, 0, i);
 122                 System.arraycopy(listenerList, i + 1, tmp, i, listenerList.length - i - 1);
 123                 listenerList = tmp;
 124 
 125                 return true;
 126             }
 127         }
 128 
 129         return false;
 130     }


 138      *
 139      * @return all the listeners contained in this aggregate (an empty
 140      *         array if there are no listeners)
 141      */
 142     public synchronized EventListener[] getListenersInternal() {
 143         return listenerList;
 144     }
 145 
 146     /**
 147      * Returns an array of all the listeners contained in this aggregate.
 148      * The array is a copy of the data structure in which listeners are stored
 149      * internally.
 150      * The runtime type of the returned array is "array of <code>listenerClass</code>"
 151      * (<code>listenerClass</code> has been specified as a parameter to
 152      * the constructor of this class).
 153      *
 154      * @return a copy of all the listeners contained in this aggregate (an empty
 155      *         array if there are no listeners)
 156      */
 157     public synchronized EventListener[] getListenersCopy() {
 158         return (listenerList.length == 0) ? listenerList : (EventListener[])listenerList.clone();
 159     }
 160 
 161     /**
 162      * Returns the number of lisetners in this aggregate.
 163      *
 164      * @return the number of lisetners in this aggregate
 165      */
 166     public synchronized int size() {
 167         return listenerList.length;
 168     }
 169 
 170     /**
 171      * Returns <code>true</code> if this aggregate contains no listeners,
 172      * <code>false</code> otherwise.
 173      *
 174      * @return <code>true</code> if this aggregate contains no listeners,
 175      *         <code>false</code> otherwise
 176      */
 177     public synchronized boolean isEmpty() {
 178         return listenerList.length == 0;


  36  * This class is thread-safe.
  37  *
  38  * @author Alexander Gerasimov
  39  *
  40  * @since 1.5
  41  */
  42 public class EventListenerAggregate {
  43 
  44     private EventListener[] listenerList;
  45 
  46     /**
  47      * Constructs an <code>EventListenerAggregate</code> object.
  48      *
  49      * @param listenerClass the type of the listeners to be managed by this object
  50      *
  51      * @throws NullPointerException if <code>listenerClass</code> is
  52      *         <code>null</code>
  53      * @throws ClassCastException if <code>listenerClass</code> is not
  54      *         assignable to <code>java.util.EventListener</code>
  55      */
  56     public EventListenerAggregate(Class<? extends EventListener> listenerClass) {
  57         if (listenerClass == null) {
  58             throw new NullPointerException("listener class is null");
  59         }
  60 





  61         listenerList = (EventListener[])Array.newInstance(listenerClass, 0);
  62     }
  63 
  64     private Class<?> getListenerClass() {
  65         return listenerList.getClass().getComponentType();
  66     }
  67 
  68     /**
  69      * Adds the listener to this aggregate.
  70      *
  71      * @param listener the listener to be added
  72      *
  73      * @throws ClassCastException if <code>listener</code> is not
  74      *         an instatce of <code>listenerClass</code> specified
  75      *         in the constructor
  76      */
  77     public synchronized void add(EventListener listener) {
  78         Class<?> listenerClass = getListenerClass();
  79 
  80         if (!listenerClass.isInstance(listener)) { // null is not an instance of any class
  81             throw new ClassCastException("listener " + listener + " is not " +
  82                     "an instance of listener class " + listenerClass);
  83         }
  84 
  85         EventListener[] tmp = (EventListener[])Array.newInstance(listenerClass, listenerList.length + 1);
  86         System.arraycopy(listenerList, 0, tmp, 0, listenerList.length);
  87         tmp[listenerList.length] = listener;
  88         listenerList = tmp;
  89     }
  90 
  91     /**
  92      * Removes a listener that is equal to the given one from this aggregate.
  93      * <code>equals()</code> method is used to compare listeners.
  94      *
  95      * @param listener the listener to be removed
  96      *
  97      * @return <code>true</code> if this aggregate contained the specified
  98      *         <code>listener</code>; <code>false</code> otherwise
  99      *
 100      * @throws ClassCastException if <code>listener</code> is not
 101      *         an instatce of <code>listenerClass</code> specified
 102      *         in the constructor
 103      */
 104     public synchronized boolean remove(EventListener listener) {
 105         Class<?> listenerClass = getListenerClass();
 106 
 107         if (!listenerClass.isInstance(listener)) { // null is not an instance of any class
 108             throw new ClassCastException("listener " + listener + " is not " +
 109                     "an instance of listener class " + listenerClass);
 110         }
 111 
 112         for (int i = 0; i < listenerList.length; i++) {
 113             if (listenerList[i].equals(listener)) {
 114                 EventListener[] tmp = (EventListener[])Array.newInstance(listenerClass,
 115                                                                          listenerList.length - 1);
 116                 System.arraycopy(listenerList, 0, tmp, 0, i);
 117                 System.arraycopy(listenerList, i + 1, tmp, i, listenerList.length - i - 1);
 118                 listenerList = tmp;
 119 
 120                 return true;
 121             }
 122         }
 123 
 124         return false;
 125     }


 133      *
 134      * @return all the listeners contained in this aggregate (an empty
 135      *         array if there are no listeners)
 136      */
 137     public synchronized EventListener[] getListenersInternal() {
 138         return listenerList;
 139     }
 140 
 141     /**
 142      * Returns an array of all the listeners contained in this aggregate.
 143      * The array is a copy of the data structure in which listeners are stored
 144      * internally.
 145      * The runtime type of the returned array is "array of <code>listenerClass</code>"
 146      * (<code>listenerClass</code> has been specified as a parameter to
 147      * the constructor of this class).
 148      *
 149      * @return a copy of all the listeners contained in this aggregate (an empty
 150      *         array if there are no listeners)
 151      */
 152     public synchronized EventListener[] getListenersCopy() {
 153         return (listenerList.length == 0) ? listenerList : listenerList.clone();
 154     }
 155 
 156     /**
 157      * Returns the number of lisetners in this aggregate.
 158      *
 159      * @return the number of lisetners in this aggregate
 160      */
 161     public synchronized int size() {
 162         return listenerList.length;
 163     }
 164 
 165     /**
 166      * Returns <code>true</code> if this aggregate contains no listeners,
 167      * <code>false</code> otherwise.
 168      *
 169      * @return <code>true</code> if this aggregate contains no listeners,
 170      *         <code>false</code> otherwise
 171      */
 172     public synchronized boolean isEmpty() {
 173         return listenerList.length == 0;