diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/ftbsc/geb/GEB.java | 18 | ||||
-rw-r--r-- | src/main/java/ftbsc/geb/api/IEvent.java | 23 | ||||
-rw-r--r-- | src/main/java/ftbsc/geb/api/IEventDispatcher.java | 23 |
3 files changed, 31 insertions, 33 deletions
diff --git a/src/main/java/ftbsc/geb/GEB.java b/src/main/java/ftbsc/geb/GEB.java index 123447b..9946986 100644 --- a/src/main/java/ftbsc/geb/GEB.java +++ b/src/main/java/ftbsc/geb/GEB.java @@ -2,6 +2,7 @@ package ftbsc.geb; import ftbsc.geb.api.IBus; import ftbsc.geb.api.IEvent; +import ftbsc.geb.api.IEventDispatcher; import java.lang.reflect.Constructor; import java.util.Map; @@ -24,7 +25,7 @@ public class GEB implements IBus { * In practice, the {@link Class} of the original event is used as key and mapped to each * class' generated {@link Constructor}. */ - private final Map<Class<? extends IEvent>, Constructor<? extends IEvent>> eventMapper; + private final Map<Class<? extends IEvent>, IEventDispatcher> dispatchMap; /** * The public constructor. @@ -32,12 +33,9 @@ public class GEB implements IBus { */ public GEB(String identifier) { this.identifier = identifier; - this.eventMapper = new ConcurrentHashMap<>(); - for(IEvent event : ServiceLoader.load(IEvent.class)) { - try { - eventMapper.put(event.getOriginalEvent(), event.getClass().getConstructor(event.getClass())); - } catch(NoSuchMethodException ignored) {} //should never happen, this code is machine-generated - } + this.dispatchMap = new ConcurrentHashMap<>(); + for(IEventDispatcher dispatcher : ServiceLoader.load(IEventDispatcher.class)) + dispatchMap.put(dispatcher.eventType(), dispatcher); } /** @@ -50,15 +48,11 @@ public class GEB implements IBus { /** * Dispatches an event, calling all of its listeners that are subscribed to this bus. - * TODO: make this as efficient as possible * @param event the event to fire * @return true if the event was canceled, false otherwise */ @Override public boolean handleEvent(IEvent event) { - try { - return eventMapper.get(event.getClass()).newInstance(event).callListeners(this.getIdentifier()); - } catch(ReflectiveOperationException ignored) {} //should never happen - return false; + return dispatchMap.get(event.getClass()).callListeners(this.identifier, event); } } diff --git a/src/main/java/ftbsc/geb/api/IEvent.java b/src/main/java/ftbsc/geb/api/IEvent.java index fd2a343..34c45db 100644 --- a/src/main/java/ftbsc/geb/api/IEvent.java +++ b/src/main/java/ftbsc/geb/api/IEvent.java @@ -2,26 +2,7 @@ package ftbsc.geb.api; /** * The common interface for all GEB events. + * It doesn't do anything special, just provide a common superclass. * @since 0.1.0 */ -public interface IEvent { - /** - * Any custom implementation of this method will be called just before the - * calls to listeners, but its return values will be ignored. You probably - * don't want to touch this. - * @param identifier the identifier of the bus that's calling this - * @return the value {@link IBus#handleEvent(IEvent)} will return for this - */ - default boolean callListeners(String identifier) { - return false; - } - - /** - * Fetches the {@link Class} containing the original implementation of this. - * If you wish to override this, make your override final. - * @return the class of the original, non-extended superclass - */ - default Class<? extends IEvent> getOriginalEvent() { - return IEvent.class; - } -} +public interface IEvent {} diff --git a/src/main/java/ftbsc/geb/api/IEventDispatcher.java b/src/main/java/ftbsc/geb/api/IEventDispatcher.java new file mode 100644 index 0000000..bdd7991 --- /dev/null +++ b/src/main/java/ftbsc/geb/api/IEventDispatcher.java @@ -0,0 +1,23 @@ +package ftbsc.geb.api; + +/** + * 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 + * use case requires it. + * @param <E> the type of event this dispatcher handles + * @since 0.1.1 + */ +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 + * @return the value {@link IBus#handleEvent(IEvent)} will return for this + */ + boolean callListeners(String identifier, IEvent event); + + /** + * @return the {@link Class} representing the event in question + */ + Class<? extends IEvent> eventType(); +} |