From b59ed568b00f0640e9cd3e3e2f7c7610f05cf91b Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sun, 29 Dec 2019 10:53:48 +0200 Subject: [PATCH] Moved event classes to envoy-common --- src/main/java/envoy/data/TextMessage.java | 2 +- src/main/java/envoy/event/Event.java | 19 +++++ src/main/java/envoy/event/EventBus.java | 81 +++++++++++++++++++++ src/main/java/envoy/event/EventHandler.java | 18 +++++ src/main/java/envoy/event/MessageEvent.java | 27 +++++++ 5 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 src/main/java/envoy/event/Event.java create mode 100644 src/main/java/envoy/event/EventBus.java create mode 100644 src/main/java/envoy/event/EventHandler.java create mode 100644 src/main/java/envoy/event/MessageEvent.java diff --git a/src/main/java/envoy/data/TextMessage.java b/src/main/java/envoy/data/TextMessage.java index 2ae7fc1..3e4517b 100644 --- a/src/main/java/envoy/data/TextMessage.java +++ b/src/main/java/envoy/data/TextMessage.java @@ -41,7 +41,7 @@ public class TextMessage extends Message { getId(), getSender(), getRecipient(), - new SimpleDateFormat("dd.MM.yyyy hh:mm:ss").format(getDate()), + new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(getDate()), getStatus(), content); } diff --git a/src/main/java/envoy/event/Event.java b/src/main/java/envoy/event/Event.java new file mode 100644 index 0000000..f17b95b --- /dev/null +++ b/src/main/java/envoy/event/Event.java @@ -0,0 +1,19 @@ +package envoy.event; + +/** + * Project: envoy-common
+ * File: Event.java
+ * Created: 04.12.2019
+ * + * @author Kai S. K. Engelbart + * @param the type of the Event + * @since Envoy v0.2-alpha + */ +public interface Event { + + /** + * @return the data associated with this event + */ + T get(); +} + diff --git a/src/main/java/envoy/event/EventBus.java b/src/main/java/envoy/event/EventBus.java new file mode 100644 index 0000000..f2e5c9f --- /dev/null +++ b/src/main/java/envoy/event/EventBus.java @@ -0,0 +1,81 @@ +package envoy.event; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * This class handles events by allowing {@link EventHandler} object 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
+ * + * @author Kai S. K. Engelbart + * @since Envoy v0.2-alpha + */ +public class EventBus { + + /** + * Contains all {@link EventHandler} instances registered at this + * {@link EventBus} as values mapped to by their supported {@link Event} + * classes. + */ + private Map>, List> handlers = new HashMap<>(); + + /** + * The singleton instance of this {@link EventBus} that is used across the + * entire application. + */ + private static EventBus eventBus = new EventBus(); + + /** + * This constructor is not accessible from outside this class because a + * singleton instance of it is provided by the {@link EventBus#getInstance()} + * method. + */ + private EventBus() {} + + /** + * @return the singleton instance of the {@link EventBus} + * @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. + * + * @param eventClass the class which the {@link EventHandler} is subscribed to + * @param handler the {@link EventHandler} to register + * @since Envoy v0.2-alpha + */ + public void register(Class> eventClass, EventHandler handler) { + if (!handlers.containsKey(eventClass)) handlers.put(eventClass, new ArrayList<>()); + handlers.get(eventClass).add(handler); + } + + /** + * Dispatches a {@link Event} to every {@link EventHandler} 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)); + } + + /** + * @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 + * @since Envoy v0.2-alpha + */ + public Map>, List> getHandlers() { return handlers; } +} diff --git a/src/main/java/envoy/event/EventHandler.java b/src/main/java/envoy/event/EventHandler.java new file mode 100644 index 0000000..f842476 --- /dev/null +++ b/src/main/java/envoy/event/EventHandler.java @@ -0,0 +1,18 @@ +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/MessageEvent.java b/src/main/java/envoy/event/MessageEvent.java new file mode 100644 index 0000000..33a7607 --- /dev/null +++ b/src/main/java/envoy/event/MessageEvent.java @@ -0,0 +1,27 @@ +package envoy.event; + +import envoy.data.Message; + +/** + * Project: envoy-common
+ * File: MessageCreationEvent.java
+ * Created: 4 Dec 2019
+ * + * @author Kai S. K. Engelbart + */ +public class MessageEvent implements Event { + + protected final Message message; + + /** + * Initializes a {@link MessageEvent} conveying information about a + * {@link Message} object. + * + * @param message the {@link Message} object to attach to this event + * @since Envoy v0.2-alpha + */ + public MessageEvent(Message message) { this.message = message; } + + @Override + public Message get() { return message; } +}