mirror of
https://github.com/DanilaFe/abacus
synced 2024-11-04 18:08:31 -08:00
Comment the PluginManager and change pluginFor to functionFor
This commit is contained in:
parent
08999350f4
commit
ac153521d4
|
@ -1,42 +1,71 @@
|
|||
package org.nwapw.abacus.plugin;
|
||||
|
||||
import org.nwapw.abacus.function.Function;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* A class that controls instances of plugins, allowing for them
|
||||
* to interact with each other and the calculator.
|
||||
*/
|
||||
public class PluginManager {
|
||||
|
||||
/**
|
||||
* A list of loaded plugins.
|
||||
*/
|
||||
private ArrayList<Plugin> plugins;
|
||||
private HashMap<String, Plugin> pluginsForFunctions;
|
||||
/**
|
||||
* List of functions that have been cached,
|
||||
* that is, found in a plugin and returned.
|
||||
*/
|
||||
private HashMap<String, Function> cachedFunctions;
|
||||
|
||||
/**
|
||||
* Creates a new plugin manager.
|
||||
*/
|
||||
public PluginManager(){
|
||||
plugins = new ArrayList<>();
|
||||
pluginsForFunctions = new HashMap<>();
|
||||
cachedFunctions = new HashMap<>();
|
||||
}
|
||||
|
||||
public Plugin pluginForFunction(String name){
|
||||
if(pluginsForFunctions.containsKey(name)) {
|
||||
return pluginsForFunctions.get(name);
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
Plugin foundPlugin = null;
|
||||
Function loadedFunction = null;
|
||||
for(Plugin plugin : plugins){
|
||||
if(plugin.hasFunction(name)) {
|
||||
foundPlugin = plugin;
|
||||
if(plugin.hasFunction(name)){
|
||||
loadedFunction = plugin.getFunction(name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
pluginsForFunctions.put(name, foundPlugin);
|
||||
|
||||
return foundPlugin;
|
||||
cachedFunctions.put(name, loadedFunction);
|
||||
return loadedFunction;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an instance of Plugin that already has been instantiated.
|
||||
* @param plugin the plugin to add.
|
||||
*/
|
||||
public void addInstantiated(Plugin plugin){
|
||||
plugin.load();
|
||||
pluginsForFunctions.clear();
|
||||
cachedFunctions.clear();
|
||||
plugins.add(plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a class of plugin, and adds it to this
|
||||
* plugin manager.
|
||||
* @param newClass the new class to instantiate.
|
||||
*/
|
||||
public void addClass(Class<?> newClass){
|
||||
if(!Plugin.class.isAssignableFrom(newClass)) return;
|
||||
try {
|
||||
|
|
Loading…
Reference in New Issue
Block a user