From 810df972ccdacdff78ab487a7ed4b839cf822759 Mon Sep 17 00:00:00 2001 From: kske Date: Wed, 12 Feb 2020 05:58:40 +0100 Subject: [PATCH] Clean-up, improved generic type usage in EventBus Fixes #13 --- .../java/envoy/event/ContactSearchResult.java | 2 +- src/main/java/envoy/event/Event.java | 2 +- src/main/java/envoy/event/EventBus.java | 49 +++++++++---------- src/main/java/envoy/event/EventHandler.java | 18 ------- .../envoy/event/HandshakeRejectionEvent.java | 2 +- 5 files changed, 26 insertions(+), 47 deletions(-) delete mode 100644 src/main/java/envoy/event/EventHandler.java diff --git a/src/main/java/envoy/event/ContactSearchResult.java b/src/main/java/envoy/event/ContactSearchResult.java index 03fd681..929ee30 100644 --- a/src/main/java/envoy/event/ContactSearchResult.java +++ b/src/main/java/envoy/event/ContactSearchResult.java @@ -22,7 +22,7 @@ public class ContactSearchResult extends Event> { * Creates an instance of {@link ContactSearchResult}. * * @param users the users found during the search + * @since Envoy Common v0.2-alpha */ public ContactSearchResult(List users) { super(users); } - } diff --git a/src/main/java/envoy/event/Event.java b/src/main/java/envoy/event/Event.java index 064ceaa..9d42bf5 100644 --- a/src/main/java/envoy/event/Event.java +++ b/src/main/java/envoy/event/Event.java @@ -25,7 +25,7 @@ public abstract class Event implements Serializable { public T get() { return value; } @Override - public String toString() { return String.format("%s[value=%s]%n", this.getClass().getSimpleName(), value); } + public String toString() { return String.format("%s[value=%s]", this.getClass().getSimpleName(), value); } /** * Serves as a super class for events that do not carry a value.
diff --git a/src/main/java/envoy/event/EventBus.java b/src/main/java/envoy/event/EventBus.java index f2e5c9f..146fb58 100644 --- a/src/main/java/envoy/event/EventBus.java +++ b/src/main/java/envoy/event/EventBus.java @@ -1,18 +1,15 @@ package envoy.event; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.function.Consumer; /** - * This class handles events by allowing {@link EventHandler} object to register - * themselves and then be notified about certain events dispatched by the event - * bus.
+ * This class handles events by allowing event handlers to register themselves + * and then be notified about certain events dispatched by the event bus.
*
* The event bus is a singleton and can be used across the entire application to * guarantee the propagation of events.
- * + *
* Project: envoy-common
* File: EventBus.java
* Created: 04.12.2019
@@ -23,14 +20,13 @@ import java.util.Map; public class EventBus { /** - * Contains all {@link EventHandler} instances registered at this - * {@link EventBus} as values mapped to by their supported {@link Event} - * classes. + * Contains all event handler instances registered at this event bus as values + * mapped to by their supported event classes. */ - private Map>, List> handlers = new HashMap<>(); + private Map>, List>>> handlers = new HashMap<>(); /** - * The singleton instance of this {@link EventBus} that is used across the + * The singleton instance of this event bus that is used across the * entire application. */ private static EventBus eventBus = new EventBus(); @@ -43,39 +39,40 @@ public class EventBus { private EventBus() {} /** - * @return the singleton instance of the {@link EventBus} + * @return the singleton instance of the event bus * @since Envoy v0.2-alpha */ public static EventBus getInstance() { return eventBus; } /** - * Registers an {@link EventHandler} to be notified when a - * {@link Event} of a certain type is dispatched. + * Registers an event handler to be notified when an + * event of a certain type is dispatched. * - * @param eventClass the class which the {@link EventHandler} is subscribed to - * @param handler the {@link EventHandler} to register + * @param the type of event values to notify the handler about + * @param eventClass the class which the event handler is subscribing to + * @param handler the event handler to register * @since Envoy v0.2-alpha */ - public void register(Class> eventClass, EventHandler handler) { + @SuppressWarnings("unchecked") + public > void register(Class eventClass, Consumer handler) { if (!handlers.containsKey(eventClass)) handlers.put(eventClass, new ArrayList<>()); - handlers.get(eventClass).add(handler); + handlers.get(eventClass).add((Consumer>) handler); } /** - * Dispatches a {@link Event} to every {@link EventHandler} subscribed to it. + * Dispatches an event to every event handler subscribed to it. * * @param event the {@link Event} to dispatch * @since Envoy v0.2-alpha */ public void dispatch(Event event) { - handlers.keySet().stream().filter(event.getClass()::isAssignableFrom).map(handlers::get).flatMap(List::stream).forEach(h -> h.handle(event)); + handlers.keySet().stream().filter(event.getClass()::isAssignableFrom).map(handlers::get).flatMap(List::stream).forEach(h -> h.accept(event)); } /** - * @return a map of all {@link EventHandler} instances currently registered at - * this {@link EventBus} with the {@link Event} classes they are - * subscribed to as keys + * @return a map of all event handler instances currently registered at this + * event bus with the event classes they are subscribed to as keys * @since Envoy v0.2-alpha */ - public Map>, List> getHandlers() { return handlers; } + public Map>, List>>> getHandlers() { return handlers; } } diff --git a/src/main/java/envoy/event/EventHandler.java b/src/main/java/envoy/event/EventHandler.java deleted file mode 100644 index f842476..0000000 --- a/src/main/java/envoy/event/EventHandler.java +++ /dev/null @@ -1,18 +0,0 @@ -package envoy.event; - -/** - * Project: envoy-common
- * File: EventHandler.java
- * Created: 04.12.2019
- * - * @author Kai S. K. Engelbart - */ -public interface EventHandler { - - /** - * Consumes an event dispatched by the event bus. - * - * @param event The event dispatched by the event bus, only of supported type - */ - void handle(Event event); -} diff --git a/src/main/java/envoy/event/HandshakeRejectionEvent.java b/src/main/java/envoy/event/HandshakeRejectionEvent.java index 8c71ffa..5d58aa0 100644 --- a/src/main/java/envoy/event/HandshakeRejectionEvent.java +++ b/src/main/java/envoy/event/HandshakeRejectionEvent.java @@ -28,5 +28,5 @@ public class HandshakeRejectionEvent extends Event { * @param reason the reason why the handshake was rejected * @since Envoy Common v0.3-alpha */ - public HandshakeRejectionEvent(String reason) { super(""); } + public HandshakeRejectionEvent(String reason) { super(reason); } }