Interface EventManager

All Known Implementing Classes:
DefaultEventManager, NoopEventManager

public interface EventManager
Acts as bridge between an Object that wants to inform others about its current state or a change thereof (Publisher) and a list of objects interested in the Subject (Listeners).

Listeners are held via weak references, so a listener that is no longer reachable from anywhere else is released together with its registration. This only works when the EventHandler is an unbound method reference (e.g. MyListener::onEvent) that does not capture the listener itself.

Since:
3.1 before 3.1 this was a concrete class.
  • Method Details

    • isSingleThreaded

      boolean isSingleThreaded()
      Returns true if this EventManager is single-threaded. If so it will throw an exception on any attempt to register an unblocking listener or dispatch a non-blocking event.
      Since:
      1.2
    • addListener

      <L, E extends EventObject> void addListener(L listener, Class<E> eventClass, EventHandler<? super L, ? super E> handler, EventSubject subject)
      Registers a listener for events sent by any sender.
      Parameters:
      listener - the object to be notified about events
      eventClass - the class of events the listener is interested in; events of other types are ignored
      handler - the callback invoked with the listener and the event, normally an unbound method reference
      subject - the event subject that the listener is interested in
      Since:
      5.0
    • addNonBlockingListener

      <L, E extends EventObject> void addNonBlockingListener(L listener, Class<E> eventClass, EventHandler<? super L, ? super E> handler, EventSubject subject)
      Registers a listener for events sent by any sender, to be notified on a dispatch thread instead of the thread that posted the event.
      Since:
      5.0
      See Also:
    • addListener

      <L, E extends EventObject> void addListener(L listener, Class<E> eventClass, EventHandler<? super L, ? super E> handler, EventSubject subject, Object sender)
      Registers a listener for events sent by a specific sender.
      Parameters:
      listener - the object to be notified about events
      eventClass - the class of events the listener is interested in; events of other types are ignored
      handler - the callback invoked with the listener and the event, normally an unbound method reference
      subject - the event subject that the listener is interested in
      sender - the object whose events the listener is interested in; null means 'any sender'
      Since:
      5.0
    • addNonBlockingListener

      <L, E extends EventObject> void addNonBlockingListener(L listener, Class<E> eventClass, EventHandler<? super L, ? super E> handler, EventSubject subject, Object sender)
      Registers a listener for events sent by a specific sender, to be notified on a dispatch thread instead of the thread that posted the event.
      Since:
      5.0
    • removeListener

      boolean removeListener(Object listener)
      Unregister the specified listener from all event subjects handled by this manager instance.
      Parameters:
      listener - the object to be unregistered
      Returns:
      true if listener could be removed for any existing subjects, else returns false.
    • removeAllListeners

      boolean removeAllListeners(EventSubject subject)
      Removes all listeners for a given subject.
    • removeListener

      boolean removeListener(Object listener, EventSubject subject)
      Unregister the specified listener for the events about the given subject.
      Parameters:
      listener - the object to be unregistered
      subject - the subject from which the listener is to be unregistered
      Returns:
      true if listener could be removed for the given subject, else returns false.
    • removeListener

      boolean removeListener(Object listener, EventSubject subject, Object sender)
      Unregister the specified listener for the events about the given subject and the given sender.
      Parameters:
      listener - the object to be unregistered
      subject - the subject from which the listener is to be unregistered
      sender - the object whose events the listener was interested in; null means 'any sender'.
      Returns:
      true if listener could be removed for the given subject, else returns false.
    • postEvent

      void postEvent(EventObject event, EventSubject subject)
      Sends an event to all registered objects about a particular subject. Event is sent synchronously, so the sender thread is blocked until all the listeners finish processing the event.
      Throws:
      IllegalArgumentException - if event or subject are null
    • postNonBlockingEvent

      void postNonBlockingEvent(EventObject event, EventSubject subject)
      Sends an event to all registered objects about a particular subject. Event is queued by EventManager, releasing the sender thread, and is later dispatched in a separate thread.
      Throws:
      IllegalArgumentException - if event or subject are null
      Since:
      1.1