From 22f1b7543abc00516570d1f20360105c150c076a Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 3 Aug 2017 10:03:13 -0700 Subject: [PATCH 01/10] Change the cache search function to be more generic. --- src/main/java/org/nwapw/abacus/plugin/PluginManager.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/nwapw/abacus/plugin/PluginManager.java b/src/main/java/org/nwapw/abacus/plugin/PluginManager.java index 757ac67..e3c6dcb 100644 --- a/src/main/java/org/nwapw/abacus/plugin/PluginManager.java +++ b/src/main/java/org/nwapw/abacus/plugin/PluginManager.java @@ -82,10 +82,10 @@ public class PluginManager { * @param the type of element being search * @return the retrieved element, or null if it was not found. */ - private static T searchCached(Collection plugins, Map cache, - java.util.function.Function> setFunction, - java.util.function.BiFunction getFunction, - String name) { + private static T searchCached(Collection plugins, Map cache, + java.util.function.Function> setFunction, + java.util.function.BiFunction getFunction, + K name) { if (cache.containsKey(name)) return cache.get(name); T loadedValue = null; From 7c0f0046f8c355e708615703a2d0ba594684bdcb Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 3 Aug 2017 10:25:10 -0700 Subject: [PATCH 02/10] Add a number provider cache. --- src/main/java/org/nwapw/abacus/plugin/PluginManager.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/org/nwapw/abacus/plugin/PluginManager.java b/src/main/java/org/nwapw/abacus/plugin/PluginManager.java index e3c6dcb..141adbd 100644 --- a/src/main/java/org/nwapw/abacus/plugin/PluginManager.java +++ b/src/main/java/org/nwapw/abacus/plugin/PluginManager.java @@ -2,6 +2,7 @@ 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; @@ -36,6 +37,11 @@ public class PluginManager { * been cached, that is, found in a plugin and returned. */ private Map> cachedNumbers; + /** + * List of registered constant providers for every + * number class. + */ + private Map, java.util.function.Function> cachedConstantProviders; /** * List of all functions loaded by the plugins. */ @@ -62,6 +68,7 @@ public class PluginManager { cachedFunctions = new HashMap<>(); cachedOperators = new HashMap<>(); cachedNumbers = new HashMap<>(); + cachedConstantProviders = new HashMap<>(); allFunctions = new HashSet<>(); allOperators = new HashSet<>(); allNumbers = new HashSet<>(); From 7dda1f2fcf41dba22b3119c5dfdf5ca95cfb5853 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 3 Aug 2017 14:10:02 -0700 Subject: [PATCH 03/10] 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. * From 542f062b5e5f30620c58fd529923eca1f1ad7772 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 3 Aug 2017 20:50:39 -0700 Subject: [PATCH 04/10] Remove old reference to the promotion system and the number class map. --- .../org/nwapw/abacus/function/Function.java | 13 --- .../java/org/nwapw/abacus/plugin/Plugin.java | 100 ------------------ .../nwapw/abacus/plugin/PluginManager.java | 63 ----------- .../nwapw/abacus/plugin/StandardPlugin.java | 5 - 4 files changed, 181 deletions(-) diff --git a/src/main/java/org/nwapw/abacus/function/Function.java b/src/main/java/org/nwapw/abacus/function/Function.java index 62a0e7d..bd5af72 100755 --- a/src/main/java/org/nwapw/abacus/function/Function.java +++ b/src/main/java/org/nwapw/abacus/function/Function.java @@ -1,10 +1,6 @@ package org.nwapw.abacus.function; -import org.nwapw.abacus.number.NaiveNumber; import org.nwapw.abacus.number.NumberInterface; -import org.nwapw.abacus.number.PreciseNumber; - -import java.util.HashMap; /** * A function that operates on one or more @@ -12,15 +8,6 @@ import java.util.HashMap; */ public abstract class Function { - /** - * A map to correctly promote different number implementations to each other. - */ - private static final HashMap, Integer> priorityMap = - new HashMap, Integer>() {{ - put(NaiveNumber.class, 0); - put(PreciseNumber.class, 1); - }}; - /** * Checks whether the given params will work for the given function. * diff --git a/src/main/java/org/nwapw/abacus/plugin/Plugin.java b/src/main/java/org/nwapw/abacus/plugin/Plugin.java index 96c6104..2757841 100644 --- a/src/main/java/org/nwapw/abacus/plugin/Plugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/Plugin.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.NumberInterface; import java.util.HashMap; import java.util.Map; @@ -25,14 +24,6 @@ public abstract class Plugin { * A hash map of operators mapped to their string names. */ private Map operators; - /** - * 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, @@ -55,8 +46,6 @@ public abstract class Plugin { this.manager = manager; functions = new HashMap<>(); operators = new HashMap<>(); - numbers = new HashMap<>(); - constantProviders = new HashMap<>(); enabled = false; } @@ -78,23 +67,6 @@ public abstract class Plugin { return operators.keySet(); } - /** - * Gets the list of all numbers provided by this plugin. - * - * @return the list of registered numbers. - */ - public final Set providedNumbers() { - 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. * @@ -115,26 +87,6 @@ public abstract class Plugin { return operators.get(operatorName); } - /** - * Gets the class under the given name. - * - * @param numberName the name of the class. - * @return the class, or null if the plugin doesn't provide it. - */ - public final Class getNumber(String numberName) { - 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. @@ -154,8 +106,6 @@ public abstract class Plugin { onDisable(); functions.clear(); operators.clear(); - numbers.clear(); - constantProviders.clear(); enabled = false; } @@ -182,32 +132,6 @@ public abstract class Plugin { operators.put(name, operator); } - /** - * To be used in load(). Registers a number class - * with the plugin internally, which makes it possible - * for the user to select it as an "implementation" for the - * number that they would like to use. - * - * @param name the name to register it under. - * @param toRegister the class to register. - */ - protected final void registerNumber(String name, Class toRegister) { - 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 @@ -232,30 +156,6 @@ 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 213fc65..1f61a56 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.NumberInterface; import java.lang.reflect.InvocationTargetException; import java.util.*; @@ -31,16 +30,6 @@ public class PluginManager { * that is, found in a plugin and returned. */ private Map cachedOperators; - /** - * List of registered number implementations that have - * been cached, that is, found in a plugin and returned. - */ - private Map> cachedNumbers; - /** - * List of registered constant providers for every - * number class. - */ - private Map, java.util.function.Function> cachedConstantProviders; /** * List of all functions loaded by the plugins. */ @@ -49,14 +38,6 @@ public class PluginManager { * List of all operators loaded by the plugins. */ private Set allOperators; - /** - * 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. */ @@ -70,12 +51,8 @@ public class PluginManager { plugins = new HashSet<>(); cachedFunctions = new HashMap<>(); cachedOperators = new HashMap<>(); - cachedNumbers = new HashMap<>(); - cachedConstantProviders = new HashMap<>(); allFunctions = new HashSet<>(); allOperators = new HashSet<>(); - allNumbers = new HashSet<>(); - allConstantProviders = new HashSet<>(); listeners = new HashSet<>(); } @@ -132,25 +109,6 @@ public class PluginManager { return searchCached(plugins, cachedOperators, Plugin::providedOperators, Plugin::getOperator, name); } - /** - * Gets a numer implementation under the given name. - * - * @param name the name of the implementation. - * @return the implementation class - */ - public Class numberFor(String name) { - 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. * @@ -185,8 +143,6 @@ public class PluginManager { for (Plugin plugin : plugins) { allFunctions.addAll(plugin.providedFunctions()); allOperators.addAll(plugin.providedOperators()); - allNumbers.addAll(plugin.providedNumbers()); - allConstantProviders.addAll(plugin.providedConstantProviders()); } listeners.forEach(e -> e.onLoad(this)); } @@ -198,8 +154,6 @@ public class PluginManager { for (Plugin plugin : plugins) plugin.disable(); allFunctions.clear(); allOperators.clear(); - allNumbers.clear(); - allConstantProviders.clear(); listeners.forEach(e -> e.onUnload(this)); } @@ -229,23 +183,6 @@ public class PluginManager { return allOperators; } - /** - * Gets all the number implementations loaded by the Plugin Manager - * - * @return the set of all implementations that were loaded - */ - public Set getAllNumbers() { - 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. * diff --git a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index c916c7c..be6b3e8 100755 --- a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -6,9 +6,7 @@ import org.nwapw.abacus.function.OperatorAssociativity; import org.nwapw.abacus.function.OperatorType; import org.nwapw.abacus.number.NaiveNumber; import org.nwapw.abacus.number.NumberInterface; -import org.nwapw.abacus.number.PreciseNumber; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; import java.util.function.BiFunction; @@ -340,9 +338,6 @@ public class StandardPlugin extends Plugin { @Override public void onEnable() { - registerNumber("naive", NaiveNumber.class); - registerNumber("precise", PreciseNumber.class); - registerOperator("+", OP_ADD); registerOperator("-", OP_SUBTRACT); registerOperator("*", OP_MULTIPLY); From 3f2c4ae2493c4f0bf8017d0241281c58fce8bdb9 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 3 Aug 2017 20:50:53 -0700 Subject: [PATCH 05/10] Add a class that keeps track of the implementation information. --- .../abacus/plugin/NumberImplementation.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java diff --git a/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java b/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java new file mode 100644 index 0000000..c57fbc6 --- /dev/null +++ b/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java @@ -0,0 +1,17 @@ +package org.nwapw.abacus.plugin; + +import org.nwapw.abacus.number.NumberInterface; + +import java.util.Map; +import java.util.function.Function; + +public abstract class NumberImplementation { + + private Class implementation; + private Map> promotionPaths; + private int priority; + + public abstract NumberInterface instanceForString(String string); + public abstract NumberInterface instanceForPi(); + +} From 8a79d0c2df803a9442b03ec2640dc75177b3720d Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 3 Aug 2017 21:05:25 -0700 Subject: [PATCH 06/10] Add the number implementation to the plugin manager, and use it. --- src/main/java/org/nwapw/abacus/Abacus.java | 21 ++++---------- .../abacus/plugin/NumberImplementation.java | 7 +++++ .../java/org/nwapw/abacus/plugin/Plugin.java | 21 ++++++++++++++ .../nwapw/abacus/plugin/PluginManager.java | 14 ++++++++++ .../nwapw/abacus/plugin/StandardPlugin.java | 28 +++++++++++++++++++ 5 files changed, 76 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/nwapw/abacus/Abacus.java b/src/main/java/org/nwapw/abacus/Abacus.java index 1ee8f8a..6e7f27e 100644 --- a/src/main/java/org/nwapw/abacus/Abacus.java +++ b/src/main/java/org/nwapw/abacus/Abacus.java @@ -1,12 +1,12 @@ package org.nwapw.abacus; import org.nwapw.abacus.config.ConfigurationObject; -import org.nwapw.abacus.number.NaiveNumber; import org.nwapw.abacus.number.NumberInterface; import org.nwapw.abacus.parsing.LexerTokenizer; import org.nwapw.abacus.parsing.ShuntingYardParser; import org.nwapw.abacus.parsing.TreeBuilder; import org.nwapw.abacus.plugin.ClassFinder; +import org.nwapw.abacus.plugin.NumberImplementation; import org.nwapw.abacus.plugin.PluginManager; import org.nwapw.abacus.plugin.StandardPlugin; import org.nwapw.abacus.tree.NumberReducer; @@ -16,7 +16,6 @@ import org.nwapw.abacus.window.Window; import javax.swing.*; import java.io.File; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; /** * The main calculator class. This is responsible @@ -25,10 +24,7 @@ import java.lang.reflect.InvocationTargetException; */ public class Abacus { - /** - * The default implementation to use for the number representation. - */ - public static final Class DEFAULT_NUMBER = NaiveNumber.class; + public static final NumberImplementation DEFAULT_IMPLEMENTATION = StandardPlugin.IMPLEMENTATION_NAIVE; /** * The file used for saving and loading configuration. */ @@ -154,15 +150,10 @@ public class Abacus { * @return the resulting number. */ public NumberInterface numberFromString(String numberString) { - Class toInstantiate = - pluginManager.numberFor(configuration.getNumberImplementation()); - if (toInstantiate == null) toInstantiate = DEFAULT_NUMBER; + NumberImplementation toInstantiate = + pluginManager.numberImplementationFor(configuration.getNumberImplementation()); + if (toInstantiate == null) toInstantiate = DEFAULT_IMPLEMENTATION; - try { - return toInstantiate.getConstructor(String.class).newInstance(numberString); - } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { - e.printStackTrace(); - } - return null; + return toInstantiate.instanceForString(numberString); } } diff --git a/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java b/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java index c57fbc6..99019cf 100644 --- a/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java +++ b/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java @@ -2,6 +2,7 @@ package org.nwapw.abacus.plugin; import org.nwapw.abacus.number.NumberInterface; +import java.util.HashMap; import java.util.Map; import java.util.function.Function; @@ -11,6 +12,12 @@ public abstract class NumberImplementation { private Map> promotionPaths; private int priority; + public NumberImplementation(Class implementation, int priority){ + this.implementation = implementation; + this.priority = priority; + promotionPaths = new HashMap<>(); + } + public abstract NumberInterface instanceForString(String string); public abstract NumberInterface instanceForPi(); diff --git a/src/main/java/org/nwapw/abacus/plugin/Plugin.java b/src/main/java/org/nwapw/abacus/plugin/Plugin.java index 2757841..e82df64 100644 --- a/src/main/java/org/nwapw/abacus/plugin/Plugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/Plugin.java @@ -24,6 +24,10 @@ public abstract class Plugin { * A hash map of operators mapped to their string names. */ private Map operators; + /** + * The map of the number implementations this plugin provides. + */ + private Map numberImplementations; /** * The plugin manager in which to search for functions * not inside this package, @@ -46,6 +50,7 @@ public abstract class Plugin { this.manager = manager; functions = new HashMap<>(); operators = new HashMap<>(); + numberImplementations = new HashMap<>(); enabled = false; } @@ -67,6 +72,10 @@ public abstract class Plugin { return operators.keySet(); } + public final Set providedNumberImplementations(){ + return numberImplementations.keySet(); + } + /** * Gets a function under the given function name. * @@ -87,6 +96,10 @@ public abstract class Plugin { return operators.get(operatorName); } + public final NumberImplementation getNumberImplementation(String name){ + return numberImplementations.get(name); + } + /** * Enables the function, loading the necessary instances * of functions. @@ -132,6 +145,10 @@ public abstract class Plugin { operators.put(name, operator); } + protected final void registerNumberImplementation(String name, NumberImplementation implementation){ + numberImplementations.put(name, implementation); + } + /** * Searches the PluginManager for the given function name. * This can be used by the plugins internally in order to call functions @@ -156,6 +173,10 @@ public abstract class Plugin { return manager.operatorFor(name); } + protected final NumberImplementation numberImplementationFor(String name){ + return manager.numberImplementationFor(name); + } + /** * 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 1f61a56..7d837cb 100644 --- a/src/main/java/org/nwapw/abacus/plugin/PluginManager.java +++ b/src/main/java/org/nwapw/abacus/plugin/PluginManager.java @@ -30,6 +30,7 @@ public class PluginManager { * that is, found in a plugin and returned. */ private Map cachedOperators; + private Map cachedNumberImplementations; /** * List of all functions loaded by the plugins. */ @@ -38,6 +39,7 @@ public class PluginManager { * List of all operators loaded by the plugins. */ private Set allOperators; + private Set allNumberImplementations; /** * The list of plugin listeners attached to this instance. */ @@ -51,8 +53,10 @@ public class PluginManager { plugins = new HashSet<>(); cachedFunctions = new HashMap<>(); cachedOperators = new HashMap<>(); + cachedNumberImplementations = new HashMap<>(); allFunctions = new HashSet<>(); allOperators = new HashSet<>(); + allNumberImplementations = new HashSet<>(); listeners = new HashSet<>(); } @@ -109,6 +113,10 @@ public class PluginManager { return searchCached(plugins, cachedOperators, Plugin::providedOperators, Plugin::getOperator, name); } + public NumberImplementation numberImplementationFor(String name){ + return searchCached(plugins, cachedNumberImplementations, Plugin::providedNumberImplementations, + Plugin::getNumberImplementation, name); + } /** * Adds an instance of Plugin that already has been instantiated. * @@ -143,6 +151,7 @@ public class PluginManager { for (Plugin plugin : plugins) { allFunctions.addAll(plugin.providedFunctions()); allOperators.addAll(plugin.providedOperators()); + allNumberImplementations.addAll(plugin.providedNumberImplementations()); } listeners.forEach(e -> e.onLoad(this)); } @@ -154,6 +163,7 @@ public class PluginManager { for (Plugin plugin : plugins) plugin.disable(); allFunctions.clear(); allOperators.clear(); + allNumberImplementations.clear(); listeners.forEach(e -> e.onUnload(this)); } @@ -183,6 +193,10 @@ public class PluginManager { return allOperators; } + public Set getAllNumberImplementations(){ + return allNumberImplementations; + } + /** * Adds a plugin change listener to this plugin manager. * diff --git a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index be6b3e8..e7729e0 100755 --- a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -6,6 +6,7 @@ import org.nwapw.abacus.function.OperatorAssociativity; import org.nwapw.abacus.function.OperatorType; import org.nwapw.abacus.number.NaiveNumber; import org.nwapw.abacus.number.NumberInterface; +import org.nwapw.abacus.number.PreciseNumber; import java.util.ArrayList; import java.util.HashMap; @@ -306,6 +307,30 @@ public class StandardPlugin extends Plugin { } }; + public static final NumberImplementation IMPLEMENTATION_NAIVE = new NumberImplementation(NaiveNumber.class, 0) { + @Override + public NumberInterface instanceForString(String string) { + return new NaiveNumber(string); + } + + @Override + public NumberInterface instanceForPi() { + return new NaiveNumber(Math.PI); + } + }; + + public static final NumberImplementation IMPLEMENTATION_PRECISE = new NumberImplementation(PreciseNumber.class, 0) { + @Override + public NumberInterface instanceForString(String string) { + return new PreciseNumber(string); + } + + @Override + public NumberInterface instanceForPi() { + return null; + } + }; + public StandardPlugin(PluginManager manager) { super(manager); } @@ -338,6 +363,9 @@ public class StandardPlugin extends Plugin { @Override public void onEnable() { + registerNumberImplementation("naive", IMPLEMENTATION_NAIVE); + registerNumberImplementation("precise", IMPLEMENTATION_PRECISE); + registerOperator("+", OP_ADD); registerOperator("-", OP_SUBTRACT); registerOperator("*", OP_MULTIPLY); From 4fd30030f90a5283923a516d1af596534f399f68 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 3 Aug 2017 21:27:53 -0700 Subject: [PATCH 07/10] Make the promotion map protected and use classes. --- src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java b/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java index 99019cf..c3827a5 100644 --- a/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java +++ b/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java @@ -8,8 +8,8 @@ import java.util.function.Function; public abstract class NumberImplementation { + protected Map, Function> promotionPaths; private Class implementation; - private Map> promotionPaths; private int priority; public NumberImplementation(Class implementation, int priority){ From b06e9fcbee295e757b647589cb607233158a0a40 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 3 Aug 2017 22:16:08 -0700 Subject: [PATCH 08/10] Add attempt to find the number implementation from the class. --- .../abacus/plugin/NumberImplementation.java | 12 +++++++++++ .../nwapw/abacus/plugin/PluginManager.java | 21 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java b/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java index c3827a5..60e02c6 100644 --- a/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java +++ b/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java @@ -18,6 +18,18 @@ public abstract class NumberImplementation { promotionPaths = new HashMap<>(); } + public final Map, Function> getPromotionPaths(){ + return promotionPaths; + } + + public final Class getImplementation(){ + return implementation; + } + + public final int getPriority(){ + return priority; + } + public abstract NumberInterface instanceForString(String string); public abstract NumberInterface instanceForPi(); diff --git a/src/main/java/org/nwapw/abacus/plugin/PluginManager.java b/src/main/java/org/nwapw/abacus/plugin/PluginManager.java index 7d837cb..9c98dfd 100644 --- a/src/main/java/org/nwapw/abacus/plugin/PluginManager.java +++ b/src/main/java/org/nwapw/abacus/plugin/PluginManager.java @@ -2,6 +2,7 @@ package org.nwapw.abacus.plugin; import org.nwapw.abacus.function.Function; import org.nwapw.abacus.function.Operator; +import org.nwapw.abacus.number.NumberInterface; import java.lang.reflect.InvocationTargetException; import java.util.*; @@ -31,6 +32,7 @@ public class PluginManager { */ private Map cachedOperators; private Map cachedNumberImplementations; + private Map, NumberImplementation> cachedInterfaceImplementations; /** * List of all functions loaded by the plugins. */ @@ -54,6 +56,7 @@ public class PluginManager { cachedFunctions = new HashMap<>(); cachedOperators = new HashMap<>(); cachedNumberImplementations = new HashMap<>(); + cachedInterfaceImplementations = new HashMap<>(); allFunctions = new HashSet<>(); allOperators = new HashSet<>(); allNumberImplementations = new HashSet<>(); @@ -117,6 +120,24 @@ public class PluginManager { return searchCached(plugins, cachedNumberImplementations, Plugin::providedNumberImplementations, Plugin::getNumberImplementation, name); } + + public NumberImplementation interfaceImplementationFor(Class name){ + if(cachedInterfaceImplementations.containsKey(name)) return cachedInterfaceImplementations.get(name); + NumberImplementation toReturn = null; + outside: + for(Plugin plugin : plugins){ + for(String implementationName : plugin.providedNumberImplementations()){ + NumberImplementation implementation = plugin.getNumberImplementation(implementationName); + if(implementation.getImplementation().equals(name)) { + toReturn = implementation; + break outside; + } + } + } + cachedInterfaceImplementations.put(name, toReturn); + return toReturn; + } + /** * Adds an instance of Plugin that already has been instantiated. * From 276b6719fde4d4dda44486b729935b6ceaf88912 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 4 Aug 2017 09:55:24 -0700 Subject: [PATCH 09/10] Implement a getPi function for the plugin, and use the new pi value. --- .../nwapw/abacus/number/PreciseNumber.java | 7 +- .../java/org/nwapw/abacus/plugin/Plugin.java | 5 ++ .../nwapw/abacus/plugin/StandardPlugin.java | 70 ++++++++----------- 3 files changed, 36 insertions(+), 46 deletions(-) diff --git a/src/main/java/org/nwapw/abacus/number/PreciseNumber.java b/src/main/java/org/nwapw/abacus/number/PreciseNumber.java index c648e7c..df88105 100755 --- a/src/main/java/org/nwapw/abacus/number/PreciseNumber.java +++ b/src/main/java/org/nwapw/abacus/number/PreciseNumber.java @@ -1,7 +1,6 @@ package org.nwapw.abacus.number; import java.math.BigDecimal; -import java.math.MathContext; import java.math.RoundingMode; public class PreciseNumber implements NumberInterface { @@ -9,15 +8,15 @@ public class PreciseNumber implements NumberInterface { /** * The number one. */ - static final PreciseNumber ONE = new PreciseNumber(BigDecimal.ONE); + public static final PreciseNumber ONE = new PreciseNumber(BigDecimal.ONE); /** * The number zero. */ - static final PreciseNumber ZERO = new PreciseNumber(BigDecimal.ZERO); + public static final PreciseNumber ZERO = new PreciseNumber(BigDecimal.ZERO); /** * The number ten. */ - static final PreciseNumber TEN = new PreciseNumber(BigDecimal.TEN); + public static final PreciseNumber TEN = new PreciseNumber(BigDecimal.TEN); /** * The value of the PreciseNumber. diff --git a/src/main/java/org/nwapw/abacus/plugin/Plugin.java b/src/main/java/org/nwapw/abacus/plugin/Plugin.java index e82df64..fbc4853 100644 --- a/src/main/java/org/nwapw/abacus/plugin/Plugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/Plugin.java @@ -2,6 +2,7 @@ package org.nwapw.abacus.plugin; import org.nwapw.abacus.function.Function; import org.nwapw.abacus.function.Operator; +import org.nwapw.abacus.number.NumberInterface; import java.util.HashMap; import java.util.Map; @@ -177,6 +178,10 @@ public abstract class Plugin { return manager.numberImplementationFor(name); } + protected final NumberInterface getPi(Class forClass){ + return manager.interfaceImplementationFor(forClass).instanceForPi(); + } + /** * 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/StandardPlugin.java b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index e7729e0..3dc2b28 100755 --- a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -19,7 +19,6 @@ import java.util.function.BiFunction; public class StandardPlugin extends Plugin { private static HashMap, ArrayList> factorialLists = new HashMap, ArrayList>(); - static HashMap, NumberInterface> piValues = new HashMap, NumberInterface>(); /** * The addition operator, + @@ -284,7 +283,7 @@ public class StandardPlugin extends Plugin { /** * The sine function (the argument is interpreted in radians). */ - public static final Function FUNCTION_SIN = new Function() { + public final Function functionSin = new Function() { @Override protected boolean matchesParams(NumberInterface[] params) { return params.length == 1; @@ -294,7 +293,7 @@ public class StandardPlugin extends Plugin { protected NumberInterface applyInternal(NumberInterface[] params) { NumberInterface pi = getPi(params[0].getClass()); NumberInterface twoPi = pi.multiply(new NaiveNumber(2).promoteTo(pi.getClass())); - NumberInterface theta = getSmallAngle(params[0]); + NumberInterface theta = getSmallAngle(params[0], pi); //System.out.println(theta); if(theta.compareTo(pi.multiply(new NaiveNumber(1.5).promoteTo(twoPi.getClass()))) >= 0){ theta = theta.subtract(twoPi); @@ -327,7 +326,29 @@ public class StandardPlugin extends Plugin { @Override public NumberInterface instanceForPi() { - return null; + NumberInterface C = FUNCTION_SQRT.apply(new PreciseNumber("10005")).multiply(new PreciseNumber("426880")); + NumberInterface M = PreciseNumber.ONE; + NumberInterface L = new PreciseNumber("13591409"); + NumberInterface X = M; + NumberInterface sum = L; + int termsNeeded = C.getMaxPrecision()/13 + 1; + + NumberInterface lSummand = new PreciseNumber("545140134"); + NumberInterface xMultiplier = new PreciseNumber("262537412") + .multiply(new PreciseNumber("1000000000")) + .add(new PreciseNumber("640768000")) + .negate(); + for(int i = 0; i < termsNeeded; i++){ + M = M + .multiply(new NaiveNumber(12*i+2).promoteTo(PreciseNumber.class)) + .multiply(new NaiveNumber(12*i+6).promoteTo(PreciseNumber.class)) + .multiply(new NaiveNumber(12*i+10).promoteTo(PreciseNumber.class)) + .divide(new NaiveNumber(Math.pow(i+1,3)).promoteTo(PreciseNumber.class)); + L = L.add(lSummand); + X = X.multiply(xMultiplier); + sum = sum.add(M.multiply(L).divide(X)); + } + return C.divide(sum); } }; @@ -377,7 +398,7 @@ public class StandardPlugin extends Plugin { registerFunction("exp", FUNCTION_EXP); registerFunction("ln", FUNCTION_LN); registerFunction("sqrt", FUNCTION_SQRT); - registerFunction("sin", FUNCTION_SIN); + registerFunction("sin", functionSin); } @Override @@ -425,48 +446,13 @@ public class StandardPlugin extends Plugin { return sum; } - /** - * Returns an approximation of Pi, with appropriate accuracy for given number class. - * @param numClass type of number. - * @return A number of class numClass, with value approximately Pi = 3.1415... - */ - public static NumberInterface getPi(Class numClass){ - if(!piValues.containsKey(numClass)){ - //https://en.wikipedia.org/wiki/Chudnovsky_algorithm - NumberInterface C = FUNCTION_SQRT.apply(new NaiveNumber(10005).promoteTo(numClass)).multiply(new NaiveNumber(426880).promoteTo(numClass)); - NumberInterface M = NaiveNumber.ONE.promoteTo(numClass); - NumberInterface L = new NaiveNumber(13591409).promoteTo(numClass); - NumberInterface X = M; - NumberInterface sum = L; - int termsNeeded = C.getMaxPrecision()/13 + 1; - - NumberInterface lSummand = new NaiveNumber(545140134).promoteTo(L.getClass()); - NumberInterface xMultiplier = new NaiveNumber(262537412).promoteTo(X.getClass()) - .multiply(new NaiveNumber(1000000000).promoteTo(X.getClass())) - .add(new NaiveNumber(640768000).promoteTo(X.getClass())) - .negate(); - for(int i = 0; i < termsNeeded; i++){ - M = M - .multiply(new NaiveNumber(12*i+2).promoteTo(M.getClass())) - .multiply(new NaiveNumber(12*i+6).promoteTo(M.getClass())) - .multiply(new NaiveNumber(12*i+10).promoteTo(M.getClass())) - .divide(new NaiveNumber(Math.pow(i+1,3)).promoteTo(M.getClass())); - L = L.add(lSummand); - X = X.multiply(xMultiplier); - sum = sum.add(M.multiply(L).divide(X)); - } - piValues.put(numClass, C.divide(sum)); - } - return piValues.get(numClass); - } - /** * Returns an equivalent angle in the interval [0, 2pi) * @param phi an angle (in radians). * @return theta in [0, 2pi) that differs from phi by a multiple of 2pi. */ - private static NumberInterface getSmallAngle(NumberInterface phi){ - NumberInterface twoPi = getPi(phi.getClass()).multiply(new NaiveNumber("2").promoteTo(phi.getClass())); + private static NumberInterface getSmallAngle(NumberInterface phi, NumberInterface pi){ + NumberInterface twoPi = pi.multiply(new NaiveNumber("2").promoteTo(phi.getClass())); NumberInterface theta = FUNCTION_ABS.apply(phi).subtract(twoPi .multiply(FUNCTION_ABS.apply(phi).divide(twoPi).floor())); //Now theta is in [0, 2pi). if(phi.signum() < 0){ From 8891b04598c3ffd7d67c8012600451b2febfb0b5 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 4 Aug 2017 11:24:07 -0700 Subject: [PATCH 10/10] Add comments to all the pi-related stuff. --- .../abacus/plugin/NumberImplementation.java | 39 +++++++++++++++ .../java/org/nwapw/abacus/plugin/Plugin.java | 35 +++++++++++++- .../nwapw/abacus/plugin/PluginManager.java | 47 +++++++++++++++++++ .../nwapw/abacus/plugin/StandardPlugin.java | 6 +++ 4 files changed, 126 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java b/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java index 60e02c6..66e2dd2 100644 --- a/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java +++ b/src/main/java/org/nwapw/abacus/plugin/NumberImplementation.java @@ -6,31 +6,70 @@ import java.util.HashMap; import java.util.Map; import java.util.function.Function; +/** + * A class that holds data about a number implementation. + */ public abstract class NumberImplementation { + /** + * The list of paths through which this implementation can be promoted. + */ protected Map, Function> promotionPaths; + /** + * The implementation class for this implementation. + */ private Class implementation; + /** + * The priority of converting into this number implementation. + */ private int priority; + /** + * Creates a new number implementation with the given data. + * @param implementation the implementation class. + * @param priority the priority, higher -> more likely to be converted into. + */ public NumberImplementation(Class implementation, int priority){ this.implementation = implementation; this.priority = priority; promotionPaths = new HashMap<>(); } + /** + * Gets the list of all promotion paths this implementation can take. + * @return the map of documentation paths. + */ public final Map, Function> getPromotionPaths(){ return promotionPaths; } + /** + * Gets the implementation class used by this implementation. + * @return the implementation class. + */ public final Class getImplementation(){ return implementation; } + /** + * Gets the priority of this number implementation. + * @return the priority. + */ public final int getPriority(){ return priority; } + /** + * Abstract function to create a new instance from a string. + * @param string the string to create a number from. + * @return the resulting number. + */ public abstract NumberInterface instanceForString(String string); + + /** + * Get the instance of pi with the given implementation. + * @return pi + */ public abstract NumberInterface instanceForPi(); } diff --git a/src/main/java/org/nwapw/abacus/plugin/Plugin.java b/src/main/java/org/nwapw/abacus/plugin/Plugin.java index fbc4853..3a0ed31 100644 --- a/src/main/java/org/nwapw/abacus/plugin/Plugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/Plugin.java @@ -73,6 +73,11 @@ public abstract class Plugin { return operators.keySet(); } + /** + * Gets the list of number implementations provided by this plugin. + * + * @return the list of registered number implementations. + */ public final Set providedNumberImplementations(){ return numberImplementations.keySet(); } @@ -97,6 +102,12 @@ public abstract class Plugin { return operators.get(operatorName); } + /** + * Gets the number implementation under the given name. + * + * @param name the name of the number implementation to look up. + * @return the number implementation associated with that name, or null if the plugin doesn't provide it. + */ public final NumberImplementation getNumberImplementation(String name){ return numberImplementations.get(name); } @@ -146,6 +157,12 @@ public abstract class Plugin { operators.put(name, operator); } + /** + * To be used in load(). Registers a new number implementation with the plugin. + * This makes it accessible to the plugin manager. + * @param name the name of the implementation. + * @param implementation the actual implementation class to register. + */ protected final void registerNumberImplementation(String name, NumberImplementation implementation){ numberImplementations.put(name, implementation); } @@ -174,12 +191,28 @@ public abstract class Plugin { return manager.operatorFor(name); } + /** + * Searches the PluginManager for the given number implementation + * name. This can be used by the plugins internally in order to find + * implementations that they do not provide. + * + * @param name the name for which to search. + * @return the resulting number implementation, or null if none was found. + */ protected final NumberImplementation numberImplementationFor(String name){ return manager.numberImplementationFor(name); } + /** + * Searches the plugin manager for a Pi value for the given number implementation. + * This is done so that number implementations with various degrees of precision + * can provide their own pi values, without losing said precision by + * promoting NaiveNumbers. + * @param forClass the class to which to find the pi instance. + * @return the pi value for the given class. + */ protected final NumberInterface getPi(Class forClass){ - return manager.interfaceImplementationFor(forClass).instanceForPi(); + return manager.piFor(forClass); } /** diff --git a/src/main/java/org/nwapw/abacus/plugin/PluginManager.java b/src/main/java/org/nwapw/abacus/plugin/PluginManager.java index 9c98dfd..0d45964 100644 --- a/src/main/java/org/nwapw/abacus/plugin/PluginManager.java +++ b/src/main/java/org/nwapw/abacus/plugin/PluginManager.java @@ -31,8 +31,20 @@ public class PluginManager { * that is, found in a plugin and returned. */ private Map cachedOperators; + /** + * The list of number implementations that have + * been cached, that is, found in a plugin and returned. + */ private Map cachedNumberImplementations; + /** + * The list of number implementations that have been + * found by their implementation class. + */ private Map, NumberImplementation> cachedInterfaceImplementations; + /** + * The pi values for each implementation class that have already been computer. + */ + private Map, NumberInterface> cachedPi; /** * List of all functions loaded by the plugins. */ @@ -41,6 +53,9 @@ public class PluginManager { * List of all operators loaded by the plugins. */ private Set allOperators; + /** + * List of all the number implementations loaded by the plugins. + */ private Set allNumberImplementations; /** * The list of plugin listeners attached to this instance. @@ -57,6 +72,7 @@ public class PluginManager { cachedOperators = new HashMap<>(); cachedNumberImplementations = new HashMap<>(); cachedInterfaceImplementations = new HashMap<>(); + cachedPi = new HashMap<>(); allFunctions = new HashSet<>(); allOperators = new HashSet<>(); allNumberImplementations = new HashSet<>(); @@ -116,11 +132,21 @@ public class PluginManager { return searchCached(plugins, cachedOperators, Plugin::providedOperators, Plugin::getOperator, name); } + /** + * Gets the number implementation under the given name. + * @param name the name of the implementation. + * @return the implementation. + */ public NumberImplementation numberImplementationFor(String name){ return searchCached(plugins, cachedNumberImplementations, Plugin::providedNumberImplementations, Plugin::getNumberImplementation, name); } + /** + * Gets the number implementation for the given implementation class. + * @param name the class for which to find the implementation. + * @return the implementation. + */ public NumberImplementation interfaceImplementationFor(Class name){ if(cachedInterfaceImplementations.containsKey(name)) return cachedInterfaceImplementations.get(name); NumberImplementation toReturn = null; @@ -138,6 +164,22 @@ public class PluginManager { return toReturn; } + /** + * Gets the mathematical constant pi for the given implementation class. + * @param forClass the class for which to find pi. + * @return pi + */ + public NumberInterface piFor(Class forClass){ + if(cachedPi.containsKey(forClass)) return cachedPi.get(forClass); + NumberImplementation implementation = interfaceImplementationFor(forClass); + NumberInterface generatedPi = null; + if(implementation != null){ + generatedPi = implementation.instanceForPi(); + } + cachedPi.put(forClass, generatedPi); + return generatedPi; + } + /** * Adds an instance of Plugin that already has been instantiated. * @@ -214,6 +256,11 @@ public class PluginManager { return allOperators; } + /** + * Gets all the number implementations loaded by the Plugin Manager. + * + * @return the set of all implementations that were loaded. + */ public Set getAllNumberImplementations(){ return allNumberImplementations; } diff --git a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index 3dc2b28..0c0ab01 100755 --- a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -306,6 +306,9 @@ public class StandardPlugin extends Plugin { } }; + /** + * The implementation for double-based naive numbers. + */ public static final NumberImplementation IMPLEMENTATION_NAIVE = new NumberImplementation(NaiveNumber.class, 0) { @Override public NumberInterface instanceForString(String string) { @@ -318,6 +321,9 @@ public class StandardPlugin extends Plugin { } }; + /** + * The implementation for the infinite-precision BigDecimal. + */ public static final NumberImplementation IMPLEMENTATION_PRECISE = new NumberImplementation(PreciseNumber.class, 0) { @Override public NumberInterface instanceForString(String string) {