mirror of
https://github.com/zaaarf/geb.git
synced 2024-11-22 14:24:49 +01:00
feat: API simplification, allow multiple instances
This commit is contained in:
parent
4badb7ce92
commit
baf61fb98e
4 changed files with 20 additions and 17 deletions
|
@ -7,6 +7,7 @@ import ftbsc.geb.api.IListener;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.ServiceLoader;
|
import java.util.ServiceLoader;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -17,7 +18,7 @@ public class GEB implements IBus {
|
||||||
/**
|
/**
|
||||||
* A {@link Map} tying each listener class to its instance.
|
* A {@link Map} tying each listener class to its instance.
|
||||||
*/
|
*/
|
||||||
private final Map<Class<? extends IListener>, IListener> listenerMap;
|
private final Map<Class<? extends IListener>, Set<IListener>> listenerMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link Map} tying each event class to the appropriate dispatcher.
|
* A {@link Map} tying each event class to the appropriate dispatcher.
|
||||||
|
@ -31,7 +32,7 @@ public class GEB implements IBus {
|
||||||
this.listenerMap = new ConcurrentHashMap<>();
|
this.listenerMap = new ConcurrentHashMap<>();
|
||||||
this.dispatchMap = new ConcurrentHashMap<>();
|
this.dispatchMap = new ConcurrentHashMap<>();
|
||||||
for(IEventDispatcher dispatcher : ServiceLoader.load(IEventDispatcher.class))
|
for(IEventDispatcher dispatcher : ServiceLoader.load(IEventDispatcher.class))
|
||||||
dispatchMap.put(dispatcher.eventType(), dispatcher);
|
this.dispatchMap.put(dispatcher.eventType(), dispatcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -40,7 +41,10 @@ public class GEB implements IBus {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void registerListener(IListener listener) {
|
public void registerListener(IListener listener) {
|
||||||
this.listenerMap.put(listener.getClass(), listener);
|
this.listenerMap.putIfAbsent(
|
||||||
|
listener.getClass(),
|
||||||
|
ConcurrentHashMap.newKeySet()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,13 +53,19 @@ public class GEB implements IBus {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void unregisterListener(IListener listener) {
|
public void unregisterListener(IListener listener) {
|
||||||
this.listenerMap.remove(listener.getClass());
|
this.listenerMap.computeIfPresent(
|
||||||
|
listener.getClass(),
|
||||||
|
(k, l) -> {
|
||||||
|
l.remove(listener);
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispatches an event, calling all of its listeners that are subscribed to this bus.
|
* Dispatches an event, calling all of its listeners that are subscribed to this bus.
|
||||||
* @param event the event to fire
|
* @param event the event to fire
|
||||||
* @return true if the event was canceled, false otherwise
|
* @return false if the event was canceled, true otherwise
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean handleEvent(IEvent event) {
|
public boolean handleEvent(IEvent event) {
|
||||||
|
|
|
@ -14,9 +14,6 @@ public interface IBus {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unregister a listener from the bus.
|
* Unregister a listener from the bus.
|
||||||
* While sensible implementations can get this quite fast, it's generally
|
|
||||||
* faster to use {@link IListener#isActive()}, so only use this if you
|
|
||||||
* *mean* to unregister for good.
|
|
||||||
* @param listener the listener
|
* @param listener the listener
|
||||||
*/
|
*/
|
||||||
void unregisterListener(IListener listener);
|
void unregisterListener(IListener listener);
|
||||||
|
@ -24,7 +21,7 @@ public interface IBus {
|
||||||
/**
|
/**
|
||||||
* Dispatches an event, calling all of its listeners that are subscribed to this bus.
|
* Dispatches an event, calling all of its listeners that are subscribed to this bus.
|
||||||
* @param event the event to fire
|
* @param event the event to fire
|
||||||
* @return true if the event was canceled, false otherwise
|
* @return false if the event was canceled, true otherwise
|
||||||
*/
|
*/
|
||||||
boolean handleEvent(IEvent event);
|
boolean handleEvent(IEvent event);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package ftbsc.geb.api;
|
package ftbsc.geb.api;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The interface that the generated dispatchers will all use.
|
* The interface that the generated dispatchers will all use.
|
||||||
|
@ -12,10 +13,10 @@ public interface IEventDispatcher {
|
||||||
/**
|
/**
|
||||||
* Calls all listeners for the given event.
|
* Calls all listeners for the given event.
|
||||||
* @param event the event to call
|
* @param event the event to call
|
||||||
* @param listeners a map mapping each {@link IListener} class to its instance
|
* @param listeners a map mapping each {@link IListener} class to its instances
|
||||||
* @return the value {@link IBus#handleEvent(IEvent)} will return for this
|
* @return the value {@link IBus#handleEvent(IEvent)} will return for this
|
||||||
*/
|
*/
|
||||||
boolean callListeners(IEvent event, Map<Class<? extends IListener>, IListener> listeners);
|
boolean callListeners(IEvent event, Map<Class<? extends IListener>, Set<IListener>> listeners);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the {@link Class} representing the event this dispatcher works with
|
* @return the {@link Class} representing the event this dispatcher works with
|
||||||
|
|
|
@ -4,9 +4,4 @@ package ftbsc.geb.api;
|
||||||
* The common interface for all classes that contain GEB listeners.
|
* The common interface for all classes that contain GEB listeners.
|
||||||
* @since 0.1.0
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
public interface IListener {
|
public interface IListener {}
|
||||||
/**
|
|
||||||
* @return whether the listeners in this class should be considered to be active
|
|
||||||
*/
|
|
||||||
boolean isActive();
|
|
||||||
}
|
|
Loading…
Reference in a new issue