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

svn commit: r812671 - in /incubator/pivot/trunk: core/src/org/apache/pivot/collections/ core/test/org/apache/pivot/collections/test/ tutorials/src/org/apache/pivot/tutorials/stocktracker/

Author: gbrown
Date: Tue Sep  8 20:17:32 2009
New Revision: 812671

URL: http://svn.apache.org/viewvc?rev=812671&view=rev
Log:
Resolve some iterator issues in ArrayList and LinkedList.

Modified:
    incubator/pivot/trunk/core/src/org/apache/pivot/collections/ArrayList.java
    incubator/pivot/trunk/core/src/org/apache/pivot/collections/LinkedList.java
    incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/ArrayListTest.java
    incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/LinkedListTest.java
    incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/stocktracker/StockTracker.java

Modified: incubator/pivot/trunk/core/src/org/apache/pivot/collections/ArrayList.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/org/apache/pivot/collections/ArrayList.java?rev=812671&r1=812670&r2=812671&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/org/apache/pivot/collections/ArrayList.java (original)
+++ incubator/pivot/trunk/core/src/org/apache/pivot/collections/ArrayList.java Tue Sep  8 20:17:32 2009
@@ -43,7 +43,7 @@
 
         @Override
         public boolean hasNext() {
-            return (index < getLength());
+            return (index < length);
         }
 
         @Override
@@ -61,7 +61,7 @@
 
         @Override
         public boolean hasPrevious() {
-            return (index >= 0);
+            return (index > 0);
         }
 
         @Override
@@ -74,7 +74,7 @@
                 throw new ConcurrentModificationException();
             }
 
-            return get(index--);
+            return get(--index);
         }
 
         @Override
