public class EventBus extends Object
Event, which may then be joined by client classes. The owner can then
push an instance of the Event to the bus using push(Event)
which will notify all registered classes listening for that event type, or
any of its superclasses.
This implementation allows for inherited and overridden event types, assuming that each superclass has defined a queue specifically for that type. When listeners are registered, they are added to all queues 'compatible' with their event type, meaning that they will be placed into the event queue for their direct event type, and any assignable superclasses. Then, when an event is pushed to the queue, only the queue for that exact type is notified. This ensures that listeners will only ever receive a single notification for events compatible via multiple superclasses.
Definition of listeners requires the EventHandler annotation. When
some client class calls register(Object), all methods with an
@EventHandler annotation are scanned and added to the appropriate
event queues.
@EventHandler takes an optional priority parameter. Events
with a higher priority (defined either as an integer or preferably using a
constant in EventPriority) will be notified before events with a
lower (more negative) priority.
Events may also be 'vetoed', in the sense that an event at a higher priority may prevent handlers further down in the queue from being executed. While useful, this should be used with caution to ensure potentially important events may still get an opportunity to be notified.
| Constructor and Description |
|---|
EventBus() |
| Modifier and Type | Method and Description |
|---|---|
void |
add(Class<? extends Event> clazz)
Defines a new event type.
|
void |
deregister(Object o)
Removes the given object from all event queues that it may be a member
of.
|
EventQueueDefinition |
getQueueForClass(Class<? extends Event> clazz)
Gets the EventQueueDefinition for the given class.
|
void |
push(Event event)
Pushes the given event to the message bus.
|
void |
push(Event event,
int priority)
Pushes the given event to the bus, but only notifies those with a
priority flag greater than or equal to the given
priority. |
void |
register(Object o)
Attempts to register all methods of the given object annotated with
EventHandler to receive events from this bus. |
protected void |
registerMethod(Object o,
Method m,
int priority,
boolean vetoable)
Registers the given method to the event bus.
|
void |
remove(Class<? extends Event> clazz)
Removes the event queue for the given class.
|
public void add(Class<? extends Event> clazz)
push(Event) will notify
registered listeners.
Note that this class is not thread-safe; add() and
remove() may cause a ConcurrentModificationException when
used concurrently.
clazz - the event class to registerpublic EventQueueDefinition getQueueForClass(Class<? extends Event> clazz)
null is returned. Note that
this will not return superclasses of the given class, only exact matches.clazz - The class to search forpublic void remove(Class<? extends Event> clazz)
clazz - the class for which to remove the queuepublic void push(Event event)
EventVetoException will cause handlers further down in the queue
to be skipped. Specifically, this notifies the event queue that exactly
matches the class (as handlers are added to superclass queues at
registration time)
If no queue exists for the given event type, no listeners will be notified and the method will fail silently.
Unlike add(Class) and remove(Class), this method
is thread-safe and can be safely used concurrently.
event - the event to pushpublic void push(Event event, int priority)
priority. Note
that the default priority is EventPriority.NORMAL (0).event - the event to pushpriority - the minimum event prioritypush(Event),
EventPriorityprotected void registerMethod(Object o, Method m, int priority, boolean vetoable)
The method parameters are
checked and added to the event queue corresponding to the Event
used as the first method parameter. In other words, if passed a reference
to the following method:
public void xyzHandler(XYZEvent event) { ... }
... xyzHandler will be added to the event queue for
XYZEvent assuming that XYZEvent extends Event and
an event queue has been created for that event type.
o - an instance of the class containing the method mm - the method to registerpriority - the event priorityvetoable - vetoable flagpublic void register(Object o)
EventHandler to receive events from this bus. Only methods
that are annotated with @EventHandler and accept a single
parameter of an event type emitted from this bus (that is, previously
added with add(java.lang.Class)) will be registered.
The EventScanMode annotation may be used to control the
behavior and degree of scanning. By default the scan mode is
EventScanType.FAST and will scan all public methods for the
EventHandler annotation, including methods inherited from some
superclass. However, private fields will not be scanned.
EventScanType.EXTENDED additionally scans and registers
private methods defined directly in the given object, but does not scan
private fields in superclasses. This incurs a higher runtime cost at
registration time as more methods are scanned.
The final scanning type, EventScanType.FULL, scans all public
methods (as with the FAST type), direct private members (as with
EXTENDED), and private members defined in any superclass. This
will incur the highest runtime cost, but will preserve any event-related
functionality.
o - the object to processEventBus#registerMethod(Object, Method, int)public void deregister(Object o)
o - the object to removeCopyright © 2014. All rights reserved.