Class WeakEventListenerList


  • public class WeakEventListenerList
    extends Object
    An event listener list that stores the listeners as weak references. This list behaves like the EventListenerList, but does not create the risk of memory leaks when maintaining listener lists for global objects.

    The list is fully synchronized and safe to be used in multi-threading environments.

    Author:
    Thomas Morgner
    • Constructor Detail

      • WeakEventListenerList

        public WeakEventListenerList()
        Creates a new listener list with a increment of 10.
    • Method Detail

      • add

        public <T extends EventListener> void add​(Class<T> t,
                                                  T l)
        Adds a new listener of the given type to this list.
        Type Parameters:
        T - the type of the listener, must be a subclass of java.util.EventListener.
        Parameters:
        t - the type of the listener.
        l - the listener.
      • remove

        public <T extends EventListener> void remove​(Class<T> t,
                                                     T l)
        Removes an existing listener of the given type from this eventlistener list.
        Type Parameters:
        T - the type of the listener, must be a subclass of java.util.EventListener.
        Parameters:
        t - the type of the listener.
        l - the listener.
      • getSize

        public int getSize()
        Returns the number of listeners of any type stored in this eventlistener list.
        Returns:
        the overall size of this list.
      • getListenerCount

        public int getListenerCount​(Class<?> t)
        Counts the number of listeners of a given type that are contained in this list.
        Parameters:
        t - the type of the listener.
        Returns:
        the number of listeners.
      • getListeners

        public <T extends EventListener> T[] getListeners​(Class<T> t)
        Returns all listeners of the given type that are stored in this list.
        Type Parameters:
        T - a implementation or derived interface of EventListener.
        Parameters:
        t - the type of the listener to be retrieved.
        Returns:
        the array containing all listeners.