mirror of
https://github.com/zaaarf/geb.git
synced 2024-11-25 11:04:48 +01:00
feat: presumably completed the first version of the api
This commit is contained in:
parent
fcea6fee70
commit
0dbbe17261
8 changed files with 175 additions and 8 deletions
59
src/main/java/ftbsc/geb/api/GEB.java
Normal file
59
src/main/java/ftbsc/geb/api/GEB.java
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
22
src/main/java/ftbsc/geb/api/IEventCancelable.java
Normal file
22
src/main/java/ftbsc/geb/api/IEventCancelable.java
Normal file
|
@ -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() {}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
|
17
src/main/java/ftbsc/geb/api/annotations/BusInstance.java
Normal file
17
src/main/java/ftbsc/geb/api/annotations/BusInstance.java
Normal file
|
@ -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 {}
|
|
@ -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 {}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {}
|
Loading…
Reference in a new issue