Add a 'fallback' to InheritanceMap

setFallback() allows an IInheritanceProvider to be specified which
will be consulted when inheritance for a requested class is unavailable.
If it returns a value, the inheritance will be cached in the InheritanceMap.
This commit is contained in:
Agaricus 2013-02-18 18:14:31 -08:00
parent a430689c64
commit d2c2ad65db

View File

@ -40,6 +40,7 @@ import java.util.*;
public class InheritanceMap implements IInheritanceProvider { public class InheritanceMap implements IInheritanceProvider {
private final Map<String, ArrayList<String>> inheritanceMap = new HashMap<String, ArrayList<String>>(); private final Map<String, ArrayList<String>> inheritanceMap = new HashMap<String, ArrayList<String>>();
private IInheritanceProvider fallback = null;
public static final InheritanceMap EMPTY = new InheritanceMap(); public static final InheritanceMap EMPTY = new InheritanceMap();
@ -70,6 +71,9 @@ public class InheritanceMap implements IInheritanceProvider {
} }
} }
/**
* Save inheritance map to disk
*/
public void save(PrintWriter writer) { public void save(PrintWriter writer) {
List<String> classes = new ArrayList<String>(inheritanceMap.keySet()); List<String> classes = new ArrayList<String>(inheritanceMap.keySet());
Collections.sort(classes); Collections.sort(classes);
@ -83,6 +87,9 @@ public class InheritanceMap implements IInheritanceProvider {
} }
} }
/**
* Load from disk, optionally remapped and unremapped through a class map
*/
public void load(BufferedReader reader, BiMap<String, String> classMap) throws IOException { public void load(BufferedReader reader, BiMap<String, String> classMap) throws IOException {
String line; String line;
@ -119,10 +126,26 @@ public class InheritanceMap implements IInheritanceProvider {
} }
} }
/**
* Get the superclass and interfaces implemented by a class
*/
public List<String> getParents(String className) { public List<String> getParents(String className) {
return inheritanceMap.get(className); List<String> parents = inheritanceMap.get(className);
if (parents == null && fallback != null) {
parents = fallback.getParents(className);
if (parents != null) {
// cache
setParents(className, (ArrayList<String>) parents);
}
} }
return parents;
}
/**
* Set the superclass and interfaces implemented by a class
*/
public void setParents(String className, ArrayList<String> parents) { public void setParents(String className, ArrayList<String> parents) {
inheritanceMap.put(className, parents); inheritanceMap.put(className, parents);
} }
@ -130,4 +153,12 @@ public class InheritanceMap implements IInheritanceProvider {
public int size() { public int size() {
return inheritanceMap.size(); return inheritanceMap.size();
} }
/**
* Set an optional fallback inheritance provider to be consulted when the class is not available
* in the inheritance map. Results from the fallback will be cached.
*/
public void setFallback(IInheritanceProvider fallback) {
this.fallback = fallback;
}
} }