diff options
author | Eric Blake <ebb9@byu.net> | 2001-10-22 05:48:01 +0000 |
---|---|---|
committer | Eric Blake <ebb9@byu.net> | 2001-10-22 05:48:01 +0000 |
commit | db0d97631e1a7c9cecd9460722dad1b2837af2f8 (patch) | |
tree | d552c12631843efb8662ca84805e10d87ba7c778 | |
parent | 5da6d2b59085f9eb8a4f28775d13351f2c4d447c (diff) | |
download | classpath-db0d97631e1a7c9cecd9460722dad1b2837af2f8.tar.gz |
2001-10-21 Eric Blake <ebb9@email.byu.edu>
* java/util/ArrayList.java (checkBoundExclusive),
(checkBoundInclusive): Rename from range??clusive, to match
AbstractList.
* java/util/LinkedList.java (checkBoundsExclusive),
(checkBoundsInclusive): ditto
* java/util/Vector.java (checkBoundExclusive),
(checkBoundInclusive): Move bounds checking into common methods.
-rw-r--r-- | ChangeLog | 10 | ||||
-rw-r--r-- | java/util/ArrayList.java | 26 | ||||
-rw-r--r-- | java/util/LinkedList.java | 16 | ||||
-rw-r--r-- | java/util/Vector.java | 55 |
4 files changed, 67 insertions, 40 deletions
@@ -1,5 +1,15 @@ 2001-10-21 Eric Blake <ebb9@email.byu.edu> + * java/util/ArrayList.java (checkBoundExclusive), + (checkBoundInclusive): Rename from range??clusive, to match + AbstractList. + * java/util/LinkedList.java (checkBoundsExclusive), + (checkBoundsInclusive): ditto + * java/util/Vector.java (checkBoundExclusive), + (checkBoundInclusive): Move bounds checking into common methods. + +2001-10-21 Eric Blake <ebb9@email.byu.edu> + * java/util/AbstractList.java: (modCount): Make sure it is updated in all needed places. * java/util/ArrayList.java: Improve javadoc. Implements diff --git a/java/util/ArrayList.java b/java/util/ArrayList.java index 45afb65ed..76b6fa7f4 100644 --- a/java/util/ArrayList.java +++ b/java/util/ArrayList.java @@ -294,7 +294,7 @@ public class ArrayList extends AbstractList */ public Object get(int index) { - rangeExclusive(index); + checkBoundExclusive(index); return data[index]; } @@ -308,7 +308,7 @@ public class ArrayList extends AbstractList */ public Object set(int index, Object e) { - rangeExclusive(index); + checkBoundExclusive(index); Object result = data[index]; data[index] = e; return result; @@ -339,7 +339,7 @@ public class ArrayList extends AbstractList */ public void add(int index, Object e) { - rangeInclusive(index); + checkBoundInclusive(index); modCount++; if (size == data.length) ensureCapacity(size + 1); @@ -358,7 +358,7 @@ public class ArrayList extends AbstractList */ public Object remove(int index) { - rangeExclusive(index); + checkBoundExclusive(index); Object r = data[index]; modCount++; if (index != --size) @@ -407,7 +407,7 @@ public class ArrayList extends AbstractList */ public boolean addAll(int index, Collection c) { - rangeInclusive(index); + checkBoundInclusive(index); Iterator itr = c.iterator(); int csize = c.size(); @@ -444,13 +444,13 @@ public class ArrayList extends AbstractList * Checks that the index is in the range of possible elements (inclusive). * * @param index the index to check - * @throws IndexOutOfBoundsException if index < 0 || index > size + * @throws IndexOutOfBoundsException if index > size */ - private void rangeInclusive(int index) + private void checkBoundInclusive(int index) { // Implementation note: we do not check for negative ranges here, since - // that will cause an ArrayIndexOutOfBoundsException, a subclass of - // the required exception, with no effort on our part. + // use of a negative index will cause an ArrayIndexOutOfBoundsException, + // a subclass of the required exception, with no effort on our part. if (index > size) throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); @@ -460,13 +460,13 @@ public class ArrayList extends AbstractList * Checks that the index is in the range of existing elements (exclusive). * * @param index the index to check - * @throws IndexOutOfBoundsException if index < 0 || index >= size + * @throws IndexOutOfBoundsException if index >= size */ - private void rangeExclusive(int index) + private void checkBoundExclusive(int index) { // Implementation note: we do not check for negative ranges here, since - // that will cause an ArrayIndexOutOfBoundsException, a subclass of - // the required exception, with no effort on our part. + // use of a negative index will cause an ArrayIndexOutOfBoundsException, + // a subclass of the required exception, with no effort on our part. if (index >= size) throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); diff --git a/java/util/LinkedList.java b/java/util/LinkedList.java index 7a0f00070..1205ba785 100644 --- a/java/util/LinkedList.java +++ b/java/util/LinkedList.java @@ -176,7 +176,7 @@ public class LinkedList extends AbstractSequentialList * @param index the index to check * @throws IndexOutOfBoundsException if index < 0 || index > size */ - private void rangeInclusive(int index) + private void checkBoundsInclusive(int index) { if (index < 0 || index > size) throw new IndexOutOfBoundsException("Index: " + index + ", Size:" @@ -189,7 +189,7 @@ public class LinkedList extends AbstractSequentialList * @param index the index to check * @throws IndexOutOfBoundsException if index < 0 || index >= size */ - private void rangeExclusive(int index) + private void checkBoundsExclusive(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException("Index: " + index + ", Size:" @@ -428,7 +428,7 @@ public class LinkedList extends AbstractSequentialList */ public boolean addAll(int index, Collection c) { - rangeInclusive(index); + checkBoundsInclusive(index); int csize = c.size(); if (csize == 0) @@ -506,7 +506,7 @@ public class LinkedList extends AbstractSequentialList */ public Object get(int index) { - rangeExclusive(index); + checkBoundsExclusive(index); return getEntry(index).data; } @@ -520,7 +520,7 @@ public class LinkedList extends AbstractSequentialList */ public Object set(int index, Object o) { - rangeExclusive(index); + checkBoundsExclusive(index); Entry e = getEntry(index); Object old = e.data; e.data = o; @@ -536,7 +536,7 @@ public class LinkedList extends AbstractSequentialList */ public void add(int index, Object o) { - rangeInclusive(index); + checkBoundsInclusive(index); Entry e = new Entry(o); if (index < size) @@ -565,7 +565,7 @@ public class LinkedList extends AbstractSequentialList */ public Object remove(int index) { - rangeExclusive(index); + checkBoundsExclusive(index); Entry e = getEntry(index); removeEntry(e); return e.data; @@ -622,7 +622,7 @@ public class LinkedList extends AbstractSequentialList */ public ListIterator listIterator(int index) { - rangeInclusive(index); + checkBoundsInclusive(index); return new LinkedListItr(index); } diff --git a/java/util/Vector.java b/java/util/Vector.java index d9ee0c9c0..4ed4e3f12 100644 --- a/java/util/Vector.java +++ b/java/util/Vector.java @@ -354,9 +354,7 @@ public class Vector extends AbstractList */ public synchronized int lastIndexOf(Object e, int index) { - if (index >= elementCount) - throw new IndexOutOfBoundsException(index + " >= " + elementCount); - + checkBoundExclusive(index); for (int i = index; i >= 0; i--) if (equals(e, elementData[i])) return i; @@ -373,11 +371,7 @@ public class Vector extends AbstractList */ public synchronized Object elementAt(int index) { - // Within the bounds of this Vector does not necessarily mean within - // the bounds of the internal array. - if (index >= elementCount) - throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); - + checkBoundExclusive(index); return elementData[index]; } @@ -446,9 +440,7 @@ public class Vector extends AbstractList */ public synchronized void insertElementAt(Object obj, int index) { - if (index > elementCount) - throw new ArrayIndexOutOfBoundsException(index + " > " + elementCount); - + checkBoundInclusive(index); if (elementCount == elementData.length) ensureCapacity(elementCount + 1); modCount++; @@ -598,9 +590,7 @@ public class Vector extends AbstractList */ public synchronized Object set(int index, Object element) { - if (index >= elementCount) - throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); - + checkBoundExclusive(index); Object temp = elementData[index]; elementData[index] = element; return temp; @@ -656,9 +646,7 @@ public class Vector extends AbstractList */ public synchronized Object remove(int index) { - if (index >= elementCount) - throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); - + checkBoundExclusive(index); Object temp = elementData[index]; modCount++; elementCount--; @@ -771,8 +759,7 @@ public class Vector extends AbstractList */ public synchronized boolean addAll(int index, Collection c) { - if (index > elementCount) - throw new ArrayIndexOutOfBoundsException(index); + checkBoundInclusive(index); Iterator itr = c.iterator(); int csize = c.size(); @@ -871,4 +858,34 @@ public class Vector extends AbstractList Arrays.fill(elementData, elementCount, save, null); } } + + /** + * Checks that the index is in the range of possible elements (inclusive). + * + * @param index the index to check + * @throws ArrayIndexOutOfBoundsException if index > size + */ + private void checkBoundInclusive(int index) + { + // Implementation note: we do not check for negative ranges here, since + // use of a negative index will cause an ArrayIndexOutOfBoundsException + // with no effort on our part. + if (index > elementCount) + throw new ArrayIndexOutOfBoundsException(index + " > " + elementCount); + } + + /** + * Checks that the index is in the range of existing elements (exclusive). + * + * @param index the index to check + * @throws ArrayIndexOutOfBoundsException if index >= size + */ + private void checkBoundExclusive(int index) + { + // Implementation note: we do not check for negative ranges here, since + // use of a negative index will cause an ArrayIndexOutOfBoundsException + // with no effort on our part. + if (index >= elementCount) + throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); + } } |