sclass ConcatOnDemandList extends RandomAccessAbstractList { LL parts; L completeList; public int size() { ret completeList().size(); } public A get(int i) { ret completeList().get(i); } L completeList() { if (parts != null) { new L out; for (part : parts) collect(part, out); parts = null; completeList = out; } ret completeList; } void collect(L part, L out) { if (part == null) ret; if (part cast ConcatOnDemandList) part.collectInto(out); else addAll(out, part); } void collectInto(L out) { if (parts != null) for (part : parts) collect(part, out); else addAll(out, completeList); } }