--- jsr166/src/main/java/util/Vector.java 2005/11/25 13:27:29 1.1 +++ jsr166/src/main/java/util/Vector.java 2005/12/05 02:56:59 1.7 @@ -1,7 +1,7 @@ /* * %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. */ @@ -147,13 +147,11 @@ public class Vector * @since 1.2 */ public Vector(Collection 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 +1052,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 +1070,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 +1102,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(); + } } } - }