Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public class ListIteratorWrapper<E> implements ResettableListIterator<E> {
/** Recall whether the wrapped iterator's "cursor" is in such a state as to allow remove() to be called */
private boolean removeState;

/** Recall whether the element last returned by the wrapped iterator can still be removed from it */
private boolean wrappedRemovable;

/**
* Constructs a new {@code ListIteratorWrapper} that will wrap
* the given iterator.
Expand Down Expand Up @@ -145,13 +148,15 @@ public E next() throws NoSuchElementException {

if (currentIndex < wrappedIteratorIndex) {
++currentIndex;
removeState = wrappedRemovable && currentIndex == wrappedIteratorIndex;
return list.get(currentIndex - 1);
}

final E retval = iterator.next();
list.add(retval);
++currentIndex;
++wrappedIteratorIndex;
wrappedRemovable = true;
removeState = true;
return retval;
}
Expand Down Expand Up @@ -187,7 +192,7 @@ public E previous() throws NoSuchElementException {
if (currentIndex == 0) {
throw new NoSuchElementException();
}
removeState = wrappedIteratorIndex == currentIndex;
removeState = wrappedRemovable && wrappedIteratorIndex == currentIndex;
return list.get(--currentIndex);
}

Expand Down Expand Up @@ -228,6 +233,7 @@ public void remove() throws IllegalStateException {
list.remove(removeIndex);
currentIndex = removeIndex;
wrappedIteratorIndex--;
wrappedRemovable = false;
removeState = false;
}

Expand All @@ -247,6 +253,7 @@ public void reset() {
return;
}
currentIndex = 0;
removeState = false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,33 @@ public void testRemove() {

}

@Test
void testRemoveAfterReset() {
final ResettableListIterator<E> iter = makeObject();
iter.next();
iter.reset();

assertThrows(IllegalStateException.class, iter::remove,
"ListIteratorWrapper#remove() should fail; must be repositioned after reset()");
assertEquals(testArray.length, list1.size());
}

@Test
void testRemoveAfterReturningToWrappedPosition() {
final ListIterator<E> iter = makeObject();
iter.next();
iter.next();
iter.previous();
iter.previous();
// read forwards out of the cache until back at the wrapped iterator's position
iter.next();
final E second = iter.next();

iter.remove();
assertEquals(testArray.length - 1, list1.size());
assertFalse(list1.contains(second), "remove() should drop the element last returned by next()");
}

@Test
void testReset() {
final ResettableListIterator<E> iter = makeObject();
Expand Down