@@ -196,7 +196,7 @@
         int index = -1;
 
         if (comparator == null) {
-            index = getLength();
+            index = length;
             insert(item, index);
         }
         else {
@@ -356,24 +356,22 @@
         int index = -1;
 
         if (comparator == null) {
-            int i = 0;
-            while (i < length) {
+            index = 0;
+            while (index < length) {
                 if (item == null) {
-                    if (items[i] == null) {
+                    if (items[index] == null) {
                         break;
                     }
                 } else {
-                    if (item.equals(items[i])) {
+                    if (item.equals(items[index])) {
                         break;
                     }
                 }
 
-                i++;
+                index++;
             }
 
-            if (i < length) {
-                index = i;
-            } else {
+            if (index == length) {
                 index = -1;
             }
         }
@@ -464,7 +462,7 @@
         if (o instanceof ArrayList<?>) {
             ArrayList<T> arrayList = (ArrayList<T>)o;
 
-            if (getLength() == arrayList.getLength()) {
+            if (arrayList.getLength() == length) {
                 Iterator<T> iterator = iterator();
                 Iterator<T> arrayListIterator = arrayList.iterator();
 

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=812671&r1=812670&r2=812671&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 Tue Sep  8 20:17:32 2009
@@ -47,29 +47,18 @@
     }
 
     private class LinkedListItemIterator implements ItemIterator<T> {
-        private boolean reverse;
-        private int index = -1;
-        private Node<T> current;
-        private Node<T> next;
+        private int index = 0;
+        private Node<T> current = null;
 
         private int modificationCount;
 
         public LinkedListItemIterator() {
-            this(false);
-        }
-
-        public LinkedListItemIterator(boolean reverse) {
-            this.reverse = reverse;
-
-            current = null;
-            next = (reverse) ? last : first;
-
             modificationCount = LinkedList.this.modificationCount;
         }
 
         @Override
         public boolean hasNext() {
-            return (next != null);
+            return (index < length);
         }
 
         @Override
@@ -82,17 +71,20 @@
                 throw new ConcurrentModificationException();
             }
 
-            T item = next.item;
-            current = next;
+            if (current == null) {
+                current = first;
+            } else {
+                current = current.next;
+            }
+
             index++;
-            next = (reverse) ? next.previous : next.next;
 
-            return item;
+            return current.item;
         }
 
         @Override
         public boolean hasPrevious() {
-            return (next != null);
+            return (index > 0);
         }
 
         @Override
@@ -105,12 +97,11 @@
                 throw new ConcurrentModificationException();
             }
 
-            T item = next.item;
-            current = next;
+            Node<T> previous = current;
+            current = current.previous;
             index--;
-            next = (reverse) ? next.next: next.previous;
 
-            return item;
+            return previous.item;
         }
 
         @Override
@@ -227,31 +218,39 @@
     @Override
     public int add(T item) {
         int index;
-        if (comparator == null) {
+        if (comparator == null
+            || length == 0) {
             // Append to the tail
             index = length;
             insert(item, index);
         } else {
             // Find the insertion point
             index = 0;
-            LinkedListItemIterator nodeIterator = new LinkedListItemIterator();
-            while (nodeIterator.hasNext()
-                && comparator.compare(item, nodeIterator.next()) > 0) {
+
+            Node<T> next = first;
+            while (next != null
+                && comparator.compare(item, next.item) > 0) {
+                next = next.next;
                 index++;
             }
 
-            if (nodeIterator.hasNext()
-                && index > 0) {
-                // Insert the new node here
-                Node<T> node = new Node<T>(nodeIterator.next, nodeIterator.next.next, item);
-                nodeIterator.next.next = node;
-                node.next.previous = node;
-                length++;
-                modificationCount++;
+            Node<T> previous = (next == null) ? last : next.previous;
+
+            Node<T> node = new Node<T>(previous, next, item);
+            if (previous == null) {
+                first = node;
             } else {
-                // Insert at the head or append to the tail
-                insert(item, index, false);
+                previous.next = node;
             }
+
+            if (next == null) {
+                last = node;
+            } else {
+                next.previous = node;
+            }
+
+            length++;
+            modificationCount++;
         }
 
         return index;
@@ -329,33 +328,14 @@
         return previousItem;
     }
 
-    /**
-     * Verifies that the specified item may be placed in this list in between
-     * the two specified nodes by throwing an exception if the placement is not
-     * valid.
-     *
-     * @param item
-     * The item to verify
-     *
-     * @param predecessor
-     * The node that will become the item's predecessor, or <tt>null</tt> if
-     * the item is being placed at the head of the list
-     *
-     * @param successor
-     * The node that will become the item's successor, or <tt>null</tt> if the
-     * item is being placed at the tail of the list
-     *
-     * @throws IllegalArgumentException
-     * If the location is not valid
-     */
-    private void verifyLocation(T item, Node<T> predecessor, Node<T> successor) {
+    private void verifyLocation(T item, Node<T> previous, Node<T> next) {
         if (comparator != null) {
             // Ensure that the new item is greater or equal to its
             // predecessor and less than or equal to its successor
-            if ((predecessor != null
-                && comparator.compare(item, predecessor.item) == -1)
-                || (successor != null
-                && comparator.compare(item, successor.item) == 1)) {
+            if ((previous != null
+                && comparator.compare(item, previous.item) == -1)
+                || (next != null
+                && comparator.compare(item, next.item) == 1)) {
                 throw new IllegalArgumentException("Illegal item modification.");
             }
         }
@@ -490,16 +470,23 @@
     public int indexOf(T item) {
         int index = 0;
 
-        LinkedListItemIterator nodeIterator = new LinkedListItemIterator();
-        while (nodeIterator.hasNext()) {
-            if (nodeIterator.next() == item) {
-                break;
+        Node<T> node = first;
+        while (node != null) {
+            if (item == null) {
+                if (node.item == null) {
+                    break;
+                }
             } else {
-                index++;
+                if (item.equals(node.item)) {
+                    break;
+                }
             }
+
+            node = node.next;
+            index++;
         }
 
-        if (!nodeIterator.hasNext()) {
+        if (node == null) {
             index = -1;
         }
 
@@ -564,11 +551,7 @@
 
     @Override
     public ItemIterator<T> iterator() {
-        return iterator(false);
-    }
-
-    public ItemIterator<T> iterator(boolean reverse) {
-        return new LinkedListItemIterator(reverse);
+        return new LinkedListItemIterator();
     }
 
     @Override

Modified: incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/ArrayListTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/ArrayListTest.java?rev=812671&r1=812670&r2=812671&view=diff
==============================================================================
--- incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/ArrayListTest.java (original)
+++ incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/ArrayListTest.java Tue Sep  8 20:17:32 2009
@@ -19,6 +19,7 @@
 import static org.junit.Assert.*;
 
 import org.apache.pivot.collections.ArrayList;
+import org.apache.pivot.collections.List;
 import org.apache.pivot.collections.Sequence;
 import org.junit.Test;
 
@@ -54,6 +55,24 @@
         assertTrue(list.equals(new ArrayList<String>("B", "E", "D")));
         assertFalse(list.equals(new ArrayList<String>("B", "E", "D", "C")));
         assertFalse(list.equals(new ArrayList<String>("E", "C", "D")));
+
+        ArrayList<String> copy = new ArrayList<String>("B", "E", "D");
+        int i = 0;
+        for (String item : list) {
+            assertEquals(item, copy.get(i++));
+        }
+
+        int j = 0;
+        List.ItemIterator<String> iterator = list.iterator();
+        while (j < list.getLength()) {
+            iterator.next();
+            j++;
+        }
+
+        while (iterator.hasPrevious()) {
+            String s = iterator.previous();
+            assertEquals(s, copy.get(--j));
+        }
     }
 
     @Test

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=812671&r1=812670&r2=812671&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 Tue Sep  8 20:17:32 2009
@@ -22,6 +22,7 @@
 
 import org.apache.pivot.collections.ArrayList;
 import org.apache.pivot.collections.LinkedList;
+import org.apache.pivot.collections.List;
 import org.apache.pivot.collections.Sequence;
 import org.junit.Test;
 
@@ -66,6 +67,18 @@
         for (String item : list) {
             assertEquals(item, copy.get(i++));
         }
+
+        int j = 0;
+        List.ItemIterator<String> iterator = list.iterator();
+        while (j < list.getLength()) {
+            iterator.next();
+            j++;
+        }
+
+        while (iterator.hasPrevious()) {
+            String s = iterator.previous();
+            assertEquals(s, copy.get(--j));
+        }
     }
 
     @Test

Modified: incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/stocktracker/StockTracker.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/stocktracker/StockTracker.java?rev=812671&r1=812670&r2=812671&view=diff
==============================================================================
--- incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/stocktracker/StockTracker.java (original)
+++ incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/stocktracker/StockTracker.java Tue Sep  8 20:17:32 2009
@@ -48,6 +48,7 @@
 import org.apache.pivot.wtk.MessageType;
 import org.apache.pivot.wtk.Span;
 import org.apache.pivot.wtk.TableView;
+import org.apache.pivot.wtk.TableViewRowListener;
 import org.apache.pivot.wtk.TableViewSelectionListener;
 import org.apache.pivot.wtk.TaskAdapter;
 import org.apache.pivot.wtk.TextInput;
@@ -112,6 +113,16 @@
         wtkxSerializer.bind(this, StockTracker.class);
 
         // Wire up event handlers
+        stocksTableView.getTableViewRowListeners().add(new TableViewRowListener.Adapter() {
+            @Override
+            public void rowsSorted(TableView tableView) {
+                List<?> tableData = stocksTableView.getTableData();
+                if (tableData.getLength() > 0) {
+                    stocksTableView.setSelectedIndex(0);
+                }
+            }
+        });
+
         stocksTableView.getTableViewSelectionListeners().add(new TableViewSelectionListener.Adapter() {
             @Override
             public void selectedRangesChanged(TableView tableView, Sequence<Span> previousSelectedRanges) {