Class PrincipesEventBus

java.lang.Object
no.ntnu.principes.event.PrincipesEventBus

public class PrincipesEventBus extends Object
A Singleton-based event bus for managing event publishing and subscribing in the Principes system.

This class provides mechanisms for listeners to subscribe to specific event types and get notified when these events are published.

The PrincipesEventBus ensures thread safety through the use of the Holder pattern for lazy initialization, ensuring only a single instance is created.
  • Method Details

    • getInstance

      public static PrincipesEventBus getInstance()
      Get the singleton instance of PrincipesEventBus through the Holder pattern.
      Returns:
      The singleton instance of the PrincipesEventBus.
    • subscribe

      public <D extends PrincipesEvent<?>> PrincipesEventBus subscribe(Class<D> eventClass, PrincipesEventListener<D> listener)
      Subscribes a listener to a specific event type. When an event of the specified type is published, the subscribed listener will be notified.
      Type Parameters:
      D - The type of event being subscribed to, extending PrincipesEvent.
      Parameters:
      eventClass - The class type of the event to subscribe to.
      listener - The listener to register for handling the specified event type.
    • subscribe

      public <D extends PrincipesEvent<?>> PrincipesEventBus subscribe(PrincipesEventListener<D> listener, List<Class<D>> eventClasses)
      Subscribes a listener to one or more event classes in the Principes event system. The listener will be notified whenever an event of the subscribed classes is published.
      Type Parameters:
      D - The type of event extending PrincipesEvent that the listener handles.
      Parameters:
      listener - The event listener to be subscribed. Must handle events of type D.
      eventClasses - A list of event classes that the listener should listen to. Each class represents an event type the listener will react to.
    • unsubscribe

      public <D extends PrincipesEvent<?>> boolean unsubscribe(Class<D> eventClass, PrincipesEventListener<D> listener)
      Unsubscribes a listener from a specific event type. Removes the given listener from receiving notifications for the specified event class if it is currently registered.
      Type Parameters:
      D - The type of the event, extending PrincipesEvent.
      Parameters:
      eventClass - The class of the event to unsubscribe from. Identifies the event type.
      listener - The listener to remove from the specified event class.
      Returns:
      true if the listener was successfully removed; false if the listener was not registered for the specified event class.
    • unsubscribe

      public <D extends PrincipesEvent<?>> void unsubscribe(PrincipesEventListener<D> listener, List<Class<D>> eventClasses)
      Unsubscribes a listener from a list of event classes. Removes the specified listener from receiving any events of the given types.
      Type Parameters:
      D - The type of the event, extending PrincipesEvent.
      Parameters:
      listener - The event listener to unsubscribe. Must be capable of handling the given event types.
      eventClasses - A list of event classes from which the listener should be unsubscribed. Each class represents an event type.
    • publish

      public <D extends PrincipesEvent<?>> void publish(D event)
      Publishes the specified event to all registered listeners of its type. Notifies each listener subscribed to the event's class by invoking their respective onEvent method.
      Type Parameters:
      D - The type of the event being published, extending PrincipesEvent.
      Parameters:
      event - The event instance to be published. Contains the data and type of the event.