You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by tv...@apache.org on 2009/09/09 14:53:14 UTC

svn commit: r812928 - in /incubator/pivot/trunk/core: src/org/apache/pivot/collections/LinkedList.java test/org/apache/pivot/collections/test/LinkedListTest.java

Author: tvolkert
Date: Wed Sep  9 12:53:14 2009
New Revision: 812928

URL: http://svn.apache.org/viewvc?rev=812928&view=rev
Log:
Fixed some bugs in LinkedList based on audit of the class

Modified:
    incubator/pivot/trunk/core/src/org/apache/pivot/collections/LinkedList.java
    incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/LinkedListTest.java

Modified: incubator/pivot/trunk/core/src/org/apache/pivot/collections/LinkedList.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/org/apache/pivot/collections/LinkedList.java?rev=812928&r1=812927&r2=812928&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/org/apache/pivot/collections/LinkedList.java (original)
+++ incubator/pivot/trunk/core/src/org/apache/pivot/collections/LinkedList.java Wed Sep  9 12:53:14 2009
@@ -143,20 +143,26 @@
                     } else {
                         // Insert before current
                         next = current;
-                        previous = current.next;
+                        previous = current.previous;
                     }
                 } else {
                     // Insert at tail
                     next = null;
                     previous = last;
                 }
-            }
 
-            verifyLocation(item, previous, next);
+                verifyLocation(item, previous, next);
+
+                if (!forward) {
+                    index++;
+                }
+            }
 
             LinkedList.this.insert(item, previous, next);
 
             length++;
+            modificationCount++;
+            LinkedList.this.modificationCount++;
 
             if (listListeners != null) {
                 listListeners.itemInserted(LinkedList.this, index);
@@ -175,6 +181,8 @@
                 verifyLocation(item, current.previous, current.next);
 
                 current.item = item;
+                modificationCount++;
+                LinkedList.this.modificationCount++;
 
                 if (listListeners != null) {
                     listListeners.itemUpdated(LinkedList.this, index, previousItem);
@@ -182,6 +190,7 @@
             }
         }
 
+        @SuppressWarnings("unchecked")
         @Override
         public void remove() {
             if (current == null) {
@@ -203,11 +212,19 @@
                 current.next.previous = current.previous;
             }
 
+            if (forward) {
+                current = current.previous;
+                index--;
+            } else {
+                current = current.next;
+            }
+
             length--;
+            modificationCount++;
+            LinkedList.this.modificationCount++;
 
             if (listListeners != null) {
-                LinkedList<T> removed = new LinkedList<T>();
-                removed.add(item);
+                LinkedList<T> removed = new LinkedList<T>(item);
 
                 listListeners.itemsRemoved(LinkedList.this, index, removed);
             }
@@ -276,6 +293,10 @@
 
             length++;
             modificationCount++;
+
+            if (listListeners != null) {
+                listListeners.itemInserted(this, index);
+            }
         }
 
         return index;
@@ -283,10 +304,6 @@
 
     @Override
     public void insert(T item, int index) {
-        insert(item, index, true);
-    }
-
-    private void insert(T item, int index, boolean validate) {
         if (index < 0
             || index > length) {
             throw new IndexOutOfBoundsException();

Modified: incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/LinkedListTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/LinkedListTest.java?rev=812928&r1=812927&r2=812928&view=diff
==============================================================================
--- incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/LinkedListTest.java (original)
+++ incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/LinkedListTest.java Wed Sep  9 12:53:14 2009
@@ -19,6 +19,7 @@
 import static org.junit.Assert.*;
 
 import java.util.Comparator;
+import java.util.ConcurrentModificationException;
 
 import org.apache.pivot.collections.ArrayList;
 import org.apache.pivot.collections.LinkedList;
@@ -141,4 +142,27 @@
 
         assertEquals(linkedList, new LinkedList<String>("A", "d", "N", "P", "z"));
     }
+
+    @Test(expected=ConcurrentModificationException.class)
+    public void simpleConcurrentModificationTest() {
+        LinkedList<String> linkedList = new LinkedList<String>("a", "b", "c", "d", "e");
+        List.ItemIterator<String> iterator = linkedList.iterator();
+        linkedList.remove(0, 1);
+        iterator.next();
+    }
+
+    @Test(expected=ConcurrentModificationException.class)
+    public void advancedConcurrentModificationTest() {
+        LinkedList<String> linkedList = new LinkedList<String>("a", "b", "c", "d", "e");
+
+        List.ItemIterator<String> iterator1 = linkedList.iterator();
+        List.ItemIterator<String> iterator2 = linkedList.iterator();
+
+        iterator1.next();
+        iterator2.next();
+
+        iterator1.insert("a1");
+        iterator2.next();
+        System.out.println("!");
+    }
 }