From d48726c80f0edb159f8df80d10267ad26a3efe03 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Fri, 18 Aug 2023 18:12:58 +0200 Subject: feat: IBus interface --- src/main/java/ftbsc/geb/GEB.java | 64 ++++++++++++++++++++++ src/main/java/ftbsc/geb/api/GEB.java | 59 -------------------- src/main/java/ftbsc/geb/api/IBus.java | 20 +++++++ src/main/java/ftbsc/geb/api/IEvent.java | 2 +- .../ftbsc/geb/api/annotations/BusInstance.java | 4 +- 5 files changed, 87 insertions(+), 62 deletions(-) create mode 100644 src/main/java/ftbsc/geb/GEB.java delete mode 100644 src/main/java/ftbsc/geb/api/GEB.java create mode 100644 src/main/java/ftbsc/geb/api/IBus.java diff --git a/src/main/java/ftbsc/geb/GEB.java b/src/main/java/ftbsc/geb/GEB.java new file mode 100644 index 0000000..123447b --- /dev/null +++ b/src/main/java/ftbsc/geb/GEB.java @@ -0,0 +1,64 @@ +package ftbsc.geb; + +import ftbsc.geb.api.IBus; +import ftbsc.geb.api.IEvent; + +import java.lang.reflect.Constructor; +import java.util.Map; +import java.util.ServiceLoader; +import java.util.concurrent.ConcurrentHashMap; + +/** + * The official GEB implementation of {@link IBus}. + * @since 0.1.0 + */ +public class GEB implements IBus { + + /** + * The identifier of this bus. Methods + */ + 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}. + */ + private final Map, Constructor> eventMapper; + + /** + * The public constructor. + * @param identifier a {@link String} uniquely identifying this bus + */ + 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 + } + } + + /** + * @return the identifier of this bus + */ + @Override + public String getIdentifier() { + return identifier; + } + + /** + * 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; + } +} diff --git a/src/main/java/ftbsc/geb/api/GEB.java b/src/main/java/ftbsc/geb/api/GEB.java deleted file mode 100644 index 5d65511..0000000 --- a/src/main/java/ftbsc/geb/api/GEB.java +++ /dev/null @@ -1,59 +0,0 @@ -package ftbsc.geb.api; - -import java.lang.reflect.Constructor; -import java.util.Map; -import java.util.ServiceLoader; -import java.util.concurrent.ConcurrentHashMap; - -/** - * The main event bus class. - * @since 0.1.0 - */ -public class GEB { - - /** - * The identifier of this bus. Methods - */ - 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}. - */ - private final Map, Constructor> eventMapper; - - /** - * The public constructor. - * @param identifier a {@link String} uniquely identifying this bus - */ - 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 - } - } - - /** - * @return the identifier of this bus - */ - public String getIdentifier() { - return identifier; - } - - /** - * 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 - */ - public boolean handleEvent(IEvent event) { - try { - return eventMapper.get(event.getClass()).newInstance(event).callListeners(this.getIdentifier()); - } catch(ReflectiveOperationException ignored) {} //should never happen - return false; - } -} diff --git a/src/main/java/ftbsc/geb/api/IBus.java b/src/main/java/ftbsc/geb/api/IBus.java new file mode 100644 index 0000000..e75e25c --- /dev/null +++ b/src/main/java/ftbsc/geb/api/IBus.java @@ -0,0 +1,20 @@ +package ftbsc.geb.api; + +/** + * A generic interface for a bus that can work with this + * event system. + * @since 0.1.0 + */ +public interface IBus { + /** + * @return the identifier of this bus + */ + String getIdentifier(); + + /** + * Dispatches an event, calling all of its listeners that are subscribed to this bus. + * @param event the event to fire + * @return true if the event was canceled, false otherwise + */ + boolean handleEvent(IEvent event); +} diff --git a/src/main/java/ftbsc/geb/api/IEvent.java b/src/main/java/ftbsc/geb/api/IEvent.java index d75ed15..fd2a343 100644 --- a/src/main/java/ftbsc/geb/api/IEvent.java +++ b/src/main/java/ftbsc/geb/api/IEvent.java @@ -10,7 +10,7 @@ public interface IEvent { * 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 GEB#handleEvent(IEvent)} will return for this + * @return the value {@link IBus#handleEvent(IEvent)} will return for this */ default boolean callListeners(String identifier) { return false; diff --git a/src/main/java/ftbsc/geb/api/annotations/BusInstance.java b/src/main/java/ftbsc/geb/api/annotations/BusInstance.java index b785b15..cacab39 100644 --- a/src/main/java/ftbsc/geb/api/annotations/BusInstance.java +++ b/src/main/java/ftbsc/geb/api/annotations/BusInstance.java @@ -1,6 +1,6 @@ package ftbsc.geb.api.annotations; -import ftbsc.geb.api.GEB; +import ftbsc.geb.api.IBus; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -8,7 +8,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * This annotation should mark either a static instance of {@link GEB} + * This annotation should mark either a static instance of {@link IBus} * or a static method returning one. * @since 0.1.0 */ -- cgit v1.2.3-56-ga3b1