Class GenericEventBus
java.lang.Object
io.github.astrapi69.design.pattern.eventbus.GenericEventBus
The
GenericEventBus
is a final utility class that provides a centralized event bus
mechanism for managing and dispatching events using EventSource
objects. It maintains a
registry of event sources keyed by strings or class types, enabling efficient event dispatching
and management.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> boolean
containsKey
(@NonNull Class<T> eventSourceTypeClass) Checks if the event bus contains an event source associated with the specified class typestatic boolean
containsKey
(String key) Checks if the event bus contains an event source associated with the specified keystatic io.github.astrapi69.design.pattern.observer.event.EventSource<?>
Retrieves the event source associated with the specified keystatic <T> io.github.astrapi69.design.pattern.observer.event.EventSource<io.github.astrapi69.design.pattern.observer.event.EventObject<T>>
getEventSource
(@NonNull Class<T> eventSourceTypeClass) Retrieves the event source associated with the specified class type.static <T> void
post
(T source) Posts an event to the event bus.static <T> void
register
(@NonNull io.github.astrapi69.design.pattern.observer.event.EventListener<io.github.astrapi69.design.pattern.observer.event.EventObject<T>> listener, @NonNull Class<T> eventSourceTypeClass) Registers a newEventListener
with the specified event source type class to this EventBus instancestatic <T> Optional<io.github.astrapi69.design.pattern.observer.event.EventSource<io.github.astrapi69.design.pattern.observer.event.EventObject<T>>>
Removes the event source associated with the specified class type
-
Method Details
-
get
Retrieves the event source associated with the specified key- Parameters:
key
- the key associated with the event source- Returns:
- the event source associated with the given key, or
null
if none is found
-
containsKey
Checks if the event bus contains an event source associated with the specified key- Parameters:
key
- the key to check- Returns:
true
if the event source is present,false
otherwise
-
containsKey
Checks if the event bus contains an event source associated with the specified class type- Type Parameters:
T
- the type of the event source- Parameters:
eventSourceTypeClass
- the class type of the event source to check- Returns:
true
if the event source is present,false
otherwise
-
getEventSource
public static <T> io.github.astrapi69.design.pattern.observer.event.EventSource<io.github.astrapi69.design.pattern.observer.event.EventObject<T>> getEventSource(@NonNull @NonNull Class<T> eventSourceTypeClass) Retrieves the event source associated with the specified class type. If it does not exist, a newEventSubject
is created and associated with the class type- Type Parameters:
T
- the type of the event source- Parameters:
eventSourceTypeClass
- the class type of the event source- Returns:
- the event source associated with the specified class type
-
register
public static <T> void register(@NonNull @NonNull io.github.astrapi69.design.pattern.observer.event.EventListener<io.github.astrapi69.design.pattern.observer.event.EventObject<T>> listener, @NonNull @NonNull Class<T> eventSourceTypeClass) Registers a newEventListener
with the specified event source type class to this EventBus instance- Type Parameters:
T
- the type parameter that represents the event source- Parameters:
listener
- the listener to registereventSourceTypeClass
- the class type of the event source
-
remove
public static <T> Optional<io.github.astrapi69.design.pattern.observer.event.EventSource<io.github.astrapi69.design.pattern.observer.event.EventObject<T>>> remove(@NonNull @NonNull Class<T> eventSourceTypeClass) Removes the event source associated with the specified class type- Type Parameters:
T
- the type of the event source- Parameters:
eventSourceTypeClass
- the class type of the event source to be removed- Returns:
- an
Optional
containing the removed event source, orOptional.empty()
if none existed
-
post
public static <T> void post(T source) Posts an event to the event bus. The event is dispatched to all registered listeners associated with the event's class type- Type Parameters:
T
- the type parameter representing the event source- Parameters:
source
- the source event to be posted
-