From ce2373be8eb8a1bda7a403d84312787d78b1163e Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 26 Jul 2017 10:15:22 -0700 Subject: [PATCH] Comment the PluginManager and change pluginFor to functionFor --- .../nwapw/abacus/plugin/PluginManager.java | 53 ++++++++++++++----- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/src/org/nwapw/abacus/plugin/PluginManager.java b/src/org/nwapw/abacus/plugin/PluginManager.java index 0e0d78a..9715597 100644 --- a/src/org/nwapw/abacus/plugin/PluginManager.java +++ b/src/org/nwapw/abacus/plugin/PluginManager.java @@ -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 plugins; - private HashMap pluginsForFunctions; + /** + * List of functions that have been cached, + * that is, found in a plugin and returned. + */ + private HashMap 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 {