mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-24 08:12:39 -08:00
Add comments to all the pi-related stuff.
This commit is contained in:
parent
276b6719fd
commit
8891b04598
|
@ -6,31 +6,70 @@ import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A class that holds data about a number implementation.
|
||||||
|
*/
|
||||||
public abstract class NumberImplementation {
|
public abstract class NumberImplementation {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The list of paths through which this implementation can be promoted.
|
||||||
|
*/
|
||||||
protected Map<Class<? extends NumberInterface>, Function<NumberInterface, NumberInterface>> promotionPaths;
|
protected Map<Class<? extends NumberInterface>, Function<NumberInterface, NumberInterface>> promotionPaths;
|
||||||
|
/**
|
||||||
|
* The implementation class for this implementation.
|
||||||
|
*/
|
||||||
private Class<? extends NumberInterface> implementation;
|
private Class<? extends NumberInterface> implementation;
|
||||||
|
/**
|
||||||
|
* The priority of converting into this number implementation.
|
||||||
|
*/
|
||||||
private int priority;
|
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<? extends NumberInterface> implementation, int priority){
|
public NumberImplementation(Class<? extends NumberInterface> implementation, int priority){
|
||||||
this.implementation = implementation;
|
this.implementation = implementation;
|
||||||
this.priority = priority;
|
this.priority = priority;
|
||||||
promotionPaths = new HashMap<>();
|
promotionPaths = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the list of all promotion paths this implementation can take.
|
||||||
|
* @return the map of documentation paths.
|
||||||
|
*/
|
||||||
public final Map<Class<? extends NumberInterface>, Function<NumberInterface, NumberInterface>> getPromotionPaths(){
|
public final Map<Class<? extends NumberInterface>, Function<NumberInterface, NumberInterface>> getPromotionPaths(){
|
||||||
return promotionPaths;
|
return promotionPaths;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the implementation class used by this implementation.
|
||||||
|
* @return the implementation class.
|
||||||
|
*/
|
||||||
public final Class<? extends NumberInterface> getImplementation(){
|
public final Class<? extends NumberInterface> getImplementation(){
|
||||||
return implementation;
|
return implementation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the priority of this number implementation.
|
||||||
|
* @return the priority.
|
||||||
|
*/
|
||||||
public final int getPriority(){
|
public final int getPriority(){
|
||||||
return priority;
|
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);
|
public abstract NumberInterface instanceForString(String string);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the instance of pi with the given implementation.
|
||||||
|
* @return pi
|
||||||
|
*/
|
||||||
public abstract NumberInterface instanceForPi();
|
public abstract NumberInterface instanceForPi();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,6 +73,11 @@ public abstract class Plugin {
|
||||||
return operators.keySet();
|
return operators.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the list of number implementations provided by this plugin.
|
||||||
|
*
|
||||||
|
* @return the list of registered number implementations.
|
||||||
|
*/
|
||||||
public final Set<String> providedNumberImplementations(){
|
public final Set<String> providedNumberImplementations(){
|
||||||
return numberImplementations.keySet();
|
return numberImplementations.keySet();
|
||||||
}
|
}
|
||||||
|
@ -97,6 +102,12 @@ public abstract class Plugin {
|
||||||
return operators.get(operatorName);
|
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){
|
public final NumberImplementation getNumberImplementation(String name){
|
||||||
return numberImplementations.get(name);
|
return numberImplementations.get(name);
|
||||||
}
|
}
|
||||||
|
@ -146,6 +157,12 @@ public abstract class Plugin {
|
||||||
operators.put(name, operator);
|
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){
|
protected final void registerNumberImplementation(String name, NumberImplementation implementation){
|
||||||
numberImplementations.put(name, implementation);
|
numberImplementations.put(name, implementation);
|
||||||
}
|
}
|
||||||
|
@ -174,12 +191,28 @@ public abstract class Plugin {
|
||||||
return manager.operatorFor(name);
|
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){
|
protected final NumberImplementation numberImplementationFor(String name){
|
||||||
return manager.numberImplementationFor(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<? extends NumberInterface> forClass){
|
protected final NumberInterface getPi(Class<? extends NumberInterface> forClass){
|
||||||
return manager.interfaceImplementationFor(forClass).instanceForPi();
|
return manager.piFor(forClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -31,8 +31,20 @@ public class PluginManager {
|
||||||
* that is, found in a plugin and returned.
|
* that is, found in a plugin and returned.
|
||||||
*/
|
*/
|
||||||
private Map<String, Operator> cachedOperators;
|
private Map<String, Operator> cachedOperators;
|
||||||
|
/**
|
||||||
|
* The list of number implementations that have
|
||||||
|
* been cached, that is, found in a plugin and returned.
|
||||||
|
*/
|
||||||
private Map<String, NumberImplementation> cachedNumberImplementations;
|
private Map<String, NumberImplementation> cachedNumberImplementations;
|
||||||
|
/**
|
||||||
|
* The list of number implementations that have been
|
||||||
|
* found by their implementation class.
|
||||||
|
*/
|
||||||
private Map<Class<? extends NumberInterface>, NumberImplementation> cachedInterfaceImplementations;
|
private Map<Class<? extends NumberInterface>, NumberImplementation> cachedInterfaceImplementations;
|
||||||
|
/**
|
||||||
|
* The pi values for each implementation class that have already been computer.
|
||||||
|
*/
|
||||||
|
private Map<Class<? extends NumberInterface>, NumberInterface> cachedPi;
|
||||||
/**
|
/**
|
||||||
* List of all functions loaded by the plugins.
|
* List of all functions loaded by the plugins.
|
||||||
*/
|
*/
|
||||||
|
@ -41,6 +53,9 @@ public class PluginManager {
|
||||||
* List of all operators loaded by the plugins.
|
* List of all operators loaded by the plugins.
|
||||||
*/
|
*/
|
||||||
private Set<String> allOperators;
|
private Set<String> allOperators;
|
||||||
|
/**
|
||||||
|
* List of all the number implementations loaded by the plugins.
|
||||||
|
*/
|
||||||
private Set<String> allNumberImplementations;
|
private Set<String> allNumberImplementations;
|
||||||
/**
|
/**
|
||||||
* The list of plugin listeners attached to this instance.
|
* The list of plugin listeners attached to this instance.
|
||||||
|
@ -57,6 +72,7 @@ public class PluginManager {
|
||||||
cachedOperators = new HashMap<>();
|
cachedOperators = new HashMap<>();
|
||||||
cachedNumberImplementations = new HashMap<>();
|
cachedNumberImplementations = new HashMap<>();
|
||||||
cachedInterfaceImplementations = new HashMap<>();
|
cachedInterfaceImplementations = new HashMap<>();
|
||||||
|
cachedPi = new HashMap<>();
|
||||||
allFunctions = new HashSet<>();
|
allFunctions = new HashSet<>();
|
||||||
allOperators = new HashSet<>();
|
allOperators = new HashSet<>();
|
||||||
allNumberImplementations = new HashSet<>();
|
allNumberImplementations = new HashSet<>();
|
||||||
|
@ -116,11 +132,21 @@ public class PluginManager {
|
||||||
return searchCached(plugins, cachedOperators, Plugin::providedOperators, Plugin::getOperator, name);
|
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){
|
public NumberImplementation numberImplementationFor(String name){
|
||||||
return searchCached(plugins, cachedNumberImplementations, Plugin::providedNumberImplementations,
|
return searchCached(plugins, cachedNumberImplementations, Plugin::providedNumberImplementations,
|
||||||
Plugin::getNumberImplementation, name);
|
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<? extends NumberInterface> name){
|
public NumberImplementation interfaceImplementationFor(Class<? extends NumberInterface> name){
|
||||||
if(cachedInterfaceImplementations.containsKey(name)) return cachedInterfaceImplementations.get(name);
|
if(cachedInterfaceImplementations.containsKey(name)) return cachedInterfaceImplementations.get(name);
|
||||||
NumberImplementation toReturn = null;
|
NumberImplementation toReturn = null;
|
||||||
|
@ -138,6 +164,22 @@ public class PluginManager {
|
||||||
return toReturn;
|
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<? extends NumberInterface> 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.
|
* Adds an instance of Plugin that already has been instantiated.
|
||||||
*
|
*
|
||||||
|
@ -214,6 +256,11 @@ public class PluginManager {
|
||||||
return allOperators;
|
return allOperators;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets all the number implementations loaded by the Plugin Manager.
|
||||||
|
*
|
||||||
|
* @return the set of all implementations that were loaded.
|
||||||
|
*/
|
||||||
public Set<String> getAllNumberImplementations(){
|
public Set<String> getAllNumberImplementations(){
|
||||||
return allNumberImplementations;
|
return allNumberImplementations;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
public static final NumberImplementation IMPLEMENTATION_NAIVE = new NumberImplementation(NaiveNumber.class, 0) {
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface instanceForString(String string) {
|
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) {
|
public static final NumberImplementation IMPLEMENTATION_PRECISE = new NumberImplementation(PreciseNumber.class, 0) {
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface instanceForString(String string) {
|
public NumberInterface instanceForString(String string) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user