Class EventDispatcher<EventType extends Enum<EventType>,Target,E extends Event<EventType,? super Target>>

java.lang.Object
dev.prozilla.pine.common.event.EventDispatcher<EventType,Target,E>
Type Parameters:
E - The type of event this object can dispatch
All Implemented Interfaces:
EventDispatcherContext<EventType,Target,E>, Destructible
Direct Known Subclasses:
AssetPoolEventDispatcher, NodeEventDispatcher, SimpleEventDispatcher

public abstract class EventDispatcher<EventType extends Enum<EventType>,Target,E extends Event<EventType,? super Target>> extends Object implements EventDispatcherContext<EventType,Target,E>, Destructible
Represents an object that can dispatch events.
  • Field Details

    • logger

      protected Logger logger
  • Constructor Details

    • EventDispatcher

      public EventDispatcher()
  • Method Details

    • addListener

      public EventListener<E> addListener(EventType eventType, EventListener<E> listener)
      Description copied from interface: EventDispatcherContext
      Adds a listener that listens to a given type of event.

      Unicity is not required. If a listener is added multiple times, it will be called multiple times per event.

      Specified by:
      addListener in interface EventDispatcherContext<EventType extends Enum<EventType>,Target,E extends Event<EventType,? super Target>>
      Parameters:
      eventType - The type of event to listen to
      listener - The listener to add
      Returns:
      The listener that was added.
    • removeListener

      public void removeListener(EventType eventType, EventListener<E> listener)
      Description copied from interface: EventDispatcherContext
      Removes a listener that was listening to a given type of event.
      Specified by:
      removeListener in interface EventDispatcherContext<EventType extends Enum<EventType>,Target,E extends Event<EventType,? super Target>>
      Parameters:
      eventType - The type of event that was being listened to
      listener - The listener to remove
    • invoke

      public void invoke(EventType eventType, Target target)
      Description copied from interface: EventDispatcherContext
      Invokes the event of a given type with a given target.
      Specified by:
      invoke in interface EventDispatcherContext<EventType extends Enum<EventType>,Target,E extends Event<EventType,? super Target>>
      Parameters:
      eventType - The type of event to invoke
      target - The target of the event
    • createEvent

      protected abstract E createEvent(EventType eventType, Target target)
      Creates an event of a given type with a given target.
      Parameters:
      eventType - The type of event to create
      target - The target of the event
      Returns:
      The new event
    • invoke

      protected void invoke(E event)
      Invokes an event.
      Parameters:
      event - The event to invoke
    • shouldInvoke

      protected boolean shouldInvoke()
    • shouldPropagate

      protected boolean shouldPropagate()
    • propagate

      protected void propagate(E event)
      Propagates the event to other objects in relation to the target object. This method is called after the event has been invoked, unless the propagation was stopped.

      The default implementation does nothing.

      Parameters:
      event - The event to propagate
    • setLogger

      public void setLogger(Logger logger)
      Sets the logger of this event dispatcher, which is used to log errors thrown by listeners.
    • getLogger

      protected Logger getLogger()
    • destroy

      public void destroy()
      Removes all listeners.
      Specified by:
      destroy in interface Destructible