private static TCast deriveCast(Map<TClass,Map<TClass,TCast>> castsBySource,
List<? extends TClass> path, int targetIndex) {
TClass source = path.get(0);
TClass target = path.get(targetIndex);
TCast alreadyThere = cast(castsBySource, source, target);
if (alreadyThere != null)
return alreadyThere;
int intermediateIndex = targetIndex - 1;
TClass intermediateClass = path.get(intermediateIndex);
TCast second = cast(castsBySource, intermediateClass, target);
if (second == null)
throw new AkibanInternalException("no explicit cast between " + intermediateClass + " and " + target
+ " while creating cast path: " + path);
TCast first = deriveCast(castsBySource, path, intermediateIndex);
if (first == null)
throw new AkibanInternalException("couldn't derive cast between " + source + " and " + intermediateClass
+ " while creating cast path: " + path);
TCast result = new ChainedCast(first, second);
putCast(castsBySource, result, null);
return result;
}