AbstractForwardSequentialList extends AbstractSequentialList<T> { @Override @Nonnull public ListIterator<T> listIterator(final int initialIndex) { final Iterator<T> initialIterator; try { initialIterator = iterator(initialIndex); } catch (NoSuchElementException ex) { throw new IndexOutOfBoundsException(); } return new AbstractListIterator<T>() { private int index = initialIndex - 1; @Nullable private Iterator<T> forwardIterator = initialIterator; @Nonnull private Iterator<T> getForwardIterator() { if (forwardIterator == null) { try { forwardIterator = iterator(index+1); } catch (IndexOutOfBoundsException ex) { throw new NoSuchElementException(); } } return forwardIterator; } @Override public boolean hasNext() { return getForwardIterator().hasNext(); } @Override public boolean hasPrevious() { return index >= 0; } @Override public T next() { T ret = getForwardIterator().next(); index++; return ret; } @Override public int nextIndex() { return index+1; } @Override public T previous() { forwardIterator = null; try { return iterator(index--).next(); } catch (IndexOutOfBoundsException ex) { throw new NoSuchElementException(); } } @Override public int previousIndex() { return index; } }; }  @Override @Nonnull abstract Iterator<T> iterator(); @Override @Nonnull ListIterator<T> listIterator(final int initialIndex); @Override @Nonnull ListIterator<T> listIterator();  }
@Test public void testForwardIteration() { testForwardIterationImpl(list.listIterator()); }
@Test public void testReverseIteration() { testReverseIterationImpl(list.listIterator(100)); }
@Test public void testAlternatingIteration() { ListIterator<Integer> iter = list.listIterator(50); for (int i=0; i<10; i++) { Assert.assertTrue(iter.hasNext()); Assert.assertTrue(iter.hasPrevious()); Assert.assertEquals(50, iter.nextIndex()); Assert.assertEquals(49, iter.previousIndex()); Assert.assertEquals(50, iter.next().intValue()); Assert.assertTrue(iter.hasNext()); Assert.assertTrue(iter.hasPrevious()); Assert.assertEquals(51, iter.nextIndex()); Assert.assertEquals(50, iter.previousIndex()); Assert.assertEquals(50, iter.previous().intValue()); } }
@Test public void testAlternatingIteration2() { ListIterator<Integer> iter = list.listIterator(0); for (int i=0; i<10; i++) { testForwardIterationImpl(iter); testReverseIterationImpl(iter); } }
@Test(expected = IndexOutOfBoundsException.class) public void testNegativeIndex() { list.listIterator(-1); }
@Test(expected = IndexOutOfBoundsException.class) public void testLargeIndex() { list.listIterator(101); }
@Test(expected = IndexOutOfBoundsException.class) public void testLargeIndex2() { list.listIterator(1000000); }
@Test public void testForwardIterationException() { ListIterator<Integer> iter = list.listIterator(0); for (int i=0; i<100; i++) { iter.next(); } try { iter.next(); } catch (NoSuchElementException ex) { return; } Assert.fail(); }
@Test(expected = NoSuchElementException.class) public void testForwardIterationException2() { ListIterator<Integer> iter = list.listIterator(100); iter.next(); }
@Test public void testReverseIterationException() { ListIterator<Integer> iter = list.listIterator(100); for (int i=0; i<100; i++) { iter.previous(); } try { iter.previous(); } catch (NoSuchElementException ex) { return; } Assert.fail(); }
@Test(expected = NoSuchElementException.class) public void testReverseIterationException2() { ListIterator<Integer> iter = list.listIterator(0); iter.previous(); }