From c70416df289c748013b83545ad758b4552b5ae89 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 4 Dec 2019 18:50:06 +0100 Subject: [PATCH] Added event system + Event interface for defining event objects + EventHandler interface for defining event handlers + EventBus singleton class for managing event handlers and dispatching events --- src/main/java/envoy/client/event/Event.java | 16 ++++++++++ .../java/envoy/client/event/EventBus.java | 32 +++++++++++++++++++ .../java/envoy/client/event/EventHandler.java | 25 +++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 src/main/java/envoy/client/event/Event.java create mode 100644 src/main/java/envoy/client/event/EventBus.java create mode 100644 src/main/java/envoy/client/event/EventHandler.java diff --git a/src/main/java/envoy/client/event/Event.java b/src/main/java/envoy/client/event/Event.java new file mode 100644 index 0000000..5d2e8a3 --- /dev/null +++ b/src/main/java/envoy/client/event/Event.java @@ -0,0 +1,16 @@ +package envoy.client.event; + +/** + * Project: envoy-clientChess
+ * File: Event.javaEvent.java
+ * Created: 04.12.2019
+ * + * @author Kai S. K. Engelbart + */ +public interface Event { + + /** + * @return the data associated with this event + */ + T get(); +} diff --git a/src/main/java/envoy/client/event/EventBus.java b/src/main/java/envoy/client/event/EventBus.java new file mode 100644 index 0000000..c97844b --- /dev/null +++ b/src/main/java/envoy/client/event/EventBus.java @@ -0,0 +1,32 @@ +package envoy.client.event; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Project: envoy-clientChess
+ * File: EventBus.javaEvent.java
+ * Created: 04.12.2019
+ * + * @author Kai S. K. Engelbart + */ +public class EventBus { + + private static EventBus eventBus; + + private List handlers = new ArrayList<>(); + + private EventBus() {} + + public EventBus getInstance() { + if (eventBus == null) eventBus = new EventBus(); + return eventBus; + } + + public void register(EventHandler... handlers) { this.handlers.addAll(Arrays.asList(handlers)); } + + public void dispatch(Event event) { handlers.stream().filter(h -> h.supports().contains(event.getClass())).forEach(h -> h.handle(event)); } + + public List getHandlers() { return handlers; } +} diff --git a/src/main/java/envoy/client/event/EventHandler.java b/src/main/java/envoy/client/event/EventHandler.java new file mode 100644 index 0000000..a6e5b81 --- /dev/null +++ b/src/main/java/envoy/client/event/EventHandler.java @@ -0,0 +1,25 @@ +package envoy.client.event; + +import java.util.Set; + +/** + * Project: envoy-clientChess
+ * File: EventHandler.javaEvent.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); + + /** + * @return A set of classes this class is supposed to handle in events + */ + Set>> supports(); +}