Interface Event<T>

Type Parameters:
T - the type of data propagated with this event
All Superinterfaces:
Consumer<T>, Observer<T>, Runnable

public interface Event<T> extends Runnable, Consumer<T>, Observer<T>

An event class implementing Observer.

Events are triggered with run() or accept(Object), depending on whether data should be propagated with the event.

Listeners are notified in the order they were added.

Note that both listeners and consumers are notified each time the event is triggered, regardless of whether run() or accept(Object) is used.

Events provide access to their Observer instance via observer(), which can be used to add listeners and consumers, but can not be used to trigger the event.

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

 
 Event<Boolean> event = Event.event();

 event.addListener(this::doSomething);

 event.run();

 event.addConsumer(this::onBoolean);

 event.accept(true);

 Observer<Boolean> observer = event.observer();

 observer.addListener(this::doSomethingElse);
 
 

Listeners and Consumers can be added using a WeakReference.

 
 observer.addWeakListener(this::doSomethingElse);
 observer.addWeakConsumer(this::onBoolean);
 
 

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

A factory for Event instances via event().

  • Method Details

    • run

      void run()
      Triggers this event.
      Specified by:
      run in interface Runnable
    • accept

      void accept(@Nullable T data)
      Triggers this event.
      Specified by:
      accept in interface Consumer<T>
      Parameters:
      data - data associated with the event
    • observer

      Observer<T> observer()
      Returns:
      an observer notified each time this event occurs
    • event

      static <T> Event<T> event()
      Creates a new Event.
      Type Parameters:
      T - the type of data propagated to listeners on event occurrence
      Returns:
      a new Event