8014066: Remove redundant restriction from ArrayList#removeRange() spec

Reviewed-by: chegar, dholmes, martin, mduigou
This commit is contained in:
Ivan Gerasimov 2014-03-26 15:58:37 +04:00
parent 99775bfc54
commit 0c0e4775d4
2 changed files with 34 additions and 1 deletions

View File

@ -609,11 +609,14 @@ public class ArrayList<E> extends AbstractList<E>
* @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,
@ -655,6 +658,13 @@ public class ArrayList<E> extends AbstractList<E>
return "Index: "+index+", Size: "+size;
}
/**
* 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.

View File

@ -54,6 +54,7 @@ import java.io.*;
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 @@ public class MOAT {
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) {