summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/ftbsc/geb/api/GEB.java59
-rw-r--r--src/main/java/ftbsc/geb/api/IEvent.java22
-rw-r--r--src/main/java/ftbsc/geb/api/IEventCancelable.java22
-rw-r--r--src/main/java/ftbsc/geb/api/IListener.java9
-rw-r--r--src/main/java/ftbsc/geb/api/annotations/BusInstance.java17
-rw-r--r--src/main/java/ftbsc/geb/api/annotations/Event.java13
-rw-r--r--src/main/java/ftbsc/geb/api/annotations/Listen.java24
-rw-r--r--src/main/java/ftbsc/geb/api/annotations/ListenerInstance.java17
8 files changed, 175 insertions, 8 deletions
diff --git a/src/main/java/ftbsc/geb/api/GEB.java b/src/main/java/ftbsc/geb/api/GEB.java
new file mode 100644
index 0000000..5d65511
--- /dev/null
+++ b/src/main/java/ftbsc/geb/api/GEB.java
@@ -0,0 +1,59 @@
+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<Class<? extends IEvent>, Constructor<? extends IEvent>> 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/IEvent.java b/src/main/java/ftbsc/geb/api/IEvent.java
index d0c4815..d75ed15 100644
--- a/src/main/java/ftbsc/geb/api/IEvent.java
+++ b/src/main/java/ftbsc/geb/api/IEvent.java
@@ -1,9 +1,27 @@
package ftbsc.geb.api;
+/**
+ * The common interface for all GEB events.
+ * @since 0.1.0
+ */
public interface IEvent {
- default boolean isCanceled() {
+ /**
+ * 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 GEB#handleEvent(IEvent)} will return for this
+ */
+ default boolean callListeners(String identifier) {
return false;
}
- void dispatch();
+ /**
+ * 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;
+ }
}
diff --git a/src/main/java/ftbsc/geb/api/IEventCancelable.java b/src/main/java/ftbsc/geb/api/IEventCancelable.java
new file mode 100644
index 0000000..32669f2
--- /dev/null
+++ b/src/main/java/ftbsc/geb/api/IEventCancelable.java
@@ -0,0 +1,22 @@
+package ftbsc.geb.api;
+
+/**
+ * The common interface for all cancelable GEB events.
+ * @since 0.1.0
+ */
+public interface IEventCancelable extends IEvent {
+ /**
+ * Checks whether the event was canceled; any user-defined
+ * implementation will be ignored.
+ * @return whether the event was canceled
+ */
+ default boolean isCanceled() {
+ return false;
+ }
+
+ /**
+ * Cancels the event. Any user-defined implementation will
+ * be ignored.
+ */
+ default void setCanceled() {}
+}
diff --git a/src/main/java/ftbsc/geb/api/IListener.java b/src/main/java/ftbsc/geb/api/IListener.java
index 9691342..fa8978f 100644
--- a/src/main/java/ftbsc/geb/api/IListener.java
+++ b/src/main/java/ftbsc/geb/api/IListener.java
@@ -1,5 +1,12 @@
package ftbsc.geb.api;
+/**
+ * The common interface for all classes that contain GEB listeners.
+ * @since 0.1.0
+ */
public interface IListener {
- boolean active();
+ /**
+ * @return whether the listeners in this class should be considered to be active
+ */
+ boolean isActive();
}
diff --git a/src/main/java/ftbsc/geb/api/annotations/BusInstance.java b/src/main/java/ftbsc/geb/api/annotations/BusInstance.java
new file mode 100644
index 0000000..b785b15
--- /dev/null
+++ b/src/main/java/ftbsc/geb/api/annotations/BusInstance.java
@@ -0,0 +1,17 @@
+package ftbsc.geb.api.annotations;
+
+import ftbsc.geb.api.GEB;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * This annotation should mark either a static instance of {@link GEB}
+ * or a static method returning one.
+ * @since 0.1.0
+ */
+@Target({ElementType.FIELD, ElementType.METHOD})
+@Retention(RetentionPolicy.CLASS)
+public @interface BusInstance {}
diff --git a/src/main/java/ftbsc/geb/api/annotations/Event.java b/src/main/java/ftbsc/geb/api/annotations/Event.java
index 780f361..ea1724c 100644
--- a/src/main/java/ftbsc/geb/api/annotations/Event.java
+++ b/src/main/java/ftbsc/geb/api/annotations/Event.java
@@ -1,8 +1,17 @@
package ftbsc.geb.api.annotations;
+import ftbsc.geb.api.IEvent;
+
import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+/**
+ * Marks a class as an Event. It should implement the {@link IEvent} interface.
+ * It doesn't need to be abstract, but it can never be final.
+ * @since 0.1.0
+ */
@Target(ElementType.TYPE)
-public @interface Event {
-}
+@Retention(RetentionPolicy.CLASS)
+public @interface Event {}
diff --git a/src/main/java/ftbsc/geb/api/annotations/Listen.java b/src/main/java/ftbsc/geb/api/annotations/Listen.java
index f8398ed..216937a 100644
--- a/src/main/java/ftbsc/geb/api/annotations/Listen.java
+++ b/src/main/java/ftbsc/geb/api/annotations/Listen.java
@@ -1,14 +1,32 @@
package ftbsc.geb.api.annotations;
+import ftbsc.geb.api.IEvent;
+import ftbsc.geb.api.IListener;
+
import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
- * Marks the method as a listener.
- * Its parent must implement the {@link ftbsc.geb.api.IListener} interface.
- * The method should only take a single input value, an event.
+ * Marks the method as a listener. Its parent must implement the {@link IListener} interface.
+ * The annotated method should only take a single input value, an instance of {@link IEvent};
+ * it should be either void or boolean; if it's boolean, the return value indicates whether
+ * the event was canceled.
+ * @since 0.1.0
*/
@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.CLASS)
public @interface Listen {
+ /**
+ * @return an integer indicating priority level for the listener, defaulting to 0
+ */
int priority() default 0;
+
+ /**
+ * @return an array of {@link String}s specifying which buses they should be listening on;
+ * an empty array means that they should listen on all buses, ignoring identifiers:
+ * that's probably what you wanted anyway.
+ */
+ String[] on() default {}; //empty array = listen on all of them
}
diff --git a/src/main/java/ftbsc/geb/api/annotations/ListenerInstance.java b/src/main/java/ftbsc/geb/api/annotations/ListenerInstance.java
new file mode 100644
index 0000000..c386592
--- /dev/null
+++ b/src/main/java/ftbsc/geb/api/annotations/ListenerInstance.java
@@ -0,0 +1,17 @@
+package ftbsc.geb.api.annotations;
+
+import ftbsc.geb.api.IListener;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * This annotation should mark either be a static instance of {@link IListener}
+ * or a static method returning one.
+ * @since 0.1.0
+ */
+@Target({ElementType.FIELD, ElementType.METHOD})
+@Retention(RetentionPolicy.CLASS)
+public @interface ListenerInstance {}