1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-23 07:50:09 -08:00

Add attempt to find the number implementation from the class.

This commit is contained in:
Danila Fedorin 2017-08-03 22:16:08 -07:00
parent 4fd30030f9
commit b06e9fcbee
2 changed files with 33 additions and 0 deletions

View File

@ -18,6 +18,18 @@ public abstract class NumberImplementation {
promotionPaths = new HashMap<>();
}
public final Map<Class<? extends NumberInterface>, Function<NumberInterface, NumberInterface>> getPromotionPaths(){
return promotionPaths;
}
public final Class<? extends NumberInterface> getImplementation(){
return implementation;
}
public final int getPriority(){
return priority;
}
public abstract NumberInterface instanceForString(String string);
public abstract NumberInterface instanceForPi();

View File

@ -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<String, Operator> cachedOperators;
private Map<String, NumberImplementation> cachedNumberImplementations;
private Map<Class<? extends NumberInterface>, 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<? extends NumberInterface> 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.
*