Walk down tree trunk, rather than to all leaves, to check for cycles
@@ -42,13 +42,9 @@ public abstract class CompositeItem extends Item {
}
public void ensureNotInSubtree(CompositeItem item) {
- for (Iterator<Item> i = item.getItemIterator(); i.hasNext();) {
- Item possibleCycle = i.next();
-
- if (this == possibleCycle) {
+ for (Item i = this; i != null; i = i.getParent()) {
+ if (i == item) {
throw new IllegalArgumentException("Cannot add " + item + " to " + this + " as it would create a cycle");
- } else if (possibleCycle instanceof CompositeItem) {
- ensureNotInSubtree((CompositeItem) possibleCycle);
}
}
}
