1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-23 16:00:09 -08:00

Comment the PluginManager and change pluginFor to functionFor

This commit is contained in:
Danila Fedorin 2017-07-26 10:15:22 -07:00
parent 7ba6f8a353
commit ce2373be8e

View File

@ -1,42 +1,71 @@
package org.nwapw.abacus.plugin; package org.nwapw.abacus.plugin;
import org.nwapw.abacus.function.Function;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; 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 { public class PluginManager {
/**
* A list of loaded plugins.
*/
private ArrayList<Plugin> 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(){ public PluginManager(){
plugins = new ArrayList<>(); plugins = new ArrayList<>();
pluginsForFunctions = new HashMap<>(); cachedFunctions = new HashMap<>();
} }
public Plugin pluginForFunction(String name){ /**
if(pluginsForFunctions.containsKey(name)) { * Gets a function under the given name.
return pluginsForFunctions.get(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){ for(Plugin plugin : plugins){
if(plugin.hasFunction(name)){ if(plugin.hasFunction(name)){
foundPlugin = plugin; loadedFunction = plugin.getFunction(name);
break; break;
} }
} }
pluginsForFunctions.put(name, foundPlugin); cachedFunctions.put(name, loadedFunction);
return loadedFunction;
return foundPlugin;
} }
/**
* Adds an instance of Plugin that already has been instantiated.
* @param plugin the plugin to add.
*/
public void addInstantiated(Plugin plugin){ public void addInstantiated(Plugin plugin){
plugin.load(); plugin.load();
pluginsForFunctions.clear(); cachedFunctions.clear();
plugins.add(plugin); 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){ public void addClass(Class<?> newClass){
if(!Plugin.class.isAssignableFrom(newClass)) return; if(!Plugin.class.isAssignableFrom(newClass)) return;
try { try {