mirror of
https://github.com/zaaarf/geb.git
synced 2024-11-22 16:44:49 +01:00
Compare commits
No commits in common. "755a8e3a91153c865e95c3753318bd60bab49270" and "4badb7ce926f0a65cc3485bdd53081ab4aff25b0" have entirely different histories.
755a8e3a91
...
4badb7ce92
5 changed files with 19 additions and 22 deletions
|
@ -4,7 +4,7 @@ plugins {
|
|||
}
|
||||
|
||||
archivesBaseName = 'geb'
|
||||
version = versionDetails().lastTag
|
||||
version = gitVersion().split('-').getAt(0).replace('dirty', '')
|
||||
|
||||
java {
|
||||
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
|
||||
|
|
|
@ -7,7 +7,6 @@ import ftbsc.geb.api.IListener;
|
|||
|
||||
import java.util.Map;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
|
@ -18,7 +17,7 @@ public class GEB implements IBus {
|
|||
/**
|
||||
* A {@link Map} tying each listener class to its instance.
|
||||
*/
|
||||
private final Map<Class<? extends IListener>, Set<IListener>> listenerMap;
|
||||
private final Map<Class<? extends IListener>, IListener> listenerMap;
|
||||
|
||||
/**
|
||||
* A {@link Map} tying each event class to the appropriate dispatcher.
|
||||
|
@ -32,7 +31,7 @@ public class GEB implements IBus {
|
|||
this.listenerMap = new ConcurrentHashMap<>();
|
||||
this.dispatchMap = new ConcurrentHashMap<>();
|
||||
for(IEventDispatcher dispatcher : ServiceLoader.load(IEventDispatcher.class))
|
||||
this.dispatchMap.put(dispatcher.eventType(), dispatcher);
|
||||
dispatchMap.put(dispatcher.eventType(), dispatcher);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -41,10 +40,7 @@ public class GEB implements IBus {
|
|||
*/
|
||||
@Override
|
||||
public void registerListener(IListener listener) {
|
||||
this.listenerMap.putIfAbsent(
|
||||
listener.getClass(),
|
||||
ConcurrentHashMap.newKeySet()
|
||||
);
|
||||
this.listenerMap.put(listener.getClass(), listener);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -53,19 +49,13 @@ public class GEB implements IBus {
|
|||
*/
|
||||
@Override
|
||||
public void unregisterListener(IListener listener) {
|
||||
this.listenerMap.computeIfPresent(
|
||||
listener.getClass(),
|
||||
(k, l) -> {
|
||||
l.remove(listener);
|
||||
return l;
|
||||
}
|
||||
);
|
||||
this.listenerMap.remove(listener.getClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatches an event, calling all of its listeners that are subscribed to this bus.
|
||||
* @param event the event to fire
|
||||
* @return false if the event was canceled, true otherwise
|
||||
* @return true if the event was canceled, false otherwise
|
||||
*/
|
||||
@Override
|
||||
public boolean handleEvent(IEvent event) {
|
||||
|
|
|
@ -14,6 +14,9 @@ public interface IBus {
|
|||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
void unregisterListener(IListener listener);
|
||||
|
@ -21,7 +24,7 @@ public interface IBus {
|
|||
/**
|
||||
* Dispatches an event, calling all of its listeners that are subscribed to this bus.
|
||||
* @param event the event to fire
|
||||
* @return false if the event was canceled, true otherwise
|
||||
* @return true if the event was canceled, false otherwise
|
||||
*/
|
||||
boolean handleEvent(IEvent event);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package ftbsc.geb.api;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* The interface that the generated dispatchers will all use.
|
||||
|
@ -13,10 +12,10 @@ public interface IEventDispatcher {
|
|||
/**
|
||||
* Calls all listeners for the given event.
|
||||
* @param event the event to call
|
||||
* @param listeners a map mapping each {@link IListener} class to its instances
|
||||
* @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(IEvent event, Map<Class<? extends IListener>, Set<IListener>> listeners);
|
||||
boolean callListeners(IEvent event, Map<Class<? extends IListener>, IListener> listeners);
|
||||
|
||||
/**
|
||||
* @return the {@link Class} representing the event this dispatcher works with
|
||||
|
|
|
@ -4,4 +4,9 @@ package ftbsc.geb.api;
|
|||
* The common interface for all classes that contain GEB listeners.
|
||||
* @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