From f29fea610670af012b6284e3c2966c35a9479642 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 27 Jul 2017 13:26:17 -0700 Subject: [PATCH] Rename load to onLoad and add onDisable to plugin. --- src/org/nwapw/abacus/plugin/Plugin.java | 36 ++++++++++- .../nwapw/abacus/plugin/PluginManager.java | 60 ++++++++++++++++++- .../nwapw/abacus/plugin/StandardPlugin.java | 7 ++- 3 files changed, 98 insertions(+), 5 deletions(-) diff --git a/src/org/nwapw/abacus/plugin/Plugin.java b/src/org/nwapw/abacus/plugin/Plugin.java index db2f2d9..d702950 100644 --- a/src/org/nwapw/abacus/plugin/Plugin.java +++ b/src/org/nwapw/abacus/plugin/Plugin.java @@ -28,6 +28,10 @@ public abstract class Plugin { * not inside this package, */ private PluginManager manager; + /** + * Whether this plugin has been loaded. + */ + private boolean enabled; private Plugin(){ } @@ -38,6 +42,8 @@ public abstract class Plugin { public Plugin(PluginManager manager) { this.manager = manager; functions = new HashMap<>(); + operators = new HashMap<>(); + enabled = false; } /** @@ -74,6 +80,28 @@ public abstract class Plugin { return operators.get(operatorName); } + /** + * Enables the function, loading the necessary instances + * of functions. + */ + public final void enable(){ + if(enabled) return; + onEnable(); + enabled = true; + } + + /** + * Disables the plugin, clearing loaded data store by default + * and calling its disable() method. + */ + public final void disable(){ + if(!enabled) return; + onDisable(); + functions.clear(); + operators.clear(); + enabled = false; + } + /** * To be used in load(). Registers a function abstract class with the * plugin internally, which makes it accessible to the plugin manager. @@ -132,6 +160,12 @@ public abstract class Plugin { * are supposed to register the functions they provide and do any other * necessary setup. */ - public abstract void load(); + public abstract void onEnable(); + + /** + * Abstract method overridden by the plugin implementation, in which the plugins + * are supposed to dispose of loaded functions, operators, and macros. + */ + public abstract void onDisable(); } diff --git a/src/org/nwapw/abacus/plugin/PluginManager.java b/src/org/nwapw/abacus/plugin/PluginManager.java index 97b421c..212bc7e 100644 --- a/src/org/nwapw/abacus/plugin/PluginManager.java +++ b/src/org/nwapw/abacus/plugin/PluginManager.java @@ -26,6 +26,18 @@ public class PluginManager { * that is, found in a plugin and returned. */ private HashMap cachedOperators; + /** + * List of all functions loaded by the plugins. + */ + private HashSet allFunctions; + /** + * List of all operators loaded by the plugins. + */ + private HashSet allOperators; + /** + * The list of plugin listeners attached to this instance. + */ + private HashSet listeners; /** * Creates a new plugin manager. @@ -34,6 +46,8 @@ public class PluginManager { plugins = new ArrayList<>(); cachedFunctions = new HashMap<>(); cachedOperators = new HashMap<>(); + allFunctions = new HashSet<>(); + allOperators = new HashSet<>(); } /** @@ -89,9 +103,6 @@ public class PluginManager { * @param plugin the plugin to add. */ public void addInstantiated(Plugin plugin){ - plugin.load(); - cachedFunctions.clear(); - cachedOperators.clear(); plugins.add(plugin); } @@ -109,4 +120,47 @@ public class PluginManager { } } + /** + * Loads all the plugins in the PluginManager. + */ + public void load(){ + for(Plugin plugin : plugins) plugin.enable(); + for(Plugin plugin : plugins){ + allFunctions.addAll(plugin.providedFunctions()); + allOperators.addAll(plugin.providedOperators()); + } + } + + /** + * Unloads all the plugins in the PluginManager. + */ + public void unload(){ + for(Plugin plugin : plugins) plugin.disable(); + allFunctions.clear(); + allOperators.clear(); + } + + /** + * Reloads all the plugins in the PluginManager. + */ + public void reload(){ + unload(); + reload(); + } + + /** + * Gets all the functions loaded by the Plugin Manager. + * @return the set of all functions that were loaded. + */ + public HashSet getAllFunctions() { + return allFunctions; + } + + /** + * Gets all the operators loaded by the Plugin Manager. + * @return the set of all operators that were loaded. + */ + public HashSet getAllOperators() { + return allOperators; + } } diff --git a/src/org/nwapw/abacus/plugin/StandardPlugin.java b/src/org/nwapw/abacus/plugin/StandardPlugin.java index 9881ff2..dfc920e 100755 --- a/src/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/org/nwapw/abacus/plugin/StandardPlugin.java @@ -17,7 +17,7 @@ public class StandardPlugin extends Plugin { } @Override - public void load() { + public void onEnable() { registerFunction("+", new Function() { @Override protected boolean matchesParams(NumberInterface[] params) { @@ -108,6 +108,11 @@ public class StandardPlugin extends Plugin { }); } + @Override + public void onDisable() { + + } + /** * Returns the nth term of the Taylor series (centered at 0) of e^x * @param n the term required (n >= 0).