package Sequenic.T2.Coverage; import java.util.Arrays; import java.util.HashMap; import java.util.Map; /** * DEPRECATED, it was a bad idea: we aren't using it anymore. * * A tree structure that can be used to cache int[] objects by maintaining a single * canonical version of each. * * Also maintains a cache of BasicPaths. * * Useful for TrieSensor and BasicPath. * * to do: Perhaps use SoftReference (or WeakReference) strategically to * allow garbage collection of unused instances. * Currently there is always a chain of strong references from EMPTY_ARRAY * to any CanonicalIntArray instance and its associated BasicPaths. * * For caches it is a Java best practice to use SoftReferences. * These are only cleared when memory is low. */ final class CanonicalIntArray { final int[] intArray; private final Map children; private final Map basicpaths; private CanonicalIntArray(final int[] intArray) { this.intArray = intArray; children = new HashMap(); basicpaths = new HashMap(); } synchronized CanonicalIntArray extend(Integer i) { CanonicalIntArray child = children.get(i); if(child==null) { int[] newPath = Arrays.copyOf(intArray, intArray.length+1); newPath[intArray.length] = i; children.put(i, child = new CanonicalIntArray(newPath)); } return child; } BasicPath basicPath(String methodName) { synchronized (basicpaths) { BasicPath ret = basicpaths.get(methodName); if(ret==null) { methodName = methodName.intern(); basicpaths.put(methodName, ret = new BasicPath(methodName, intArray)); } return ret; } } static final CanonicalIntArray EMPTY_ARRAY = new CanonicalIntArray(new int[0]); static CanonicalIntArray intern(int[] array) { CanonicalIntArray ret = EMPTY_ARRAY; for(int i : array) ret = ret.extend(i); return ret; } static CanonicalIntArray intern(Iterable list) { CanonicalIntArray ret = EMPTY_ARRAY; for(Integer i : list) ret = ret.extend(i); return ret; } }