From commits-return-63412-apmail-harmony-commits-archive=harmony.apache.org@harmony.apache.org Wed Aug 04 13:50:58 2010 Return-Path: Delivered-To: apmail-harmony-commits-archive@www.apache.org Received: (qmail 11605 invoked from network); 4 Aug 2010 13:50:58 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 4 Aug 2010 13:50:58 -0000 Received: (qmail 70910 invoked by uid 500); 4 Aug 2010 13:50:58 -0000 Delivered-To: apmail-harmony-commits-archive@harmony.apache.org Received: (qmail 70827 invoked by uid 500); 4 Aug 2010 13:50:56 -0000 Mailing-List: contact commits-help@harmony.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@harmony.apache.org Delivered-To: mailing list commits@harmony.apache.org Received: (qmail 70817 invoked by uid 99); 4 Aug 2010 13:50:55 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 04 Aug 2010 13:50:55 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 04 Aug 2010 13:50:51 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 6338523889BF; Wed, 4 Aug 2010 13:49:33 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r982250 - /harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/util/ArrayList.java Date: Wed, 04 Aug 2010 13:49:33 -0000 To: commits@harmony.apache.org From: hindessm@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100804134933.6338523889BF@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: hindessm Date: Wed Aug 4 13:49:32 2010 New Revision: 982250 URL: http://svn.apache.org/viewvc?rev=982250&view=rev Log: Move exception handling to beginning of methods. Make sure all exceptions are using Integer.valueOf(...) for int arguments. In case where the exception moves lead to restructuring of if else statements, I also re-ordered some of the statements to make the more complicated conditions/cases come later. This should actually marginally help optimize for the (more typical?) cases of adding items to the beginning and/or end of the list. Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/util/ArrayList.java Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/util/ArrayList.java URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/util/ArrayList.java?rev=982250&r1=982249&r2=982250&view=diff ============================================================================== --- harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/util/ArrayList.java (original) +++ harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/util/ArrayList.java Wed Aug 4 13:49:32 2010 @@ -105,7 +105,24 @@ public class ArrayList extends Abstra @Override public void add(int location, E object) { int size = lastIndex - firstIndex; - if (0 < location && location < size) { + if (location < 0 || location > size) { + throw new IndexOutOfBoundsException( + // luni.0A=Index: {0}, Size: {1} + Messages.getString("luni.0A", //$NON-NLS-1$ + Integer.valueOf(location), + Integer.valueOf(size))); + } + if (location == 0) { + if (firstIndex == 0) { + growAtFront(1); + } + array[--firstIndex] = object; + } else if (location == size) { + if (lastIndex == array.length) { + growAtEnd(1); + } + array[lastIndex++] = object; + } else { // must be case: (0 < location && location < size) if (firstIndex == 0 && lastIndex == array.length) { growForInsert(location, 1); } else if ((location < size / 2 && firstIndex > 0) @@ -119,22 +136,6 @@ public class ArrayList extends Abstra lastIndex++; } array[location + firstIndex] = object; - } else if (location == 0) { - if (firstIndex == 0) { - growAtFront(1); - } - array[--firstIndex] = object; - } else if (location == size) { - if (lastIndex == array.length) { - growAtEnd(1); - } - array[lastIndex++] = object; - } else { - throw new IndexOutOfBoundsException( - // luni.0A=Index: {0}, Size: {1} - Messages.getString("luni.0A", //$NON-NLS-1$ - Integer.valueOf(location), - Integer.valueOf(lastIndex - firstIndex))); } modCount++; @@ -179,7 +180,7 @@ public class ArrayList extends Abstra // luni.0A=Index: {0}, Size: {1} Messages.getString("luni.0A", //$NON-NLS-1$ Integer.valueOf(location), - Integer.valueOf(lastIndex - firstIndex))); + Integer.valueOf(size))); } if (this == collection) { collection = (ArrayList)clone(); @@ -190,7 +191,15 @@ public class ArrayList extends Abstra return false; } - if (0 < location && location < size) { + if (location == 0) { + growAtFront(growSize); + firstIndex -= growSize; + } else if (location == size) { + if (lastIndex > array.length - growSize) { + growAtEnd(growSize); + } + lastIndex += growSize; + } else { // must be case: (0 < location && location < size) if (array.length - size < growSize) { growForInsert(location, growSize); } else if ((location < size / 2 && firstIndex > 0) @@ -211,14 +220,6 @@ public class ArrayList extends Abstra - location); lastIndex += growSize; } - } else if (location == 0) { - growAtFront(growSize); - firstIndex -= growSize; - } else if (location == size) { - if (lastIndex > array.length - growSize) { - growAtEnd(growSize); - } - lastIndex += growSize; } System.arraycopy(dumparray, 0, this.array, location + firstIndex, @@ -329,14 +330,15 @@ public class ArrayList extends Abstra @Override public E get(int location) { - if (0 <= location && location < (lastIndex - firstIndex)) { - return array[firstIndex + location]; - } - throw new IndexOutOfBoundsException( + int size = lastIndex - firstIndex; + if (location < 0 || location >= size) { + throw new IndexOutOfBoundsException( // luni.0A=Index: {0}, Size: {1} Messages.getString("luni.0A", //$NON-NLS-1$ Integer.valueOf(location), - Integer.valueOf(lastIndex - firstIndex))); + Integer.valueOf(size))); + } + return array[firstIndex + location]; } private void growAtEnd(int required) { @@ -476,35 +478,34 @@ public class ArrayList extends Abstra public E remove(int location) { E result; int size = lastIndex - firstIndex; - if (0 <= location && location < size) { - if (location == size - 1) { - result = array[--lastIndex]; - array[lastIndex] = null; - } else if (location == 0) { - result = array[firstIndex]; - array[firstIndex++] = null; - } else { - int elementIndex = firstIndex + location; - result = array[elementIndex]; - if (location < size / 2) { - System.arraycopy(array, firstIndex, array, firstIndex + 1, - location); - array[firstIndex++] = null; - } else { - System.arraycopy(array, elementIndex + 1, array, - elementIndex, size - location - 1); - array[--lastIndex] = null; - } - } - if (firstIndex == lastIndex) { - firstIndex = lastIndex = 0; - } - } else { + if (location < 0 || location >= size) { throw new IndexOutOfBoundsException( // luni.0A=Index: {0}, Size: {1} Messages.getString("luni.0A", //$NON-NLS-1$ Integer.valueOf(location), - Integer.valueOf(lastIndex - firstIndex))); + Integer.valueOf(size))); + } + if (location == size - 1) { + result = array[--lastIndex]; + array[lastIndex] = null; + } else if (location == 0) { + result = array[firstIndex]; + array[firstIndex++] = null; + } else { + int elementIndex = firstIndex + location; + result = array[elementIndex]; + if (location < size / 2) { + System.arraycopy(array, firstIndex, array, firstIndex + 1, + location); + array[firstIndex++] = null; + } else { + System.arraycopy(array, elementIndex + 1, array, + elementIndex, size - location - 1); + array[--lastIndex] = null; + } + } + if (firstIndex == lastIndex) { + firstIndex = lastIndex = 0; } modCount++; @@ -534,31 +535,41 @@ public class ArrayList extends Abstra */ @Override protected void removeRange(int start, int end) { - if (start >= 0 && start <= end && end <= (lastIndex - firstIndex)) { - if (start == end) { - return; - } - int size = lastIndex - firstIndex; - if (end == size) { - Arrays.fill(array, firstIndex + start, lastIndex, null); - lastIndex = firstIndex + start; - } else if (start == 0) { - Arrays.fill(array, firstIndex, firstIndex + end, null); - firstIndex += end; - } else { - System.arraycopy(array, firstIndex + end, array, firstIndex - + start, size - end); - int newLast = lastIndex + start - end; - Arrays.fill(array, newLast, lastIndex, null); - lastIndex = newLast; - } - modCount++; - } else { + int size = lastIndex - firstIndex; + if (start < 0) { throw new IndexOutOfBoundsException( // luni.0B=Array index out of range: {0} Messages.getString("luni.0B", //$NON-NLS-1$ - lastIndex - firstIndex - end)); + Integer.valueOf(start))); + } else if (end > size) { + throw new IndexOutOfBoundsException( + // luni.0A=Index: {0}, Size: {1} + Messages.getString("luni.0A", //$NON-NLS-1$ + Integer.valueOf(end), Integer.valueOf(size))); + } else if (start > end) { + throw new IndexOutOfBoundsException( + // luni.35=Start index ({0}) is greater than end index ({1}) + Messages.getString("luni.35", //$NON-NLS-1$ + Integer.valueOf(start), Integer.valueOf(end))); + } + + if (start == end) { + return; + } + if (end == size) { + Arrays.fill(array, firstIndex + start, lastIndex, null); + lastIndex = firstIndex + start; + } else if (start == 0) { + Arrays.fill(array, firstIndex, firstIndex + end, null); + firstIndex += end; + } else { + System.arraycopy(array, firstIndex + end, array, firstIndex + + start, size - end); + int newLast = lastIndex + start - end; + Arrays.fill(array, newLast, lastIndex, null); + lastIndex = newLast; } + modCount++; } /** @@ -575,16 +586,17 @@ public class ArrayList extends Abstra */ @Override public E set(int location, E object) { - if (0 <= location && location < (lastIndex - firstIndex)) { - E result = array[firstIndex + location]; - array[firstIndex + location] = object; - return result; + int size = lastIndex - firstIndex; + if (location < 0 || location >= size) { + throw new IndexOutOfBoundsException( + // luni.0A=Index: {0}, Size: {1} + Messages.getString("luni.0A", //$NON-NLS-1$ + Integer.valueOf(location), + Integer.valueOf(size))); } - throw new IndexOutOfBoundsException( - // luni.0A=Index: {0}, Size: {1} - Messages.getString("luni.0A", //$NON-NLS-1$ - Integer.valueOf(location), - Integer.valueOf(lastIndex - firstIndex))); + E result = array[firstIndex + location]; + array[firstIndex + location] = object; + return result; } /**