All Superinterfaces:
Runnable, Sensor

public interface Event extends Runnable, Sensor

An event, notifying its listeners each time it occurs, without data. For an event carrying data, see Signal.

An event is triggered with run(), notifying its listeners in the order they were added.

Events provide access to their Sensor via sensor(), which can be used to add listeners, but can not be used to trigger the event.

Unhandled exceptions occurring in a listener will prevent further listeners from being notified.

Thread Safety: Listener management (add/remove) is thread-safe. However, triggering via run() is NOT thread-safe and should be performed from a single thread (such as an application UI thread).

Event event = Event.event();

event.addListener(this::doSomething);

event.run();

Sensor sensor = event.sensor();

sensor.addListener(this::doSomethingElse);

Listeners can be added using a WeakReference, so they must be strongly referenced elsewhere, a lambda or method reference only referenced by the sensor being garbage collected right away.

// listener is a field
sensor.addWeakListener(listener);

Any weak references that no longer refer to a listener instance are cleared when listeners are added or removed.

A factory for Event instances via event().

See Also:
  • Method Details

    • run

      void run()
      Triggers this event, notifying its listeners.
      Specified by:
      run in interface Runnable
    • sensor

      Sensor sensor()
      Description copied from interface: Sensor
      Returns the underlying Sensor to which listener operations are delegated.

      For AbstractSensor and AbstractObserver subclasses this method returns this. For wrapper types, this returns the delegate sensor, and for an Observer implemented via Observer.observer(), its observer.

      Specified by:
      sensor in interface Sensor
      Returns:
      a sensor notified each time this event occurs, which can not trigger it
    • event

      static Event event()
      Creates a new Event.
      Returns:
      a new Event