'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" })
public class EventHelper {
/**
* Contains all precomputed event listeners.
* Contains all cached event listeners.
*
* @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.
@ -72,10 +72,10 @@ public class EventHelper {
}
/**
* Returns all annotated methods.
* Returns all event listeners.
*
* @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
* @since 1-alpha0
*/
@ -83,7 +83,7 @@ public class EventHelper {
public static LinkedList<Method> getAnnotatedMethods(@NotNull Class<? extends Event> clazz, boolean forceScanning) {
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
Reflections reflections = new Reflections(
new ConfigurationBuilder()
@ -102,7 +102,7 @@ public class EventHelper {
// Sort 'methods' linked list
methods.sort(Comparator.comparing(method0 -> method0.getAnnotation(EventListener.class).priority()));
} else
methods = precomputedEventListeners.get(clazz);
methods = cachedEventListeners.get(clazz);
return methods;
}
@ -137,37 +137,37 @@ public class EventHelper {
/**
* 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
*/
public static void precomputeEventListeners(@Nullable Class<? extends Event> clazz) {
if (EngineConfiguration.getInstance().isOptimizeEvents()) return;
if (clazz == null)
for (Class<? extends Event> event : precomputedEventListeners.keySet())
for (Class<? extends Event> event : cachedEventListeners.keySet())
precomputeEventListeners(event);
else {
LinkedList<@NotNull Method> annotatedMethods = getAnnotatedMethods(clazz);
if (precomputedEventListeners.containsKey(clazz))
precomputedEventListeners.replace(clazz, annotatedMethods);
if (cachedEventListeners.containsKey(clazz))
cachedEventListeners.replace(clazz, annotatedMethods);
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
*/
public static void removePrecomputedEventListeners(@Nullable Class<? extends Event> clazz) {
if (clazz == null)
precomputedEventListeners.clear();
cachedEventListeners.clear();
else
precomputedEventListeners.remove(clazz);
cachedEventListeners.remove(clazz);
}
}