diff options
-rw-r--r-- | src/main/java/ftbsc/geb/GEB.java | 21 | ||||
-rw-r--r-- | src/main/java/ftbsc/geb/api/IBus.java | 5 | ||||
-rw-r--r-- | src/main/java/ftbsc/geb/api/IEventDispatcher.java | 7 |
3 files changed, 21 insertions, 12 deletions
diff --git a/src/main/java/ftbsc/geb/GEB.java b/src/main/java/ftbsc/geb/GEB.java index 9946986..58ad079 100644 --- a/src/main/java/ftbsc/geb/GEB.java +++ b/src/main/java/ftbsc/geb/GEB.java @@ -3,8 +3,8 @@ package ftbsc.geb; import ftbsc.geb.api.IBus; import ftbsc.geb.api.IEvent; import ftbsc.geb.api.IEventDispatcher; +import ftbsc.geb.api.IListener; -import java.lang.reflect.Constructor; import java.util.Map; import java.util.ServiceLoader; import java.util.concurrent.ConcurrentHashMap; @@ -21,9 +21,12 @@ public class GEB implements IBus { private final String identifier; /** - * A {@link Map} tying each user-defined event class to its machine-generated implementation. - * In practice, the {@link Class} of the original event is used as key and mapped to each - * class' generated {@link Constructor}. + * A {@link Map} tying each listener class to its instance. + */ + private final Map<Class<? extends IListener>, IListener> listenerMap; + + /** + * A {@link Map} tying each event class to the appropriate dispatcher. */ private final Map<Class<? extends IEvent>, IEventDispatcher> dispatchMap; @@ -33,17 +36,19 @@ public class GEB implements IBus { */ public GEB(String identifier) { this.identifier = identifier; + this.listenerMap = new ConcurrentHashMap<>(); this.dispatchMap = new ConcurrentHashMap<>(); for(IEventDispatcher dispatcher : ServiceLoader.load(IEventDispatcher.class)) dispatchMap.put(dispatcher.eventType(), dispatcher); } /** - * @return the identifier of this bus + * Registers a new listener on the bus. + * @param listener the listener */ @Override - public String getIdentifier() { - return identifier; + public void registerListener(IListener listener) { + this.listenerMap.put(listener.getClass(), listener); } /** @@ -53,6 +58,6 @@ public class GEB implements IBus { */ @Override public boolean handleEvent(IEvent event) { - return dispatchMap.get(event.getClass()).callListeners(this.identifier, event); + return this.dispatchMap.get(event.getClass()).callListeners(this.identifier, event, this.listenerMap); } } diff --git a/src/main/java/ftbsc/geb/api/IBus.java b/src/main/java/ftbsc/geb/api/IBus.java index e75e25c..6a56243 100644 --- a/src/main/java/ftbsc/geb/api/IBus.java +++ b/src/main/java/ftbsc/geb/api/IBus.java @@ -7,9 +7,10 @@ package ftbsc.geb.api; */ public interface IBus { /** - * @return the identifier of this bus + * Registers a new listener on the bus. + * @param listener the listener */ - String getIdentifier(); + void registerListener(IListener listener); /** * Dispatches an event, calling all of its listeners that are subscribed to this bus. diff --git a/src/main/java/ftbsc/geb/api/IEventDispatcher.java b/src/main/java/ftbsc/geb/api/IEventDispatcher.java index 7b3b303..25ec657 100644 --- a/src/main/java/ftbsc/geb/api/IEventDispatcher.java +++ b/src/main/java/ftbsc/geb/api/IEventDispatcher.java @@ -1,5 +1,7 @@ package ftbsc.geb.api; +import java.util.Map; + /** * The interface that the generated dispatchers will all use. * This interface isn't really meant to be used by humans, but it should work if your @@ -11,12 +13,13 @@ public interface IEventDispatcher { * Calls all listeners for the given identifier. * @param identifier the identifier of the bus that's calling this * @param event the event to call + * @param listeners a map mapping each {@link IListener} class to its instance * @return the value {@link IBus#handleEvent(IEvent)} will return for this */ - boolean callListeners(String identifier, IEvent event); + boolean callListeners(String identifier, IEvent event, Map<Class<? extends IListener>, IListener> listeners); /** - * @return the {@link Class} representing the event in question + * @return the {@link Class} representing the event this dispatcher works with */ Class<? extends IEvent> eventType(); } |