feat: made more efficient by using a separate interface for dispatchers

This commit is contained in:
zaaarf 2023-08-23 22:57:12 +02:00
parent b5338abcad
commit eb63d98240
No known key found for this signature in database
GPG key ID: 6445A5CD15E5B40C
3 changed files with 31 additions and 33 deletions

View file

@ -2,6 +2,7 @@ package ftbsc.geb;
import ftbsc.geb.api.IBus; import ftbsc.geb.api.IBus;
import ftbsc.geb.api.IEvent; import ftbsc.geb.api.IEvent;
import ftbsc.geb.api.IEventDispatcher;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.util.Map; 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 * In practice, the {@link Class} of the original event is used as key and mapped to each
* class' generated {@link Constructor}. * 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. * The public constructor.
@ -32,12 +33,9 @@ public class GEB implements IBus {
*/ */
public GEB(String identifier) { public GEB(String identifier) {
this.identifier = identifier; this.identifier = identifier;
this.eventMapper = new ConcurrentHashMap<>(); this.dispatchMap = new ConcurrentHashMap<>();
for(IEvent event : ServiceLoader.load(IEvent.class)) { for(IEventDispatcher dispatcher : ServiceLoader.load(IEventDispatcher.class))
try { dispatchMap.put(dispatcher.eventType(), dispatcher);
eventMapper.put(event.getOriginalEvent(), event.getClass().getConstructor(event.getClass()));
} catch(NoSuchMethodException ignored) {} //should never happen, this code is machine-generated
}
} }
/** /**
@ -50,15 +48,11 @@ public class GEB implements IBus {
/** /**
* Dispatches an event, calling all of its listeners that are subscribed to this bus. * 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 * @param event the event to fire
* @return true if the event was canceled, false otherwise * @return true if the event was canceled, false otherwise
*/ */
@Override @Override
public boolean handleEvent(IEvent event) { public boolean handleEvent(IEvent event) {
try { return dispatchMap.get(event.getClass()).callListeners(this.identifier, event);
return eventMapper.get(event.getClass()).newInstance(event).callListeners(this.getIdentifier());
} catch(ReflectiveOperationException ignored) {} //should never happen
return false;
} }
} }

View file

@ -2,26 +2,7 @@ package ftbsc.geb.api;
/** /**
* The common interface for all GEB events. * The common interface for all GEB events.
* It doesn't do anything special, just provide a common superclass.
* @since 0.1.0 * @since 0.1.0
*/ */
public interface IEvent { 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;
}
}

View file

@ -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();
}