mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-22 15:30:09 -08:00
Add operator map to Plugin class, and use it in PluginManager.
This commit is contained in:
parent
189f8c6e15
commit
79e85832ce
|
@ -1,8 +1,10 @@
|
|||
package org.nwapw.abacus.plugin;
|
||||
|
||||
import org.nwapw.abacus.function.Function;
|
||||
import org.nwapw.abacus.function.Operator;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A plugin class that can be externally implemented and loaded via the
|
||||
|
@ -17,6 +19,10 @@ public abstract class Plugin {
|
|||
* A hash map of functions mapped to their string names.
|
||||
*/
|
||||
private HashMap<String, Function> functions;
|
||||
/**
|
||||
* A hash map of operators mapped to their string names.
|
||||
*/
|
||||
private HashMap<String, Operator> operators;
|
||||
/**
|
||||
* The plugin manager in which to search for functions
|
||||
* not inside this package,
|
||||
|
@ -35,12 +41,19 @@ public abstract class Plugin {
|
|||
}
|
||||
|
||||
/**
|
||||
* Determines whether the current plugin provides the given function name.
|
||||
* @param functionName the name of the function provided.
|
||||
* @return true of the function exists, false if it doesn't.
|
||||
* Gets the list of functions provided by this plugin.
|
||||
* @return the list of registered functions.
|
||||
*/
|
||||
public final boolean hasFunction(String functionName) {
|
||||
return functions.containsKey(functionName);
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -52,6 +65,15 @@ public abstract class Plugin {
|
|||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* To be used in load(). Registers a function abstract class with the
|
||||
* plugin internally, which makes it accessible to the plugin manager.
|
||||
|
@ -67,17 +89,44 @@ public abstract class Plugin {
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* To be used in load(). Registers an operator abstract class
|
||||
* with the plugin internally, which makes it accessible to
|
||||
* the plugin manager.
|
||||
* @param name the name of the operator.
|
||||
* @param operator the operator to register.
|
||||
* @return true if the operator was registered successfully, false if not.
|
||||
*/
|
||||
protected final boolean registerOperator(String name, Operator operator) {
|
||||
if(operatorFor(name) == null){
|
||||
operators.put(name, operator);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches the PluginManager for the given function name.
|
||||
* This can be used by the plugins internally in order to call functions
|
||||
* they do not provide.
|
||||
* @param name then name for which to search
|
||||
* @param name the name for which to search
|
||||
* @return the resulting function, or null if none was found for that name.
|
||||
*/
|
||||
protected final Function functionFor(String name) {
|
||||
return manager.functionFor(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches the PluginManager for the given operator name.
|
||||
* This can be used by the plugins internally in order to call
|
||||
* operations they do not provide.
|
||||
* @param name the name for which to search
|
||||
* @return the resulting operator, or null if none was found for that name.
|
||||
*/
|
||||
protected final Operator operatorFor(String name) {
|
||||
return manager.operatorFor(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method to be overridden by plugin implementation, in which the plugins
|
||||
* are supposed to register the functions they provide and do any other
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue
Block a user