mirror of
https://github.com/DanilaFe/abacus
synced 2024-11-12 13:49:54 -08:00
Rename ExternalFunction --> Plugin, and implement plugin loading.
This commit is contained in:
parent
ee63eb270e
commit
b3c795e42f
|
@ -1,28 +0,0 @@
|
|||
package org.nwapw.abacus.number;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public abstract class externalFunction {
|
||||
|
||||
private HashMap<String, Function> 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();
|
||||
}
|
43
src/org/nwapw/abacus/plugin/Plugin.java
Normal file
43
src/org/nwapw/abacus/plugin/Plugin.java
Normal file
|
@ -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<String, Function> 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();
|
||||
|
||||
}
|
51
src/org/nwapw/abacus/plugin/PluginManager.java
Normal file
51
src/org/nwapw/abacus/plugin/PluginManager.java
Normal file
|
@ -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<Plugin> plugins;
|
||||
private HashMap<String, Plugin> 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user