package Examples; import java.util.ConcurrentModificationException; import java.util.ListIterator; import java.util.NoSuchElementException; //originally an inner class of LinkedList, but taken out because pitest //can't deal with inner class class ListItr implements ListIterator { private JavaLinkedList parentList ; private Node lastReturned = null; private Node next; private int nextIndex; private int expectedModCount ; ListItr(JavaLinkedList parentList, int index) { this.parentList = parentList ; // assert isPositionIndex(index); next = (index == parentList.size) ? null : parentList.node(index); nextIndex = index; expectedModCount = parentList.getModCount() ; } public boolean hasNext() { return nextIndex < parentList.size; } public E next() { checkForComodification(); if (!hasNext()) throw new NoSuchElementException(); lastReturned = next; next = next.next; nextIndex++; return lastReturned.item; } public boolean hasPrevious() { return nextIndex > 0; } public E previous() { checkForComodification(); if (!hasPrevious()) throw new NoSuchElementException(); lastReturned = next = (next == null) ? parentList.last : next.prev; nextIndex--; return lastReturned.item; } public int nextIndex() { return nextIndex; } public int previousIndex() { return nextIndex - 1; } public void remove() { checkForComodification(); if (lastReturned == null) throw new IllegalStateException(); Node lastNext = lastReturned.next; parentList.unlink(lastReturned); if (next == lastReturned) next = lastNext; else nextIndex--; lastReturned = null; expectedModCount++; } public void set(E e) { if (lastReturned == null) throw new IllegalStateException(); checkForComodification(); lastReturned.item = e; } public void add(E e) { checkForComodification(); lastReturned = null; if (next == null) parentList.linkLast(e); else parentList.linkBefore(e, next); nextIndex++; expectedModCount++; } final void checkForComodification() { if (parentList.getModCount() != expectedModCount) throw new ConcurrentModificationException(); } }