public class EventBus
extends java.lang.Object
This class is based on Guava's EventBus
but priority is supported
and events are dispatched at the time of call, rather than being queued up.
This does allow dispatching during an in-progress dispatch.
This implementation utilizes naive synchronization on all getter and setter methods. Dispatch does not occur when a lock has been acquired, however.
Constructor and Description |
---|
EventBus() |
Modifier and Type | Method and Description |
---|---|
protected void |
dispatch(java.lang.Object event,
EventHandler handler)
Dispatches
event to the handler in handler . |
protected java.util.Set<EventHandler> |
newHandlerSet()
Creates a new Set for insertion into the handler map.
|
void |
post(java.lang.Object event)
Posts an event to all registered handlers.
|
void |
register(java.lang.Object object)
Registers all handler methods on
object to receive events. |
void |
subscribe(java.lang.Class<?> clazz,
EventHandler handler)
Registers the given handler for the given class to receive events.
|
void |
subscribeAll(com.google.common.collect.Multimap<java.lang.Class<?>,EventHandler> handlers)
Registers the given handler for the given class to receive events.
|
void |
unregister(java.lang.Object object)
Unregisters all handler methods on a registered
object . |
void |
unsubscribe(java.lang.Class<?> clazz,
EventHandler handler)
Unregisters the given handler for the given class.
|
void |
unsubscribeAll(com.google.common.collect.Multimap<java.lang.Class<?>,EventHandler> handlers)
Unregisters the given handlers.
|
public void subscribe(java.lang.Class<?> clazz, EventHandler handler)
clazz
- the event class to registerhandler
- the handler to registerpublic void subscribeAll(com.google.common.collect.Multimap<java.lang.Class<?>,EventHandler> handlers)
handlers
- a map of handlerspublic void unsubscribe(java.lang.Class<?> clazz, EventHandler handler)
clazz
- the classhandler
- the handlerpublic void unsubscribeAll(com.google.common.collect.Multimap<java.lang.Class<?>,EventHandler> handlers)
handlers
- a map of handlerspublic void register(java.lang.Object object)
object
to receive events.
Handler methods are selected and classified using this EventBus's
SubscriberFindingStrategy
; the default strategy is the
AnnotatedSubscriberFinder
.object
- object whose handler methods should be registered.public void unregister(java.lang.Object object)
object
.object
- object whose handler methods should be unregistered.java.lang.IllegalArgumentException
- if the object was not previously registered.public void post(java.lang.Object event)
If no handlers have been subscribed for event
's class, and
event
is not already a DeadEvent
, it will be wrapped in a
DeadEvent and reposted.
event
- event to post.protected void dispatch(java.lang.Object event, EventHandler handler)
event
to the handler in handler
. This method
is an appropriate override point for subclasses that wish to make
event delivery asynchronous.event
- event to dispatch.handler
- handler that will call the handler.protected java.util.Set<EventHandler> newHandlerSet()