public class Bus
extends java.lang.Object
The Bus allows publish-subscribe-style communication between components without requiring the components to explicitly register with one another (and thus be aware of each other). It is designed exclusively to replace traditional Android in-process event distribution using explicit registration or listeners. It is not a general-purpose publish-subscribe system, nor is it intended for interprocess communication.
Subscribe
annotation;register(Object)
method.
post(Object)
method. The Bus instance will
determine the type of event and route it to all registered listeners.
Events are routed based on their type — an event will be delivered to any handler for any type to which the event is assignable. This includes implemented interfaces, all superclasses, and all interfaces implemented by superclasses.
When post
is called, all registered handlers for an event are run in sequence, so handlers should be
reasonably quick. If an event may trigger an extended process (such as a database load), spawn a thread or queue it
for later.
Handlers should not, in general, throw. If they do, the Bus will wrap the exception and re-throw it.
The Bus by default enforces that all interactions occur on the main thread. You can provide an alternate
enforcement by passing a ThreadEnforcer
to the constructor.
DeadEvent
and
reposted.
This class is safe for concurrent use.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_IDENTIFIER |
Constructor and Description |
---|
Bus()
Creates a new Bus named "default" that enforces actions on the main thread.
|
Bus(java.lang.String identifier)
Creates a new Bus with the given
identifier that enforces actions on the main thread. |
Bus(ThreadEnforcer enforcer)
Creates a new Bus named "default" with the given
enforcer for actions. |
Bus(ThreadEnforcer enforcer,
java.lang.String identifier)
Creates a new Bus with the given
enforcer for actions and the given identifier . |
Bus(ThreadEnforcer enforcer,
java.lang.String identifier,
HandlerFinder handlerFinder)
Test constructor which allows replacing the default
HandlerFinder . |
Modifier and Type | Method and Description |
---|---|
protected void |
dispatch(java.lang.Object event,
EventHandler wrapper)
Dispatches
event to the handler in wrapper . |
protected void |
dispatchQueuedEvents()
Drain the queue of events to be dispatched.
|
protected void |
enqueueEvent(java.lang.Object event,
EventHandler handler)
Queue the
event for dispatch during dispatchQueuedEvents() . |
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 and producer methods to provide events. |
java.lang.String |
toString() |
void |
unregister(java.lang.Object object)
Unregisters all producer and handler methods on a registered
object . |
public static final java.lang.String DEFAULT_IDENTIFIER
public Bus()
public Bus(java.lang.String identifier)
identifier
that enforces actions on the main thread.identifier
- a brief name for this bus, for debugging purposes. Should be a valid Java identifier.public Bus(ThreadEnforcer enforcer)
enforcer
for actions.enforcer
- Thread enforcer for register, unregister, and post actions.public Bus(ThreadEnforcer enforcer, java.lang.String identifier)
enforcer
for actions and the given identifier
.enforcer
- Thread enforcer for register, unregister, and post actions.identifier
- A brief name for this bus, for debugging purposes. Should be a valid Java identifier.public Bus(ThreadEnforcer enforcer, java.lang.String identifier, HandlerFinder handlerFinder)
HandlerFinder
.enforcer
- Thread enforcer for register, unregister, and post actions.identifier
- A brief name for this bus, for debugging purposes. Should be a valid Java identifier.handlerFinder
- Used to discover event handlers and producers when registering/unregistering an object.public java.lang.String toString()
toString
in class java.lang.Object
public void register(java.lang.Object object)
object
to receive events and producer methods to provide events.
If any subscribers are registering for types which already have a producer they will be called immediately with the result of calling that producer.
If any producers are registering for types which already have subscribers, each subscriber will be called with the value from the result of calling the producer.
object
- object whose handler methods should be registered.java.lang.NullPointerException
- if the object is null.public void unregister(java.lang.Object object)
object
.object
- object whose producer and handler methods should be unregistered.java.lang.IllegalArgumentException
- if the object was not previously registered.java.lang.NullPointerException
- if the object is null.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.java.lang.NullPointerException
- if the event is null.protected void enqueueEvent(java.lang.Object event, EventHandler handler)
event
for dispatch during dispatchQueuedEvents()
. Events are queued in-order of
occurrence so they can be dispatched in the same order.protected void dispatchQueuedEvents()
protected void dispatch(java.lang.Object event, EventHandler wrapper)
event
to the handler in wrapper
. This method is an appropriate override point for
subclasses that wish to make event delivery asynchronous.event
- event to dispatch.wrapper
- wrapper that will call the handler.