12 Commits

Author SHA1 Message Date
856a2e8cbf Bump version to 1.2.0 2021-11-26 15:54:37 +01:00
11860d1469 Merge pull request 'Document Latest Features in README' (#27) from f/improved-readme into develop
Reviewed-on: https://git.kske.dev/kske/event-bus/pulls/27
Reviewed-by: delvh <leon@kske.dev>
2021-11-26 13:54:58 +01:00
f620f06208 Merge branch 'develop' into f/improved-readme
Conflicts:
	event-bus-core/src/test/java/dev/kske/eventbus/core/DispatchTest.java
2021-11-25 14:36:07 +01:00
5a6d8bcf35 Rename EventBus#printExecutionOrder(Class) to debugExecutionOrder
The method doesn't print anything, but rather returns a string
containing the debug information.
2021-11-25 14:34:13 +01:00
39ffb5c82a Fix module-info instructions in README
Reflective access has to be allowed from the Event Bus core package to a
package in the user's project, not the entire module. Thank you @delvh
for noticing this!
2021-11-25 14:29:06 +01:00
5ddef71c26 Merge pull request 'Support JDK-style Javadoc Tags' (#28) from b/javadoc-tags into develop
Reviewed-on: https://git.kske.dev/kske/event-bus/pulls/28
Reviewed-by: delvh <leon@kske.dev>
2021-11-25 12:05:32 +01:00
85b2da391a Merge pull request 'Make Unit Tests Executable by Maven' (#29) from b/unit-test-execution into develop
Reviewed-on: https://git.kske.dev/kske/event-bus/pulls/29
Reviewed-by: delvh <leon@kske.dev>
2021-11-25 12:04:26 +01:00
46a358da97 Make unit tests executable by Maven 2021-11-24 12:52:59 +01:00
6bf9e1097a Support JDK-style Javadoc tags 2021-11-24 11:30:36 +01:00
3fccb809c8 Move installation section up in README 2021-11-24 10:49:30 +01:00
d1c4bcc7eb Add callback listener section to README 2021-11-24 10:45:58 +01:00
ad29a93ccb Add debugging section to README 2021-11-24 10:37:21 +01:00
10 changed files with 104 additions and 50 deletions

View File

@ -54,6 +54,33 @@ private static void onSimpleEvent(SimpleEvent event) { ... }
is technically possible, however you would still have to create an instance of the event listener to register it at an event bus. is technically possible, however you would still have to create an instance of the event listener to register it at an event bus.
## Installation
Event Bus is available in Maven Central.
To include it inside your project, just add the following dependency to your `pom.xml`:
```xml
<dependencies>
<dependency>
<groupId>dev.kske</groupId>
<artifactId>event-bus-core</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
```
Then, require the Event Bus Core module in your `module-info.java`:
```java
requires dev.kske.eventbus.core;
```
If you intend to use event handlers that are inaccessible to Event Bus by means of Java language access control, make sure to allow reflective access to your package for Event Bus:
```java
opens my.package to dev.kske.eventbus.core;
```
## Polymorphic Event Handlers ## Polymorphic Event Handlers
On certain occasions it's practical for an event handler to accept both events of the specified type, as well as subclasses of that event. On certain occasions it's practical for an event handler to accept both events of the specified type, as well as subclasses of that event.
@ -94,6 +121,18 @@ private void onSimpleEvent() {
Make sure that you **do not** both declare a parameter and specify the event type in the annotation, as this would be ambiguous. Make sure that you **do not** both declare a parameter and specify the event type in the annotation, as this would be ambiguous.
## Callback listeners
While defining event handlers as annotated methods is rather simple and readable, sometimes a more flexible approach is required.
For this reason, there are callback event handlers that allow the registration of an "inline" event listener consisting of just one handler in the form of a consumer:
```java
EventBus.getInstance().registerListener(SimpleEvent.class, e -> System.out.println("Received " + e));
```
The event type has to be defined explicitly, with the priority and polymorphism parameters being optional.
If you intend to remove the listener later, remember to keep a reference to it, as you would have to clear the entire event bus if you didn't.
## Listener-Level Properties ## Listener-Level Properties
When defining a dedicated event listener that, for example, performs pre- or post-processing, all event handlers will probably have the same non-standard priority. When defining a dedicated event listener that, for example, performs pre- or post-processing, all event handlers will probably have the same non-standard priority.
@ -159,32 +198,16 @@ The same applies when an exception event handler throws an exception.
To avoid this, system events never cause system events and instead just issue a warning to the logger. To avoid this, system events never cause system events and instead just issue a warning to the logger.
## Installation ## Debugging
Event Bus is available in Maven Central. In more complex setups, taking a look at the event handler execution order can be helpful for debugging.
To include it inside your project, just add the following dependency to your `pom.xml`: Event Bus offers a method for this purpose which can be used as follows:
```xml
<dependencies>
<dependency>
<groupId>dev.kske</groupId>
<artifactId>event-bus-core</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
```
Then, require the Event Bus Core module in your `module-info.java`:
```java ```java
requires dev.kske.eventbus.core; System.out.println(EventBus.getInstance().debugExecutionOrder(SimpleEvent.class));
``` ```
If you intend to use event handlers that are inaccessible to Event Bus by means of Java language access control, make sure to allow reflective access from your module: Then, the execution order can be inspected in the console.
```java
opens my.module to dev.kske.eventbus.core;
```
## Compile-Time Error Checking with Event Bus Proc ## Compile-Time Error Checking with Event Bus Proc
@ -205,7 +228,7 @@ When using Maven, it can be registered using the Maven Compiler Plugin:
<annotationProcessorPath> <annotationProcessorPath>
<groupId>dev.kske</groupId> <groupId>dev.kske</groupId>
<artifactId>event-bus-proc</artifactId> <artifactId>event-bus-proc</artifactId>
<version>1.1.0</version> <version>1.2.0</version>
</annotationProcessorPath> </annotationProcessorPath>
</annotationProcessorPaths> </annotationProcessorPaths>
</configuration> </configuration>

View File

@ -9,14 +9,14 @@
<parent> <parent>
<groupId>dev.kske</groupId> <groupId>dev.kske</groupId>
<artifactId>event-bus</artifactId> <artifactId>event-bus</artifactId>
<version>1.1.0</version> <version>1.2.0</version>
</parent> </parent>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId> <artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version> <version>5.8.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
@ -26,5 +26,14 @@
<!-- Disable resource folder --> <!-- Disable resource folder -->
<resources /> <resources />
<!-- Run unit tests -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build> </build>
</project> </project>

View File

@ -381,7 +381,7 @@ public final class EventBus {
* @return a human-readable event handler list suitable for debugging purposes * @return a human-readable event handler list suitable for debugging purposes
* @since 1.2.0 * @since 1.2.0
*/ */
public String printExecutionOrder(Class<?> eventType) { public String debugExecutionOrder(Class<?> eventType) {
var handlers = getHandlersFor(eventType); var handlers = getHandlersFor(eventType);
var sj = new StringJoiner("\n"); var sj = new StringJoiner("\n");

View File

@ -11,7 +11,7 @@ import org.junit.jupiter.api.*;
* @author Leon Hofmeister * @author Leon Hofmeister
* @since 0.1.0 * @since 0.1.0
*/ */
class CancelTest { public class CancelTest {
EventBus bus; EventBus bus;
int hits; int hits;
@ -22,7 +22,7 @@ class CancelTest {
* @since 0.1.0 * @since 0.1.0
*/ */
@BeforeEach @BeforeEach
void registerListener() { public void registerListener() {
bus = new EventBus(); bus = new EventBus();
bus.registerListener(this); bus.registerListener(this);
} }
@ -34,7 +34,7 @@ class CancelTest {
* @since 0.1.0 * @since 0.1.0
*/ */
@Test @Test
void testCancellation() { public void testCancellation() {
bus.dispatch(new SimpleEvent()); bus.dispatch(new SimpleEvent());
assertEquals(1, hits); assertEquals(1, hits);
} }

View File

@ -10,7 +10,7 @@ import org.junit.jupiter.api.Test;
* @author Kai S. K. Engelbart * @author Kai S. K. Engelbart
* @since 1.1.0 * @since 1.1.0
*/ */
class DeadTest { public class DeadTest {
EventBus bus = new EventBus(); EventBus bus = new EventBus();
String event = "This event has no handler"; String event = "This event has no handler";
@ -18,11 +18,11 @@ class DeadTest {
/** /**
* Tests dead event delivery. * Tests dead event delivery.
* *
* @since 1.1.0 * @since 1.1.0
*/ */
@Test @Test
void testDeadEvent() { public void testDeadEvent() {
bus.registerListener(this); bus.registerListener(this);
bus.dispatch(event); bus.dispatch(event);
assertTrue(deadEventHandled); assertTrue(deadEventHandled);
@ -32,11 +32,11 @@ class DeadTest {
/** /**
* Tests how the event bus reacts to an unhandled dead event. This should not lead to an * Tests how the event bus reacts to an unhandled dead event. This should not lead to an
* exception or an endless recursion and should be logged instead. * exception or an endless recursion and should be logged instead.
* *
* @since 1.1.0 * @since 1.1.0
*/ */
@Test @Test
void testUnhandledDeadEvent() { public void testUnhandledDeadEvent() {
bus.dispatch(event); bus.dispatch(event);
} }

View File

@ -12,7 +12,7 @@ import org.junit.jupiter.api.*;
*/ */
@Polymorphic @Polymorphic
@Priority(150) @Priority(150)
class DispatchTest { public class DispatchTest {
EventBus bus; EventBus bus;
static int hits; static int hits;
@ -23,7 +23,7 @@ class DispatchTest {
* @since 0.0.1 * @since 0.0.1
*/ */
@BeforeEach @BeforeEach
void registerListener() { public void registerListener() {
bus = new EventBus(); bus = new EventBus();
bus.registerListener(this); bus.registerListener(this);
bus.registerListener(SimpleEvent.class, e -> { bus.registerListener(SimpleEvent.class, e -> {
@ -39,19 +39,19 @@ class DispatchTest {
* @since 0.0.1 * @since 0.0.1
*/ */
@Test @Test
void testDispatch() { public void testDispatch() {
bus.dispatch(new SimpleEventSub()); bus.dispatch(new SimpleEventSub());
bus.dispatch(new SimpleEvent()); bus.dispatch(new SimpleEvent());
} }
/** /**
* Tests {@link EventBus#printExecutionOrder(Class)} based on the currently registered handlers. * Tests {@link EventBus#debugExecutionOrder(Class)} based on the currently registered handlers.
* *
* @since 1.2.0 * @since 1.2.0
*/ */
@Test @Test
void testPrintExecutionOrder() { public void testDebugExecutionOrder() {
String executionOrder = bus.printExecutionOrder(SimpleEvent.class); String executionOrder = bus.debugExecutionOrder(SimpleEvent.class);
System.out.println(executionOrder); System.out.println(executionOrder);
assertEquals( assertEquals(
"Event handler execution order for class dev.kske.eventbus.core.SimpleEvent (3 handler(s)):\n" "Event handler execution order for class dev.kske.eventbus.core.SimpleEvent (3 handler(s)):\n"

View File

@ -6,11 +6,11 @@ import org.junit.jupiter.api.Test;
/** /**
* Tests the dispatching of an exception event if an event handler threw an exception. * Tests the dispatching of an exception event if an event handler threw an exception.
* *
* @author Kai S. K. Engelbart * @author Kai S. K. Engelbart
* @since 1.1.0 * @since 1.1.0
*/ */
class ExceptionTest { public class ExceptionTest {
EventBus bus = new EventBus(); EventBus bus = new EventBus();
String event = "This event will cause an exception"; String event = "This event will cause an exception";
@ -19,11 +19,11 @@ class ExceptionTest {
/** /**
* Tests exception event delivery. * Tests exception event delivery.
* *
* @since 1.1.0 * @since 1.1.0
*/ */
@Test @Test
void testExceptionEvent() { public void testExceptionEvent() {
bus.registerListener(this); bus.registerListener(this);
bus.registerListener(new ExceptionListener()); bus.registerListener(new ExceptionListener());
bus.dispatch(event); bus.dispatch(event);
@ -34,11 +34,11 @@ class ExceptionTest {
/** /**
* Tests how the event bus reacts to an unhandled exception event. This should not lead to an * Tests how the event bus reacts to an unhandled exception event. This should not lead to an
* exception or an endless recursion and should be logged instead. * exception or an endless recursion and should be logged instead.
* *
* @since 1.1.0 * @since 1.1.0
*/ */
@Test @Test
void testUnhandledExceptionEvent() { public void testUnhandledExceptionEvent() {
bus.registerListener(this); bus.registerListener(this);
bus.dispatch(event); bus.dispatch(event);
bus.removeListener(this); bus.removeListener(this);

View File

@ -10,7 +10,7 @@ import org.junit.jupiter.api.*;
* @author Kai S. K. Engelbart * @author Kai S. K. Engelbart
* @since 1.2.0 * @since 1.2.0
*/ */
class NestedTest { public class NestedTest {
EventBus bus; EventBus bus;
boolean nestedHit; boolean nestedHit;
@ -21,7 +21,7 @@ class NestedTest {
* @since 1.2.0 * @since 1.2.0
*/ */
@BeforeEach @BeforeEach
void registerListener() { public void registerListener() {
bus = new EventBus(); bus = new EventBus();
bus.registerListener(this); bus.registerListener(this);
} }
@ -34,7 +34,7 @@ class NestedTest {
* @since 1.2.0 * @since 1.2.0
*/ */
@Test @Test
void testNestedDispatch() { public void testNestedDispatch() {
bus.dispatch(new SimpleEvent()); bus.dispatch(new SimpleEvent());
assertTrue(nestedHit); assertTrue(nestedHit);
} }

View File

@ -11,7 +11,7 @@
<parent> <parent>
<groupId>dev.kske</groupId> <groupId>dev.kske</groupId>
<artifactId>event-bus</artifactId> <artifactId>event-bus</artifactId>
<version>1.1.0</version> <version>1.2.0</version>
</parent> </parent>
<dependencies> <dependencies>

24
pom.xml
View File

@ -5,7 +5,7 @@
<groupId>dev.kske</groupId> <groupId>dev.kske</groupId>
<artifactId>event-bus</artifactId> <artifactId>event-bus</artifactId>
<version>1.1.0</version> <version>1.2.0</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<name>Event Bus</name> <name>Event Bus</name>
@ -120,6 +120,28 @@
</goals> </goals>
</execution> </execution>
</executions> </executions>
<!-- Support JDK-style Javadoc tags -->
<configuration>
<tags>
<tag>
<name>apiNote</name>
<placement>a</placement>
<head>API Note:</head>
</tag>
<tag>
<name>implSpec</name>
<placement>a</placement>
<head>Implementation Requirements:</head>
</tag>
<tag>
<name>implNote</name>
<placement>a</placement>
<head>Implementation Note:</head>
</tag>
</tags>
</configuration>
</plugin> </plugin>
<!-- GPG sign JAR --> <!-- GPG sign JAR -->