1 |
|
/* |
2 |
|
* %W% %E% |
3 |
|
* |
4 |
< |
* Copyright 2005 Sun Microsystems, Inc. All rights reserved. |
4 |
> |
* Copyright 2006 Sun Microsystems, Inc. All rights reserved. |
5 |
|
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
6 |
|
*/ |
7 |
|
|
8 |
|
package java.util; |
9 |
– |
import java.util.*; // for javadoc (till 6280605 is fixed) |
9 |
|
|
10 |
|
/** |
11 |
|
* The <code>Vector</code> class implements a growable array of |
12 |
|
* objects. Like an array, it contains components that can be |
13 |
|
* accessed using an integer index. However, the size of a |
14 |
|
* <code>Vector</code> can grow or shrink as needed to accommodate |
15 |
< |
* adding and removing items after the <code>Vector</code> has been created.<p> |
15 |
> |
* adding and removing items after the <code>Vector</code> has been created. |
16 |
|
* |
17 |
< |
* Each vector tries to optimize storage management by maintaining a |
17 |
> |
* <p>Each vector tries to optimize storage management by maintaining a |
18 |
|
* <code>capacity</code> and a <code>capacityIncrement</code>. The |
19 |
|
* <code>capacity</code> is always at least as large as the vector |
20 |
|
* size; it is usually larger because as components are added to the |
21 |
|
* vector, the vector's storage increases in chunks the size of |
22 |
|
* <code>capacityIncrement</code>. An application can increase the |
23 |
|
* capacity of a vector before inserting a large number of |
24 |
< |
* components; this reduces the amount of incremental reallocation. <p> |
24 |
> |
* components; this reduces the amount of incremental reallocation. |
25 |
|
* |
26 |
< |
* As of the Java 2 platform v1.2, this class has been retrofitted to |
28 |
< |
* implement List, so that it becomes a part of Java's collection framework. |
29 |
< |
* Unlike the new collection implementations, Vector is synchronized.<p> |
30 |
< |
* |
31 |
< |
* The Iterators returned by Vector's iterator and listIterator |
26 |
> |
* <p>The Iterators returned by Vector's iterator and listIterator |
27 |
|
* methods are <em>fail-fast</em>: if the Vector is structurally modified |
28 |
|
* at any time after the Iterator is created, in any way except through the |
29 |
|
* Iterator's own remove or add methods, the Iterator will throw a |
39 |
|
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
40 |
|
* Therefore, it would be wrong to write a program that depended on this |
41 |
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
42 |
< |
* should be used only to detect bugs.</i><p> |
42 |
> |
* should be used only to detect bugs.</i> |
43 |
|
* |
44 |
< |
* This class is a member of the |
45 |
< |
* <a href="{@docRoot}/../guide/collections/index.html"> |
46 |
< |
* Java Collections Framework</a>. |
44 |
> |
* <p>As of the Java 2 platform v1.2, this class was retrofitted to |
45 |
> |
* implement the {@link List} interface, making it a member of the |
46 |
> |
* <a href="{@docRoot}/../guide/collections/index.html"> Java |
47 |
> |
* Collections Framework</a>. Unlike the new collection |
48 |
> |
* implementations, {@code Vector} is synchronized. |
49 |
|
* |
50 |
|
* @author Lee Boynton |
51 |
|
* @author Jonathan Payne |
144 |
|
* @since 1.2 |
145 |
|
*/ |
146 |
|
public Vector(Collection<? extends E> c) { |
147 |
< |
Object[] a = c.toArray(); |
148 |
< |
elementCount = a.length; |
149 |
< |
// If c.toArray incorrectly doesn't return Object[], copy it. |
150 |
< |
if (a.getClass() == Object[].class) |
151 |
< |
elementData = a; |
155 |
< |
else |
156 |
< |
elementData = Arrays.copyOf(a, a.length, Object[].class); |
147 |
> |
elementData = c.toArray(); |
148 |
> |
elementCount = elementData.length; |
149 |
> |
// c.toArray might (incorrectly) not return Object[] (see 6260652) |
150 |
> |
if (elementData.getClass() != Object[].class) |
151 |
> |
elementData = Arrays.copyOf(elementData, elementCount, Object[].class); |
152 |
|
} |
153 |
|
|
154 |
|
/** |
1049 |
|
throw new IndexOutOfBoundsException("Index: "+index); |
1050 |
|
return new VectorIterator(index); |
1051 |
|
} |
1052 |
< |
|
1052 |
> |
|
1053 |
> |
/** |
1054 |
> |
* {@inheritDoc} |
1055 |
> |
*/ |
1056 |
> |
public synchronized ListIterator<E> listIterator() { |
1057 |
> |
return new VectorIterator(0); |
1058 |
> |
} |
1059 |
> |
|
1060 |
|
/** |
1061 |
|
* Returns an iterator over the elements in this list in proper sequence. |
1062 |
|
* |
1067 |
|
} |
1068 |
|
|
1069 |
|
/** |
1070 |
< |
* A streamlined version of AbstractList.Itr. |
1070 |
> |
* A streamlined version of AbstractList.ListItr. |
1071 |
|
*/ |
1072 |
< |
final class VectorIterator implements ListIterator<E> { |
1073 |
< |
int cursor; // index of next element to return; |
1074 |
< |
int lastRet; // index of last element, or -1 if no such |
1075 |
< |
int expectedModCount; // to check for CME |
1072 |
> |
private final class VectorIterator implements ListIterator<E> { |
1073 |
> |
int cursor; // current position |
1074 |
> |
int lastRet; // index of last returned element |
1075 |
> |
int expectedModCount; // to check for CME |
1076 |
|
|
1077 |
|
VectorIterator(int index) { |
1078 |
|
cursor = index; |
1077 |
– |
lastRet = -1; |
1079 |
|
expectedModCount = modCount; |
1080 |
+ |
lastRet = -1; |
1081 |
|
} |
1082 |
|
|
1083 |
|
public boolean hasNext() { |
1084 |
< |
// Racy but within spec and backwards-compatible |
1085 |
< |
return cursor < elementCount; |
1084 |
> |
// Racy but within spec, since modifications are checked |
1085 |
> |
// within or after synchronization in next/previous |
1086 |
> |
return cursor != elementCount; |
1087 |
|
} |
1088 |
|
|
1089 |
|
public boolean hasPrevious() { |
1090 |
< |
return cursor > 0; |
1090 |
> |
return cursor != 0; |
1091 |
|
} |
1092 |
|
|
1093 |
|
public int nextIndex() { |
1099 |
|
} |
1100 |
|
|
1101 |
|
public E next() { |
1102 |
< |
synchronized(Vector.this) { |
1103 |
< |
if (expectedModCount == modCount) { |
1104 |
< |
int i = cursor; |
1105 |
< |
if (i < elementCount) { |
1106 |
< |
try { |
1107 |
< |
E e = (E)elementData[i]; |
1108 |
< |
lastRet = i; |
1109 |
< |
cursor = i + 1; |
1110 |
< |
return e; |
1111 |
< |
} catch (IndexOutOfBoundsException fallthrough) { |
1112 |
< |
} |
1110 |
< |
} |
1111 |
< |
} |
1112 |
< |
// Prefer reporting CME if applicable on failures |
1113 |
< |
if (expectedModCount == modCount) |
1114 |
< |
throw new NoSuchElementException(); |
1115 |
< |
throw new ConcurrentModificationException(); |
1102 |
> |
try { |
1103 |
> |
int i = cursor; |
1104 |
> |
E next = get(i); |
1105 |
> |
lastRet = i; |
1106 |
> |
cursor = i + 1; |
1107 |
> |
return next; |
1108 |
> |
} catch (IndexOutOfBoundsException ex) { |
1109 |
> |
throw new NoSuchElementException(); |
1110 |
> |
} finally { |
1111 |
> |
if (expectedModCount != modCount) |
1112 |
> |
throw new ConcurrentModificationException(); |
1113 |
|
} |
1114 |
|
} |
1115 |
|
|
1116 |
< |
public E previous() { |
1117 |
< |
synchronized(Vector.this) { |
1118 |
< |
if (expectedModCount == modCount) { |
1119 |
< |
int i = cursor - 1; |
1120 |
< |
if (i < elementCount) { |
1121 |
< |
try { |
1122 |
< |
E e = (E)elementData[i]; |
1123 |
< |
lastRet = i; |
1124 |
< |
cursor = i; |
1125 |
< |
return e; |
1126 |
< |
} catch (IndexOutOfBoundsException fallthrough) { |
1127 |
< |
} |
1131 |
< |
} |
1132 |
< |
} |
1133 |
< |
if (expectedModCount == modCount) |
1134 |
< |
throw new NoSuchElementException(); |
1135 |
< |
throw new ConcurrentModificationException(); |
1116 |
> |
public E previous() { |
1117 |
> |
try { |
1118 |
> |
int i = cursor - 1; |
1119 |
> |
E prev = get(i); |
1120 |
> |
lastRet = i; |
1121 |
> |
cursor = i; |
1122 |
> |
return prev; |
1123 |
> |
} catch (IndexOutOfBoundsException ex) { |
1124 |
> |
throw new NoSuchElementException(); |
1125 |
> |
} finally { |
1126 |
> |
if (expectedModCount != modCount) |
1127 |
> |
throw new ConcurrentModificationException(); |
1128 |
|
} |
1129 |
|
} |
1130 |
|
|
1131 |
|
public void remove() { |
1132 |
< |
if (lastRet < 0) |
1132 |
> |
if (lastRet == -1) |
1133 |
|
throw new IllegalStateException(); |
1134 |
< |
synchronized(Vector.this) { |
1135 |
< |
if (modCount != expectedModCount) |
1136 |
< |
throw new ConcurrentModificationException(); |
1137 |
< |
Vector.this.remove(lastRet); |
1138 |
< |
if (lastRet < cursor) |
1139 |
< |
cursor--; |
1140 |
< |
lastRet = -1; |
1141 |
< |
expectedModCount = modCount; |
1142 |
< |
} |
1134 |
> |
if (expectedModCount != modCount) |
1135 |
> |
throw new ConcurrentModificationException(); |
1136 |
> |
try { |
1137 |
> |
Vector.this.remove(lastRet); |
1138 |
> |
if (lastRet < cursor) |
1139 |
> |
cursor--; |
1140 |
> |
lastRet = -1; |
1141 |
> |
expectedModCount = modCount; |
1142 |
> |
} catch (IndexOutOfBoundsException ex) { |
1143 |
> |
throw new ConcurrentModificationException(); |
1144 |
> |
} |
1145 |
|
} |
1146 |
|
|
1147 |
|
public void set(E e) { |
1148 |
< |
if (lastRet < 0) |
1148 |
> |
if (lastRet == -1) |
1149 |
|
throw new IllegalStateException(); |
1150 |
< |
synchronized(Vector.this) { |
1151 |
< |
if (modCount != expectedModCount) |
1152 |
< |
throw new ConcurrentModificationException(); |
1153 |
< |
Vector.this.set(lastRet, e); |
1154 |
< |
expectedModCount = modCount; |
1155 |
< |
} |
1150 |
> |
if (expectedModCount != modCount) |
1151 |
> |
throw new ConcurrentModificationException(); |
1152 |
> |
try { |
1153 |
> |
Vector.this.set(lastRet, e); |
1154 |
> |
expectedModCount = modCount; |
1155 |
> |
} catch (IndexOutOfBoundsException ex) { |
1156 |
> |
throw new ConcurrentModificationException(); |
1157 |
> |
} |
1158 |
|
} |
1159 |
|
|
1160 |
|
public void add(E e) { |
1161 |
< |
synchronized(Vector.this) { |
1162 |
< |
if (modCount != expectedModCount) |
1163 |
< |
throw new ConcurrentModificationException(); |
1164 |
< |
Vector.this.add(cursor++, e); |
1165 |
< |
lastRet = -1; |
1166 |
< |
expectedModCount = modCount; |
1167 |
< |
} |
1161 |
> |
if (expectedModCount != modCount) |
1162 |
> |
throw new ConcurrentModificationException(); |
1163 |
> |
try { |
1164 |
> |
int i = cursor; |
1165 |
> |
Vector.this.add(i, e); |
1166 |
> |
cursor = i + 1; |
1167 |
> |
lastRet = -1; |
1168 |
> |
expectedModCount = modCount; |
1169 |
> |
} catch (IndexOutOfBoundsException ex) { |
1170 |
> |
throw new ConcurrentModificationException(); |
1171 |
> |
} |
1172 |
|
} |
1173 |
|
} |
1174 |
– |
|
1174 |
|
} |