|
|
|
@ -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.<br>
|
|
|
|
|
* This class handles events by allowing event handlers to register themselves
|
|
|
|
|
* and then be notified about certain events dispatched by the event bus.<br>
|
|
|
|
|
* <br>
|
|
|
|
|
* The event bus is a singleton and can be used across the entire application to
|
|
|
|
|
* guarantee the propagation of events.<br>
|
|
|
|
|
*
|
|
|
|
|
* <br>
|
|
|
|
|
* Project: <strong>envoy-common</strong><br>
|
|
|
|
|
* File: <strong>EventBus.java</strong><br>
|
|
|
|
|
* Created: <strong>04.12.2019</strong><br>
|
|
|
|
@ -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<Class<? extends Event<?>>, List<EventHandler>> handlers = new HashMap<>();
|
|
|
|
|
private Map<Class<? extends Event<?>>, List<Consumer<Event<?>>>> 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 <T> 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<? extends Event<?>> eventClass, EventHandler handler) {
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public <T extends Event<?>> void register(Class<T> eventClass, Consumer<T> handler) {
|
|
|
|
|
if (!handlers.containsKey(eventClass)) handlers.put(eventClass, new ArrayList<>());
|
|
|
|
|
handlers.get(eventClass).add(handler);
|
|
|
|
|
handlers.get(eventClass).add((Consumer<Event<?>>) 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<Class<? extends Event<?>>, List<EventHandler>> getHandlers() { return handlers; }
|
|
|
|
|
public Map<Class<? extends Event<?>>, List<Consumer<Event<?>>>> getHandlers() { return handlers; }
|
|
|
|
|
}
|
|
|
|
|