From b3c795e42f2470b3afec1f3969a99d52f0714b3b Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 25 Jul 2017 21:50:30 -0700 Subject: [PATCH] Rename ExternalFunction --> Plugin, and implement plugin loading. --- .../nwapw/abacus/number/externalFunction.java | 28 ---------- src/org/nwapw/abacus/plugin/Plugin.java | 43 ++++++++++++++++ .../nwapw/abacus/plugin/PluginManager.java | 51 +++++++++++++++++++ 3 files changed, 94 insertions(+), 28 deletions(-) delete mode 100644 src/org/nwapw/abacus/number/externalFunction.java create mode 100644 src/org/nwapw/abacus/plugin/Plugin.java create mode 100644 src/org/nwapw/abacus/plugin/PluginManager.java diff --git a/src/org/nwapw/abacus/number/externalFunction.java b/src/org/nwapw/abacus/number/externalFunction.java deleted file mode 100644 index bcc4786..0000000 --- a/src/org/nwapw/abacus/number/externalFunction.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.nwapw.abacus.number; - -import java.util.HashMap; - -public abstract class externalFunction { - - private HashMap functions; - - public externalFunction(){ - functions=new HashMap<>(); - } - - public boolean hasFunction(Function x){ - return functions.containsKey(x); - } - public Function getFunction(String x){ - return functions.get(x); - } - public boolean registerFunction(String x, Function y){ - if(!functions.containsKey(x)) - return functions.put(x,y)==null; - return false; - } - public Function functionFor(String x){ - return null; - } - public abstract void load(); -} diff --git a/src/org/nwapw/abacus/plugin/Plugin.java b/src/org/nwapw/abacus/plugin/Plugin.java new file mode 100644 index 0000000..7f286db --- /dev/null +++ b/src/org/nwapw/abacus/plugin/Plugin.java @@ -0,0 +1,43 @@ +package org.nwapw.abacus.plugin; + +import org.nwapw.abacus.number.Function; + +import java.util.HashMap; + +public abstract class Plugin { + + private HashMap functions; + private PluginManager manager; + + private Plugin(){ } + + public Plugin(PluginManager manager) { + this.manager = manager; + functions = new HashMap<>(); + } + + public final boolean hasFunction(String functionName) { + return functions.containsKey(functionName); + } + + public final Function getFunction(String functionName) { + return functions.get(functionName); + } + + protected final boolean registerFunction(String name, Function toRegister) { + if(functionFor(name) == null){ + functions.put(name, toRegister); + return true; + } + return false; + } + + protected final Function functionFor(String name) { + Plugin ownerPlugin = manager.pluginForFunction(name); + if(ownerPlugin == null) return null; + return ownerPlugin.getFunction(name); + } + + public abstract void load(); + +} diff --git a/src/org/nwapw/abacus/plugin/PluginManager.java b/src/org/nwapw/abacus/plugin/PluginManager.java new file mode 100644 index 0000000..413c6e1 --- /dev/null +++ b/src/org/nwapw/abacus/plugin/PluginManager.java @@ -0,0 +1,51 @@ +package org.nwapw.abacus.plugin; + +import org.nwapw.abacus.number.Function; + +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.HashMap; + +public class PluginManager { + + private ArrayList plugins; + private HashMap pluginsForFunctions; + + public PluginManager(){ + plugins = new ArrayList<>(); + pluginsForFunctions = new HashMap<>(); + } + + public Plugin pluginForFunction(String name){ + if(pluginsForFunctions.containsKey(name)) { + return pluginsForFunctions.get(name); + } + + Plugin foundPlugin = null; + for(Plugin plugin : plugins){ + if(plugin.hasFunction(name)) { + foundPlugin = plugin; + break; + } + } + pluginsForFunctions.put(name, foundPlugin); + + return foundPlugin; + } + + public void addInstantiated(Plugin plugin){ + plugin.load(); + pluginsForFunctions.clear(); + plugins.add(plugin); + } + + public void addClass(Class newClass){ + if(!Plugin.class.isAssignableFrom(newClass)) return; + try { + addInstantiated((Plugin) newClass.getConstructor(PluginManager.class).newInstance(this)); + } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + e.printStackTrace(); + } + } + +}