diff --git a/README.md b/README.md
index 78b8268..3345abb 100644
--- a/README.md
+++ b/README.md
@@ -12,6 +12,7 @@ In addition, a singleton instance of the event bus is provided by the `EventBus#
To listen to events, register event handling methods using the `Event` annotation.
For this to work, the method must have a return type of `void` and declare a single parameter of the desired event type.
+Alternatively, a parameter-less event handler can be declared as shown [below](#parameter-less-event-handlers).
Additionally, the class containing the method must implement the `EventListener` interface.
## A Simple Example
@@ -47,7 +48,22 @@ public class SimpleEventListener implements EventListener {
}
```
-In this case, an event bus is created and used locally. In a more sophisticated example the class would acquire an external event bus that is used by multiple classes.
+In this case, an event bus is created and used locally.
+In a more sophisticated example the class would acquire an external event bus that is used by multiple classes.
+
+## Parameter-less event handlers
+
+In some cases an event handler is not interested in the dispatched event instance.
+To avoid declaring a useless parameter just to specify the event type of the handler, there is an alternative:
+
+```java
+@Event(eventType = SimpleEvent.class)
+private void onSimpleEvent() {
+ System.out.println("SimpleEvent received!");
+}
+```
+
+Make sure that you **do not** declare both a parameter and the `eventType` value of the annotation, as this would be ambiguous.
## Installation
@@ -66,7 +82,7 @@ To include it inside your project, just add the Maven repository and the depende
+ * This is useful when the event handler does not utilize the event instance.
+ *
+ * @since 0.0.3
+ */
+ Class extends IEvent> eventType() default USE_PARAMETER.class;
+
+ /**
+ * Signifies that the event type the handler listens to is determined by the type of its only
+ * parameter.
+ *
+ * @since 0.0.3
+ */
+ static final class USE_PARAMETER implements IEvent {}
}
diff --git a/src/main/java/dev/kske/eventbus/EventBus.java b/src/main/java/dev/kske/eventbus/EventBus.java
index f4f54f5..cdb93af 100644
--- a/src/main/java/dev/kske/eventbus/EventBus.java
+++ b/src/main/java/dev/kske/eventbus/EventBus.java
@@ -43,6 +43,7 @@ public final class EventBus {
* @since 0.0.1
*/
public void dispatch(IEvent event) {
+ Objects.requireNonNull(event);
getHandlersFor(event.getClass()).forEach(handler -> handler.execute(event));
}
@@ -63,11 +64,12 @@ public final class EventBus {
*
* @param listener the listener to register
* @throws EventBusException if the listener is already registered or a declared event handler
- * does not comply to the specification
+ * does not comply with the specification
* @since 0.0.1
* @see Event
*/
public void registerListener(EventListener listener) throws EventBusException {
+ Objects.requireNonNull(listener);
if (registeredListeners.contains(listener))
throw new EventBusException(listener + " already registered!");
@@ -79,23 +81,12 @@ public final class EventBus {
if (annotation == null)
continue;
- // Check for correct method signature and return type
- if (method.getParameterCount() != 1)
- throw new EventBusException(method + " does not have an argument count of 1!");
-
- if (!method.getReturnType().equals(void.class))
- throw new EventBusException(method + " does not have a return type of void!");
-
- var param = method.getParameterTypes()[0];
- if (!IEvent.class.isAssignableFrom(param))
- throw new EventBusException(param + " is not of type IEvent!");
-
- @SuppressWarnings("unchecked")
- var realParam = (Class extends IEvent>) param;
- if (!bindings.containsKey(realParam))
- bindings.put(realParam, new TreeSet<>());
-
- bindings.get(realParam).add(new EventHandler(listener, method, annotation));
+ // Initialize and bind the handler
+ var handler = new EventHandler(listener, method, annotation);
+ if (!bindings.containsKey(handler.getEventType()))
+ bindings.put(handler.getEventType(), new TreeSet<>());
+ bindings.get(handler.getEventType())
+ .add(handler);
}
}
@@ -106,6 +97,7 @@ public final class EventBus {
* @since 0.0.1
*/
public void removeListener(EventListener listener) {
+ Objects.requireNonNull(listener);
for (var binding : bindings.values()) {
var it = binding.iterator();
while (it.hasNext())
diff --git a/src/main/java/dev/kske/eventbus/EventHandler.java b/src/main/java/dev/kske/eventbus/EventHandler.java
index f434dfa..f4aa355 100644
--- a/src/main/java/dev/kske/eventbus/EventHandler.java
+++ b/src/main/java/dev/kske/eventbus/EventHandler.java
@@ -2,6 +2,8 @@ package dev.kske.eventbus;
import java.lang.reflect.*;
+import dev.kske.eventbus.Event.USE_PARAMETER;
+
/**
* Internal representation of an event handling method.
*
@@ -14,6 +16,7 @@ final class EventHandler implements Comparable