Class EventBus

  • Direct Known Subclasses:
    DeferredEventBus

    @ThreadSafe
    public class EventBus
    extends java.lang.Object
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  EventBus.Subscriber  
    • Constructor Summary

      Constructors 
      Constructor Description
      EventBus()
      Instantiates EventBus with default exception handler
      EventBus​(java.util.function.Consumer<java.lang.Throwable> exceptionHandler)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void post​(java.lang.Object event)
      Posts provided event to all registered subscribers.
      <T> EventBus.Subscriber register​(java.lang.Class<T> clazz, java.util.function.Consumer<T> subFn, float priority)  
      void register​(java.lang.Object object)
      Registers subscriber to EventBus.
      void unregister​(java.lang.Object object)
      Unregisters all subscribed methods from provided subscriber object.
      void unregister​(EventBus.Subscriber sub)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • EventBus

        public EventBus()
        Instantiates EventBus with default exception handler
      • EventBus

        public EventBus​(java.util.function.Consumer<java.lang.Throwable> exceptionHandler)
    • Method Detail

      • register

        public void register​(@Nonnull
                             java.lang.Object object)
        Registers subscriber to EventBus. All methods in subscriber and it's parent classes are checked for Subscribe annotation and then added to map of subscriptions.
        Parameters:
        object - subscriber to register
        Throws:
        java.lang.IllegalArgumentException - in case subscriber method name is wrong (correct format is 'on' + EventName
      • register

        public <T> EventBus.Subscriber register​(java.lang.Class<T> clazz,
                                                java.util.function.Consumer<T> subFn,
                                                float priority)
      • unregister

        public void unregister​(@Nonnull
                               java.lang.Object object)
        Unregisters all subscribed methods from provided subscriber object.
        Parameters:
        object - object to unsubscribe from
      • post

        public void post​(@Nonnull
                         java.lang.Object event)
        Posts provided event to all registered subscribers. Subscriber calls are invoked immediately, ordered by priority then their declaring class' name.
        Parameters:
        event - event to post