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 the Observer interface.
 
 Event<Boolean> event = Event.event();

 event.addListener(this::doSomething);

 event.run();

 event.addConsumer(this::onBoolean);

 event.accept(true);
 
 
The event observer observes the event but can not trigger it.
 
 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 are added or removed, but to manually clear these empty weak references call Observer.removeWeakListener(Runnable) or Observer.removeWeakConsumer(Consumer) with a listener/consumer instance which has not been registered on the observer, such as a new one.
 
 observer.removeWeakListener(() -> {});
 observer.removeWeakConsumer(value -> {});
 
 
A factory for Event instances via event().
  • Method Details

    • run

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

      void accept(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