// still efficient when used to build a list recursively
persistable sclass ConcatOnDemandList extends RandomAccessAbstractList {
new LL parts;
L completeList;
*(L... parts) { fOr (part : parts) addPart(part); }
void addPart(L part) { parts.add(part); }
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
out.addAll(part);
}
void collectInto(L out) {
if (parts != null)
for (part : parts)
collect(part, out);
else
out.addAll(completeList);
}
}