--- jsr166/src/main/java/util/Vector.java 2005/11/25 13:27:29 1.1
+++ jsr166/src/main/java/util/Vector.java 2006/03/19 17:59:39 1.9
@@ -1,34 +1,29 @@
/*
* %W% %E%
*
- * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
+ * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.util;
-import java.util.*; // for javadoc (till 6280605 is fixed)
/**
* The Vector
class implements a growable array of
* objects. Like an array, it contains components that can be
* accessed using an integer index. However, the size of a
* Vector
can grow or shrink as needed to accommodate
- * adding and removing items after the Vector
has been created.
+ * adding and removing items after the Vector
has been created.
*
- * Each vector tries to optimize storage management by maintaining a
+ *
Each vector tries to optimize storage management by maintaining a
* capacity
and a capacityIncrement
. The
* capacity
is always at least as large as the vector
* size; it is usually larger because as components are added to the
* vector, the vector's storage increases in chunks the size of
* capacityIncrement
. An application can increase the
* capacity of a vector before inserting a large number of
- * components; this reduces the amount of incremental reallocation.
+ * components; this reduces the amount of incremental reallocation.
*
- * As of the Java 2 platform v1.2, this class has been retrofitted to
- * implement List, so that it becomes a part of Java's collection framework.
- * Unlike the new collection implementations, Vector is synchronized.
- *
- * The Iterators returned by Vector's iterator and listIterator
+ *
The Iterators returned by Vector's iterator and listIterator
* methods are fail-fast: if the Vector is structurally modified
* at any time after the Iterator is created, in any way except through the
* Iterator's own remove or add methods, the Iterator will throw a
@@ -44,11 +39,13 @@ import java.util.*; // for javadoc (till
* throw ConcurrentModificationException on a best-effort basis.
* Therefore, it would be wrong to write a program that depended on this
* exception for its correctness: the fail-fast behavior of iterators
- * should be used only to detect bugs.
+ * should be used only to detect bugs.
*
- * This class is a member of the
- *
- * Java Collections Framework.
+ *
As of the Java 2 platform v1.2, this class was retrofitted to
+ * implement the {@link List} interface, making it a member of the
+ * Java
+ * Collections Framework. Unlike the new collection
+ * implementations, {@code Vector} is synchronized.
*
* @author Lee Boynton
* @author Jonathan Payne
@@ -147,13 +144,11 @@ public class Vector
* @since 1.2
*/
public Vector(Collection extends E> c) {
- Object[] a = c.toArray();
- elementCount = a.length;
- // If c.toArray incorrectly doesn't return Object[], copy it.
- if (a.getClass() == Object[].class)
- elementData = a;
- else
- elementData = Arrays.copyOf(a, a.length, Object[].class);
+ elementData = c.toArray();
+ elementCount = elementData.length;
+ // c.toArray might (incorrectly) not return Object[] (see 6260652)
+ if (elementData.getClass() != Object[].class)
+ elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
}
/**
@@ -1054,7 +1049,14 @@ public class Vector
throw new IndexOutOfBoundsException("Index: "+index);
return new VectorIterator(index);
}
-
+
+ /**
+ * {@inheritDoc}
+ */
+ public synchronized ListIterator listIterator() {
+ return new VectorIterator(0);
+ }
+
/**
* Returns an iterator over the elements in this list in proper sequence.
*
@@ -1065,26 +1067,27 @@ public class Vector
}
/**
- * A streamlined version of AbstractList.Itr.
+ * A streamlined version of AbstractList.ListItr.
*/
- final class VectorIterator 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
+ private final class VectorIterator implements ListIterator {
+ int cursor; // current position
+ int lastRet; // index of last returned element
+ int expectedModCount; // to check for CME
VectorIterator(int index) {
cursor = index;
- lastRet = -1;
expectedModCount = modCount;
+ lastRet = -1;
}
public boolean hasNext() {
- // Racy but within spec and backwards-compatible
- return cursor < elementCount;
+ // Racy but within spec, since modifications are checked
+ // within or after synchronization in next/previous
+ return cursor != elementCount;
}
public boolean hasPrevious() {
- return cursor > 0;
+ return cursor != 0;
}
public int nextIndex() {
@@ -1096,80 +1099,76 @@ public class Vector
}
public E next() {
- synchronized(Vector.this) {
- if (expectedModCount == modCount) {
- int i = cursor;
- if (i < elementCount) {
- try {
- E e = (E)elementData[i];
- lastRet = i;
- cursor = i + 1;
- return e;
- } catch (IndexOutOfBoundsException fallthrough) {
- }
- }
- }
- // Prefer reporting CME if applicable on failures
- if (expectedModCount == modCount)
- throw new NoSuchElementException();
- throw new ConcurrentModificationException();
+ 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() {
- synchronized(Vector.this) {
- if (expectedModCount == modCount) {
- int i = cursor - 1;
- if (i < elementCount) {
- try {
- E e = (E)elementData[i];
- lastRet = i;
- cursor = i;
- return e;
- } catch (IndexOutOfBoundsException fallthrough) {
- }
- }
- }
- if (expectedModCount == modCount)
- throw new NoSuchElementException();
- 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)
+ if (lastRet == -1)
throw new IllegalStateException();
- synchronized(Vector.this) {
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- Vector.this.remove(lastRet);
- if (lastRet < cursor)
- cursor--;
- lastRet = -1;
- expectedModCount = modCount;
- }
+ if (expectedModCount != modCount)
+ throw new ConcurrentModificationException();
+ try {
+ Vector.this.remove(lastRet);
+ if (lastRet < cursor)
+ cursor--;
+ lastRet = -1;
+ expectedModCount = modCount;
+ } catch (IndexOutOfBoundsException ex) {
+ throw new ConcurrentModificationException();
+ }
}
public void set(E e) {
- if (lastRet < 0)
+ if (lastRet == -1)
throw new IllegalStateException();
- synchronized(Vector.this) {
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- Vector.this.set(lastRet, e);
- expectedModCount = modCount;
- }
+ if (expectedModCount != modCount)
+ throw new ConcurrentModificationException();
+ try {
+ Vector.this.set(lastRet, e);
+ expectedModCount = modCount;
+ } catch (IndexOutOfBoundsException ex) {
+ throw new ConcurrentModificationException();
+ }
}
public void add(E e) {
- synchronized(Vector.this) {
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- Vector.this.add(cursor++, e);
- lastRet = -1;
- expectedModCount = modCount;
- }
+ if (expectedModCount != modCount)
+ throw new ConcurrentModificationException();
+ try {
+ int i = cursor;
+ Vector.this.add(i, e);
+ cursor = i + 1;
+ lastRet = -1;
+ expectedModCount = modCount;
+ } catch (IndexOutOfBoundsException ex) {
+ throw new ConcurrentModificationException();
+ }
}
}
-
}