'cached' is probably a much better word

This commit is contained in:
JeremyStar™ 2024-06-11 23:06:41 +02:00
parent 6be1f7f2cf
commit c5f6726ff8
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D

View file

@ -46,11 +46,11 @@ import java.util.*;
@SuppressWarnings({ "unused" }) @SuppressWarnings({ "unused" })
public class EventHelper { public class EventHelper {
/** /**
* Contains all precomputed event listeners. * Contains all cached event listeners.
* *
* @since 1-alpha0 * @since 1-alpha0
*/ */
private static final HashMap<@NotNull Class<? extends Event>, LinkedList<@NotNull Method>> precomputedEventListeners = new HashMap<>(); private static final HashMap<@NotNull Class<? extends Event>, LinkedList<@NotNull Method>> cachedEventListeners = new HashMap<>();
/** /**
* Constructor. * Constructor.
@ -72,10 +72,10 @@ public class EventHelper {
} }
/** /**
* Returns all annotated methods. * Returns all event listeners.
* *
* @param clazz event class * @param clazz event class
* @param forceScanning forces the method to ignore precomputed event listeners * @param forceScanning forces the method to ignore cached event listeners, not recommended
* @return list of annotated methods * @return list of annotated methods
* @since 1-alpha0 * @since 1-alpha0
*/ */
@ -83,7 +83,7 @@ public class EventHelper {
public static LinkedList<Method> getAnnotatedMethods(@NotNull Class<? extends Event> clazz, boolean forceScanning) { public static LinkedList<Method> getAnnotatedMethods(@NotNull Class<? extends Event> clazz, boolean forceScanning) {
LinkedList<Method> methods = new LinkedList<>(); LinkedList<Method> methods = new LinkedList<>();
if (forceScanning || !precomputedEventListeners.containsKey(clazz) || EngineConfiguration.getInstance().isOptimizeEvents()) { if (forceScanning || !cachedEventListeners.containsKey(clazz) || EngineConfiguration.getInstance().isOptimizeEvents()) {
// Scan entire classpath through Reflections library // Scan entire classpath through Reflections library
Reflections reflections = new Reflections( Reflections reflections = new Reflections(
new ConfigurationBuilder() new ConfigurationBuilder()
@ -102,7 +102,7 @@ public class EventHelper {
// Sort 'methods' linked list // Sort 'methods' linked list
methods.sort(Comparator.comparing(method0 -> method0.getAnnotation(EventListener.class).priority())); methods.sort(Comparator.comparing(method0 -> method0.getAnnotation(EventListener.class).priority()));
} else } else
methods = precomputedEventListeners.get(clazz); methods = cachedEventListeners.get(clazz);
return methods; return methods;
} }
@ -137,37 +137,37 @@ public class EventHelper {
/** /**
* Precomputes all event listeners listening on some event. * Precomputes all event listeners listening on some event.
* Will recompute all event listeners if the specified event is already precomputed. * Will recompute all event listeners if the specified event is already cached.
* *
* @param clazz event class to (p)recompute, set to {@code null} to recompute all precomputed events * @param clazz event class to (p)recompute, set to {@code null} to recompute all cached events
* @since 1-alpha0 * @since 1-alpha0
*/ */
public static void precomputeEventListeners(@Nullable Class<? extends Event> clazz) { public static void precomputeEventListeners(@Nullable Class<? extends Event> clazz) {
if (EngineConfiguration.getInstance().isOptimizeEvents()) return; if (EngineConfiguration.getInstance().isOptimizeEvents()) return;
if (clazz == null) if (clazz == null)
for (Class<? extends Event> event : precomputedEventListeners.keySet()) for (Class<? extends Event> event : cachedEventListeners.keySet())
precomputeEventListeners(event); precomputeEventListeners(event);
else { else {
LinkedList<@NotNull Method> annotatedMethods = getAnnotatedMethods(clazz); LinkedList<@NotNull Method> annotatedMethods = getAnnotatedMethods(clazz);
if (precomputedEventListeners.containsKey(clazz)) if (cachedEventListeners.containsKey(clazz))
precomputedEventListeners.replace(clazz, annotatedMethods); cachedEventListeners.replace(clazz, annotatedMethods);
else else
precomputedEventListeners.put(clazz, annotatedMethods); cachedEventListeners.put(clazz, annotatedMethods);
} }
} }
/** /**
* Unloads precomputed event listeners. * Removes events from the event listener cache.
* *
* @param clazz event class to remove precomputed event listeners for, set to {@code null} to remove all precomputed event listeners * @param clazz event class to remove cached event listeners for, set to {@code null} to remove all cached event listeners
* @since 1-alpha0 * @since 1-alpha0
*/ */
public static void removePrecomputedEventListeners(@Nullable Class<? extends Event> clazz) { public static void removePrecomputedEventListeners(@Nullable Class<? extends Event> clazz) {
if (clazz == null) if (clazz == null)
precomputedEventListeners.clear(); cachedEventListeners.clear();
else else
precomputedEventListeners.remove(clazz); cachedEventListeners.remove(clazz);
} }
} }