Class EventListenerManager


  • public class EventListenerManager
    extends java.lang.Object
    Class that stores and retrieves event listeners, facilitating the creation of another class that allows event listeners to be registered with it.

    This class maintains weak references to event listeners so that they may be collected by the garbage collector when they are no longer in ordinary use. TODO update comment when weak reference option is added

    A class is used as a key to a set of event listeners, all of which must be instances of that class or a subclass. Generics are used to ensure that only instances of the class or subclasses are keyed to a particular class. If the event listener type itself is generic, using classes should ensure that all listeners keyed to a particular class are indeed generic subclasses of the class, as Java 5 Class<T> objects do not keep track of the generic type of T.

    This class uses thread-safe access methods. Returned listener iterators are "snapshots" of currently registered listeners, so may be accessed even though other threads (or even the event listener itself) may add and/or remove listeners.

    Example:

     
     
            if(haslisteners(MyListener.class)
            {
                    final MyEvent myEvent=new MyEvent();
                    for(final myListener:getListeners(MyListener.class))
                    {
                            myListener.fireEvent(myEvent);          
                    }
            }
     
     
     

    This class uses little memory if there are no registered event listeners.

    This class was inspired by javax.swing.EventListenerList 1.33 12/03/01 by Georges Saab, Hans Muller, and James Gosling.

    Author:
    Garret Wilson
    See Also:
    EventListenerList
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T extends java.util.EventListener,​L extends T>
      void
      add​(java.lang.Class<T> key, L listener)
      Adds a listener to the manager, associated with the given key.
      <T extends java.util.EventListener>
      int
      getListenerCount​(java.lang.Class<T> key)
      Returns the number of listeners associated with the given key.
      <T extends java.util.EventListener>
      java.lang.Iterable<T>
      getListeners​(java.lang.Class<T> key)
      Retrieves a thread-safe snapshot iterable of listeners associated with the given key.
      <T extends java.util.EventListener>
      boolean
      hasListeners​(java.lang.Class<T> key)
      Determines whether there are listeners associated with the given key.
      <T extends java.util.EventListener,​L extends T>
      boolean
      remove​(java.lang.Class<T> key, L listener)
      Removes a listener from the manager as associated with the given key.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • EventListenerManager

        public EventListenerManager()
    • Method Detail

      • add

        public <T extends java.util.EventListener,​L extends T> void add​(java.lang.Class<T> key,
                                                                              L listener)
        Adds a listener to the manager, associated with the given key. If no listener set map or no listener set exists, it will be created.

        Example: add(MyListener.class, listener);.

        Type Parameters:
        T - The type of the key used for registering the event listener.
        L - The type of the listener.
        Parameters:
        key - The key with which the listener should be associated.
        listener - The event listener to be associated with the key.
      • remove

        public <T extends java.util.EventListener,​L extends T> boolean remove​(java.lang.Class<T> key,
                                                                                    L listener)
        Removes a listener from the manager as associated with the given key. If the listener is not associated with the key, no action is taken. If all listeners for a class are removed, its set is removed from from the map. If the map is consequently empty, the map is removed.

        Example: remove(MyListener.class, listener);

        Type Parameters:
        T - The type of the event listener.
        L - The type of the listener.
        Parameters:
        key - The key with which the listener was associated.
        listener - The listener to remove from the manager.
        Returns:
        true if the manager contained the specified listener.
      • getListenerCount

        public <T extends java.util.EventListener> int getListenerCount​(java.lang.Class<T> key)
        Returns the number of listeners associated with the given key.

        Example: getListenerCount(MyListener.class);

        Type Parameters:
        T - The type of the event listener.
        Parameters:
        key - The key with which the listeners are associated.
        Returns:
        The number of listeners associated with the given key.
      • hasListeners

        public <T extends java.util.EventListener> boolean hasListeners​(java.lang.Class<T> key)
        Determines whether there are listeners associated with the given key.

        Example: hasListeners(MyListener.class);

        Type Parameters:
        T - The type of the event listener.
        Parameters:
        key - The key with which the listeners are associated.
        Returns:
        true if there is at least one listener associated with the given key.
        See Also:
        getListenerCount(Class)
      • getListeners

        public <T extends java.util.EventListener> java.lang.Iterable<T> getListeners​(java.lang.Class<T> key)
        Retrieves a thread-safe snapshot iterable of listeners associated with the given key.

        Example: getListeners(MyListener.class);

        Type Parameters:
        T - The type of the event listener.
        Parameters:
        key - The key with which listeners have been associated.
        Returns:
        An iterable of all currently registered listeners.