mirror of
				https://github.com/DanilaFe/abacus
				synced 2025-11-04 02:43:41 -08:00 
			
		
		
		
	Add the list of number interface providers to Plugins.
This commit is contained in:
		
							parent
							
								
									7c0f0046f8
								
							
						
					
					
						commit
						7dda1f2fcf
					
				@ -29,6 +29,10 @@ public abstract class Plugin {
 | 
				
			|||||||
     * A hash map of operators mapped to their string names.
 | 
					     * A hash map of operators mapped to their string names.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    private Map<String, Class<? extends NumberInterface>> numbers;
 | 
					    private Map<String, Class<? extends NumberInterface>> numbers;
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * A hash map of constant providers for each number type.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    private Map<Class<?>, java.util.function.Function<String, NumberInterface>> constantProviders;
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * The plugin manager in which to search for functions
 | 
					     * The plugin manager in which to search for functions
 | 
				
			||||||
     * not inside this package,
 | 
					     * not inside this package,
 | 
				
			||||||
@ -52,6 +56,7 @@ public abstract class Plugin {
 | 
				
			|||||||
        functions = new HashMap<>();
 | 
					        functions = new HashMap<>();
 | 
				
			||||||
        operators = new HashMap<>();
 | 
					        operators = new HashMap<>();
 | 
				
			||||||
        numbers = new HashMap<>();
 | 
					        numbers = new HashMap<>();
 | 
				
			||||||
 | 
					        constantProviders = new HashMap<>();
 | 
				
			||||||
        enabled = false;
 | 
					        enabled = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -82,6 +87,14 @@ public abstract class Plugin {
 | 
				
			|||||||
        return numbers.keySet();
 | 
					        return numbers.keySet();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Gets the list of all constant providers provided by this plugin.
 | 
				
			||||||
 | 
					     * @return the list of constant providers.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public final Set<Class<?>> providedConstantProviders() {
 | 
				
			||||||
 | 
					        return constantProviders.keySet();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Gets a function under the given function name.
 | 
					     * Gets a function under the given function name.
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
@ -112,6 +125,16 @@ public abstract class Plugin {
 | 
				
			|||||||
        return numbers.get(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<String, NumberInterface> getConstantProvider(Class<?> pluginClass){
 | 
				
			||||||
 | 
					        return constantProviders.get(pluginClass);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Enables the function, loading the necessary instances
 | 
					     * Enables the function, loading the necessary instances
 | 
				
			||||||
     * of functions.
 | 
					     * of functions.
 | 
				
			||||||
@ -131,6 +154,8 @@ public abstract class Plugin {
 | 
				
			|||||||
        onDisable();
 | 
					        onDisable();
 | 
				
			||||||
        functions.clear();
 | 
					        functions.clear();
 | 
				
			||||||
        operators.clear();
 | 
					        operators.clear();
 | 
				
			||||||
 | 
					        numbers.clear();
 | 
				
			||||||
 | 
					        constantProviders.clear();
 | 
				
			||||||
        enabled = false;
 | 
					        enabled = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -170,6 +195,19 @@ public abstract class Plugin {
 | 
				
			|||||||
        numbers.put(name, 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<String, NumberInterface> constantProvider) {
 | 
				
			||||||
 | 
					        constantProviders.put(providerFor, constantProvider);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Searches the PluginManager for the given function name.
 | 
					     * Searches the PluginManager for the given function name.
 | 
				
			||||||
     * This can be used by the plugins internally in order to call functions
 | 
					     * 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);
 | 
					        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<? extends NumberInterface> 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<String, NumberInterface> constantProviderFor(Class<?> forClass){
 | 
				
			||||||
 | 
					        return manager.constantProviderFor(forClass);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Abstract method to be overridden by plugin implementation, in which the plugins
 | 
					     * Abstract method to be overridden by plugin implementation, in which the plugins
 | 
				
			||||||
     * are supposed to register the functions they provide and do any other
 | 
					     * are supposed to register the functions they provide and do any other
 | 
				
			||||||
 | 
				
			|||||||
@ -2,7 +2,6 @@ package org.nwapw.abacus.plugin;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import org.nwapw.abacus.function.Function;
 | 
					import org.nwapw.abacus.function.Function;
 | 
				
			||||||
import org.nwapw.abacus.function.Operator;
 | 
					import org.nwapw.abacus.function.Operator;
 | 
				
			||||||
import org.nwapw.abacus.number.NaiveNumber;
 | 
					 | 
				
			||||||
import org.nwapw.abacus.number.NumberInterface;
 | 
					import org.nwapw.abacus.number.NumberInterface;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.lang.reflect.InvocationTargetException;
 | 
					import java.lang.reflect.InvocationTargetException;
 | 
				
			||||||
@ -41,7 +40,7 @@ public class PluginManager {
 | 
				
			|||||||
     * List of registered constant providers for every
 | 
					     * List of registered constant providers for every
 | 
				
			||||||
     * number class.
 | 
					     * number class.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    private Map<Class<?>, java.util.function.Function<String, NaiveNumber>> cachedConstantProviders;
 | 
					    private Map<Class<?>, java.util.function.Function<String, NumberInterface>> cachedConstantProviders;
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * List of all functions loaded by the plugins.
 | 
					     * List of all functions loaded by the plugins.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
@ -54,6 +53,10 @@ public class PluginManager {
 | 
				
			|||||||
     * List of all numbers loaded by the plugins.
 | 
					     * List of all numbers loaded by the plugins.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    private Set<String> allNumbers;
 | 
					    private Set<String> allNumbers;
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * List of all the constant providers loaded by the plugins.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    private Set<Class<?>> allConstantProviders;
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * The list of plugin listeners attached to this instance.
 | 
					     * The list of plugin listeners attached to this instance.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
@ -72,6 +75,7 @@ public class PluginManager {
 | 
				
			|||||||
        allFunctions = new HashSet<>();
 | 
					        allFunctions = new HashSet<>();
 | 
				
			||||||
        allOperators = new HashSet<>();
 | 
					        allOperators = new HashSet<>();
 | 
				
			||||||
        allNumbers = new HashSet<>();
 | 
					        allNumbers = new HashSet<>();
 | 
				
			||||||
 | 
					        allConstantProviders = new HashSet<>();
 | 
				
			||||||
        listeners = 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 getFunction the function to get the T value under the given name
 | 
				
			||||||
     * @param name        the name to search for
 | 
					     * @param name        the name to search for
 | 
				
			||||||
     * @param <T>         the type of element being search
 | 
					     * @param <T>         the type of element being search
 | 
				
			||||||
 | 
					     * @param <K>         the type of key that the cache is indexed by.
 | 
				
			||||||
     * @return the retrieved element, or null if it was not found.
 | 
					     * @return the retrieved element, or null if it was not found.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    private static <T, K> T searchCached(Collection<Plugin> plugins, Map<K, T> cache,
 | 
					    private static <T, K> T searchCached(Collection<Plugin> plugins, Map<K, T> cache,
 | 
				
			||||||
@ -137,6 +142,15 @@ public class PluginManager {
 | 
				
			|||||||
        return searchCached(plugins, cachedNumbers, Plugin::providedNumbers, Plugin::getNumber, 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<String, NumberInterface> constantProviderFor(Class<?> forClass){
 | 
				
			||||||
 | 
					        return searchCached(plugins, cachedConstantProviders, Plugin::providedConstantProviders, Plugin::getConstantProvider, forClass);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Adds an instance of Plugin that already has been instantiated.
 | 
					     * Adds an instance of Plugin that already has been instantiated.
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
@ -172,6 +186,7 @@ public class PluginManager {
 | 
				
			|||||||
            allFunctions.addAll(plugin.providedFunctions());
 | 
					            allFunctions.addAll(plugin.providedFunctions());
 | 
				
			||||||
            allOperators.addAll(plugin.providedOperators());
 | 
					            allOperators.addAll(plugin.providedOperators());
 | 
				
			||||||
            allNumbers.addAll(plugin.providedNumbers());
 | 
					            allNumbers.addAll(plugin.providedNumbers());
 | 
				
			||||||
 | 
					            allConstantProviders.addAll(plugin.providedConstantProviders());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        listeners.forEach(e -> e.onLoad(this));
 | 
					        listeners.forEach(e -> e.onLoad(this));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -184,6 +199,7 @@ public class PluginManager {
 | 
				
			|||||||
        allFunctions.clear();
 | 
					        allFunctions.clear();
 | 
				
			||||||
        allOperators.clear();
 | 
					        allOperators.clear();
 | 
				
			||||||
        allNumbers.clear();
 | 
					        allNumbers.clear();
 | 
				
			||||||
 | 
					        allConstantProviders.clear();
 | 
				
			||||||
        listeners.forEach(e -> e.onUnload(this));
 | 
					        listeners.forEach(e -> e.onUnload(this));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -222,6 +238,14 @@ public class PluginManager {
 | 
				
			|||||||
        return allNumbers;
 | 
					        return allNumbers;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Gets all the constant providers loaded by the Plugin Manager.
 | 
				
			||||||
 | 
					     * @return the set of all constant providers that were loaded.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public Set<Class<?>> getAllConstantProviders() {
 | 
				
			||||||
 | 
					        return allConstantProviders;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Adds a plugin change listener to this plugin manager.
 | 
					     * Adds a plugin change listener to this plugin manager.
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user