java.lang.Object
no.ntnu.principes.event.PrincipesEventBus
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.
ThePrincipesEventBus ensures thread safety through the use of the Holder pattern
for lazy initialization, ensuring only a single instance is created.-
Method Summary
Modifier and TypeMethodDescriptionstatic PrincipesEventBusGet the singleton instance ofPrincipesEventBusthrough the Holder pattern.<D extends PrincipesEvent<?>>
voidpublish(D event) Publishes the specified event to all registered listeners of its type.<D extends PrincipesEvent<?>>
PrincipesEventBussubscribe(Class<D> eventClass, PrincipesEventListener<D> listener) Subscribes a listener to a specific event type.<D extends PrincipesEvent<?>>
PrincipesEventBussubscribe(PrincipesEventListener<D> listener, List<Class<D>> eventClasses) Subscribes a listener to one or more event classes in the Principes event system.<D extends PrincipesEvent<?>>
booleanunsubscribe(Class<D> eventClass, PrincipesEventListener<D> listener) Unsubscribes a listener from a specific event type.<D extends PrincipesEvent<?>>
voidunsubscribe(PrincipesEventListener<D> listener, List<Class<D>> eventClasses) Unsubscribes a listener from a list of event classes.
-
Method Details
-
getInstance
Get the singleton instance ofPrincipesEventBusthrough 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, extendingPrincipesEvent.- 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 extendingPrincipesEventthat the listener handles.- Parameters:
listener- The event listener to be subscribed. Must handle events of typeD.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, extendingPrincipesEvent.- 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, extendingPrincipesEvent.- 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
Publishes the specified event to all registered listeners of its type. Notifies each listener subscribed to the event's class by invoking their respectiveonEventmethod.- Type Parameters:
D- The type of the event being published, extendingPrincipesEvent.- Parameters:
event- The event instance to be published. Contains the data and type of the event.
-