java.lang.Object
io.github.astrapi69.design.pattern.eventbus.GenericEventBus

public final class GenericEventBus extends Object
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 Type
    Method
    Description
    static <T> boolean
    containsKey(@NonNull Class<T> eventSourceTypeClass)
    Checks if the event bus contains an event source associated with the specified class type
    static boolean
    Checks if the event bus contains an event source associated with the specified key
    static io.github.astrapi69.design.pattern.observer.event.EventSource<?>
    get(String key)
    Retrieves the event source associated with the specified key
    static <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 new EventListener with the specified event source type class to this EventBus instance
    static <T> Optional<io.github.astrapi69.design.pattern.observer.event.EventSource<io.github.astrapi69.design.pattern.observer.event.EventObject<T>>>
    remove(@NonNull Class<T> eventSourceTypeClass)
    Removes the event source associated with the specified class type

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • get

      public static io.github.astrapi69.design.pattern.observer.event.EventSource<?> get(String key)
      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

      public static boolean containsKey(String key)
      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

      public static <T> boolean containsKey(@NonNull @NonNull Class<T> eventSourceTypeClass)
      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 new EventSubject 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 new EventListener 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 register
      eventSourceTypeClass - 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, or Optional.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