Interface Bus
- All Known Implementing Classes:
BusImplementation
public interface Bus
A general Bus interface that can handle events of any type, and allows adding subscribers
to events.
The actual subscriber is always of type
Subscriber
. If, however, an Object of any other
type is passed instead, the respective methods of that class will be looked up and searched for
subscribers. Thus, one can either add method-based subscribers from the annotation, or define
a subscriber at runtime directly.
Each bus has an identifier for logging purposes. It can either be generated or provided by the user.-
Method Summary
Modifier and TypeMethodDescriptionvoid
acceptEvents
(boolean accept) Sets whether events can be posted to this bus or not.void
Closes the registration of subscribers.The unique identifer of this bus.static Bus
instance()
Returns a new instance of this bus with a default identifier.static Bus
Returns a new instance of this bus with the identifier given byname
.void
Posts an event and calls each handler that are subscribed to, if the interceptors allow that.void
Registers an object that contains method annotated bySubscribe
annotations.<T> void
register
(Subscriber<T> subscriber) Registers a subscriber for the event of typeT
.void
unregister
(Object object) Unregisters either an object containing subscriber methods, or a certain subscriber through the name.
-
Method Details
-
getIdentifier
String getIdentifier()The unique identifer of this bus. This can be generated or provided by the user.- Returns:
- The unique identifier.
-
register
Registers a subscriber for the event of typeT
. The subscriber holds a name that is used to identify it and later be unsubscribed again usingunregister(Object)
.- Type Parameters:
T
- The event type- Parameters:
subscriber
- The subscriber holding the event handler.
-
register
Registers an object that contains method annotated bySubscribe
annotations. This object must also be annotated withOnEvent
s to be correctly registered. The id of these subscribers will be the full canonical name of the method (including the name of the class).- Parameters:
object
- The object containing subscriber methods.
-
unregister
Unregisters either an object containing subscriber methods, or a certain subscriber through the name. That is, theobject
parameter can be an arbitrary object of subscriber methods, or of typeString
that identifies a certain subscriber. Afterwards, those affected subscribers will be ignored upon the respective events.- Parameters:
object
- The object holding the subscriber methods OR the id of the subscriber as a String.
-
post
Posts an event and calls each handler that are subscribed to, if the interceptors allow that.- Parameters:
event
- The event to post.
-
closeRegistration
void closeRegistration()Closes the registration of subscribers. After this method is called, it is not possible to register new objects. -
acceptEvents
void acceptEvents(boolean accept) Sets whether events can be posted to this bus or not.- Parameters:
accept
- A flag saying whether events can be posted here. Iftrue
, then events can be posted (default behavior). Iffalse
, events are not allowed to be posted here until it is opened again.
-
instance
Returns a new instance of this bus with a default identifier.- Returns:
- A new instance with default identifier.
-
instance
Returns a new instance of this bus with the identifier given byname
.- Parameters:
name
- The identifier of the bus that should be created.- Returns:
- A new bus with the given identifier.
-