2017-07-25 21:50:30 -07:00
|
|
|
package org.nwapw.abacus.plugin;
|
|
|
|
|
2017-07-26 10:15:22 -07:00
|
|
|
import org.nwapw.abacus.function.Function;
|
|
|
|
|
2017-07-25 21:50:30 -07:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
2017-07-26 10:15:22 -07:00
|
|
|
/**
|
|
|
|
* A class that controls instances of plugins, allowing for them
|
|
|
|
* to interact with each other and the calculator.
|
|
|
|
*/
|
2017-07-25 21:50:30 -07:00
|
|
|
public class PluginManager {
|
|
|
|
|
2017-07-26 10:15:22 -07:00
|
|
|
/**
|
|
|
|
* A list of loaded plugins.
|
|
|
|
*/
|
2017-07-25 21:50:30 -07:00
|
|
|
private ArrayList<Plugin> plugins;
|
2017-07-26 10:15:22 -07:00
|
|
|
/**
|
|
|
|
* List of functions that have been cached,
|
|
|
|
* that is, found in a plugin and returned.
|
|
|
|
*/
|
|
|
|
private HashMap<String, Function> cachedFunctions;
|
2017-07-25 21:50:30 -07:00
|
|
|
|
2017-07-26 10:15:22 -07:00
|
|
|
/**
|
|
|
|
* Creates a new plugin manager.
|
|
|
|
*/
|
2017-07-25 21:50:30 -07:00
|
|
|
public PluginManager(){
|
|
|
|
plugins = new ArrayList<>();
|
2017-07-26 10:15:22 -07:00
|
|
|
cachedFunctions = new HashMap<>();
|
2017-07-25 21:50:30 -07:00
|
|
|
}
|
|
|
|
|
2017-07-26 10:15:22 -07:00
|
|
|
/**
|
|
|
|
* 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);
|
2017-07-25 21:50:30 -07:00
|
|
|
}
|
|
|
|
|
2017-07-26 10:15:22 -07:00
|
|
|
Function loadedFunction = null;
|
2017-07-25 21:50:30 -07:00
|
|
|
for(Plugin plugin : plugins){
|
2017-07-26 10:15:22 -07:00
|
|
|
if(plugin.hasFunction(name)){
|
|
|
|
loadedFunction = plugin.getFunction(name);
|
2017-07-25 21:50:30 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-07-26 10:15:22 -07:00
|
|
|
cachedFunctions.put(name, loadedFunction);
|
|
|
|
return loadedFunction;
|
2017-07-25 21:50:30 -07:00
|
|
|
}
|
|
|
|
|
2017-07-26 10:15:22 -07:00
|
|
|
/**
|
|
|
|
* Adds an instance of Plugin that already has been instantiated.
|
|
|
|
* @param plugin the plugin to add.
|
|
|
|
*/
|
2017-07-25 21:50:30 -07:00
|
|
|
public void addInstantiated(Plugin plugin){
|
|
|
|
plugin.load();
|
2017-07-26 10:15:22 -07:00
|
|
|
cachedFunctions.clear();
|
2017-07-25 21:50:30 -07:00
|
|
|
plugins.add(plugin);
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:15:22 -07:00
|
|
|
/**
|
|
|
|
* Instantiates a class of plugin, and adds it to this
|
|
|
|
* plugin manager.
|
|
|
|
* @param newClass the new class to instantiate.
|
|
|
|
*/
|
2017-07-25 21:50:30 -07:00
|
|
|
public void addClass(Class<?> newClass){
|
|
|
|
if(!Plugin.class.isAssignableFrom(newClass)) return;
|
|
|
|
try {
|
|
|
|
addInstantiated((Plugin) newClass.getConstructor(PluginManager.class).newInstance(this));
|
|
|
|
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|