EventBus Adapter for Java
The EventBus Adapter wraps various EventBus implementations for Java and Android. It uses a uniform interface (com.cookingfox.eventbus.EventBus
) to type your classes to, so that it's possible to change the implementation when required.
Download
The distribution is hosted on Bintray. To include the package in your projects, you can add the jCenter repository.
Gradle
Add jCenter to your repositories
block:
repositories {
jcenter()
}
and add the project to the dependencies
block in your build.gradle
:
dependencies {
compile 'com.cookingfox:eventbus-adapter-java:3.0.0'
}
Maven
Add jCenter to your repositories in pom.xml
or settings.xml
:
<repositories>
<repository>
<id>jcenter</id>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
and add the project declaration to your pom.xml
:
<dependency>
<groupId>com.cookingfox</groupId>
<artifactId>eventbus-adapter-java</artifactId>
<version>3.0.0</version>
</dependency>
Features
Currently the library has adapters for:
-
Google Guava EventBus (tested with version 19.0):
GuavaEventBusAdapter
-
GreenRobot EventBus:
The main EventBus
interface actually inherits from the EventBusPublisher
and EventBusSubscriber
interfaces. This allows the user to restrict the available functionality of the consuming class.
Usage
Include in your project's dependencies:
- This wrapper library. (see "Download")
- The library you want to wrap. (see "Features")
Example for the GreenRobot EventBus version 3:
class ExampleEvent {}
class ExampleSubscriber {
com.cookingfox.eventbus.EventBus eventBus;
ExampleSubscriber(com.cookingfox.eventbus.EventBus eventBus) {
this.eventBus = eventBus;
}
void onCreate() {
eventBus.register(this);
}
void onEvent(ExampleEvent event) {
// handle event
}
void onDestroy() {
eventBus.unregister(this);
}
}
// create real EventBus and adapter
org.greenrobot.eventbus.EventBus realEventBus = new org.greenrobot.eventbus.EventBus();
com.cookingfox.eventbus.EventBus eventBusAdapter = new GreenRobot3Adapter(realEventBus);
// create and register subscriber
ExampleSubscriber subscriber = new ExampleSubscriber(eventBusAdapter);
subscriber.onCreate();
// post an event
eventBusAdapter.post(new ExampleEvent());
// unregister subscriber
subscriber.onDestroy();
Restricting publish / subscribe capabilities
If you want to restrict your consumer classes' EventBus capabilities, use the following interfaces:
EventBusPublisher
: the class will only be able to use thepost
method.EventBusSubscriber
: the class will only be able to use theregister
andunregister
methods.
F.A.Q.
Is it possible to support EventBus library X?
If you make an issue for it, we'll take a look at it! :)
Copyright and license
Code and documentation copyright 2017 Cooking Fox. Code released under the Apache 2.0 license.