changeset 8359:31fa4e4e6d0e

address review comments for 8005051 and more postcondition assertions
author akhil
date Tue, 23 Apr 2013 11:28:09 -0700
parents dcafeb140b38
children 17f3af761c70
files src/share/classes/java/util/LinkedList.java src/share/classes/java/util/Vector.java src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java test/java/util/Iterator/IteratorDefaults.java
diffstat 4 files changed, 55 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/LinkedList.java	Mon Apr 22 21:13:22 2013 -0400
+++ b/src/share/classes/java/util/LinkedList.java	Tue Apr 23 11:28:09 2013 -0700
@@ -957,6 +957,7 @@
                 next = next.next;
                 nextIndex++;
             }
+            lastReturned = next;
             checkForComodification();
         }
 
--- a/src/share/classes/java/util/Vector.java	Mon Apr 22 21:13:22 2013 -0400
+++ b/src/share/classes/java/util/Vector.java	Tue Apr 23 11:28:09 2013 -0700
@@ -1159,7 +1159,7 @@
         public void forEachRemaining(Consumer<? super E> action) {
             Objects.requireNonNull(action);
             synchronized (Vector.this) {
-                final int size = Vector.this.elementCount;
+                final int size = elementCount;
                 int i = cursor;
                 if (i >= size) {
                     return;
--- a/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java	Mon Apr 22 21:13:22 2013 -0400
+++ b/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java	Tue Apr 23 11:28:09 2013 -0700
@@ -1090,6 +1090,7 @@
             for (int i=cursor; i < size; i++) {
                 action.accept((E) snapshot[i]);
             }
+            cursor = size;
         }
     }
 
--- a/test/java/util/Iterator/IteratorDefaults.java	Mon Apr 22 21:13:22 2013 -0400
+++ b/test/java/util/Iterator/IteratorDefaults.java	Tue Apr 23 11:28:09 2013 -0700
@@ -30,8 +30,11 @@
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
+import java.util.ListIterator;
+import java.util.NoSuchElementException;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
 
@@ -167,10 +170,29 @@
             trimmedSubList(list, new Callback() {
                 @Override
                 public void call(final List<Integer> list) {
+                    if (list.size() < 1) {
+                        return;
+                    }
                     final List<Integer> target = new ArrayList<>(list.size());
-                    final Iterator<Integer> iterator = list.iterator();
+                    final ListIterator<Integer> iterator = list.listIterator();
+                    assertTrue(iterator.hasNext());
+                    assertFalse(iterator.hasPrevious());
+                    assertEquals(iterator.nextIndex(), 0);
+                    assertEquals(iterator.previousIndex(), -1);
+
                     iterator.forEachRemaining(target::add);
                     assertEquals(target, list);
+
+                    assertFalse(iterator.hasNext());
+                    assertTrue(iterator.hasPrevious());
+                    assertEquals(iterator.nextIndex(), list.size());
+                    assertEquals(iterator.previousIndex(), list.size() - 1);
+
+                    try {
+                        iterator.next();
+                        fail(listClass + " iterator advanced beyond end");
+                    } catch (NoSuchElementException ignore) {
+                    }
                 }
             });
         }
@@ -183,24 +205,46 @@
         }
         final List<Integer> source = Arrays.asList(data);
 
-        final String[] iterableCollectionClasses = {
+        final String[] listClasses = {
                 "java.util.ArrayList",
+                "java.util.LinkedList",
                 "java.util.Vector",
                 "java.util.concurrent.CopyOnWriteArrayList"
         };
 
+        final int OFFSET = 3;
         final List<Integer> target = new ArrayList<>(source);
-        for (final String iterableClass : iterableCollectionClasses) {
-            final Iterable<Integer> iterable =
-                    (Iterable<Integer>) Class.forName(iterableClass).newInstance();
-            ((Collection<Integer>) iterable).addAll(source);
-            final Iterator<Integer> iterator = iterable.iterator();
+        for (final String listClass : listClasses) {
+            final List<Integer> list =
+                    (List<Integer>) Class.forName(listClass).newInstance();
+            list.addAll(source);
+            final ListIterator<Integer> iterator = list.listIterator();
+            assertFalse(iterator.hasPrevious());
+            for (int i=0; i < OFFSET; i++) {
+                iterator.next();
+            }
+            assertTrue(iterator.hasNext());
+            assertTrue(iterator.hasPrevious());
+            assertEquals(iterator.nextIndex(), OFFSET);
+            assertEquals(iterator.previousIndex(), OFFSET - 1);
+
             iterator.forEachRemaining(e -> {
                 target.set(e, e + 1);
             });
-            for (int i=0; i < data.length; i++) {
+            for (int i=OFFSET; i < data.length; i++) {
                 assertEquals(target.get(i).intValue(), source.get(i)+1);
             }
+
+            assertFalse(iterator.hasNext());
+            assertTrue(iterator.hasPrevious());
+            assertEquals(iterator.nextIndex(), data.length);
+            assertEquals(iterator.previousIndex(), data.length - 1);
+
+            try {
+                iterator.next();
+                fail(listClass + " iterator advanced beyond end");
+            } catch (NoSuchElementException ignore) {
+            }
         }
     }