Iterator<ChangeSet> () { return new Iterator<>() { private final IntOpenHashSet recursionGuard = new IntOpenHashSet(1000); private ChangeSetHolder currentBlock; private ChangeSet next = fetchNext(); @Override public boolean hasNext() { return next != null; } @Override public ChangeSet next() { ChangeSet result = next; next = fetchNext(); return result; } private ChangeSet fetchNext() { if (currentBlock == null) { synchronized (ChangeList.this) { if (myCurrentChangeSet != null) { currentBlock = new ChangeSetHolder(-1, myCurrentChangeSet); } else { currentBlock = myStorage.readPrevious(-1, recursionGuard); } } } else { synchronized (ChangeList.this) { currentBlock = myStorage.readPrevious(currentBlock.id(), recursionGuard); } } if (currentBlock == null) return null; return currentBlock.changeSet(); } @Override public void remove() { throw new UnsupportedOperationException(); } }; }