mirror of
https://github.com/DanilaFe/abacus
synced 2024-11-18 00:19:32 -08:00
Remove the useless caching in the plugin manager and the maps in plugins
This commit is contained in:
parent
fcb264d134
commit
2c40387e03
|
@ -17,18 +17,6 @@ import java.util.Set;
|
||||||
*/
|
*/
|
||||||
public abstract class Plugin {
|
public abstract class Plugin {
|
||||||
|
|
||||||
/**
|
|
||||||
* A hash map of functions mapped to their string names.
|
|
||||||
*/
|
|
||||||
private Map<String, Function> functions;
|
|
||||||
/**
|
|
||||||
* A hash map of operators mapped to their string names.
|
|
||||||
*/
|
|
||||||
private Map<String, Operator> operators;
|
|
||||||
/**
|
|
||||||
* The map of the number implementations this plugin provides.
|
|
||||||
*/
|
|
||||||
private Map<String, NumberImplementation> numberImplementations;
|
|
||||||
/**
|
/**
|
||||||
* The plugin manager in which to search for functions
|
* The plugin manager in which to search for functions
|
||||||
* not inside this package,
|
* not inside this package,
|
||||||
|
@ -49,69 +37,9 @@ public abstract class Plugin {
|
||||||
*/
|
*/
|
||||||
public Plugin(PluginManager manager) {
|
public Plugin(PluginManager manager) {
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
functions = new HashMap<>();
|
|
||||||
operators = new HashMap<>();
|
|
||||||
numberImplementations = new HashMap<>();
|
|
||||||
enabled = false;
|
enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the list of functions provided by this plugin.
|
|
||||||
*
|
|
||||||
* @return the list of registered functions.
|
|
||||||
*/
|
|
||||||
public final Set<String> providedFunctions() {
|
|
||||||
return functions.keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the list of functions provided by this plugin.
|
|
||||||
*
|
|
||||||
* @return the list of registered functions.
|
|
||||||
*/
|
|
||||||
public final Set<String> providedOperators() {
|
|
||||||
return operators.keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the list of number implementations provided by this plugin.
|
|
||||||
*
|
|
||||||
* @return the list of registered number implementations.
|
|
||||||
*/
|
|
||||||
public final Set<String> providedNumberImplementations() {
|
|
||||||
return numberImplementations.keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets a function under the given function name.
|
|
||||||
*
|
|
||||||
* @param functionName the name of the function to get
|
|
||||||
* @return the function, or null if this plugin doesn't provide it.
|
|
||||||
*/
|
|
||||||
public final Function getFunction(String functionName) {
|
|
||||||
return functions.get(functionName);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets an operator under the given operator name.
|
|
||||||
*
|
|
||||||
* @param operatorName the name of the operator to get.
|
|
||||||
* @return the operator, or null if this plugin doesn't provide it.
|
|
||||||
*/
|
|
||||||
public final Operator getOperator(String operatorName) {
|
|
||||||
return operators.get(operatorName);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the number implementation under the given name.
|
|
||||||
*
|
|
||||||
* @param name the name of the number implementation to look up.
|
|
||||||
* @return the number implementation associated with that name, or null if the plugin doesn't provide it.
|
|
||||||
*/
|
|
||||||
public final NumberImplementation getNumberImplementation(String name) {
|
|
||||||
return numberImplementations.get(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enables the function, loading the necessary instances
|
* Enables the function, loading the necessary instances
|
||||||
* of functions.
|
* of functions.
|
||||||
|
@ -129,8 +57,6 @@ public abstract class Plugin {
|
||||||
public final void disable() {
|
public final void disable() {
|
||||||
if (!enabled) return;
|
if (!enabled) return;
|
||||||
onDisable();
|
onDisable();
|
||||||
functions.clear();
|
|
||||||
operators.clear();
|
|
||||||
enabled = false;
|
enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,7 +68,7 @@ public abstract class Plugin {
|
||||||
* @param toRegister the function implementation.
|
* @param toRegister the function implementation.
|
||||||
*/
|
*/
|
||||||
protected final void registerFunction(String name, Function toRegister) {
|
protected final void registerFunction(String name, Function toRegister) {
|
||||||
functions.put(name, toRegister);
|
manager.registerFunction(name, toRegister);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -154,7 +80,7 @@ public abstract class Plugin {
|
||||||
* @param operator the operator to register.
|
* @param operator the operator to register.
|
||||||
*/
|
*/
|
||||||
protected final void registerOperator(String name, Operator operator) {
|
protected final void registerOperator(String name, Operator operator) {
|
||||||
operators.put(name, operator);
|
manager.registerOperator(name, operator);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -165,7 +91,7 @@ public abstract class Plugin {
|
||||||
* @param implementation the actual implementation class to register.
|
* @param implementation the actual implementation class to register.
|
||||||
*/
|
*/
|
||||||
protected final void registerNumberImplementation(String name, NumberImplementation implementation) {
|
protected final void registerNumberImplementation(String name, NumberImplementation implementation) {
|
||||||
numberImplementations.put(name, implementation);
|
manager.registerNumberImplementation(name, implementation);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -23,20 +23,17 @@ public class PluginManager {
|
||||||
*/
|
*/
|
||||||
private Set<Plugin> plugins;
|
private Set<Plugin> plugins;
|
||||||
/**
|
/**
|
||||||
* List of functions that have been cached,
|
* The map of functions registered by the plugins.
|
||||||
* that is, found in a plugin and returned.
|
|
||||||
*/
|
*/
|
||||||
private Map<String, Function> cachedFunctions;
|
private Map<String, Function> registeredFunctions;
|
||||||
/**
|
/**
|
||||||
* List of operators that have been cached,
|
* The map of operators registered by the plugins
|
||||||
* that is, found in a plugin and returned.
|
|
||||||
*/
|
*/
|
||||||
private Map<String, Operator> cachedOperators;
|
private Map<String, Operator> registeredOperators;
|
||||||
/**
|
/**
|
||||||
* The list of number implementations that have
|
* The map of number implementations registered by the plugins.
|
||||||
* been cached, that is, found in a plugin and returned.
|
|
||||||
*/
|
*/
|
||||||
private Map<String, NumberImplementation> cachedNumberImplementations;
|
private Map<String, NumberImplementation> registeredNumberImplementations;
|
||||||
/**
|
/**
|
||||||
* The list of number implementations that have been
|
* The list of number implementations that have been
|
||||||
* found by their implementation class.
|
* found by their implementation class.
|
||||||
|
@ -46,18 +43,6 @@ public class PluginManager {
|
||||||
* The pi values for each implementation class that have already been computer.
|
* The pi values for each implementation class that have already been computer.
|
||||||
*/
|
*/
|
||||||
private Map<Class<? extends NumberInterface>, NumberInterface> cachedPi;
|
private Map<Class<? extends NumberInterface>, NumberInterface> cachedPi;
|
||||||
/**
|
|
||||||
* List of all functions loaded by the plugins.
|
|
||||||
*/
|
|
||||||
private Set<String> allFunctions;
|
|
||||||
/**
|
|
||||||
* List of all operators loaded by the plugins.
|
|
||||||
*/
|
|
||||||
private Set<String> allOperators;
|
|
||||||
/**
|
|
||||||
* List of all the number implementations loaded by the plugins.
|
|
||||||
*/
|
|
||||||
private Set<String> allNumberImplementations;
|
|
||||||
/**
|
/**
|
||||||
* The list of plugin listeners attached to this instance.
|
* The list of plugin listeners attached to this instance.
|
||||||
*/
|
*/
|
||||||
|
@ -77,79 +62,66 @@ public class PluginManager {
|
||||||
this.abacus = abacus;
|
this.abacus = abacus;
|
||||||
loadedPluginClasses = new HashSet<>();
|
loadedPluginClasses = new HashSet<>();
|
||||||
plugins = new HashSet<>();
|
plugins = new HashSet<>();
|
||||||
cachedFunctions = new HashMap<>();
|
registeredFunctions = new HashMap<>();
|
||||||
cachedOperators = new HashMap<>();
|
registeredOperators = new HashMap<>();
|
||||||
cachedNumberImplementations = new HashMap<>();
|
registeredNumberImplementations = new HashMap<>();
|
||||||
cachedInterfaceImplementations = new HashMap<>();
|
cachedInterfaceImplementations = new HashMap<>();
|
||||||
cachedPi = new HashMap<>();
|
cachedPi = new HashMap<>();
|
||||||
allFunctions = new HashSet<>();
|
|
||||||
allOperators = new HashSet<>();
|
|
||||||
allNumberImplementations = new HashSet<>();
|
|
||||||
listeners = new HashSet<>();
|
listeners = new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches the plugin list for a certain value, retrieving the Plugin's
|
* Registers a function under the given name.
|
||||||
* list of items of the type using the setFunction and getting the value
|
* @param name the name of the function.
|
||||||
* of it is available via getFunction. If the value is contained
|
* @param function the function to register.
|
||||||
* in the cache, it returns the cached value instead.
|
|
||||||
*
|
|
||||||
* @param plugins the plugin list to search.
|
|
||||||
* @param cache the cache to use
|
|
||||||
* @param setFunction the function to retrieve a set of available T's from the plugin
|
|
||||||
* @param getFunction the function to get the T value under the given name
|
|
||||||
* @param name the name to search for
|
|
||||||
* @param <T> the type of element being search
|
|
||||||
* @param <K> the type of key that the cache is indexed by.
|
|
||||||
* @return the retrieved element, or null if it was not found.
|
|
||||||
*/
|
*/
|
||||||
private static <T, K> T searchCached(Collection<Plugin> plugins, Map<K, T> cache,
|
public void registerFunction(String name, Function function){
|
||||||
java.util.function.Function<Plugin, Set<K>> setFunction,
|
registeredFunctions.put(name, function);
|
||||||
java.util.function.BiFunction<Plugin, K, T> getFunction,
|
|
||||||
K name) {
|
|
||||||
if (cache.containsKey(name)) return cache.get(name);
|
|
||||||
|
|
||||||
T loadedValue = null;
|
|
||||||
for (Plugin plugin : plugins) {
|
|
||||||
if (setFunction.apply(plugin).contains(name)) {
|
|
||||||
loadedValue = getFunction.apply(plugin, name);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cache.put(name, loadedValue);
|
|
||||||
return loadedValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a function under the given name.
|
* Registers an operator under the given name.
|
||||||
*
|
|
||||||
* @param name the name of the function
|
|
||||||
* @return the function under the given name.
|
|
||||||
*/
|
|
||||||
public Function functionFor(String name) {
|
|
||||||
return searchCached(plugins, cachedFunctions, Plugin::providedFunctions, Plugin::getFunction, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets an operator under the given name.
|
|
||||||
*
|
|
||||||
* @param name the name of the operator.
|
* @param name the name of the operator.
|
||||||
* @return the operator under the given name.
|
* @param operator the operator to register.
|
||||||
*/
|
*/
|
||||||
public Operator operatorFor(String name) {
|
public void registerOperator(String name, Operator operator){
|
||||||
return searchCached(plugins, cachedOperators, Plugin::providedOperators, Plugin::getOperator, name);
|
registeredOperators.put(name, operator);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the number implementation under the given name.
|
* Registers a number implementation under the given name.
|
||||||
*
|
* @param name the name of the number implementation.
|
||||||
* @param name the name of the implementation.
|
* @param implementation the number implementation to register.
|
||||||
* @return the implementation.
|
|
||||||
*/
|
*/
|
||||||
public NumberImplementation numberImplementationFor(String name) {
|
public void registerNumberImplementation(String name, NumberImplementation implementation){
|
||||||
return searchCached(plugins, cachedNumberImplementations, Plugin::providedNumberImplementations,
|
registeredNumberImplementations.put(name, implementation);
|
||||||
Plugin::getNumberImplementation, name);
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the function registered under the given name.
|
||||||
|
* @param name the name of the function.
|
||||||
|
* @return the function, or null if it was not found.
|
||||||
|
*/
|
||||||
|
public Function functionFor(String name){
|
||||||
|
return registeredFunctions.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the operator registered under the given name.
|
||||||
|
* @param name the name of the operator.
|
||||||
|
* @return the operator, or null if it was not found.
|
||||||
|
*/
|
||||||
|
public Operator operatorFor(String name){
|
||||||
|
return registeredOperators.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the number implementation registered under the given name.
|
||||||
|
* @param name the name of the number implementation.
|
||||||
|
* @return the number implementation, or null if it was not found.
|
||||||
|
*/
|
||||||
|
public NumberImplementation numberImplementationFor(String name){
|
||||||
|
return registeredNumberImplementations.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -161,14 +133,11 @@ public class PluginManager {
|
||||||
public NumberImplementation interfaceImplementationFor(Class<? extends NumberInterface> name) {
|
public NumberImplementation interfaceImplementationFor(Class<? extends NumberInterface> name) {
|
||||||
if (cachedInterfaceImplementations.containsKey(name)) return cachedInterfaceImplementations.get(name);
|
if (cachedInterfaceImplementations.containsKey(name)) return cachedInterfaceImplementations.get(name);
|
||||||
NumberImplementation toReturn = null;
|
NumberImplementation toReturn = null;
|
||||||
outside:
|
for(String key : registeredNumberImplementations.keySet()){
|
||||||
for (Plugin plugin : plugins) {
|
NumberImplementation implementation = registeredNumberImplementations.get(key);
|
||||||
for (String implementationName : plugin.providedNumberImplementations()) {
|
if(implementation.getImplementation() == name) {
|
||||||
NumberImplementation implementation = plugin.getNumberImplementation(implementationName);
|
toReturn = implementation;
|
||||||
if (implementation.getImplementation().equals(name)) {
|
break;
|
||||||
toReturn = implementation;
|
|
||||||
break outside;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cachedInterfaceImplementations.put(name, toReturn);
|
cachedInterfaceImplementations.put(name, toReturn);
|
||||||
|
@ -227,12 +196,6 @@ public class PluginManager {
|
||||||
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
|
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
|
||||||
plugin.enable();
|
plugin.enable();
|
||||||
}
|
}
|
||||||
for (Plugin plugin : plugins) {
|
|
||||||
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
|
|
||||||
allFunctions.addAll(plugin.providedFunctions());
|
|
||||||
allOperators.addAll(plugin.providedOperators());
|
|
||||||
allNumberImplementations.addAll(plugin.providedNumberImplementations());
|
|
||||||
}
|
|
||||||
listeners.forEach(e -> e.onLoad(this));
|
listeners.forEach(e -> e.onLoad(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,14 +209,8 @@ public class PluginManager {
|
||||||
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
|
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
|
||||||
plugin.disable();
|
plugin.disable();
|
||||||
}
|
}
|
||||||
cachedFunctions.clear();
|
|
||||||
cachedOperators.clear();
|
|
||||||
cachedNumberImplementations.clear();
|
|
||||||
cachedInterfaceImplementations.clear();
|
cachedInterfaceImplementations.clear();
|
||||||
cachedPi.clear();
|
cachedPi.clear();
|
||||||
allFunctions.clear();
|
|
||||||
allOperators.clear();
|
|
||||||
allNumberImplementations.clear();
|
|
||||||
listeners.forEach(e -> e.onUnload(this));
|
listeners.forEach(e -> e.onUnload(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -271,7 +228,7 @@ public class PluginManager {
|
||||||
* @return the set of all functions that were loaded.
|
* @return the set of all functions that were loaded.
|
||||||
*/
|
*/
|
||||||
public Set<String> getAllFunctions() {
|
public Set<String> getAllFunctions() {
|
||||||
return allFunctions;
|
return registeredFunctions.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -280,7 +237,7 @@ public class PluginManager {
|
||||||
* @return the set of all operators that were loaded.
|
* @return the set of all operators that were loaded.
|
||||||
*/
|
*/
|
||||||
public Set<String> getAllOperators() {
|
public Set<String> getAllOperators() {
|
||||||
return allOperators;
|
return registeredOperators.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -289,7 +246,7 @@ public class PluginManager {
|
||||||
* @return the set of all implementations that were loaded.
|
* @return the set of all implementations that were loaded.
|
||||||
*/
|
*/
|
||||||
public Set<String> getAllNumberImplementations() {
|
public Set<String> getAllNumberImplementations() {
|
||||||
return allNumberImplementations;
|
return registeredNumberImplementations.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue
Block a user