From 0f02867a4e455ae177c8c7e3769b2da43b4d566a Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 3 Aug 2017 14:10:02 -0700 Subject: [PATCH] Add the list of number interface providers to Plugins. --- .../java/org/nwapw/abacus/plugin/Plugin.java | 62 +++++++++++++++++++ .../nwapw/abacus/plugin/PluginManager.java | 28 ++++++++- 2 files changed, 88 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/nwapw/abacus/plugin/Plugin.java b/src/main/java/org/nwapw/abacus/plugin/Plugin.java index 62f6ecf..96c6104 100644 --- a/src/main/java/org/nwapw/abacus/plugin/Plugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/Plugin.java @@ -29,6 +29,10 @@ public abstract class Plugin { * A hash map of operators mapped to their string names. */ private Map> numbers; + /** + * A hash map of constant providers for each number type. + */ + private Map, java.util.function.Function> constantProviders; /** * The plugin manager in which to search for functions * not inside this package, @@ -52,6 +56,7 @@ public abstract class Plugin { functions = new HashMap<>(); operators = new HashMap<>(); numbers = new HashMap<>(); + constantProviders = new HashMap<>(); enabled = false; } @@ -82,6 +87,14 @@ public abstract class Plugin { return numbers.keySet(); } + /** + * Gets the list of all constant providers provided by this plugin. + * @return the list of constant providers. + */ + public final Set> providedConstantProviders() { + return constantProviders.keySet(); + } + /** * Gets a function under the given function name. * @@ -112,6 +125,16 @@ public abstract class Plugin { return numbers.get(numberName); } + /** + * Gets the constant provider for the given class. + * + * @param pluginClass the class for which to provide constants. + * @return the provider, or null, if the plugin doesn't provide it. + */ + public final java.util.function.Function getConstantProvider(Class pluginClass){ + return constantProviders.get(pluginClass); + } + /** * Enables the function, loading the necessary instances * of functions. @@ -131,6 +154,8 @@ public abstract class Plugin { onDisable(); functions.clear(); operators.clear(); + numbers.clear(); + constantProviders.clear(); enabled = false; } @@ -170,6 +195,19 @@ public abstract class Plugin { numbers.put(name, toRegister); } + /** + * To be used in load(). Registers a constant provider + * with the plugin internally, which makes it possible + * for the calculations to look up constants for each different + * number type. + * @param providerFor the class the provider works with. + * @param constantProvider the provider to register. + */ + protected final void registerConstantProvider(Class providerFor, + java.util.function.Function constantProvider) { + constantProviders.put(providerFor, constantProvider); + } + /** * Searches the PluginManager for the given function name. * This can be used by the plugins internally in order to call functions @@ -194,6 +232,30 @@ public abstract class Plugin { return manager.operatorFor(name); } + /** + * Searches the PluginManager for the given number implementation. + * This can be used by the plugins internally in order to + * find classes by name that they do not provide. + * + * @param name the name for which to search + * @return the resulting number class. + */ + protected final Class numberFor(String name) { + return manager.numberFor(name); + } + + /** + * Searches the PluginManager for the given constant provider. + * This can be used by the plugins internally in order + * to find constant providers for number provider they do not provide. + * + * @param forClass the class for which to get a generator for. + * @return the resulting generator + */ + protected final java.util.function.Function constantProviderFor(Class forClass){ + return manager.constantProviderFor(forClass); + } + /** * Abstract method to be overridden by plugin implementation, in which the plugins * are supposed to register the functions they provide and do any other diff --git a/src/main/java/org/nwapw/abacus/plugin/PluginManager.java b/src/main/java/org/nwapw/abacus/plugin/PluginManager.java index 141adbd..213fc65 100644 --- a/src/main/java/org/nwapw/abacus/plugin/PluginManager.java +++ b/src/main/java/org/nwapw/abacus/plugin/PluginManager.java @@ -2,7 +2,6 @@ package org.nwapw.abacus.plugin; import org.nwapw.abacus.function.Function; import org.nwapw.abacus.function.Operator; -import org.nwapw.abacus.number.NaiveNumber; import org.nwapw.abacus.number.NumberInterface; import java.lang.reflect.InvocationTargetException; @@ -41,7 +40,7 @@ public class PluginManager { * List of registered constant providers for every * number class. */ - private Map, java.util.function.Function> cachedConstantProviders; + private Map, java.util.function.Function> cachedConstantProviders; /** * List of all functions loaded by the plugins. */ @@ -54,6 +53,10 @@ public class PluginManager { * List of all numbers loaded by the plugins. */ private Set allNumbers; + /** + * List of all the constant providers loaded by the plugins. + */ + private Set> allConstantProviders; /** * The list of plugin listeners attached to this instance. */ @@ -72,6 +75,7 @@ public class PluginManager { allFunctions = new HashSet<>(); allOperators = new HashSet<>(); allNumbers = new HashSet<>(); + allConstantProviders = new HashSet<>(); listeners = new HashSet<>(); } @@ -87,6 +91,7 @@ public class PluginManager { * @param getFunction the function to get the T value under the given name * @param name the name to search for * @param the type of element being search + * @param the type of key that the cache is indexed by. * @return the retrieved element, or null if it was not found. */ private static T searchCached(Collection plugins, Map cache, @@ -137,6 +142,15 @@ public class PluginManager { return searchCached(plugins, cachedNumbers, Plugin::providedNumbers, Plugin::getNumber, name); } + /** + * Gets the constant provider for the given class. + * @param forClass the class to get the provider for. + * @return the provider. + */ + public java.util.function.Function constantProviderFor(Class forClass){ + return searchCached(plugins, cachedConstantProviders, Plugin::providedConstantProviders, Plugin::getConstantProvider, forClass); + } + /** * Adds an instance of Plugin that already has been instantiated. * @@ -172,6 +186,7 @@ public class PluginManager { allFunctions.addAll(plugin.providedFunctions()); allOperators.addAll(plugin.providedOperators()); allNumbers.addAll(plugin.providedNumbers()); + allConstantProviders.addAll(plugin.providedConstantProviders()); } listeners.forEach(e -> e.onLoad(this)); } @@ -184,6 +199,7 @@ public class PluginManager { allFunctions.clear(); allOperators.clear(); allNumbers.clear(); + allConstantProviders.clear(); listeners.forEach(e -> e.onUnload(this)); } @@ -222,6 +238,14 @@ public class PluginManager { return allNumbers; } + /** + * Gets all the constant providers loaded by the Plugin Manager. + * @return the set of all constant providers that were loaded. + */ + public Set> getAllConstantProviders() { + return allConstantProviders; + } + /** * Adds a plugin change listener to this plugin manager. *