2017-07-25 21:50:30 -07:00
|
|
|
package org.nwapw.abacus.plugin;
|
|
|
|
|
2017-07-25 21:57:14 -07:00
|
|
|
import org.nwapw.abacus.function.Function;
|
2017-07-27 10:38:18 -07:00
|
|
|
import org.nwapw.abacus.function.Operator;
|
2017-07-25 21:50:30 -07:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
2017-07-27 10:38:18 -07:00
|
|
|
import java.util.Set;
|
2017-07-25 21:50:30 -07:00
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* A plugin class that can be externally implemented and loaded via the
|
|
|
|
* plugin manager. Plugins provide functionality to the calculator
|
|
|
|
* with the "hasFunction" and "getFunction" functions,
|
|
|
|
* and can use "registerFunction" and "functionFor" for
|
|
|
|
* loading internally.
|
|
|
|
*/
|
2017-07-25 21:50:30 -07:00
|
|
|
public abstract class Plugin {
|
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* A hash map of functions mapped to their string names.
|
|
|
|
*/
|
2017-07-25 21:50:30 -07:00
|
|
|
private HashMap<String, Function> functions;
|
2017-07-27 10:38:18 -07:00
|
|
|
/**
|
|
|
|
* A hash map of operators mapped to their string names.
|
|
|
|
*/
|
|
|
|
private HashMap<String, Operator> operators;
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* The plugin manager in which to search for functions
|
|
|
|
* not inside this package,
|
|
|
|
*/
|
2017-07-25 21:50:30 -07:00
|
|
|
private PluginManager manager;
|
|
|
|
|
|
|
|
private Plugin(){ }
|
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* Creates a new plugin with the given PluginManager.
|
|
|
|
* @param manager the manager controlling this plugin.
|
|
|
|
*/
|
2017-07-25 21:50:30 -07:00
|
|
|
public Plugin(PluginManager manager) {
|
|
|
|
this.manager = manager;
|
|
|
|
functions = new HashMap<>();
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
2017-07-27 10:38:18 -07:00
|
|
|
* 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.
|
2017-07-26 10:10:37 -07:00
|
|
|
*/
|
2017-07-27 10:38:18 -07:00
|
|
|
public final Set<String> providedOperators(){
|
|
|
|
return operators.keySet();
|
2017-07-25 21:50:30 -07:00
|
|
|
}
|
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-07-25 21:50:30 -07:00
|
|
|
public final Function getFunction(String functionName) {
|
|
|
|
return functions.get(functionName);
|
|
|
|
}
|
|
|
|
|
2017-07-27 10:38:18 -07:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* To be used in load(). Registers a function abstract class with the
|
|
|
|
* plugin internally, which makes it accessible to the plugin manager.
|
|
|
|
* @param name the name to register by.
|
|
|
|
* @param toRegister the function implementation.
|
|
|
|
* @return true if the function was registered successfully, false if not.
|
|
|
|
*/
|
2017-07-25 21:50:30 -07:00
|
|
|
protected final boolean registerFunction(String name, Function toRegister) {
|
|
|
|
if(functionFor(name) == null){
|
|
|
|
functions.put(name, toRegister);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-27 10:38:18 -07:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* 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.
|
2017-07-27 10:38:18 -07:00
|
|
|
* @param name the name for which to search
|
2017-07-26 10:10:37 -07:00
|
|
|
* @return the resulting function, or null if none was found for that name.
|
|
|
|
*/
|
2017-07-25 21:50:30 -07:00
|
|
|
protected final Function functionFor(String name) {
|
2017-07-26 10:10:37 -07:00
|
|
|
return manager.functionFor(name);
|
2017-07-25 21:50:30 -07:00
|
|
|
}
|
|
|
|
|
2017-07-27 10:38:18 -07:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* Abstract method to be overridden by plugin implementation, in which the plugins
|
|
|
|
* are supposed to register the functions they provide and do any other
|
|
|
|
* necessary setup.
|
|
|
|
*/
|
2017-07-25 21:50:30 -07:00
|
|
|
public abstract void load();
|
|
|
|
|
|
|
|
}
|