Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 2302 invoked from network); 20 Oct 2003 22:20:19 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 20 Oct 2003 22:20:19 -0000 Received: (qmail 47716 invoked by uid 500); 20 Oct 2003 22:19:57 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 47605 invoked by uid 500); 20 Oct 2003 22:19:56 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 47589 invoked by uid 500); 20 Oct 2003 22:19:55 -0000 Received: (qmail 47586 invoked from network); 20 Oct 2003 22:19:55 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 20 Oct 2003 22:19:55 -0000 Received: (qmail 2271 invoked by uid 1529); 20 Oct 2003 22:20:08 -0000 Date: 20 Oct 2003 22:20:08 -0000 Message-ID: <20031020222008.2270.qmail@minotaur.apache.org> From: scolebourne@apache.org To: jakarta-commons-sandbox-cvs@apache.org Subject: cvs commit: jakarta-commons-sandbox/primitives/src/codegen/org/apache/commons/primitive/list/impl AbstractXXXList.vm ArrayXXXList.vm X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N scolebourne 2003/10/20 15:20:08 Modified: primitives/src/java/org/apache/commons/primitive/list/impl ArrayLongList.java StringCharList.java AbstractShortList.java ArrayDoubleList.java AbstractFloatList.java ArrayShortList.java AbstractBooleanList.java AbstractByteList.java AbstractLongList.java AbstractIntList.java ArrayBooleanList.java ArrayCharList.java AbstractDoubleList.java ArrayByteList.java AbstractCharList.java ArrayIntList.java ArrayFloatList.java primitives/src/codegen/org/apache/commons/primitive/list/impl AbstractXXXList.vm ArrayXXXList.vm Log: Update primitive lists to check for indices Revision Changes Path 1.2 +14 -11 jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/ArrayLongList.java Index: ArrayLongList.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/ArrayLongList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ArrayLongList.java 19 Oct 2003 00:28:58 -0000 1.1 +++ ArrayLongList.java 20 Oct 2003 22:20:07 -0000 1.2 @@ -151,6 +151,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public long getLong(int index) { + checkIndexExists(index); return elements[index]; } @@ -164,7 +165,8 @@ */ public boolean add(int index, long value) { checkAddModifiable(); - ensureCapacity(1); + checkIndex(index); + ensureCapacity(size + 1); System.arraycopy(elements, index, elements, index + 1, size - index); elements[index] = value; size++; @@ -180,6 +182,7 @@ */ public long removeIndex(int index) { checkRemoveModifiable(); + checkIndexExists(index); long result = elements[index]; System.arraycopy(elements, index + 1, elements, index, size - 1 - index); size--; @@ -196,6 +199,7 @@ */ public long set(int index, long value) { checkSetModifiable(); + checkIndexExists(index); long result = elements[index]; elements[index] = value; return result; @@ -238,11 +242,12 @@ */ public boolean addAll(int index, long[] values) { checkAddModifiable(); + checkIndex(index); if (values == null || values.length == 0) { return false; } int len = values.length; - ensureCapacity(len); + ensureCapacity(size + len); System.arraycopy(elements, index, elements, index + len, size - index); System.arraycopy(values, 0, elements, index, len); size += len; @@ -313,20 +318,18 @@ // Internal implementation //----------------------------------------------------------------------- /** - * Expands the array by the amount specified + * Ensures that the internal storage array has at least the specified size. * - * @param array the old array - * @param expansion the amount to expand by - * @return the new array + * @param capacity the amount to expand to */ - protected void ensureCapacity(int expansion) { - if (expansion <= 0) { + protected void ensureCapacity(int capacity) { + int len = elements.length; + if (capacity <= len) { return; } - int len = elements.length; int newLen = len * GROWTH_FACTOR_MULTIPLIER / GROWTH_FACTOR_DIVISOR; - if (newLen < len + expansion) { - newLen = len + expansion; + if (newLen < capacity) { + newLen = capacity; } if (newLen < MIN_GROWTH_SIZE) { newLen = MIN_GROWTH_SIZE; 1.2 +12 -1 jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/StringCharList.java Index: StringCharList.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/StringCharList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- StringCharList.java 19 Oct 2003 00:28:58 -0000 1.1 +++ StringCharList.java 20 Oct 2003 22:20:07 -0000 1.2 @@ -107,6 +107,17 @@ this.string = str; } + // Extra API + //----------------------------------------------------------------------- + /** + * Gets the String underlying the list. + * + * @return the underlying string + */ + public String asString() { + return string; + } + // Implementation //----------------------------------------------------------------------- /** 1.2 +40 -2 jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/AbstractShortList.java Index: AbstractShortList.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/AbstractShortList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- AbstractShortList.java 19 Oct 2003 00:28:58 -0000 1.1 +++ AbstractShortList.java 20 Oct 2003 22:20:07 -0000 1.2 @@ -120,6 +120,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public ShortListIterator shortListIterator(int index) { + checkIndex(index); return new PListIterator(this, index); } @@ -186,6 +187,9 @@ * @return the zero-based index, or -1 if not found */ public int indexOf(short value, int fromIndexInclusive) { + if (fromIndexInclusive < 0) { + fromIndexInclusive = 0; + } for (int i = fromIndexInclusive, isize = size(); i < isize; i++) { if (getShort(i) == value) { return i; @@ -340,6 +344,7 @@ */ public boolean addAll(int index, short[] values) { checkAddModifiable(); + checkIndex(index); boolean changed = false; if (values != null) { for (int i = 0; i < values.length; i++) { @@ -565,6 +570,7 @@ */ public void add(int index, Object value) { checkAddModifiable(); + checkIndex(index); add(index, ShortUtils.toPrimitive(value)); } @@ -584,6 +590,7 @@ */ public boolean addAll(int index, Collection coll) { checkAddModifiable(); + checkIndex(index); return addAll(index, ShortUtils.toPrimitiveArray(coll)); } @@ -616,6 +623,7 @@ */ public Object set(int index, Object value) { checkSetModifiable(); + checkIndexExists(index); return ShortUtils.toObject(set(index, ShortUtils.toPrimitive(value))); } @@ -743,6 +751,36 @@ } /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndexExists(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index >= size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than/equal to size(): " + index + " >= " + size()); + } + } + + /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndex(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index > size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than size(): " + index + " > " + size()); + } + } + + /** * Checks whether a range is valid or not. * * @param fromIndexInclusive the index to start from, inclusive @@ -751,10 +789,10 @@ */ protected void checkRange(int fromIndexInclusive, int toIndexExclusive) { if (fromIndexInclusive < 0) { - throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive); + throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive + " < 0"); } if (toIndexExclusive > size()) { - throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive); + throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive + " > " + size()); } if (fromIndexInclusive > toIndexExclusive) { throw new ArrayIndexOutOfBoundsException("To index greater than from index: " + fromIndexInclusive + " > " + toIndexExclusive); 1.2 +14 -11 jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/ArrayDoubleList.java Index: ArrayDoubleList.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/ArrayDoubleList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ArrayDoubleList.java 19 Oct 2003 00:28:58 -0000 1.1 +++ ArrayDoubleList.java 20 Oct 2003 22:20:07 -0000 1.2 @@ -151,6 +151,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public double getDouble(int index) { + checkIndexExists(index); return elements[index]; } @@ -164,7 +165,8 @@ */ public boolean add(int index, double value) { checkAddModifiable(); - ensureCapacity(1); + checkIndex(index); + ensureCapacity(size + 1); System.arraycopy(elements, index, elements, index + 1, size - index); elements[index] = value; size++; @@ -180,6 +182,7 @@ */ public double removeIndex(int index) { checkRemoveModifiable(); + checkIndexExists(index); double result = elements[index]; System.arraycopy(elements, index + 1, elements, index, size - 1 - index); size--; @@ -196,6 +199,7 @@ */ public double set(int index, double value) { checkSetModifiable(); + checkIndexExists(index); double result = elements[index]; elements[index] = value; return result; @@ -238,11 +242,12 @@ */ public boolean addAll(int index, double[] values) { checkAddModifiable(); + checkIndex(index); if (values == null || values.length == 0) { return false; } int len = values.length; - ensureCapacity(len); + ensureCapacity(size + len); System.arraycopy(elements, index, elements, index + len, size - index); System.arraycopy(values, 0, elements, index, len); size += len; @@ -313,20 +318,18 @@ // Internal implementation //----------------------------------------------------------------------- /** - * Expands the array by the amount specified + * Ensures that the internal storage array has at least the specified size. * - * @param array the old array - * @param expansion the amount to expand by - * @return the new array + * @param capacity the amount to expand to */ - protected void ensureCapacity(int expansion) { - if (expansion <= 0) { + protected void ensureCapacity(int capacity) { + int len = elements.length; + if (capacity <= len) { return; } - int len = elements.length; int newLen = len * GROWTH_FACTOR_MULTIPLIER / GROWTH_FACTOR_DIVISOR; - if (newLen < len + expansion) { - newLen = len + expansion; + if (newLen < capacity) { + newLen = capacity; } if (newLen < MIN_GROWTH_SIZE) { newLen = MIN_GROWTH_SIZE; 1.2 +40 -2 jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/AbstractFloatList.java Index: AbstractFloatList.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/AbstractFloatList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- AbstractFloatList.java 19 Oct 2003 00:28:58 -0000 1.1 +++ AbstractFloatList.java 20 Oct 2003 22:20:07 -0000 1.2 @@ -120,6 +120,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public FloatListIterator floatListIterator(int index) { + checkIndex(index); return new PListIterator(this, index); } @@ -186,6 +187,9 @@ * @return the zero-based index, or -1 if not found */ public int indexOf(float value, int fromIndexInclusive) { + if (fromIndexInclusive < 0) { + fromIndexInclusive = 0; + } for (int i = fromIndexInclusive, isize = size(); i < isize; i++) { if (getFloat(i) == value) { return i; @@ -340,6 +344,7 @@ */ public boolean addAll(int index, float[] values) { checkAddModifiable(); + checkIndex(index); boolean changed = false; if (values != null) { for (int i = 0; i < values.length; i++) { @@ -565,6 +570,7 @@ */ public void add(int index, Object value) { checkAddModifiable(); + checkIndex(index); add(index, FloatUtils.toPrimitive(value)); } @@ -584,6 +590,7 @@ */ public boolean addAll(int index, Collection coll) { checkAddModifiable(); + checkIndex(index); return addAll(index, FloatUtils.toPrimitiveArray(coll)); } @@ -616,6 +623,7 @@ */ public Object set(int index, Object value) { checkSetModifiable(); + checkIndexExists(index); return FloatUtils.toObject(set(index, FloatUtils.toPrimitive(value))); } @@ -743,6 +751,36 @@ } /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndexExists(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index >= size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than/equal to size(): " + index + " >= " + size()); + } + } + + /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndex(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index > size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than size(): " + index + " > " + size()); + } + } + + /** * Checks whether a range is valid or not. * * @param fromIndexInclusive the index to start from, inclusive @@ -751,10 +789,10 @@ */ protected void checkRange(int fromIndexInclusive, int toIndexExclusive) { if (fromIndexInclusive < 0) { - throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive); + throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive + " < 0"); } if (toIndexExclusive > size()) { - throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive); + throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive + " > " + size()); } if (fromIndexInclusive > toIndexExclusive) { throw new ArrayIndexOutOfBoundsException("To index greater than from index: " + fromIndexInclusive + " > " + toIndexExclusive); 1.2 +14 -11 jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/ArrayShortList.java Index: ArrayShortList.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/ArrayShortList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ArrayShortList.java 19 Oct 2003 00:28:58 -0000 1.1 +++ ArrayShortList.java 20 Oct 2003 22:20:07 -0000 1.2 @@ -151,6 +151,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public short getShort(int index) { + checkIndexExists(index); return elements[index]; } @@ -164,7 +165,8 @@ */ public boolean add(int index, short value) { checkAddModifiable(); - ensureCapacity(1); + checkIndex(index); + ensureCapacity(size + 1); System.arraycopy(elements, index, elements, index + 1, size - index); elements[index] = value; size++; @@ -180,6 +182,7 @@ */ public short removeIndex(int index) { checkRemoveModifiable(); + checkIndexExists(index); short result = elements[index]; System.arraycopy(elements, index + 1, elements, index, size - 1 - index); size--; @@ -196,6 +199,7 @@ */ public short set(int index, short value) { checkSetModifiable(); + checkIndexExists(index); short result = elements[index]; elements[index] = value; return result; @@ -238,11 +242,12 @@ */ public boolean addAll(int index, short[] values) { checkAddModifiable(); + checkIndex(index); if (values == null || values.length == 0) { return false; } int len = values.length; - ensureCapacity(len); + ensureCapacity(size + len); System.arraycopy(elements, index, elements, index + len, size - index); System.arraycopy(values, 0, elements, index, len); size += len; @@ -313,20 +318,18 @@ // Internal implementation //----------------------------------------------------------------------- /** - * Expands the array by the amount specified + * Ensures that the internal storage array has at least the specified size. * - * @param array the old array - * @param expansion the amount to expand by - * @return the new array + * @param capacity the amount to expand to */ - protected void ensureCapacity(int expansion) { - if (expansion <= 0) { + protected void ensureCapacity(int capacity) { + int len = elements.length; + if (capacity <= len) { return; } - int len = elements.length; int newLen = len * GROWTH_FACTOR_MULTIPLIER / GROWTH_FACTOR_DIVISOR; - if (newLen < len + expansion) { - newLen = len + expansion; + if (newLen < capacity) { + newLen = capacity; } if (newLen < MIN_GROWTH_SIZE) { newLen = MIN_GROWTH_SIZE; 1.2 +40 -2 jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/AbstractBooleanList.java Index: AbstractBooleanList.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/AbstractBooleanList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- AbstractBooleanList.java 19 Oct 2003 00:28:58 -0000 1.1 +++ AbstractBooleanList.java 20 Oct 2003 22:20:07 -0000 1.2 @@ -120,6 +120,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public BooleanListIterator booleanListIterator(int index) { + checkIndex(index); return new PListIterator(this, index); } @@ -186,6 +187,9 @@ * @return the zero-based index, or -1 if not found */ public int indexOf(boolean value, int fromIndexInclusive) { + if (fromIndexInclusive < 0) { + fromIndexInclusive = 0; + } for (int i = fromIndexInclusive, isize = size(); i < isize; i++) { if (getBoolean(i) == value) { return i; @@ -340,6 +344,7 @@ */ public boolean addAll(int index, boolean[] values) { checkAddModifiable(); + checkIndex(index); boolean changed = false; if (values != null) { for (int i = 0; i < values.length; i++) { @@ -565,6 +570,7 @@ */ public void add(int index, Object value) { checkAddModifiable(); + checkIndex(index); add(index, BooleanUtils.toPrimitive(value)); } @@ -584,6 +590,7 @@ */ public boolean addAll(int index, Collection coll) { checkAddModifiable(); + checkIndex(index); return addAll(index, BooleanUtils.toPrimitiveArray(coll)); } @@ -616,6 +623,7 @@ */ public Object set(int index, Object value) { checkSetModifiable(); + checkIndexExists(index); return BooleanUtils.toObject(set(index, BooleanUtils.toPrimitive(value))); } @@ -743,6 +751,36 @@ } /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndexExists(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index >= size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than/equal to size(): " + index + " >= " + size()); + } + } + + /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndex(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index > size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than size(): " + index + " > " + size()); + } + } + + /** * Checks whether a range is valid or not. * * @param fromIndexInclusive the index to start from, inclusive @@ -751,10 +789,10 @@ */ protected void checkRange(int fromIndexInclusive, int toIndexExclusive) { if (fromIndexInclusive < 0) { - throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive); + throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive + " < 0"); } if (toIndexExclusive > size()) { - throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive); + throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive + " > " + size()); } if (fromIndexInclusive > toIndexExclusive) { throw new ArrayIndexOutOfBoundsException("To index greater than from index: " + fromIndexInclusive + " > " + toIndexExclusive); 1.2 +40 -2 jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/AbstractByteList.java Index: AbstractByteList.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/AbstractByteList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- AbstractByteList.java 19 Oct 2003 00:28:58 -0000 1.1 +++ AbstractByteList.java 20 Oct 2003 22:20:07 -0000 1.2 @@ -120,6 +120,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public ByteListIterator byteListIterator(int index) { + checkIndex(index); return new PListIterator(this, index); } @@ -186,6 +187,9 @@ * @return the zero-based index, or -1 if not found */ public int indexOf(byte value, int fromIndexInclusive) { + if (fromIndexInclusive < 0) { + fromIndexInclusive = 0; + } for (int i = fromIndexInclusive, isize = size(); i < isize; i++) { if (getByte(i) == value) { return i; @@ -340,6 +344,7 @@ */ public boolean addAll(int index, byte[] values) { checkAddModifiable(); + checkIndex(index); boolean changed = false; if (values != null) { for (int i = 0; i < values.length; i++) { @@ -565,6 +570,7 @@ */ public void add(int index, Object value) { checkAddModifiable(); + checkIndex(index); add(index, ByteUtils.toPrimitive(value)); } @@ -584,6 +590,7 @@ */ public boolean addAll(int index, Collection coll) { checkAddModifiable(); + checkIndex(index); return addAll(index, ByteUtils.toPrimitiveArray(coll)); } @@ -616,6 +623,7 @@ */ public Object set(int index, Object value) { checkSetModifiable(); + checkIndexExists(index); return ByteUtils.toObject(set(index, ByteUtils.toPrimitive(value))); } @@ -743,6 +751,36 @@ } /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndexExists(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index >= size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than/equal to size(): " + index + " >= " + size()); + } + } + + /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndex(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index > size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than size(): " + index + " > " + size()); + } + } + + /** * Checks whether a range is valid or not. * * @param fromIndexInclusive the index to start from, inclusive @@ -751,10 +789,10 @@ */ protected void checkRange(int fromIndexInclusive, int toIndexExclusive) { if (fromIndexInclusive < 0) { - throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive); + throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive + " < 0"); } if (toIndexExclusive > size()) { - throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive); + throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive + " > " + size()); } if (fromIndexInclusive > toIndexExclusive) { throw new ArrayIndexOutOfBoundsException("To index greater than from index: " + fromIndexInclusive + " > " + toIndexExclusive); 1.2 +40 -2 jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/AbstractLongList.java Index: AbstractLongList.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/AbstractLongList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- AbstractLongList.java 19 Oct 2003 00:28:58 -0000 1.1 +++ AbstractLongList.java 20 Oct 2003 22:20:07 -0000 1.2 @@ -120,6 +120,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public LongListIterator longListIterator(int index) { + checkIndex(index); return new PListIterator(this, index); } @@ -186,6 +187,9 @@ * @return the zero-based index, or -1 if not found */ public int indexOf(long value, int fromIndexInclusive) { + if (fromIndexInclusive < 0) { + fromIndexInclusive = 0; + } for (int i = fromIndexInclusive, isize = size(); i < isize; i++) { if (getLong(i) == value) { return i; @@ -340,6 +344,7 @@ */ public boolean addAll(int index, long[] values) { checkAddModifiable(); + checkIndex(index); boolean changed = false; if (values != null) { for (int i = 0; i < values.length; i++) { @@ -565,6 +570,7 @@ */ public void add(int index, Object value) { checkAddModifiable(); + checkIndex(index); add(index, LongUtils.toPrimitive(value)); } @@ -584,6 +590,7 @@ */ public boolean addAll(int index, Collection coll) { checkAddModifiable(); + checkIndex(index); return addAll(index, LongUtils.toPrimitiveArray(coll)); } @@ -616,6 +623,7 @@ */ public Object set(int index, Object value) { checkSetModifiable(); + checkIndexExists(index); return LongUtils.toObject(set(index, LongUtils.toPrimitive(value))); } @@ -743,6 +751,36 @@ } /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndexExists(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index >= size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than/equal to size(): " + index + " >= " + size()); + } + } + + /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndex(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index > size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than size(): " + index + " > " + size()); + } + } + + /** * Checks whether a range is valid or not. * * @param fromIndexInclusive the index to start from, inclusive @@ -751,10 +789,10 @@ */ protected void checkRange(int fromIndexInclusive, int toIndexExclusive) { if (fromIndexInclusive < 0) { - throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive); + throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive + " < 0"); } if (toIndexExclusive > size()) { - throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive); + throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive + " > " + size()); } if (fromIndexInclusive > toIndexExclusive) { throw new ArrayIndexOutOfBoundsException("To index greater than from index: " + fromIndexInclusive + " > " + toIndexExclusive); 1.2 +40 -2 jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/AbstractIntList.java Index: AbstractIntList.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/AbstractIntList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- AbstractIntList.java 19 Oct 2003 00:28:58 -0000 1.1 +++ AbstractIntList.java 20 Oct 2003 22:20:07 -0000 1.2 @@ -120,6 +120,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public IntListIterator intListIterator(int index) { + checkIndex(index); return new PListIterator(this, index); } @@ -186,6 +187,9 @@ * @return the zero-based index, or -1 if not found */ public int indexOf(int value, int fromIndexInclusive) { + if (fromIndexInclusive < 0) { + fromIndexInclusive = 0; + } for (int i = fromIndexInclusive, isize = size(); i < isize; i++) { if (getInt(i) == value) { return i; @@ -340,6 +344,7 @@ */ public boolean addAll(int index, int[] values) { checkAddModifiable(); + checkIndex(index); boolean changed = false; if (values != null) { for (int i = 0; i < values.length; i++) { @@ -565,6 +570,7 @@ */ public void add(int index, Object value) { checkAddModifiable(); + checkIndex(index); add(index, IntUtils.toPrimitive(value)); } @@ -584,6 +590,7 @@ */ public boolean addAll(int index, Collection coll) { checkAddModifiable(); + checkIndex(index); return addAll(index, IntUtils.toPrimitiveArray(coll)); } @@ -616,6 +623,7 @@ */ public Object set(int index, Object value) { checkSetModifiable(); + checkIndexExists(index); return IntUtils.toObject(set(index, IntUtils.toPrimitive(value))); } @@ -743,6 +751,36 @@ } /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndexExists(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index >= size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than/equal to size(): " + index + " >= " + size()); + } + } + + /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndex(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index > size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than size(): " + index + " > " + size()); + } + } + + /** * Checks whether a range is valid or not. * * @param fromIndexInclusive the index to start from, inclusive @@ -751,10 +789,10 @@ */ protected void checkRange(int fromIndexInclusive, int toIndexExclusive) { if (fromIndexInclusive < 0) { - throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive); + throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive + " < 0"); } if (toIndexExclusive > size()) { - throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive); + throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive + " > " + size()); } if (fromIndexInclusive > toIndexExclusive) { throw new ArrayIndexOutOfBoundsException("To index greater than from index: " + fromIndexInclusive + " > " + toIndexExclusive); 1.2 +14 -11 jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/ArrayBooleanList.java Index: ArrayBooleanList.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/ArrayBooleanList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ArrayBooleanList.java 19 Oct 2003 00:28:58 -0000 1.1 +++ ArrayBooleanList.java 20 Oct 2003 22:20:07 -0000 1.2 @@ -151,6 +151,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public boolean getBoolean(int index) { + checkIndexExists(index); return elements[index]; } @@ -164,7 +165,8 @@ */ public boolean add(int index, boolean value) { checkAddModifiable(); - ensureCapacity(1); + checkIndex(index); + ensureCapacity(size + 1); System.arraycopy(elements, index, elements, index + 1, size - index); elements[index] = value; size++; @@ -180,6 +182,7 @@ */ public boolean removeIndex(int index) { checkRemoveModifiable(); + checkIndexExists(index); boolean result = elements[index]; System.arraycopy(elements, index + 1, elements, index, size - 1 - index); size--; @@ -196,6 +199,7 @@ */ public boolean set(int index, boolean value) { checkSetModifiable(); + checkIndexExists(index); boolean result = elements[index]; elements[index] = value; return result; @@ -238,11 +242,12 @@ */ public boolean addAll(int index, boolean[] values) { checkAddModifiable(); + checkIndex(index); if (values == null || values.length == 0) { return false; } int len = values.length; - ensureCapacity(len); + ensureCapacity(size + len); System.arraycopy(elements, index, elements, index + len, size - index); System.arraycopy(values, 0, elements, index, len); size += len; @@ -313,20 +318,18 @@ // Internal implementation //----------------------------------------------------------------------- /** - * Expands the array by the amount specified + * Ensures that the internal storage array has at least the specified size. * - * @param array the old array - * @param expansion the amount to expand by - * @return the new array + * @param capacity the amount to expand to */ - protected void ensureCapacity(int expansion) { - if (expansion <= 0) { + protected void ensureCapacity(int capacity) { + int len = elements.length; + if (capacity <= len) { return; } - int len = elements.length; int newLen = len * GROWTH_FACTOR_MULTIPLIER / GROWTH_FACTOR_DIVISOR; - if (newLen < len + expansion) { - newLen = len + expansion; + if (newLen < capacity) { + newLen = capacity; } if (newLen < MIN_GROWTH_SIZE) { newLen = MIN_GROWTH_SIZE; 1.2 +14 -11 jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/ArrayCharList.java Index: ArrayCharList.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/ArrayCharList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ArrayCharList.java 19 Oct 2003 00:28:58 -0000 1.1 +++ ArrayCharList.java 20 Oct 2003 22:20:07 -0000 1.2 @@ -151,6 +151,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public char getChar(int index) { + checkIndexExists(index); return elements[index]; } @@ -164,7 +165,8 @@ */ public boolean add(int index, char value) { checkAddModifiable(); - ensureCapacity(1); + checkIndex(index); + ensureCapacity(size + 1); System.arraycopy(elements, index, elements, index + 1, size - index); elements[index] = value; size++; @@ -180,6 +182,7 @@ */ public char removeIndex(int index) { checkRemoveModifiable(); + checkIndexExists(index); char result = elements[index]; System.arraycopy(elements, index + 1, elements, index, size - 1 - index); size--; @@ -196,6 +199,7 @@ */ public char set(int index, char value) { checkSetModifiable(); + checkIndexExists(index); char result = elements[index]; elements[index] = value; return result; @@ -238,11 +242,12 @@ */ public boolean addAll(int index, char[] values) { checkAddModifiable(); + checkIndex(index); if (values == null || values.length == 0) { return false; } int len = values.length; - ensureCapacity(len); + ensureCapacity(size + len); System.arraycopy(elements, index, elements, index + len, size - index); System.arraycopy(values, 0, elements, index, len); size += len; @@ -313,20 +318,18 @@ // Internal implementation //----------------------------------------------------------------------- /** - * Expands the array by the amount specified + * Ensures that the internal storage array has at least the specified size. * - * @param array the old array - * @param expansion the amount to expand by - * @return the new array + * @param capacity the amount to expand to */ - protected void ensureCapacity(int expansion) { - if (expansion <= 0) { + protected void ensureCapacity(int capacity) { + int len = elements.length; + if (capacity <= len) { return; } - int len = elements.length; int newLen = len * GROWTH_FACTOR_MULTIPLIER / GROWTH_FACTOR_DIVISOR; - if (newLen < len + expansion) { - newLen = len + expansion; + if (newLen < capacity) { + newLen = capacity; } if (newLen < MIN_GROWTH_SIZE) { newLen = MIN_GROWTH_SIZE; 1.2 +40 -2 jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/AbstractDoubleList.java Index: AbstractDoubleList.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/AbstractDoubleList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- AbstractDoubleList.java 19 Oct 2003 00:28:58 -0000 1.1 +++ AbstractDoubleList.java 20 Oct 2003 22:20:07 -0000 1.2 @@ -120,6 +120,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public DoubleListIterator doubleListIterator(int index) { + checkIndex(index); return new PListIterator(this, index); } @@ -186,6 +187,9 @@ * @return the zero-based index, or -1 if not found */ public int indexOf(double value, int fromIndexInclusive) { + if (fromIndexInclusive < 0) { + fromIndexInclusive = 0; + } for (int i = fromIndexInclusive, isize = size(); i < isize; i++) { if (getDouble(i) == value) { return i; @@ -340,6 +344,7 @@ */ public boolean addAll(int index, double[] values) { checkAddModifiable(); + checkIndex(index); boolean changed = false; if (values != null) { for (int i = 0; i < values.length; i++) { @@ -565,6 +570,7 @@ */ public void add(int index, Object value) { checkAddModifiable(); + checkIndex(index); add(index, DoubleUtils.toPrimitive(value)); } @@ -584,6 +590,7 @@ */ public boolean addAll(int index, Collection coll) { checkAddModifiable(); + checkIndex(index); return addAll(index, DoubleUtils.toPrimitiveArray(coll)); } @@ -616,6 +623,7 @@ */ public Object set(int index, Object value) { checkSetModifiable(); + checkIndexExists(index); return DoubleUtils.toObject(set(index, DoubleUtils.toPrimitive(value))); } @@ -743,6 +751,36 @@ } /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndexExists(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index >= size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than/equal to size(): " + index + " >= " + size()); + } + } + + /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndex(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index > size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than size(): " + index + " > " + size()); + } + } + + /** * Checks whether a range is valid or not. * * @param fromIndexInclusive the index to start from, inclusive @@ -751,10 +789,10 @@ */ protected void checkRange(int fromIndexInclusive, int toIndexExclusive) { if (fromIndexInclusive < 0) { - throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive); + throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive + " < 0"); } if (toIndexExclusive > size()) { - throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive); + throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive + " > " + size()); } if (fromIndexInclusive > toIndexExclusive) { throw new ArrayIndexOutOfBoundsException("To index greater than from index: " + fromIndexInclusive + " > " + toIndexExclusive); 1.2 +14 -11 jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/ArrayByteList.java Index: ArrayByteList.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/ArrayByteList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ArrayByteList.java 19 Oct 2003 00:28:58 -0000 1.1 +++ ArrayByteList.java 20 Oct 2003 22:20:07 -0000 1.2 @@ -151,6 +151,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public byte getByte(int index) { + checkIndexExists(index); return elements[index]; } @@ -164,7 +165,8 @@ */ public boolean add(int index, byte value) { checkAddModifiable(); - ensureCapacity(1); + checkIndex(index); + ensureCapacity(size + 1); System.arraycopy(elements, index, elements, index + 1, size - index); elements[index] = value; size++; @@ -180,6 +182,7 @@ */ public byte removeIndex(int index) { checkRemoveModifiable(); + checkIndexExists(index); byte result = elements[index]; System.arraycopy(elements, index + 1, elements, index, size - 1 - index); size--; @@ -196,6 +199,7 @@ */ public byte set(int index, byte value) { checkSetModifiable(); + checkIndexExists(index); byte result = elements[index]; elements[index] = value; return result; @@ -238,11 +242,12 @@ */ public boolean addAll(int index, byte[] values) { checkAddModifiable(); + checkIndex(index); if (values == null || values.length == 0) { return false; } int len = values.length; - ensureCapacity(len); + ensureCapacity(size + len); System.arraycopy(elements, index, elements, index + len, size - index); System.arraycopy(values, 0, elements, index, len); size += len; @@ -313,20 +318,18 @@ // Internal implementation //----------------------------------------------------------------------- /** - * Expands the array by the amount specified + * Ensures that the internal storage array has at least the specified size. * - * @param array the old array - * @param expansion the amount to expand by - * @return the new array + * @param capacity the amount to expand to */ - protected void ensureCapacity(int expansion) { - if (expansion <= 0) { + protected void ensureCapacity(int capacity) { + int len = elements.length; + if (capacity <= len) { return; } - int len = elements.length; int newLen = len * GROWTH_FACTOR_MULTIPLIER / GROWTH_FACTOR_DIVISOR; - if (newLen < len + expansion) { - newLen = len + expansion; + if (newLen < capacity) { + newLen = capacity; } if (newLen < MIN_GROWTH_SIZE) { newLen = MIN_GROWTH_SIZE; 1.2 +40 -2 jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/AbstractCharList.java Index: AbstractCharList.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/AbstractCharList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- AbstractCharList.java 19 Oct 2003 00:28:58 -0000 1.1 +++ AbstractCharList.java 20 Oct 2003 22:20:07 -0000 1.2 @@ -120,6 +120,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public CharListIterator charListIterator(int index) { + checkIndex(index); return new PListIterator(this, index); } @@ -186,6 +187,9 @@ * @return the zero-based index, or -1 if not found */ public int indexOf(char value, int fromIndexInclusive) { + if (fromIndexInclusive < 0) { + fromIndexInclusive = 0; + } for (int i = fromIndexInclusive, isize = size(); i < isize; i++) { if (getChar(i) == value) { return i; @@ -340,6 +344,7 @@ */ public boolean addAll(int index, char[] values) { checkAddModifiable(); + checkIndex(index); boolean changed = false; if (values != null) { for (int i = 0; i < values.length; i++) { @@ -565,6 +570,7 @@ */ public void add(int index, Object value) { checkAddModifiable(); + checkIndex(index); add(index, CharUtils.toPrimitive(value)); } @@ -584,6 +590,7 @@ */ public boolean addAll(int index, Collection coll) { checkAddModifiable(); + checkIndex(index); return addAll(index, CharUtils.toPrimitiveArray(coll)); } @@ -616,6 +623,7 @@ */ public Object set(int index, Object value) { checkSetModifiable(); + checkIndexExists(index); return CharUtils.toObject(set(index, CharUtils.toPrimitive(value))); } @@ -743,6 +751,36 @@ } /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndexExists(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index >= size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than/equal to size(): " + index + " >= " + size()); + } + } + + /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndex(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index > size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than size(): " + index + " > " + size()); + } + } + + /** * Checks whether a range is valid or not. * * @param fromIndexInclusive the index to start from, inclusive @@ -751,10 +789,10 @@ */ protected void checkRange(int fromIndexInclusive, int toIndexExclusive) { if (fromIndexInclusive < 0) { - throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive); + throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive + " < 0"); } if (toIndexExclusive > size()) { - throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive); + throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive + " > " + size()); } if (fromIndexInclusive > toIndexExclusive) { throw new ArrayIndexOutOfBoundsException("To index greater than from index: " + fromIndexInclusive + " > " + toIndexExclusive); 1.2 +14 -11 jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/ArrayIntList.java Index: ArrayIntList.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/ArrayIntList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ArrayIntList.java 19 Oct 2003 00:28:58 -0000 1.1 +++ ArrayIntList.java 20 Oct 2003 22:20:07 -0000 1.2 @@ -151,6 +151,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public int getInt(int index) { + checkIndexExists(index); return elements[index]; } @@ -164,7 +165,8 @@ */ public boolean add(int index, int value) { checkAddModifiable(); - ensureCapacity(1); + checkIndex(index); + ensureCapacity(size + 1); System.arraycopy(elements, index, elements, index + 1, size - index); elements[index] = value; size++; @@ -180,6 +182,7 @@ */ public int removeIndex(int index) { checkRemoveModifiable(); + checkIndexExists(index); int result = elements[index]; System.arraycopy(elements, index + 1, elements, index, size - 1 - index); size--; @@ -196,6 +199,7 @@ */ public int set(int index, int value) { checkSetModifiable(); + checkIndexExists(index); int result = elements[index]; elements[index] = value; return result; @@ -238,11 +242,12 @@ */ public boolean addAll(int index, int[] values) { checkAddModifiable(); + checkIndex(index); if (values == null || values.length == 0) { return false; } int len = values.length; - ensureCapacity(len); + ensureCapacity(size + len); System.arraycopy(elements, index, elements, index + len, size - index); System.arraycopy(values, 0, elements, index, len); size += len; @@ -313,20 +318,18 @@ // Internal implementation //----------------------------------------------------------------------- /** - * Expands the array by the amount specified + * Ensures that the internal storage array has at least the specified size. * - * @param array the old array - * @param expansion the amount to expand by - * @return the new array + * @param capacity the amount to expand to */ - protected void ensureCapacity(int expansion) { - if (expansion <= 0) { + protected void ensureCapacity(int capacity) { + int len = elements.length; + if (capacity <= len) { return; } - int len = elements.length; int newLen = len * GROWTH_FACTOR_MULTIPLIER / GROWTH_FACTOR_DIVISOR; - if (newLen < len + expansion) { - newLen = len + expansion; + if (newLen < capacity) { + newLen = capacity; } if (newLen < MIN_GROWTH_SIZE) { newLen = MIN_GROWTH_SIZE; 1.2 +14 -11 jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/ArrayFloatList.java Index: ArrayFloatList.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/ArrayFloatList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ArrayFloatList.java 19 Oct 2003 00:28:58 -0000 1.1 +++ ArrayFloatList.java 20 Oct 2003 22:20:07 -0000 1.2 @@ -151,6 +151,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public float getFloat(int index) { + checkIndexExists(index); return elements[index]; } @@ -164,7 +165,8 @@ */ public boolean add(int index, float value) { checkAddModifiable(); - ensureCapacity(1); + checkIndex(index); + ensureCapacity(size + 1); System.arraycopy(elements, index, elements, index + 1, size - index); elements[index] = value; size++; @@ -180,6 +182,7 @@ */ public float removeIndex(int index) { checkRemoveModifiable(); + checkIndexExists(index); float result = elements[index]; System.arraycopy(elements, index + 1, elements, index, size - 1 - index); size--; @@ -196,6 +199,7 @@ */ public float set(int index, float value) { checkSetModifiable(); + checkIndexExists(index); float result = elements[index]; elements[index] = value; return result; @@ -238,11 +242,12 @@ */ public boolean addAll(int index, float[] values) { checkAddModifiable(); + checkIndex(index); if (values == null || values.length == 0) { return false; } int len = values.length; - ensureCapacity(len); + ensureCapacity(size + len); System.arraycopy(elements, index, elements, index + len, size - index); System.arraycopy(values, 0, elements, index, len); size += len; @@ -313,20 +318,18 @@ // Internal implementation //----------------------------------------------------------------------- /** - * Expands the array by the amount specified + * Ensures that the internal storage array has at least the specified size. * - * @param array the old array - * @param expansion the amount to expand by - * @return the new array + * @param capacity the amount to expand to */ - protected void ensureCapacity(int expansion) { - if (expansion <= 0) { + protected void ensureCapacity(int capacity) { + int len = elements.length; + if (capacity <= len) { return; } - int len = elements.length; int newLen = len * GROWTH_FACTOR_MULTIPLIER / GROWTH_FACTOR_DIVISOR; - if (newLen < len + expansion) { - newLen = len + expansion; + if (newLen < capacity) { + newLen = capacity; } if (newLen < MIN_GROWTH_SIZE) { newLen = MIN_GROWTH_SIZE; 1.2 +40 -2 jakarta-commons-sandbox/primitives/src/codegen/org/apache/commons/primitive/list/impl/AbstractXXXList.vm Index: AbstractXXXList.vm =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/codegen/org/apache/commons/primitive/list/impl/AbstractXXXList.vm,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- AbstractXXXList.vm 19 Oct 2003 00:29:00 -0000 1.1 +++ AbstractXXXList.vm 20 Oct 2003 22:20:08 -0000 1.2 @@ -68,6 +68,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public ${Type}ListIterator ${type}ListIterator(int index) { + checkIndex(index); return new PListIterator(this, index); } @@ -134,6 +135,9 @@ * @return the zero-based index, or -1 if not found */ public int indexOf(${type} value, int fromIndexInclusive) { + if (fromIndexInclusive < 0) { + fromIndexInclusive = 0; + } for (int i = fromIndexInclusive, isize = size(); i < isize; i++) { if (get${Type}(i) == value) { return i; @@ -288,6 +292,7 @@ */ public boolean addAll(int index, ${type}[] values) { checkAddModifiable(); + checkIndex(index); boolean changed = false; if (values != null) { for (int i = 0; i < values.length; i++) { @@ -513,6 +518,7 @@ */ public void add(int index, Object value) { checkAddModifiable(); + checkIndex(index); add(index, ${Type}Utils.toPrimitive(value)); } @@ -532,6 +538,7 @@ */ public boolean addAll(int index, Collection coll) { checkAddModifiable(); + checkIndex(index); return addAll(index, ${Type}Utils.toPrimitiveArray(coll)); } @@ -564,6 +571,7 @@ */ public Object set(int index, Object value) { checkSetModifiable(); + checkIndexExists(index); return ${Type}Utils.toObject(set(index, ${Type}Utils.toPrimitive(value))); } @@ -691,6 +699,36 @@ } /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndexExists(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index >= size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than/equal to size(): " + index + " >= " + size()); + } + } + + /** + * Checks whether an index is valid or not. + * + * @param index the index to check + * @throws IndexOutOfBoundsException if either index is invalid + */ + protected void checkIndex(int index) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Index less than zero: " + index + " < 0"); + } + if (index > size()) { + throw new ArrayIndexOutOfBoundsException("Index greater than size(): " + index + " > " + size()); + } + } + + /** * Checks whether a range is valid or not. * * @param fromIndexInclusive the index to start from, inclusive @@ -699,10 +737,10 @@ */ protected void checkRange(int fromIndexInclusive, int toIndexExclusive) { if (fromIndexInclusive < 0) { - throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive); + throw new ArrayIndexOutOfBoundsException("From index less than zero: " + fromIndexInclusive + " < 0"); } if (toIndexExclusive > size()) { - throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive); + throw new ArrayIndexOutOfBoundsException("To index greater than size(): " + toIndexExclusive + " > " + size()); } if (fromIndexInclusive > toIndexExclusive) { throw new ArrayIndexOutOfBoundsException("To index greater than from index: " + fromIndexInclusive + " > " + toIndexExclusive); 1.2 +14 -11 jakarta-commons-sandbox/primitives/src/codegen/org/apache/commons/primitive/list/impl/ArrayXXXList.vm Index: ArrayXXXList.vm =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/primitives/src/codegen/org/apache/commons/primitive/list/impl/ArrayXXXList.vm,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ArrayXXXList.vm 19 Oct 2003 00:29:00 -0000 1.1 +++ ArrayXXXList.vm 20 Oct 2003 22:20:08 -0000 1.2 @@ -99,6 +99,7 @@ * @throws IndexOutOfBoundsException if the index is invalid */ public ${type} get${Type}(int index) { + checkIndexExists(index); return elements[index]; } @@ -112,7 +113,8 @@ */ public boolean add(int index, ${type} value) { checkAddModifiable(); - ensureCapacity(1); + checkIndex(index); + ensureCapacity(size + 1); System.arraycopy(elements, index, elements, index + 1, size - index); elements[index] = value; size++; @@ -128,6 +130,7 @@ */ public ${type} removeIndex(int index) { checkRemoveModifiable(); + checkIndexExists(index); ${type} result = elements[index]; System.arraycopy(elements, index + 1, elements, index, size - 1 - index); size--; @@ -144,6 +147,7 @@ */ public ${type} set(int index, ${type} value) { checkSetModifiable(); + checkIndexExists(index); ${type} result = elements[index]; elements[index] = value; return result; @@ -186,11 +190,12 @@ */ public boolean addAll(int index, ${type}[] values) { checkAddModifiable(); + checkIndex(index); if (values == null || values.length == 0) { return false; } int len = values.length; - ensureCapacity(len); + ensureCapacity(size + len); System.arraycopy(elements, index, elements, index + len, size - index); System.arraycopy(values, 0, elements, index, len); size += len; @@ -261,20 +266,18 @@ // Internal implementation //----------------------------------------------------------------------- /** - * Expands the array by the amount specified + * Ensures that the internal storage array has at least the specified size. * - * @param array the old array - * @param expansion the amount to expand by - * @return the new array + * @param capacity the amount to expand to */ - protected void ensureCapacity(int expansion) { - if (expansion <= 0) { + protected void ensureCapacity(int capacity) { + int len = elements.length; + if (capacity <= len) { return; } - int len = elements.length; int newLen = len * GROWTH_FACTOR_MULTIPLIER / GROWTH_FACTOR_DIVISOR; - if (newLen < len + expansion) { - newLen = len + expansion; + if (newLen < capacity) { + newLen = capacity; } if (newLen < MIN_GROWTH_SIZE) { newLen = MIN_GROWTH_SIZE; --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org