Vector.java: Improve exception messages.

2000-11-23  Bryce McKinlay  <bryce@albatross.co.nz>

	* java/util/Vector.java: Improve exception messages.
	(Vector): Check initialCapacity for IllegalArgumentException.
	(tromToSize): Don't check for elementCount == elementData.length
	case.
	(toArray): Don't try to set null marker if target array is the same
	length as the vector.

From-SVN: r37685
This commit is contained in:
Bryce McKinlay 2000-11-23 05:18:41 +00:00 committed by Bryce McKinlay
parent 23a5b4448c
commit ebb66eb5fa
2 changed files with 32 additions and 18 deletions

View File

@ -1,3 +1,12 @@
2000-11-23 Bryce McKinlay <bryce@albatross.co.nz>
* java/util/Vector.java: Improve exception messages.
(Vector): Check initialCapacity for IllegalArgumentException.
(tromToSize): Don't check for elementCount == elementData.length
case.
(toArray): Don't try to set null marker if target array is the same
length as the vector.
2000-11-22 Bryce McKinlay <bryce@albatross.co.nz> 2000-11-22 Bryce McKinlay <bryce@albatross.co.nz>
* Makefile.in: Rebuilt. * Makefile.in: Rebuilt.

View File

@ -115,6 +115,8 @@ public class Vector extends AbstractList
*/ */
public Vector(int initialCapacity, int capacityIncrement) public Vector(int initialCapacity, int capacityIncrement)
{ {
if (initialCapacity < 0)
throw new IllegalArgumentException();
elementData = new Object[initialCapacity]; elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement; this.capacityIncrement = capacityIncrement;
} }
@ -126,6 +128,8 @@ public class Vector extends AbstractList
*/ */
public Vector(int initialCapacity) public Vector(int initialCapacity)
{ {
if (initialCapacity < 0)
throw new IllegalArgumentException();
elementData = new Object[initialCapacity]; elementData = new Object[initialCapacity];
} }
@ -152,10 +156,9 @@ public class Vector extends AbstractList
*/ */
public synchronized void trimToSize() public synchronized void trimToSize()
{ {
// Check if the Vector is already trimmed, to save execution time // Don't bother checking for the case where size() == the capacity of the
if (elementCount == elementData.length) // vector since that is a much less likely case; it's more efficient to
return; // not do the check and lose a bit of performance in that infrequent case
// Guess not
Object[] newArray = new Object[elementCount]; Object[] newArray = new Object[elementCount];
System.arraycopy(elementData, 0, newArray, 0, elementCount); System.arraycopy(elementData, 0, newArray, 0, elementCount);
@ -296,7 +299,7 @@ public class Vector extends AbstractList
public synchronized int lastIndexOf(Object e, int index) public synchronized int lastIndexOf(Object e, int index)
{ {
if (index >= elementCount) if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index); throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
for (int i = index; i >= 0; i--) for (int i = index; i >= 0; i--)
{ {
@ -332,7 +335,7 @@ public class Vector extends AbstractList
//Within the bounds of this Vector does not necessarily mean within //Within the bounds of this Vector does not necessarily mean within
//the bounds of the internal array //the bounds of the internal array
if (index >= elementCount) if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index); throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
return elementData[index]; return elementData[index];
} }
@ -378,8 +381,8 @@ public class Vector extends AbstractList
*/ */
public synchronized void setElementAt(Object obj, int index) public synchronized void setElementAt(Object obj, int index)
{ {
if ((index < 0) || (index >= elementCount)) if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index); throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
elementData[index] = obj; elementData[index] = obj;
} }
@ -397,7 +400,7 @@ public class Vector extends AbstractList
public synchronized Object set(int index, Object element) public synchronized Object set(int index, Object element)
{ {
if (index >= elementCount) if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index); throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
Object temp = elementData[index]; Object temp = elementData[index];
elementData[index] = element; elementData[index] = element;
@ -413,7 +416,7 @@ public class Vector extends AbstractList
public synchronized void removeElementAt(int index) public synchronized void removeElementAt(int index)
{ {
if (index >= elementCount) if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index); throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
modCount++; modCount++;
elementCount--; elementCount--;
@ -434,9 +437,10 @@ public class Vector extends AbstractList
*/ */
public void insertElementAt(Object obj, int index) public void insertElementAt(Object obj, int index)
{ {
if ((index < 0) || (index > elementCount)) if (index > elementCount)
throw new ArrayIndexOutOfBoundsException(index); throw new ArrayIndexOutOfBoundsException(index + " > " + elementCount);
if (elementCount == elementData.length)
ensureCapacity(++elementCount); ensureCapacity(++elementCount);
modCount++; modCount++;
System.arraycopy(elementData, index, elementData, index + 1, System.arraycopy(elementData, index, elementData, index + 1,
@ -454,7 +458,8 @@ public class Vector extends AbstractList
*/ */
public synchronized void addElement(Object obj) public synchronized void addElement(Object obj)
{ {
ensureCapacity(elementCount + 1); if (elementCount == elementData.length)
ensureCapacity(++elementCount);
modCount++; modCount++;
elementData[elementCount++] = obj; elementData[elementCount++] = obj;
} }
@ -488,7 +493,7 @@ public class Vector extends AbstractList
if (elementCount == 0) if (elementCount == 0)
return; return;
for (int i = 0; i < elementCount; i++) for (int i = elementCount - 1; i >= 0; --i)
{ {
elementData[i] = null; elementData[i] = null;
} }
@ -553,7 +558,7 @@ public class Vector extends AbstractList
if (array.length < elementCount) if (array.length < elementCount)
array = (Object[]) Array.newInstance(array.getClass().getComponentType(), array = (Object[]) Array.newInstance(array.getClass().getComponentType(),
elementCount); elementCount);
else else if (array.length > elementCount)
array[elementCount] = null; array[elementCount] = null;
System.arraycopy(elementData, 0, array, 0, elementCount); System.arraycopy(elementData, 0, array, 0, elementCount);
return array; return array;
@ -617,7 +622,7 @@ public class Vector extends AbstractList
public synchronized Object remove(int index) public synchronized Object remove(int index)
{ {
if (index >= elementCount) if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index); throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
Object temp = elementData[index]; Object temp = elementData[index];
removeElementAt(index); removeElementAt(index);