Rename load to onLoad and add onDisable to plugin.

This commit is contained in:
Danila Fedorin 2017-07-27 13:26:17 -07:00
parent f931b9f322
commit e6559015b3
3 changed files with 98 additions and 5 deletions

View File

@ -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();
}

View File

@ -26,6 +26,18 @@ public class PluginManager {
* that is, found in a plugin and returned.
*/
private HashMap<String, Operator> cachedOperators;
/**
* List of all functions loaded by the plugins.
*/
private HashSet<String> allFunctions;
/**
* List of all operators loaded by the plugins.
*/
private HashSet<String> allOperators;
/**
* The list of plugin listeners attached to this instance.
*/
private HashSet<PluginListener> 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<String> getAllFunctions() {
return allFunctions;
}
/**
* Gets all the operators loaded by the Plugin Manager.
* @return the set of all operators that were loaded.
*/
public HashSet<String> getAllOperators() {
return allOperators;
}
}

View File

@ -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).