Replace the internal event bus with Event Bus 0.0.3
The Event class has been retrofitted to implement IEvent, so that no event implementations had to be changed.
This commit is contained in:
@ -2,7 +2,13 @@ package envoy.event;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import dev.kske.eventbus.IEvent;
|
||||
|
||||
/**
|
||||
* This class serves as a convenience base class for all events. It implements
|
||||
* the {@link IEvent} interface and provides a generic value. For events without
|
||||
* a value there also is {@link envoy.event.Event.Valueless}.
|
||||
* <p>
|
||||
* Project: <strong>envoy-common</strong><br>
|
||||
* File: <strong>Event.java</strong><br>
|
||||
* Created: <strong>04.12.2019</strong><br>
|
||||
@ -11,7 +17,7 @@ import java.io.Serializable;
|
||||
* @param <T> the type of the Event
|
||||
* @since Envoy v0.2-alpha
|
||||
*/
|
||||
public abstract class Event<T> implements Serializable {
|
||||
public abstract class Event<T> implements IEvent, Serializable {
|
||||
|
||||
protected final T value;
|
||||
|
||||
|
@ -1,82 +0,0 @@
|
||||
package envoy.event;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* 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>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since Envoy v0.2-alpha
|
||||
*/
|
||||
public final class EventBus {
|
||||
|
||||
/**
|
||||
* 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<Consumer<Event<?>>>> handlers = new HashMap<>();
|
||||
|
||||
/**
|
||||
* The singleton instance of this event bus 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 event bus
|
||||
* @since Envoy v0.2-alpha
|
||||
*/
|
||||
public static EventBus getInstance() { return eventBus; }
|
||||
|
||||
/**
|
||||
* Registers an event handler to be notified when an
|
||||
* event of a certain type is dispatched.
|
||||
*
|
||||
* @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 <T extends Event<?>> void register(Class<T> eventClass, Consumer<T> handler) {
|
||||
if (!handlers.containsKey(eventClass)) handlers.put(eventClass, new ArrayList<>());
|
||||
handlers.get(eventClass).add((Consumer<Event<?>>) handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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()::equals)
|
||||
.map(handlers::get)
|
||||
.flatMap(List::stream)
|
||||
.forEach(h -> h.accept(event));
|
||||
}
|
||||
|
||||
/**
|
||||
* @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<Consumer<Event<?>>>> getHandlers() { return handlers; }
|
||||
}
|
@ -16,4 +16,5 @@ module envoy.common {
|
||||
exports envoy.event.contact;
|
||||
|
||||
requires transitive java.logging;
|
||||
requires transitive dev.kske.eventbus;
|
||||
}
|
||||
|
Reference in New Issue
Block a user