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/08 19:23:30 UTC

svn commit: r812586 - /incubator/pivot/trunk/core/src/org/apache/pivot/collections/LinkedList.java

Author: tvolkert
Date: Tue Sep  8 17:23:30 2009
New Revision: 812586

URL: http://svn.apache.org/viewvc?rev=812586&view=rev
Log:
PIVOT-282 :: Unify illegal item modification checks in LinkedList

Modified:
    incubator/pivot/trunk/core/src/org/apache/pivot/collections/LinkedList.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=812586&r1=812585&r2=812586&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 17:23:30 2009
@@ -119,6 +119,9 @@
                 throw new IllegalStateException();
             }
 
+            // Verify that the item is allowed at the specified index
+            verifyLocation(item, current.previous, current);
+
             // Insert a new node immediately prior to the current node
             Node<T> node = new Node<T>(current.previous, current, item);
 
@@ -144,19 +147,8 @@
 
             T previousItem = current.item;
             if (previousItem != item) {
-                if (comparator != null) {
-                    // Ensure that the new item is greater or equal to its
-                    // predecessor and less than or equal to its successor
-                    T predecessorItem = (current.previous != null ? current.previous.item : null);
-                    T successorItem = (current.next != null ? current.next.item : null);
-
-                    if ((predecessorItem != null
-                        && comparator.compare(item, predecessorItem) == -1)
-                        || (successorItem != null
-                        && comparator.compare(item, successorItem) == 1)) {
-                        throw new IllegalArgumentException("Illegal item modification.");
-                    }
-                }
+                // Verify that the item is allowed at the specified index
+                verifyLocation(item, current.previous, current.next);
 
                 current.item = item;
 
@@ -284,16 +276,8 @@
             Node<T> next = (index == length) ? null : getNode(index);
             Node<T> previous = (next == null) ? last : next.previous;
 
-            if (comparator != null) {
-                // Ensure that the new item is greater or equal to its
-                // predecessor and less than or equal to its successor
-                if ((previous != null
-                        && comparator.compare(item, previous.item) == -1)
-                    || (next != null
-                        && comparator.compare(item, next.item) == 1)) {
-                    throw new IllegalArgumentException("Illegal item modification.");
-                }
-            }
+            // Verify that the item is allowed at the specified index
+            verifyLocation(item, previous, next);
 
             Node<T> node = new Node<T>(previous, next, item);
             if (previous == null) {
@@ -330,16 +314,8 @@
         T previousItem = node.item;
 
         if (previousItem != item) {
-            if (comparator != null) {
-                // Ensure that the new item is greater or equal to its
-                // predecessor and less than or equal to its successor
-                if ((node.previous != null
-                        && comparator.compare(item, node.previous.item) == -1)
-                    || (node.next != null
-                        && comparator.compare(item, node.next.item) == 1)) {
-                    throw new IllegalArgumentException("Illegal item modification.");
-                }
-            }
+            // Verify that the item is allowed at the specified index
+            verifyLocation(item, node.previous, node.next);
 
             // Update the item
             node.item = item;
@@ -353,6 +329,38 @@
         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) {
+        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)) {
+                throw new IllegalArgumentException("Illegal item modification.");
+            }
+        }
+    }
+
     @Override
     public int remove(T item) {
         int index = 0;