mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-27 00:55:19 +00:00
Add operator map to Plugin class, and use it in PluginManager.
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
package org.nwapw.abacus.plugin;
|
||||
|
||||
import org.nwapw.abacus.function.Function;
|
||||
import org.nwapw.abacus.function.Operator;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* A class that controls instances of plugins, allowing for them
|
||||
@@ -21,6 +21,11 @@ public class PluginManager {
|
||||
* that is, found in a plugin and returned.
|
||||
*/
|
||||
private HashMap<String, Function> cachedFunctions;
|
||||
/**
|
||||
* List of operators tha have been cached,
|
||||
* that is, found in a plugin and returned.
|
||||
*/
|
||||
private HashMap<String, Operator> cachedOperators;
|
||||
|
||||
/**
|
||||
* Creates a new plugin manager.
|
||||
@@ -30,25 +35,39 @@ public class PluginManager {
|
||||
cachedFunctions = new HashMap<>();
|
||||
}
|
||||
|
||||
private static <T> T searchCached(Collection<Plugin> plugins, Map<String, T> cache,
|
||||
java.util.function.Function<Plugin, Set<String>> setFunction,
|
||||
java.util.function.BiFunction<Plugin, String, T> getFunction,
|
||||
String 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.
|
||||
* @param name the name of the function
|
||||
* @return the function under the given name.
|
||||
*/
|
||||
public Function functionFor(String name){
|
||||
if(cachedFunctions.containsKey(name)) {
|
||||
return cachedFunctions.get(name);
|
||||
}
|
||||
return searchCached(plugins, cachedFunctions, Plugin::providedFunctions, Plugin::getFunction, name);
|
||||
}
|
||||
|
||||
Function loadedFunction = null;
|
||||
for(Plugin plugin : plugins){
|
||||
if(plugin.hasFunction(name)){
|
||||
loadedFunction = plugin.getFunction(name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
cachedFunctions.put(name, loadedFunction);
|
||||
return loadedFunction;
|
||||
/**
|
||||
* Gets an operator under the given name.
|
||||
* @param name the name of the operator.
|
||||
* @return the operator under the given name.
|
||||
*/
|
||||
public Operator operatorFor(String name){
|
||||
return searchCached(plugins, cachedOperators, Plugin::providedOperators, Plugin::getOperator, name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user