8014066: Remove redundant restriction from ArrayList#removeRange() spec
authorigerasim
Wed, 26 Mar 2014 15:58:37 +0400
changeset 23580 1055a611c69b
parent 23579 6f6bae8b67a7
child 23581 0ca496340112
8014066: Remove redundant restriction from ArrayList#removeRange() spec Reviewed-by: chegar, dholmes, martin, mduigou
jdk/src/share/classes/java/util/ArrayList.java
jdk/test/java/util/Collection/MOAT.java
--- a/jdk/src/share/classes/java/util/ArrayList.java	Tue Mar 25 20:32:46 2014 -0400
+++ b/jdk/src/share/classes/java/util/ArrayList.java	Wed Mar 26 15:58:37 2014 +0400
@@ -609,11 +609,14 @@
      * @throws IndexOutOfBoundsException if {@code fromIndex} or
      *         {@code toIndex} is out of range
      *         ({@code fromIndex < 0 ||
-     *          fromIndex >= size() ||
      *          toIndex > size() ||
      *          toIndex < fromIndex})
      */
     protected void removeRange(int fromIndex, int toIndex) {
+        if (fromIndex > toIndex) {
+            throw new IndexOutOfBoundsException(
+                    outOfBoundsMsg(fromIndex, toIndex));
+        }
         modCount++;
         int numMoved = size - toIndex;
         System.arraycopy(elementData, toIndex, elementData, fromIndex,
@@ -656,6 +659,13 @@
     }
 
     /**
+     * A version used in checking (fromIndex > toIndex) condition
+     */
+    private static String outOfBoundsMsg(int fromIndex, int toIndex) {
+        return "From Index: " + fromIndex + " > To Index: " + toIndex;
+    }
+
+    /**
      * Removes from this list all of its elements that are contained in the
      * specified collection.
      *
--- a/jdk/test/java/util/Collection/MOAT.java	Tue Mar 25 20:32:46 2014 -0400
+++ b/jdk/test/java/util/Collection/MOAT.java	Wed Mar 26 15:58:37 2014 +0400
@@ -54,6 +54,7 @@
 import java.util.*;
 import java.util.concurrent.*;
 import static java.util.Collections.*;
+import java.lang.reflect.*;
 
 public class MOAT {
     public static void realMain(String[] args) {
@@ -721,6 +722,28 @@
 
         equal(l instanceof RandomAccess,
               l.subList(0,0) instanceof RandomAccess);
+
+        l.iterator();
+        l.listIterator();
+        l.listIterator(0);
+        l.listIterator(l.size());
+        THROWS(IndexOutOfBoundsException.class,
+               new Fun(){void f(){l.listIterator(-1);}},
+               new Fun(){void f(){l.listIterator(l.size() + 1);}});
+
+        if (l instanceof AbstractList) {
+            try {
+                int size = l.size();
+                AbstractList<Integer> abList = (AbstractList<Integer>) l;
+                Method m = AbstractList.class.getDeclaredMethod("removeRange", new Class[] { int.class, int.class });
+                m.setAccessible(true);
+                m.invoke(abList, new Object[] { 0, 0 });
+                m.invoke(abList, new Object[] { size, size });
+                equal(size, l.size());
+            }
+            catch (UnsupportedOperationException ignored) {/* OK */}
+            catch (Throwable t) { unexpected(t); }
+        }
     }
 
     private static void testCollection(Collection<Integer> c) {