--- jsr166/src/main/java/util/ArrayList.java 2006/03/19 17:40:40 1.18
+++ jsr166/src/main/java/util/ArrayList.java 2007/05/20 07:54:01 1.24
@@ -1,8 +1,26 @@
/*
- * %W% %E%
+ * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
- * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
- * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
*/
package java.util;
@@ -66,7 +84,7 @@ package java.util;
* should be used only to detect bugs.
*
* This class is a member of the
- *
+ *
* Java Collections Framework.
*
* @author Josh Bloch
@@ -170,15 +188,15 @@ public class ArrayList extends Abstra
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
int oldCapacity = elementData.length;
- // Double size if small; else grow by 50%
- int newCapacity = ((oldCapacity < 64) ?
- ((oldCapacity + 1) * 2) :
- ((oldCapacity / 2) * 3));
- if (newCapacity < 0) // overflow
- newCapacity = Integer.MAX_VALUE;
- if (newCapacity < minCapacity)
- newCapacity = minCapacity;
- elementData = Arrays.copyOf(elementData, newCapacity);
+ // Double size if small; else grow by 50%
+ int newCapacity = ((oldCapacity < 64) ?
+ ((oldCapacity + 1) * 2) :
+ ((oldCapacity / 2) * 3));
+ if (newCapacity < 0) // overflow
+ newCapacity = Integer.MAX_VALUE;
+ if (newCapacity < minCapacity)
+ newCapacity = minCapacity;
+ elementData = Arrays.copyOf(elementData, newCapacity);
}
/**
@@ -325,10 +343,10 @@ public class ArrayList extends Abstra
// Positional Access Operations
/**
- * Returns error message string for IndexOutOfBoundsExceptions
+ * Throws an appropriate exception for indexing errors.
*/
- private String ioobe(int index) {
- return "Index: " + index + ", Size: " + size;
+ private static void indexOutOfBounds(int i, int s) {
+ throw new IndexOutOfBoundsException("Index: " + i + ", Size: " + s);
}
/**
@@ -340,8 +358,8 @@ public class ArrayList extends Abstra
*/
public E get(int index) {
if (index >= size)
- throw new IndexOutOfBoundsException(ioobe(index));
- return (E)elementData[index];
+ indexOutOfBounds(index, size);
+ return (E) elementData[index];
}
/**
@@ -355,8 +373,7 @@ public class ArrayList extends Abstra
*/
public E set(int index, E element) {
if (index >= size)
- throw new IndexOutOfBoundsException(ioobe(index));
-
+ indexOutOfBounds(index, size);
E oldValue = (E) elementData[index];
elementData[index] = element;
return oldValue;
@@ -374,8 +391,8 @@ public class ArrayList extends Abstra
if (s >= elementData.length)
growArray(s + 1);
elementData[s] = e;
- size = s + 1;
- return true;
+ size = s + 1;
+ return true;
}
/**
@@ -390,12 +407,12 @@ public class ArrayList extends Abstra
public void add(int index, E element) {
int s = size;
if (index > s || index < 0)
- throw new IndexOutOfBoundsException(ioobe(index));
+ indexOutOfBounds(index, s);
modCount++;
if (s >= elementData.length)
growArray(s + 1);
System.arraycopy(elementData, index,
- elementData, index + 1, s - index);
+ elementData, index + 1, s - index);
elementData[index] = element;
size = s + 1;
}
@@ -412,15 +429,15 @@ public class ArrayList extends Abstra
public E remove(int index) {
int s = size - 1;
if (index > s)
- throw new IndexOutOfBoundsException(ioobe(index));
+ indexOutOfBounds(index, size);
modCount++;
- E oldValue = (E)elementData[index];
+ E oldValue = (E) elementData[index];
int numMoved = s - index;
if (numMoved > 0)
System.arraycopy(elementData, index + 1,
- elementData, index, numMoved);
+ elementData, index, numMoved);
elementData[s] = null;
- size = s;
+ size = s;
return oldValue;
}
@@ -520,7 +537,7 @@ public class ArrayList extends Abstra
*/
public boolean addAll(int index, Collection extends E> c) {
if (index > size || index < 0)
- throw new IndexOutOfBoundsException(ioobe(index));
+ indexOutOfBounds(index, size);
Object[] a = c.toArray();
int numNew = a.length;
@@ -605,142 +622,4 @@ public class ArrayList extends Abstra
for (int i=0; iList.listIterator(int).
- *
- * The list-iterator is fail-fast: if the list is structurally
- * modified at any time after the Iterator is created, in any way except
- * through the list-iterator's own remove or add
- * methods, the list-iterator will throw a
- * ConcurrentModificationException. Thus, in the face of
- * concurrent modification, the iterator fails quickly and cleanly, rather
- * than risking arbitrary, non-deterministic behavior at an undetermined
- * time in the future.
- *
- * @param index index of the first element to be returned from the
- * list-iterator (by a call to next)
- * @return a ListIterator of the elements in this list (in proper
- * sequence), starting at the specified position in the list
- * @throws IndexOutOfBoundsException {@inheritDoc}
- * @see List#listIterator(int)
- */
- public ListIterator listIterator(int index) {
- if (index < 0 || index > size)
- throw new IndexOutOfBoundsException(ioobe(index));
- return new ArrayListIterator(index);
- }
-
- /**
- * {@inheritDoc}
- */
- public ListIterator listIterator() {
- return new ArrayListIterator(0);
- }
-
- /**
- * Returns an iterator over the elements in this list in proper sequence.
- *
- * @return an iterator over the elements in this list in proper sequence
- */
- public Iterator iterator() {
- return new ArrayListIterator(0);
- }
-
- /**
- * A streamlined version of AbstractList.ListItr
- */
- final class ArrayListIterator implements ListIterator {
- int cursor; // index of next element to return;
- int lastRet; // index of last element, or -1 if no such
- int expectedModCount; // to check for CME
-
- ArrayListIterator(int index) {
- cursor = index;
- lastRet = -1;
- expectedModCount = modCount;
- }
-
- public boolean hasNext() {
- return cursor != size;
- }
-
- public boolean hasPrevious() {
- return cursor != 0;
- }
-
- public int nextIndex() {
- return cursor;
- }
-
- public int previousIndex() {
- return cursor - 1;
- }
-
- public E next() {
- try {
- int i = cursor;
- E next = get(i);
- lastRet = i;
- cursor = i + 1;
- return next;
- } catch (IndexOutOfBoundsException ex) {
- throw new NoSuchElementException();
- } finally {
- if (expectedModCount != modCount)
- throw new ConcurrentModificationException();
- }
- }
-
- public E previous() {
- try {
- int i = cursor - 1;
- E prev = get(i);
- lastRet = i;
- cursor = i;
- return prev;
- } catch (IndexOutOfBoundsException ex) {
- throw new NoSuchElementException();
- } finally {
- if (expectedModCount != modCount)
- throw new ConcurrentModificationException();
- }
- }
-
- public void remove() {
- if (lastRet < 0)
- throw new IllegalStateException();
- if (expectedModCount != modCount)
- throw new ConcurrentModificationException();
- ArrayList.this.remove(lastRet);
- if (lastRet < cursor)
- cursor--;
- lastRet = -1;
- expectedModCount = modCount;
- }
-
- public void set(E e) {
- if (lastRet < 0)
- throw new IllegalStateException();
- if (expectedModCount != modCount)
- throw new ConcurrentModificationException();
- ArrayList.this.set(lastRet, e);
- expectedModCount = modCount;
- }
-
- public void add(E e) {
- if (expectedModCount != modCount)
- throw new ConcurrentModificationException();
- try {
- ArrayList.this.add(cursor++, e);
- lastRet = -1;
- expectedModCount = modCount;
- } catch (IndexOutOfBoundsException ex) {
- throw new ConcurrentModificationException();
- }
- }
- }
}