mirror of
https://github.com/zaaarf/geb.git
synced 2024-11-22 08:44:50 +01:00
feat: implemented listener registration
This commit is contained in:
parent
3438b7a38f
commit
e24269a30b
3 changed files with 21 additions and 12 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue