Clean-up, improved generic type usage in EventBus

Fixes #13
This commit is contained in:
Kai S. K. Engelbart 2020-02-12 05:58:40 +01:00
parent c6a53487be
commit 45d77ef9b9
5 changed files with 26 additions and 47 deletions

View File

@ -22,7 +22,7 @@ public class ContactSearchResult extends Event<List<User>> {
* Creates an instance of {@link ContactSearchResult}. * Creates an instance of {@link ContactSearchResult}.
* *
* @param users the users found during the search * @param users the users found during the search
* @since Envoy Common v0.2-alpha
*/ */
public ContactSearchResult(List<User> users) { super(users); } public ContactSearchResult(List<User> users) { super(users); }
} }

View File

@ -25,7 +25,7 @@ public abstract class Event<T> implements Serializable {
public T get() { return value; } public T get() { return value; }
@Override @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.<br> * Serves as a super class for events that do not carry a value.<br>

View File

@ -1,18 +1,15 @@
package envoy.event; package envoy.event;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap; import java.util.function.Consumer;
import java.util.List;
import java.util.Map;
/** /**
* This class handles events by allowing {@link EventHandler} object to register * This class handles events by allowing event handlers to register themselves
* themselves and then be notified about certain events dispatched by the event * and then be notified about certain events dispatched by the event bus.<br>
* bus.<br>
* <br> * <br>
* The event bus is a singleton and can be used across the entire application to * The event bus is a singleton and can be used across the entire application to
* guarantee the propagation of events.<br> * guarantee the propagation of events.<br>
* * <br>
* Project: <strong>envoy-common</strong><br> * Project: <strong>envoy-common</strong><br>
* File: <strong>EventBus.java</strong><br> * File: <strong>EventBus.java</strong><br>
* Created: <strong>04.12.2019</strong><br> * Created: <strong>04.12.2019</strong><br>
@ -23,14 +20,13 @@ import java.util.Map;
public class EventBus { public class EventBus {
/** /**
* Contains all {@link EventHandler} instances registered at this * Contains all event handler instances registered at this event bus as values
* {@link EventBus} as values mapped to by their supported {@link Event} * mapped to by their supported event classes.
* 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. * entire application.
*/ */
private static EventBus eventBus = new EventBus(); private static EventBus eventBus = new EventBus();
@ -43,39 +39,40 @@ public class EventBus {
private EventBus() {} private EventBus() {}
/** /**
* @return the singleton instance of the {@link EventBus} * @return the singleton instance of the event bus
* @since Envoy v0.2-alpha * @since Envoy v0.2-alpha
*/ */
public static EventBus getInstance() { return eventBus; } public static EventBus getInstance() { return eventBus; }
/** /**
* Registers an {@link EventHandler} to be notified when a * Registers an event handler to be notified when an
* {@link Event} of a certain type is dispatched. * event of a certain type is dispatched.
* *
* @param eventClass the class which the {@link EventHandler} is subscribed to * @param <T> the type of event values to notify the handler about
* @param handler the {@link EventHandler} to register * @param eventClass the class which the event handler is subscribing to
* @param handler the event handler to register
* @since Envoy v0.2-alpha * @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<>()); 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 * @param event the {@link Event} to dispatch
* @since Envoy v0.2-alpha * @since Envoy v0.2-alpha
*/ */
public void dispatch(Event<?> event) { 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 * @return a map of all event handler instances currently registered at this
* this {@link EventBus} with the {@link Event} classes they are * event bus with the event classes they are subscribed to as keys
* subscribed to as keys
* @since Envoy v0.2-alpha * @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; }
} }

View File

@ -1,18 +0,0 @@
package envoy.event;
/**
* Project: <strong>envoy-common</strong><br>
* File: <strong>EventHandler.java</strong><br>
* Created: <strong>04.12.2019</strong><br>
*
* @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);
}

View File

@ -28,5 +28,5 @@ public class HandshakeRejectionEvent extends Event<String> {
* @param reason the reason why the handshake was rejected * @param reason the reason why the handshake was rejected
* @since Envoy Common v0.3-alpha * @since Envoy Common v0.3-alpha
*/ */
public HandshakeRejectionEvent(String reason) { super(""); } public HandshakeRejectionEvent(String reason) { super(reason); }
} }