1 |
dl |
1.1 |
/* |
2 |
jsr166 |
1.48 |
* Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved. |
3 |
jsr166 |
1.21 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
dl |
1.1 |
* |
5 |
jsr166 |
1.21 |
* This code is free software; you can redistribute it and/or modify it |
6 |
|
|
* under the terms of the GNU General Public License version 2 only, as |
7 |
jsr166 |
1.30 |
* published by the Free Software Foundation. Oracle designates this |
8 |
jsr166 |
1.21 |
* particular file as subject to the "Classpath" exception as provided |
9 |
jsr166 |
1.30 |
* by Oracle in the LICENSE file that accompanied this code. |
10 |
jsr166 |
1.21 |
* |
11 |
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT |
12 |
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 |
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 |
|
|
* version 2 for more details (a copy is included in the LICENSE file that |
15 |
|
|
* accompanied this code). |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License version |
18 |
|
|
* 2 along with this work; if not, write to the Free Software Foundation, |
19 |
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
|
|
* |
21 |
jsr166 |
1.27 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
|
|
* or visit www.oracle.com if you need additional information or have any |
23 |
|
|
* questions. |
24 |
dl |
1.1 |
*/ |
25 |
|
|
|
26 |
|
|
package java.util; |
27 |
|
|
|
28 |
jsr166 |
1.49 |
import java.io.IOException; |
29 |
|
|
import java.io.ObjectInputStream; |
30 |
|
|
import java.io.StreamCorruptedException; |
31 |
jsr166 |
1.30 |
import java.util.function.Consumer; |
32 |
|
|
import java.util.function.Predicate; |
33 |
|
|
import java.util.function.UnaryOperator; |
34 |
|
|
|
35 |
dl |
1.1 |
/** |
36 |
jsr166 |
1.14 |
* The {@code Vector} class implements a growable array of |
37 |
dl |
1.1 |
* objects. Like an array, it contains components that can be |
38 |
|
|
* accessed using an integer index. However, the size of a |
39 |
jsr166 |
1.14 |
* {@code Vector} can grow or shrink as needed to accommodate |
40 |
|
|
* adding and removing items after the {@code Vector} has been created. |
41 |
dl |
1.1 |
* |
42 |
jsr166 |
1.9 |
* <p>Each vector tries to optimize storage management by maintaining a |
43 |
jsr166 |
1.14 |
* {@code capacity} and a {@code capacityIncrement}. The |
44 |
|
|
* {@code capacity} is always at least as large as the vector |
45 |
dl |
1.1 |
* size; it is usually larger because as components are added to the |
46 |
|
|
* vector, the vector's storage increases in chunks the size of |
47 |
jsr166 |
1.14 |
* {@code capacityIncrement}. An application can increase the |
48 |
dl |
1.1 |
* capacity of a vector before inserting a large number of |
49 |
jsr166 |
1.9 |
* components; this reduces the amount of incremental reallocation. |
50 |
dl |
1.1 |
* |
51 |
jsr166 |
1.30 |
* <p id="fail-fast"> |
52 |
jsr166 |
1.22 |
* The iterators returned by this class's {@link #iterator() iterator} and |
53 |
|
|
* {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>: |
54 |
|
|
* if the vector is structurally modified at any time after the iterator is |
55 |
|
|
* created, in any way except through the iterator's own |
56 |
|
|
* {@link ListIterator#remove() remove} or |
57 |
|
|
* {@link ListIterator#add(Object) add} methods, the iterator will throw a |
58 |
|
|
* {@link ConcurrentModificationException}. Thus, in the face of |
59 |
|
|
* concurrent modification, the iterator fails quickly and cleanly, rather |
60 |
|
|
* than risking arbitrary, non-deterministic behavior at an undetermined |
61 |
|
|
* time in the future. The {@link Enumeration Enumerations} returned by |
62 |
jsr166 |
1.30 |
* the {@link #elements() elements} method are <em>not</em> fail-fast; if the |
63 |
|
|
* Vector is structurally modified at any time after the enumeration is |
64 |
|
|
* created then the results of enumerating are undefined. |
65 |
dl |
1.1 |
* |
66 |
|
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
67 |
|
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
68 |
|
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
69 |
jsr166 |
1.14 |
* throw {@code ConcurrentModificationException} on a best-effort basis. |
70 |
dl |
1.1 |
* Therefore, it would be wrong to write a program that depended on this |
71 |
|
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
72 |
jsr166 |
1.9 |
* should be used only to detect bugs.</i> |
73 |
dl |
1.1 |
* |
74 |
jsr166 |
1.9 |
* <p>As of the Java 2 platform v1.2, this class was retrofitted to |
75 |
|
|
* implement the {@link List} interface, making it a member of the |
76 |
jsr166 |
1.46 |
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework"> |
77 |
jsr166 |
1.25 |
* Java Collections Framework</a>. Unlike the new collection |
78 |
|
|
* implementations, {@code Vector} is synchronized. If a thread-safe |
79 |
|
|
* implementation is not needed, it is recommended to use {@link |
80 |
|
|
* ArrayList} in place of {@code Vector}. |
81 |
dl |
1.1 |
* |
82 |
jsr166 |
1.30 |
* @param <E> Type of component elements |
83 |
|
|
* |
84 |
dl |
1.1 |
* @author Lee Boynton |
85 |
|
|
* @author Jonathan Payne |
86 |
|
|
* @see Collection |
87 |
|
|
* @see LinkedList |
88 |
jsr166 |
1.30 |
* @since 1.0 |
89 |
dl |
1.1 |
*/ |
90 |
|
|
public class Vector<E> |
91 |
|
|
extends AbstractList<E> |
92 |
|
|
implements List<E>, RandomAccess, Cloneable, java.io.Serializable |
93 |
|
|
{ |
94 |
|
|
/** |
95 |
|
|
* The array buffer into which the components of the vector are |
96 |
|
|
* stored. The capacity of the vector is the length of this array buffer, |
97 |
jsr166 |
1.15 |
* and is at least large enough to contain all the vector's elements. |
98 |
dl |
1.1 |
* |
99 |
jsr166 |
1.15 |
* <p>Any array elements following the last element in the Vector are null. |
100 |
dl |
1.1 |
* |
101 |
|
|
* @serial |
102 |
|
|
*/ |
103 |
|
|
protected Object[] elementData; |
104 |
|
|
|
105 |
|
|
/** |
106 |
jsr166 |
1.14 |
* The number of valid components in this {@code Vector} object. |
107 |
|
|
* Components {@code elementData[0]} through |
108 |
|
|
* {@code elementData[elementCount-1]} are the actual items. |
109 |
dl |
1.1 |
* |
110 |
|
|
* @serial |
111 |
|
|
*/ |
112 |
|
|
protected int elementCount; |
113 |
|
|
|
114 |
|
|
/** |
115 |
|
|
* The amount by which the capacity of the vector is automatically |
116 |
|
|
* incremented when its size becomes greater than its capacity. If |
117 |
|
|
* the capacity increment is less than or equal to zero, the capacity |
118 |
|
|
* of the vector is doubled each time it needs to grow. |
119 |
|
|
* |
120 |
|
|
* @serial |
121 |
|
|
*/ |
122 |
|
|
protected int capacityIncrement; |
123 |
|
|
|
124 |
|
|
/** use serialVersionUID from JDK 1.0.2 for interoperability */ |
125 |
|
|
private static final long serialVersionUID = -2767605614048989439L; |
126 |
|
|
|
127 |
|
|
/** |
128 |
|
|
* Constructs an empty vector with the specified initial capacity and |
129 |
|
|
* capacity increment. |
130 |
|
|
* |
131 |
|
|
* @param initialCapacity the initial capacity of the vector |
132 |
|
|
* @param capacityIncrement the amount by which the capacity is |
133 |
|
|
* increased when the vector overflows |
134 |
jsr166 |
1.15 |
* @throws IllegalArgumentException if the specified initial capacity |
135 |
|
|
* is negative |
136 |
dl |
1.1 |
*/ |
137 |
|
|
public Vector(int initialCapacity, int capacityIncrement) { |
138 |
jsr166 |
1.23 |
super(); |
139 |
dl |
1.1 |
if (initialCapacity < 0) |
140 |
|
|
throw new IllegalArgumentException("Illegal Capacity: "+ |
141 |
|
|
initialCapacity); |
142 |
jsr166 |
1.23 |
this.elementData = new Object[initialCapacity]; |
143 |
|
|
this.capacityIncrement = capacityIncrement; |
144 |
dl |
1.1 |
} |
145 |
|
|
|
146 |
|
|
/** |
147 |
|
|
* Constructs an empty vector with the specified initial capacity and |
148 |
|
|
* with its capacity increment equal to zero. |
149 |
|
|
* |
150 |
|
|
* @param initialCapacity the initial capacity of the vector |
151 |
jsr166 |
1.15 |
* @throws IllegalArgumentException if the specified initial capacity |
152 |
|
|
* is negative |
153 |
dl |
1.1 |
*/ |
154 |
|
|
public Vector(int initialCapacity) { |
155 |
jsr166 |
1.23 |
this(initialCapacity, 0); |
156 |
dl |
1.1 |
} |
157 |
|
|
|
158 |
|
|
/** |
159 |
|
|
* Constructs an empty vector so that its internal data array |
160 |
jsr166 |
1.14 |
* has size {@code 10} and its standard capacity increment is |
161 |
dl |
1.1 |
* zero. |
162 |
|
|
*/ |
163 |
|
|
public Vector() { |
164 |
jsr166 |
1.23 |
this(10); |
165 |
dl |
1.1 |
} |
166 |
|
|
|
167 |
|
|
/** |
168 |
|
|
* Constructs a vector containing the elements of the specified |
169 |
|
|
* collection, in the order they are returned by the collection's |
170 |
|
|
* iterator. |
171 |
|
|
* |
172 |
|
|
* @param c the collection whose elements are to be placed into this |
173 |
|
|
* vector |
174 |
|
|
* @throws NullPointerException if the specified collection is null |
175 |
|
|
* @since 1.2 |
176 |
|
|
*/ |
177 |
|
|
public Vector(Collection<? extends E> c) { |
178 |
jsr166 |
1.23 |
elementData = c.toArray(); |
179 |
|
|
elementCount = elementData.length; |
180 |
jsr166 |
1.30 |
// defend against c.toArray (incorrectly) not returning Object[] |
181 |
|
|
// (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652) |
182 |
jsr166 |
1.23 |
if (elementData.getClass() != Object[].class) |
183 |
|
|
elementData = Arrays.copyOf(elementData, elementCount, Object[].class); |
184 |
dl |
1.1 |
} |
185 |
|
|
|
186 |
|
|
/** |
187 |
|
|
* Copies the components of this vector into the specified array. |
188 |
jsr166 |
1.14 |
* The item at index {@code k} in this vector is copied into |
189 |
|
|
* component {@code k} of {@code anArray}. |
190 |
dl |
1.1 |
* |
191 |
|
|
* @param anArray the array into which the components get copied |
192 |
|
|
* @throws NullPointerException if the given array is null |
193 |
|
|
* @throws IndexOutOfBoundsException if the specified array is not |
194 |
|
|
* large enough to hold all the components of this vector |
195 |
|
|
* @throws ArrayStoreException if a component of this vector is not of |
196 |
|
|
* a runtime type that can be stored in the specified array |
197 |
|
|
* @see #toArray(Object[]) |
198 |
|
|
*/ |
199 |
|
|
public synchronized void copyInto(Object[] anArray) { |
200 |
jsr166 |
1.23 |
System.arraycopy(elementData, 0, anArray, 0, elementCount); |
201 |
dl |
1.1 |
} |
202 |
|
|
|
203 |
|
|
/** |
204 |
|
|
* Trims the capacity of this vector to be the vector's current |
205 |
|
|
* size. If the capacity of this vector is larger than its current |
206 |
|
|
* size, then the capacity is changed to equal the size by replacing |
207 |
jsr166 |
1.14 |
* its internal data array, kept in the field {@code elementData}, |
208 |
dl |
1.1 |
* with a smaller one. An application can use this operation to |
209 |
|
|
* minimize the storage of a vector. |
210 |
|
|
*/ |
211 |
|
|
public synchronized void trimToSize() { |
212 |
jsr166 |
1.23 |
modCount++; |
213 |
|
|
int oldCapacity = elementData.length; |
214 |
|
|
if (elementCount < oldCapacity) { |
215 |
dl |
1.1 |
elementData = Arrays.copyOf(elementData, elementCount); |
216 |
jsr166 |
1.23 |
} |
217 |
dl |
1.1 |
} |
218 |
|
|
|
219 |
|
|
/** |
220 |
|
|
* Increases the capacity of this vector, if necessary, to ensure |
221 |
|
|
* that it can hold at least the number of components specified by |
222 |
|
|
* the minimum capacity argument. |
223 |
|
|
* |
224 |
|
|
* <p>If the current capacity of this vector is less than |
225 |
jsr166 |
1.14 |
* {@code minCapacity}, then its capacity is increased by replacing its |
226 |
|
|
* internal data array, kept in the field {@code elementData}, with a |
227 |
dl |
1.1 |
* larger one. The size of the new data array will be the old size plus |
228 |
jsr166 |
1.14 |
* {@code capacityIncrement}, unless the value of |
229 |
|
|
* {@code capacityIncrement} is less than or equal to zero, in which case |
230 |
dl |
1.1 |
* the new capacity will be twice the old capacity; but if this new size |
231 |
jsr166 |
1.14 |
* is still smaller than {@code minCapacity}, then the new capacity will |
232 |
|
|
* be {@code minCapacity}. |
233 |
dl |
1.1 |
* |
234 |
|
|
* @param minCapacity the desired minimum capacity |
235 |
|
|
*/ |
236 |
|
|
public synchronized void ensureCapacity(int minCapacity) { |
237 |
jsr166 |
1.30 |
if (minCapacity > 0) { |
238 |
|
|
modCount++; |
239 |
|
|
if (minCapacity > elementData.length) |
240 |
|
|
grow(minCapacity); |
241 |
|
|
} |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
/** |
245 |
|
|
* The maximum size of array to allocate (unless necessary). |
246 |
|
|
* Some VMs reserve some header words in an array. |
247 |
|
|
* Attempts to allocate larger arrays may result in |
248 |
|
|
* OutOfMemoryError: Requested array size exceeds VM limit |
249 |
|
|
*/ |
250 |
|
|
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
251 |
|
|
|
252 |
|
|
/** |
253 |
|
|
* Increases the capacity to ensure that it can hold at least the |
254 |
|
|
* number of elements specified by the minimum capacity argument. |
255 |
|
|
* |
256 |
|
|
* @param minCapacity the desired minimum capacity |
257 |
|
|
* @throws OutOfMemoryError if minCapacity is less than zero |
258 |
|
|
*/ |
259 |
|
|
private Object[] grow(int minCapacity) { |
260 |
|
|
return elementData = Arrays.copyOf(elementData, |
261 |
|
|
newCapacity(minCapacity)); |
262 |
|
|
} |
263 |
|
|
|
264 |
|
|
private Object[] grow() { |
265 |
|
|
return grow(elementCount + 1); |
266 |
dl |
1.1 |
} |
267 |
|
|
|
268 |
|
|
/** |
269 |
jsr166 |
1.30 |
* Returns a capacity at least as large as the given minimum capacity. |
270 |
|
|
* Will not return a capacity greater than MAX_ARRAY_SIZE unless |
271 |
|
|
* the given minimum capacity is greater than MAX_ARRAY_SIZE. |
272 |
dl |
1.1 |
* |
273 |
jsr166 |
1.30 |
* @param minCapacity the desired minimum capacity |
274 |
|
|
* @throws OutOfMemoryError if minCapacity is less than zero |
275 |
dl |
1.1 |
*/ |
276 |
jsr166 |
1.30 |
private int newCapacity(int minCapacity) { |
277 |
|
|
// overflow-conscious code |
278 |
jsr166 |
1.23 |
int oldCapacity = elementData.length; |
279 |
jsr166 |
1.30 |
int newCapacity = oldCapacity + ((capacityIncrement > 0) ? |
280 |
|
|
capacityIncrement : oldCapacity); |
281 |
|
|
if (newCapacity - minCapacity <= 0) { |
282 |
|
|
if (minCapacity < 0) // overflow |
283 |
|
|
throw new OutOfMemoryError(); |
284 |
|
|
return minCapacity; |
285 |
|
|
} |
286 |
|
|
return (newCapacity - MAX_ARRAY_SIZE <= 0) |
287 |
|
|
? newCapacity |
288 |
|
|
: hugeCapacity(minCapacity); |
289 |
|
|
} |
290 |
|
|
|
291 |
|
|
private static int hugeCapacity(int minCapacity) { |
292 |
|
|
if (minCapacity < 0) // overflow |
293 |
|
|
throw new OutOfMemoryError(); |
294 |
|
|
return (minCapacity > MAX_ARRAY_SIZE) ? |
295 |
|
|
Integer.MAX_VALUE : |
296 |
|
|
MAX_ARRAY_SIZE; |
297 |
dl |
1.1 |
} |
298 |
|
|
|
299 |
|
|
/** |
300 |
|
|
* Sets the size of this vector. If the new size is greater than the |
301 |
jsr166 |
1.14 |
* current size, new {@code null} items are added to the end of |
302 |
dl |
1.1 |
* the vector. If the new size is less than the current size, all |
303 |
jsr166 |
1.14 |
* components at index {@code newSize} and greater are discarded. |
304 |
dl |
1.1 |
* |
305 |
jsr166 |
1.16 |
* @param newSize the new size of this vector |
306 |
|
|
* @throws ArrayIndexOutOfBoundsException if the new size is negative |
307 |
dl |
1.1 |
*/ |
308 |
|
|
public synchronized void setSize(int newSize) { |
309 |
jsr166 |
1.23 |
modCount++; |
310 |
jsr166 |
1.30 |
if (newSize > elementData.length) |
311 |
|
|
grow(newSize); |
312 |
jsr166 |
1.40 |
final Object[] es = elementData; |
313 |
jsr166 |
1.45 |
for (int to = elementCount, i = newSize; i < to; i++) |
314 |
jsr166 |
1.40 |
es[i] = null; |
315 |
jsr166 |
1.45 |
elementCount = newSize; |
316 |
dl |
1.1 |
} |
317 |
|
|
|
318 |
|
|
/** |
319 |
|
|
* Returns the current capacity of this vector. |
320 |
|
|
* |
321 |
|
|
* @return the current capacity (the length of its internal |
322 |
jsr166 |
1.14 |
* data array, kept in the field {@code elementData} |
323 |
dl |
1.1 |
* of this vector) |
324 |
|
|
*/ |
325 |
|
|
public synchronized int capacity() { |
326 |
jsr166 |
1.23 |
return elementData.length; |
327 |
dl |
1.1 |
} |
328 |
|
|
|
329 |
|
|
/** |
330 |
|
|
* Returns the number of components in this vector. |
331 |
|
|
* |
332 |
|
|
* @return the number of components in this vector |
333 |
|
|
*/ |
334 |
|
|
public synchronized int size() { |
335 |
jsr166 |
1.23 |
return elementCount; |
336 |
dl |
1.1 |
} |
337 |
|
|
|
338 |
|
|
/** |
339 |
|
|
* Tests if this vector has no components. |
340 |
|
|
* |
341 |
jsr166 |
1.14 |
* @return {@code true} if and only if this vector has |
342 |
dl |
1.1 |
* no components, that is, its size is zero; |
343 |
jsr166 |
1.14 |
* {@code false} otherwise. |
344 |
dl |
1.1 |
*/ |
345 |
|
|
public synchronized boolean isEmpty() { |
346 |
jsr166 |
1.23 |
return elementCount == 0; |
347 |
dl |
1.1 |
} |
348 |
|
|
|
349 |
|
|
/** |
350 |
|
|
* Returns an enumeration of the components of this vector. The |
351 |
jsr166 |
1.14 |
* returned {@code Enumeration} object will generate all items in |
352 |
|
|
* this vector. The first item generated is the item at index {@code 0}, |
353 |
jsr166 |
1.30 |
* then the item at index {@code 1}, and so on. If the vector is |
354 |
|
|
* structurally modified while enumerating over the elements then the |
355 |
|
|
* results of enumerating are undefined. |
356 |
dl |
1.1 |
* |
357 |
|
|
* @return an enumeration of the components of this vector |
358 |
|
|
* @see Iterator |
359 |
|
|
*/ |
360 |
|
|
public Enumeration<E> elements() { |
361 |
jsr166 |
1.23 |
return new Enumeration<E>() { |
362 |
|
|
int count = 0; |
363 |
dl |
1.1 |
|
364 |
jsr166 |
1.23 |
public boolean hasMoreElements() { |
365 |
|
|
return count < elementCount; |
366 |
|
|
} |
367 |
|
|
|
368 |
|
|
public E nextElement() { |
369 |
|
|
synchronized (Vector.this) { |
370 |
|
|
if (count < elementCount) { |
371 |
|
|
return elementData(count++); |
372 |
|
|
} |
373 |
|
|
} |
374 |
|
|
throw new NoSuchElementException("Vector Enumeration"); |
375 |
|
|
} |
376 |
|
|
}; |
377 |
dl |
1.1 |
} |
378 |
|
|
|
379 |
|
|
/** |
380 |
jsr166 |
1.14 |
* Returns {@code true} if this vector contains the specified element. |
381 |
|
|
* More formally, returns {@code true} if and only if this vector |
382 |
|
|
* contains at least one element {@code e} such that |
383 |
jsr166 |
1.30 |
* {@code Objects.equals(o, e)}. |
384 |
dl |
1.1 |
* |
385 |
|
|
* @param o element whose presence in this vector is to be tested |
386 |
jsr166 |
1.14 |
* @return {@code true} if this vector contains the specified element |
387 |
dl |
1.1 |
*/ |
388 |
|
|
public boolean contains(Object o) { |
389 |
jsr166 |
1.23 |
return indexOf(o, 0) >= 0; |
390 |
dl |
1.1 |
} |
391 |
|
|
|
392 |
|
|
/** |
393 |
|
|
* Returns the index of the first occurrence of the specified element |
394 |
|
|
* in this vector, or -1 if this vector does not contain the element. |
395 |
jsr166 |
1.14 |
* More formally, returns the lowest index {@code i} such that |
396 |
jsr166 |
1.30 |
* {@code Objects.equals(o, get(i))}, |
397 |
dl |
1.1 |
* or -1 if there is no such index. |
398 |
|
|
* |
399 |
|
|
* @param o element to search for |
400 |
|
|
* @return the index of the first occurrence of the specified element in |
401 |
|
|
* this vector, or -1 if this vector does not contain the element |
402 |
|
|
*/ |
403 |
|
|
public int indexOf(Object o) { |
404 |
jsr166 |
1.23 |
return indexOf(o, 0); |
405 |
dl |
1.1 |
} |
406 |
|
|
|
407 |
|
|
/** |
408 |
|
|
* Returns the index of the first occurrence of the specified element in |
409 |
jsr166 |
1.14 |
* this vector, searching forwards from {@code index}, or returns -1 if |
410 |
dl |
1.1 |
* the element is not found. |
411 |
jsr166 |
1.14 |
* More formally, returns the lowest index {@code i} such that |
412 |
jsr166 |
1.30 |
* {@code (i >= index && Objects.equals(o, get(i)))}, |
413 |
dl |
1.1 |
* or -1 if there is no such index. |
414 |
|
|
* |
415 |
|
|
* @param o element to search for |
416 |
|
|
* @param index index to start searching from |
417 |
|
|
* @return the index of the first occurrence of the element in |
418 |
jsr166 |
1.14 |
* this vector at position {@code index} or later in the vector; |
419 |
|
|
* {@code -1} if the element is not found. |
420 |
dl |
1.1 |
* @throws IndexOutOfBoundsException if the specified index is negative |
421 |
|
|
* @see Object#equals(Object) |
422 |
|
|
*/ |
423 |
|
|
public synchronized int indexOf(Object o, int index) { |
424 |
jsr166 |
1.23 |
if (o == null) { |
425 |
|
|
for (int i = index ; i < elementCount ; i++) |
426 |
|
|
if (elementData[i]==null) |
427 |
|
|
return i; |
428 |
|
|
} else { |
429 |
|
|
for (int i = index ; i < elementCount ; i++) |
430 |
|
|
if (o.equals(elementData[i])) |
431 |
|
|
return i; |
432 |
|
|
} |
433 |
|
|
return -1; |
434 |
dl |
1.1 |
} |
435 |
|
|
|
436 |
|
|
/** |
437 |
|
|
* Returns the index of the last occurrence of the specified element |
438 |
|
|
* in this vector, or -1 if this vector does not contain the element. |
439 |
jsr166 |
1.14 |
* More formally, returns the highest index {@code i} such that |
440 |
jsr166 |
1.30 |
* {@code Objects.equals(o, get(i))}, |
441 |
dl |
1.1 |
* or -1 if there is no such index. |
442 |
|
|
* |
443 |
|
|
* @param o element to search for |
444 |
|
|
* @return the index of the last occurrence of the specified element in |
445 |
|
|
* this vector, or -1 if this vector does not contain the element |
446 |
|
|
*/ |
447 |
|
|
public synchronized int lastIndexOf(Object o) { |
448 |
jsr166 |
1.23 |
return lastIndexOf(o, elementCount-1); |
449 |
dl |
1.1 |
} |
450 |
|
|
|
451 |
|
|
/** |
452 |
|
|
* Returns the index of the last occurrence of the specified element in |
453 |
jsr166 |
1.14 |
* this vector, searching backwards from {@code index}, or returns -1 if |
454 |
dl |
1.1 |
* the element is not found. |
455 |
jsr166 |
1.14 |
* More formally, returns the highest index {@code i} such that |
456 |
jsr166 |
1.30 |
* {@code (i <= index && Objects.equals(o, get(i)))}, |
457 |
dl |
1.1 |
* or -1 if there is no such index. |
458 |
|
|
* |
459 |
|
|
* @param o element to search for |
460 |
|
|
* @param index index to start searching backwards from |
461 |
|
|
* @return the index of the last occurrence of the element at position |
462 |
jsr166 |
1.14 |
* less than or equal to {@code index} in this vector; |
463 |
dl |
1.1 |
* -1 if the element is not found. |
464 |
|
|
* @throws IndexOutOfBoundsException if the specified index is greater |
465 |
|
|
* than or equal to the current size of this vector |
466 |
|
|
*/ |
467 |
|
|
public synchronized int lastIndexOf(Object o, int index) { |
468 |
|
|
if (index >= elementCount) |
469 |
|
|
throw new IndexOutOfBoundsException(index + " >= "+ elementCount); |
470 |
|
|
|
471 |
jsr166 |
1.23 |
if (o == null) { |
472 |
|
|
for (int i = index; i >= 0; i--) |
473 |
|
|
if (elementData[i]==null) |
474 |
|
|
return i; |
475 |
|
|
} else { |
476 |
|
|
for (int i = index; i >= 0; i--) |
477 |
|
|
if (o.equals(elementData[i])) |
478 |
|
|
return i; |
479 |
|
|
} |
480 |
|
|
return -1; |
481 |
dl |
1.1 |
} |
482 |
|
|
|
483 |
|
|
/** |
484 |
jsr166 |
1.15 |
* Returns the component at the specified index. |
485 |
dl |
1.1 |
* |
486 |
jsr166 |
1.15 |
* <p>This method is identical in functionality to the {@link #get(int)} |
487 |
|
|
* method (which is part of the {@link List} interface). |
488 |
dl |
1.1 |
* |
489 |
|
|
* @param index an index into this vector |
490 |
|
|
* @return the component at the specified index |
491 |
jsr166 |
1.15 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
492 |
jsr166 |
1.23 |
* ({@code index < 0 || index >= size()}) |
493 |
dl |
1.1 |
*/ |
494 |
|
|
public synchronized E elementAt(int index) { |
495 |
jsr166 |
1.23 |
if (index >= elementCount) { |
496 |
|
|
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
497 |
|
|
} |
498 |
dl |
1.1 |
|
499 |
jsr166 |
1.22 |
return elementData(index); |
500 |
dl |
1.1 |
} |
501 |
|
|
|
502 |
|
|
/** |
503 |
jsr166 |
1.14 |
* Returns the first component (the item at index {@code 0}) of |
504 |
dl |
1.1 |
* this vector. |
505 |
|
|
* |
506 |
|
|
* @return the first component of this vector |
507 |
jsr166 |
1.15 |
* @throws NoSuchElementException if this vector has no components |
508 |
dl |
1.1 |
*/ |
509 |
|
|
public synchronized E firstElement() { |
510 |
jsr166 |
1.23 |
if (elementCount == 0) { |
511 |
|
|
throw new NoSuchElementException(); |
512 |
|
|
} |
513 |
|
|
return elementData(0); |
514 |
dl |
1.1 |
} |
515 |
|
|
|
516 |
|
|
/** |
517 |
|
|
* Returns the last component of the vector. |
518 |
|
|
* |
519 |
|
|
* @return the last component of the vector, i.e., the component at index |
520 |
jsr166 |
1.31 |
* {@code size() - 1} |
521 |
jsr166 |
1.15 |
* @throws NoSuchElementException if this vector is empty |
522 |
dl |
1.1 |
*/ |
523 |
|
|
public synchronized E lastElement() { |
524 |
jsr166 |
1.23 |
if (elementCount == 0) { |
525 |
|
|
throw new NoSuchElementException(); |
526 |
|
|
} |
527 |
|
|
return elementData(elementCount - 1); |
528 |
dl |
1.1 |
} |
529 |
|
|
|
530 |
|
|
/** |
531 |
jsr166 |
1.14 |
* Sets the component at the specified {@code index} of this |
532 |
dl |
1.1 |
* vector to be the specified object. The previous component at that |
533 |
jsr166 |
1.16 |
* position is discarded. |
534 |
dl |
1.1 |
* |
535 |
jsr166 |
1.16 |
* <p>The index must be a value greater than or equal to {@code 0} |
536 |
|
|
* and less than the current size of the vector. |
537 |
dl |
1.1 |
* |
538 |
jsr166 |
1.17 |
* <p>This method is identical in functionality to the |
539 |
|
|
* {@link #set(int, Object) set(int, E)} |
540 |
|
|
* method (which is part of the {@link List} interface). Note that the |
541 |
|
|
* {@code set} method reverses the order of the parameters, to more closely |
542 |
|
|
* match array usage. Note also that the {@code set} method returns the |
543 |
|
|
* old value that was stored at the specified position. |
544 |
dl |
1.1 |
* |
545 |
|
|
* @param obj what the component is to be set to |
546 |
|
|
* @param index the specified index |
547 |
jsr166 |
1.17 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
548 |
jsr166 |
1.23 |
* ({@code index < 0 || index >= size()}) |
549 |
dl |
1.1 |
*/ |
550 |
|
|
public synchronized void setElementAt(E obj, int index) { |
551 |
jsr166 |
1.23 |
if (index >= elementCount) { |
552 |
|
|
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
553 |
|
|
elementCount); |
554 |
|
|
} |
555 |
|
|
elementData[index] = obj; |
556 |
dl |
1.1 |
} |
557 |
|
|
|
558 |
|
|
/** |
559 |
|
|
* Deletes the component at the specified index. Each component in |
560 |
|
|
* this vector with an index greater or equal to the specified |
561 |
jsr166 |
1.14 |
* {@code index} is shifted downward to have an index one |
562 |
dl |
1.1 |
* smaller than the value it had previously. The size of this vector |
563 |
jsr166 |
1.15 |
* is decreased by {@code 1}. |
564 |
dl |
1.1 |
* |
565 |
jsr166 |
1.15 |
* <p>The index must be a value greater than or equal to {@code 0} |
566 |
|
|
* and less than the current size of the vector. |
567 |
dl |
1.1 |
* |
568 |
jsr166 |
1.17 |
* <p>This method is identical in functionality to the {@link #remove(int)} |
569 |
|
|
* method (which is part of the {@link List} interface). Note that the |
570 |
|
|
* {@code remove} method returns the old value that was stored at the |
571 |
|
|
* specified position. |
572 |
dl |
1.1 |
* |
573 |
|
|
* @param index the index of the object to remove |
574 |
jsr166 |
1.17 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
575 |
jsr166 |
1.23 |
* ({@code index < 0 || index >= size()}) |
576 |
dl |
1.1 |
*/ |
577 |
|
|
public synchronized void removeElementAt(int index) { |
578 |
jsr166 |
1.23 |
if (index >= elementCount) { |
579 |
|
|
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
580 |
|
|
elementCount); |
581 |
|
|
} |
582 |
|
|
else if (index < 0) { |
583 |
|
|
throw new ArrayIndexOutOfBoundsException(index); |
584 |
|
|
} |
585 |
|
|
int j = elementCount - index - 1; |
586 |
|
|
if (j > 0) { |
587 |
|
|
System.arraycopy(elementData, index + 1, elementData, index, j); |
588 |
|
|
} |
589 |
jsr166 |
1.30 |
modCount++; |
590 |
jsr166 |
1.23 |
elementCount--; |
591 |
|
|
elementData[elementCount] = null; /* to let gc do its work */ |
592 |
jsr166 |
1.35 |
// checkInvariants(); |
593 |
dl |
1.1 |
} |
594 |
|
|
|
595 |
|
|
/** |
596 |
|
|
* Inserts the specified object as a component in this vector at the |
597 |
jsr166 |
1.14 |
* specified {@code index}. Each component in this vector with |
598 |
|
|
* an index greater or equal to the specified {@code index} is |
599 |
dl |
1.1 |
* shifted upward to have an index one greater than the value it had |
600 |
jsr166 |
1.15 |
* previously. |
601 |
dl |
1.1 |
* |
602 |
jsr166 |
1.15 |
* <p>The index must be a value greater than or equal to {@code 0} |
603 |
dl |
1.1 |
* and less than or equal to the current size of the vector. (If the |
604 |
|
|
* index is equal to the current size of the vector, the new element |
605 |
jsr166 |
1.15 |
* is appended to the Vector.) |
606 |
dl |
1.1 |
* |
607 |
jsr166 |
1.17 |
* <p>This method is identical in functionality to the |
608 |
|
|
* {@link #add(int, Object) add(int, E)} |
609 |
|
|
* method (which is part of the {@link List} interface). Note that the |
610 |
|
|
* {@code add} method reverses the order of the parameters, to more closely |
611 |
|
|
* match array usage. |
612 |
dl |
1.1 |
* |
613 |
|
|
* @param obj the component to insert |
614 |
|
|
* @param index where to insert the new component |
615 |
jsr166 |
1.17 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
616 |
jsr166 |
1.23 |
* ({@code index < 0 || index > size()}) |
617 |
dl |
1.1 |
*/ |
618 |
|
|
public synchronized void insertElementAt(E obj, int index) { |
619 |
jsr166 |
1.23 |
if (index > elementCount) { |
620 |
|
|
throw new ArrayIndexOutOfBoundsException(index |
621 |
|
|
+ " > " + elementCount); |
622 |
|
|
} |
623 |
jsr166 |
1.30 |
modCount++; |
624 |
|
|
final int s = elementCount; |
625 |
|
|
Object[] elementData = this.elementData; |
626 |
|
|
if (s == elementData.length) |
627 |
|
|
elementData = grow(); |
628 |
|
|
System.arraycopy(elementData, index, |
629 |
|
|
elementData, index + 1, |
630 |
|
|
s - index); |
631 |
jsr166 |
1.23 |
elementData[index] = obj; |
632 |
jsr166 |
1.30 |
elementCount = s + 1; |
633 |
dl |
1.1 |
} |
634 |
|
|
|
635 |
|
|
/** |
636 |
|
|
* Adds the specified component to the end of this vector, |
637 |
|
|
* increasing its size by one. The capacity of this vector is |
638 |
jsr166 |
1.16 |
* increased if its size becomes greater than its capacity. |
639 |
dl |
1.1 |
* |
640 |
jsr166 |
1.17 |
* <p>This method is identical in functionality to the |
641 |
jsr166 |
1.18 |
* {@link #add(Object) add(E)} |
642 |
|
|
* method (which is part of the {@link List} interface). |
643 |
dl |
1.1 |
* |
644 |
|
|
* @param obj the component to be added |
645 |
|
|
*/ |
646 |
|
|
public synchronized void addElement(E obj) { |
647 |
jsr166 |
1.23 |
modCount++; |
648 |
jsr166 |
1.30 |
add(obj, elementData, elementCount); |
649 |
dl |
1.1 |
} |
650 |
|
|
|
651 |
|
|
/** |
652 |
|
|
* Removes the first (lowest-indexed) occurrence of the argument |
653 |
|
|
* from this vector. If the object is found in this vector, each |
654 |
|
|
* component in the vector with an index greater or equal to the |
655 |
|
|
* object's index is shifted downward to have an index one smaller |
656 |
jsr166 |
1.16 |
* than the value it had previously. |
657 |
dl |
1.1 |
* |
658 |
jsr166 |
1.18 |
* <p>This method is identical in functionality to the |
659 |
|
|
* {@link #remove(Object)} method (which is part of the |
660 |
|
|
* {@link List} interface). |
661 |
dl |
1.1 |
* |
662 |
|
|
* @param obj the component to be removed |
663 |
jsr166 |
1.14 |
* @return {@code true} if the argument was a component of this |
664 |
|
|
* vector; {@code false} otherwise. |
665 |
dl |
1.1 |
*/ |
666 |
|
|
public synchronized boolean removeElement(Object obj) { |
667 |
jsr166 |
1.23 |
modCount++; |
668 |
|
|
int i = indexOf(obj); |
669 |
|
|
if (i >= 0) { |
670 |
|
|
removeElementAt(i); |
671 |
|
|
return true; |
672 |
|
|
} |
673 |
|
|
return false; |
674 |
dl |
1.1 |
} |
675 |
|
|
|
676 |
|
|
/** |
677 |
jsr166 |
1.17 |
* Removes all components from this vector and sets its size to zero. |
678 |
dl |
1.1 |
* |
679 |
jsr166 |
1.17 |
* <p>This method is identical in functionality to the {@link #clear} |
680 |
|
|
* method (which is part of the {@link List} interface). |
681 |
dl |
1.1 |
*/ |
682 |
|
|
public synchronized void removeAllElements() { |
683 |
jsr166 |
1.40 |
final Object[] es = elementData; |
684 |
|
|
for (int to = elementCount, i = elementCount = 0; i < to; i++) |
685 |
|
|
es[i] = null; |
686 |
jsr166 |
1.30 |
modCount++; |
687 |
dl |
1.1 |
} |
688 |
|
|
|
689 |
|
|
/** |
690 |
|
|
* Returns a clone of this vector. The copy will contain a |
691 |
|
|
* reference to a clone of the internal data array, not a reference |
692 |
jsr166 |
1.14 |
* to the original internal data array of this {@code Vector} object. |
693 |
dl |
1.1 |
* |
694 |
|
|
* @return a clone of this vector |
695 |
|
|
*/ |
696 |
|
|
public synchronized Object clone() { |
697 |
jsr166 |
1.23 |
try { |
698 |
|
|
@SuppressWarnings("unchecked") |
699 |
jsr166 |
1.34 |
Vector<E> v = (Vector<E>) super.clone(); |
700 |
jsr166 |
1.23 |
v.elementData = Arrays.copyOf(elementData, elementCount); |
701 |
|
|
v.modCount = 0; |
702 |
|
|
return v; |
703 |
|
|
} catch (CloneNotSupportedException e) { |
704 |
|
|
// this shouldn't happen, since we are Cloneable |
705 |
jsr166 |
1.30 |
throw new InternalError(e); |
706 |
jsr166 |
1.23 |
} |
707 |
dl |
1.1 |
} |
708 |
|
|
|
709 |
|
|
/** |
710 |
|
|
* Returns an array containing all of the elements in this Vector |
711 |
|
|
* in the correct order. |
712 |
|
|
* |
713 |
|
|
* @since 1.2 |
714 |
|
|
*/ |
715 |
|
|
public synchronized Object[] toArray() { |
716 |
|
|
return Arrays.copyOf(elementData, elementCount); |
717 |
|
|
} |
718 |
|
|
|
719 |
|
|
/** |
720 |
|
|
* Returns an array containing all of the elements in this Vector in the |
721 |
|
|
* correct order; the runtime type of the returned array is that of the |
722 |
|
|
* specified array. If the Vector fits in the specified array, it is |
723 |
|
|
* returned therein. Otherwise, a new array is allocated with the runtime |
724 |
jsr166 |
1.16 |
* type of the specified array and the size of this Vector. |
725 |
dl |
1.1 |
* |
726 |
jsr166 |
1.16 |
* <p>If the Vector fits in the specified array with room to spare |
727 |
dl |
1.1 |
* (i.e., the array has more elements than the Vector), |
728 |
|
|
* the element in the array immediately following the end of the |
729 |
|
|
* Vector is set to null. (This is useful in determining the length |
730 |
|
|
* of the Vector <em>only</em> if the caller knows that the Vector |
731 |
|
|
* does not contain any null elements.) |
732 |
|
|
* |
733 |
jsr166 |
1.30 |
* @param <T> type of array elements. The same type as {@code <E>} or a |
734 |
|
|
* supertype of {@code <E>}. |
735 |
dl |
1.1 |
* @param a the array into which the elements of the Vector are to |
736 |
jsr166 |
1.23 |
* be stored, if it is big enough; otherwise, a new array of the |
737 |
|
|
* same runtime type is allocated for this purpose. |
738 |
dl |
1.1 |
* @return an array containing the elements of the Vector |
739 |
jsr166 |
1.30 |
* @throws ArrayStoreException if the runtime type of a, {@code <T>}, is not |
740 |
|
|
* a supertype of the runtime type, {@code <E>}, of every element in this |
741 |
|
|
* Vector |
742 |
dl |
1.1 |
* @throws NullPointerException if the given array is null |
743 |
|
|
* @since 1.2 |
744 |
|
|
*/ |
745 |
jsr166 |
1.22 |
@SuppressWarnings("unchecked") |
746 |
dl |
1.1 |
public synchronized <T> T[] toArray(T[] a) { |
747 |
|
|
if (a.length < elementCount) |
748 |
|
|
return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass()); |
749 |
|
|
|
750 |
jsr166 |
1.23 |
System.arraycopy(elementData, 0, a, 0, elementCount); |
751 |
dl |
1.1 |
|
752 |
|
|
if (a.length > elementCount) |
753 |
|
|
a[elementCount] = null; |
754 |
|
|
|
755 |
|
|
return a; |
756 |
|
|
} |
757 |
|
|
|
758 |
|
|
// Positional Access Operations |
759 |
|
|
|
760 |
jsr166 |
1.22 |
@SuppressWarnings("unchecked") |
761 |
|
|
E elementData(int index) { |
762 |
jsr166 |
1.23 |
return (E) elementData[index]; |
763 |
jsr166 |
1.22 |
} |
764 |
|
|
|
765 |
jsr166 |
1.33 |
@SuppressWarnings("unchecked") |
766 |
|
|
static <E> E elementAt(Object[] es, int index) { |
767 |
|
|
return (E) es[index]; |
768 |
|
|
} |
769 |
|
|
|
770 |
dl |
1.1 |
/** |
771 |
|
|
* Returns the element at the specified position in this Vector. |
772 |
|
|
* |
773 |
|
|
* @param index index of the element to return |
774 |
|
|
* @return object at the specified index |
775 |
jsr166 |
1.17 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
776 |
|
|
* ({@code index < 0 || index >= size()}) |
777 |
dl |
1.1 |
* @since 1.2 |
778 |
|
|
*/ |
779 |
|
|
public synchronized E get(int index) { |
780 |
jsr166 |
1.23 |
if (index >= elementCount) |
781 |
|
|
throw new ArrayIndexOutOfBoundsException(index); |
782 |
dl |
1.1 |
|
783 |
jsr166 |
1.23 |
return elementData(index); |
784 |
dl |
1.1 |
} |
785 |
|
|
|
786 |
|
|
/** |
787 |
|
|
* Replaces the element at the specified position in this Vector with the |
788 |
|
|
* specified element. |
789 |
|
|
* |
790 |
|
|
* @param index index of the element to replace |
791 |
|
|
* @param element element to be stored at the specified position |
792 |
|
|
* @return the element previously at the specified position |
793 |
jsr166 |
1.17 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
794 |
jsr166 |
1.23 |
* ({@code index < 0 || index >= size()}) |
795 |
dl |
1.1 |
* @since 1.2 |
796 |
|
|
*/ |
797 |
|
|
public synchronized E set(int index, E element) { |
798 |
jsr166 |
1.23 |
if (index >= elementCount) |
799 |
|
|
throw new ArrayIndexOutOfBoundsException(index); |
800 |
dl |
1.1 |
|
801 |
jsr166 |
1.23 |
E oldValue = elementData(index); |
802 |
|
|
elementData[index] = element; |
803 |
|
|
return oldValue; |
804 |
dl |
1.1 |
} |
805 |
|
|
|
806 |
|
|
/** |
807 |
jsr166 |
1.30 |
* This helper method split out from add(E) to keep method |
808 |
|
|
* bytecode size under 35 (the -XX:MaxInlineSize default value), |
809 |
|
|
* which helps when add(E) is called in a C1-compiled loop. |
810 |
|
|
*/ |
811 |
|
|
private void add(E e, Object[] elementData, int s) { |
812 |
|
|
if (s == elementData.length) |
813 |
|
|
elementData = grow(); |
814 |
|
|
elementData[s] = e; |
815 |
|
|
elementCount = s + 1; |
816 |
jsr166 |
1.35 |
// checkInvariants(); |
817 |
jsr166 |
1.30 |
} |
818 |
|
|
|
819 |
|
|
/** |
820 |
dl |
1.1 |
* Appends the specified element to the end of this Vector. |
821 |
|
|
* |
822 |
|
|
* @param e element to be appended to this Vector |
823 |
jsr166 |
1.14 |
* @return {@code true} (as specified by {@link Collection#add}) |
824 |
dl |
1.1 |
* @since 1.2 |
825 |
|
|
*/ |
826 |
|
|
public synchronized boolean add(E e) { |
827 |
jsr166 |
1.23 |
modCount++; |
828 |
jsr166 |
1.30 |
add(e, elementData, elementCount); |
829 |
dl |
1.1 |
return true; |
830 |
|
|
} |
831 |
|
|
|
832 |
|
|
/** |
833 |
|
|
* Removes the first occurrence of the specified element in this Vector |
834 |
|
|
* If the Vector does not contain the element, it is unchanged. More |
835 |
|
|
* formally, removes the element with the lowest index i such that |
836 |
jsr166 |
1.30 |
* {@code Objects.equals(o, get(i))} (if such |
837 |
dl |
1.1 |
* an element exists). |
838 |
|
|
* |
839 |
|
|
* @param o element to be removed from this Vector, if present |
840 |
|
|
* @return true if the Vector contained the specified element |
841 |
|
|
* @since 1.2 |
842 |
|
|
*/ |
843 |
|
|
public boolean remove(Object o) { |
844 |
|
|
return removeElement(o); |
845 |
|
|
} |
846 |
|
|
|
847 |
|
|
/** |
848 |
|
|
* Inserts the specified element at the specified position in this Vector. |
849 |
|
|
* Shifts the element currently at that position (if any) and any |
850 |
|
|
* subsequent elements to the right (adds one to their indices). |
851 |
|
|
* |
852 |
|
|
* @param index index at which the specified element is to be inserted |
853 |
|
|
* @param element element to be inserted |
854 |
jsr166 |
1.17 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
855 |
|
|
* ({@code index < 0 || index > size()}) |
856 |
dl |
1.1 |
* @since 1.2 |
857 |
|
|
*/ |
858 |
|
|
public void add(int index, E element) { |
859 |
|
|
insertElementAt(element, index); |
860 |
|
|
} |
861 |
|
|
|
862 |
|
|
/** |
863 |
|
|
* Removes the element at the specified position in this Vector. |
864 |
|
|
* Shifts any subsequent elements to the left (subtracts one from their |
865 |
|
|
* indices). Returns the element that was removed from the Vector. |
866 |
|
|
* |
867 |
jsr166 |
1.31 |
* @param index the index of the element to be removed |
868 |
|
|
* @return element that was removed |
869 |
jsr166 |
1.18 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
870 |
|
|
* ({@code index < 0 || index >= size()}) |
871 |
dl |
1.1 |
* @since 1.2 |
872 |
|
|
*/ |
873 |
|
|
public synchronized E remove(int index) { |
874 |
jsr166 |
1.23 |
modCount++; |
875 |
|
|
if (index >= elementCount) |
876 |
|
|
throw new ArrayIndexOutOfBoundsException(index); |
877 |
|
|
E oldValue = elementData(index); |
878 |
|
|
|
879 |
|
|
int numMoved = elementCount - index - 1; |
880 |
|
|
if (numMoved > 0) |
881 |
|
|
System.arraycopy(elementData, index+1, elementData, index, |
882 |
|
|
numMoved); |
883 |
|
|
elementData[--elementCount] = null; // Let gc do its work |
884 |
dl |
1.1 |
|
885 |
jsr166 |
1.35 |
// checkInvariants(); |
886 |
jsr166 |
1.23 |
return oldValue; |
887 |
dl |
1.1 |
} |
888 |
|
|
|
889 |
|
|
/** |
890 |
|
|
* Removes all of the elements from this Vector. The Vector will |
891 |
|
|
* be empty after this call returns (unless it throws an exception). |
892 |
|
|
* |
893 |
|
|
* @since 1.2 |
894 |
|
|
*/ |
895 |
|
|
public void clear() { |
896 |
|
|
removeAllElements(); |
897 |
|
|
} |
898 |
|
|
|
899 |
|
|
// Bulk Operations |
900 |
|
|
|
901 |
|
|
/** |
902 |
|
|
* Returns true if this Vector contains all of the elements in the |
903 |
|
|
* specified Collection. |
904 |
|
|
* |
905 |
|
|
* @param c a collection whose elements will be tested for containment |
906 |
|
|
* in this Vector |
907 |
|
|
* @return true if this Vector contains all of the elements in the |
908 |
jsr166 |
1.23 |
* specified collection |
909 |
dl |
1.1 |
* @throws NullPointerException if the specified collection is null |
910 |
|
|
*/ |
911 |
|
|
public synchronized boolean containsAll(Collection<?> c) { |
912 |
|
|
return super.containsAll(c); |
913 |
|
|
} |
914 |
|
|
|
915 |
|
|
/** |
916 |
|
|
* Appends all of the elements in the specified Collection to the end of |
917 |
|
|
* this Vector, in the order that they are returned by the specified |
918 |
|
|
* Collection's Iterator. The behavior of this operation is undefined if |
919 |
|
|
* the specified Collection is modified while the operation is in progress. |
920 |
|
|
* (This implies that the behavior of this call is undefined if the |
921 |
|
|
* specified Collection is this Vector, and this Vector is nonempty.) |
922 |
|
|
* |
923 |
|
|
* @param c elements to be inserted into this Vector |
924 |
jsr166 |
1.14 |
* @return {@code true} if this Vector changed as a result of the call |
925 |
dl |
1.1 |
* @throws NullPointerException if the specified collection is null |
926 |
|
|
* @since 1.2 |
927 |
|
|
*/ |
928 |
jsr166 |
1.30 |
public boolean addAll(Collection<? extends E> c) { |
929 |
|
|
Object[] a = c.toArray(); |
930 |
jsr166 |
1.23 |
modCount++; |
931 |
dl |
1.1 |
int numNew = a.length; |
932 |
jsr166 |
1.30 |
if (numNew == 0) |
933 |
|
|
return false; |
934 |
|
|
synchronized (this) { |
935 |
|
|
Object[] elementData = this.elementData; |
936 |
|
|
final int s = elementCount; |
937 |
|
|
if (numNew > elementData.length - s) |
938 |
|
|
elementData = grow(s + numNew); |
939 |
|
|
System.arraycopy(a, 0, elementData, s, numNew); |
940 |
|
|
elementCount = s + numNew; |
941 |
jsr166 |
1.35 |
// checkInvariants(); |
942 |
jsr166 |
1.30 |
return true; |
943 |
|
|
} |
944 |
dl |
1.1 |
} |
945 |
|
|
|
946 |
|
|
/** |
947 |
|
|
* Removes from this Vector all of its elements that are contained in the |
948 |
|
|
* specified Collection. |
949 |
|
|
* |
950 |
|
|
* @param c a collection of elements to be removed from the Vector |
951 |
|
|
* @return true if this Vector changed as a result of the call |
952 |
|
|
* @throws ClassCastException if the types of one or more elements |
953 |
|
|
* in this vector are incompatible with the specified |
954 |
jsr166 |
1.30 |
* collection |
955 |
|
|
* (<a href="Collection.html#optional-restrictions">optional</a>) |
956 |
dl |
1.1 |
* @throws NullPointerException if this vector contains one or more null |
957 |
|
|
* elements and the specified collection does not support null |
958 |
jsr166 |
1.30 |
* elements |
959 |
|
|
* (<a href="Collection.html#optional-restrictions">optional</a>), |
960 |
|
|
* or if the specified collection is null |
961 |
dl |
1.1 |
* @since 1.2 |
962 |
|
|
*/ |
963 |
jsr166 |
1.31 |
public boolean removeAll(Collection<?> c) { |
964 |
|
|
Objects.requireNonNull(c); |
965 |
|
|
return bulkRemove(e -> c.contains(e)); |
966 |
dl |
1.1 |
} |
967 |
|
|
|
968 |
|
|
/** |
969 |
|
|
* Retains only the elements in this Vector that are contained in the |
970 |
|
|
* specified Collection. In other words, removes from this Vector all |
971 |
|
|
* of its elements that are not contained in the specified Collection. |
972 |
|
|
* |
973 |
|
|
* @param c a collection of elements to be retained in this Vector |
974 |
|
|
* (all other elements are removed) |
975 |
|
|
* @return true if this Vector changed as a result of the call |
976 |
|
|
* @throws ClassCastException if the types of one or more elements |
977 |
|
|
* in this vector are incompatible with the specified |
978 |
jsr166 |
1.30 |
* collection |
979 |
|
|
* (<a href="Collection.html#optional-restrictions">optional</a>) |
980 |
dl |
1.1 |
* @throws NullPointerException if this vector contains one or more null |
981 |
|
|
* elements and the specified collection does not support null |
982 |
jsr166 |
1.30 |
* elements |
983 |
|
|
* (<a href="Collection.html#optional-restrictions">optional</a>), |
984 |
|
|
* or if the specified collection is null |
985 |
dl |
1.1 |
* @since 1.2 |
986 |
|
|
*/ |
987 |
jsr166 |
1.31 |
public boolean retainAll(Collection<?> c) { |
988 |
|
|
Objects.requireNonNull(c); |
989 |
|
|
return bulkRemove(e -> !c.contains(e)); |
990 |
|
|
} |
991 |
|
|
|
992 |
jsr166 |
1.41 |
/** |
993 |
|
|
* @throws NullPointerException {@inheritDoc} |
994 |
|
|
*/ |
995 |
jsr166 |
1.31 |
@Override |
996 |
|
|
public boolean removeIf(Predicate<? super E> filter) { |
997 |
|
|
Objects.requireNonNull(filter); |
998 |
|
|
return bulkRemove(filter); |
999 |
|
|
} |
1000 |
|
|
|
1001 |
jsr166 |
1.33 |
// A tiny bit set implementation |
1002 |
|
|
|
1003 |
|
|
private static long[] nBits(int n) { |
1004 |
|
|
return new long[((n - 1) >> 6) + 1]; |
1005 |
|
|
} |
1006 |
|
|
private static void setBit(long[] bits, int i) { |
1007 |
|
|
bits[i >> 6] |= 1L << i; |
1008 |
|
|
} |
1009 |
|
|
private static boolean isClear(long[] bits, int i) { |
1010 |
|
|
return (bits[i >> 6] & (1L << i)) == 0; |
1011 |
|
|
} |
1012 |
|
|
|
1013 |
jsr166 |
1.31 |
private synchronized boolean bulkRemove(Predicate<? super E> filter) { |
1014 |
|
|
int expectedModCount = modCount; |
1015 |
|
|
final Object[] es = elementData; |
1016 |
jsr166 |
1.33 |
final int end = elementCount; |
1017 |
|
|
int i; |
1018 |
jsr166 |
1.31 |
// Optimize for initial run of survivors |
1019 |
jsr166 |
1.33 |
for (i = 0; i < end && !filter.test(elementAt(es, i)); i++) |
1020 |
jsr166 |
1.32 |
; |
1021 |
jsr166 |
1.33 |
// Tolerate predicates that reentrantly access the collection for |
1022 |
|
|
// read (but writers still get CME), so traverse once to find |
1023 |
|
|
// elements to delete, a second pass to physically expunge. |
1024 |
jsr166 |
1.36 |
if (i < end) { |
1025 |
jsr166 |
1.33 |
final int beg = i; |
1026 |
|
|
final long[] deathRow = nBits(end - beg); |
1027 |
|
|
deathRow[0] = 1L; // set bit 0 |
1028 |
|
|
for (i = beg + 1; i < end; i++) |
1029 |
|
|
if (filter.test(elementAt(es, i))) |
1030 |
|
|
setBit(deathRow, i - beg); |
1031 |
jsr166 |
1.36 |
if (modCount != expectedModCount) |
1032 |
|
|
throw new ConcurrentModificationException(); |
1033 |
|
|
modCount++; |
1034 |
jsr166 |
1.33 |
int w = beg; |
1035 |
|
|
for (i = beg; i < end; i++) |
1036 |
|
|
if (isClear(deathRow, i - beg)) |
1037 |
|
|
es[w++] = es[i]; |
1038 |
jsr166 |
1.40 |
for (i = elementCount = w; i < end; i++) |
1039 |
|
|
es[i] = null; |
1040 |
jsr166 |
1.36 |
// checkInvariants(); |
1041 |
|
|
return true; |
1042 |
|
|
} else { |
1043 |
|
|
if (modCount != expectedModCount) |
1044 |
|
|
throw new ConcurrentModificationException(); |
1045 |
|
|
// checkInvariants(); |
1046 |
|
|
return false; |
1047 |
jsr166 |
1.31 |
} |
1048 |
dl |
1.1 |
} |
1049 |
|
|
|
1050 |
|
|
/** |
1051 |
|
|
* Inserts all of the elements in the specified Collection into this |
1052 |
|
|
* Vector at the specified position. Shifts the element currently at |
1053 |
|
|
* that position (if any) and any subsequent elements to the right |
1054 |
|
|
* (increases their indices). The new elements will appear in the Vector |
1055 |
|
|
* in the order that they are returned by the specified Collection's |
1056 |
|
|
* iterator. |
1057 |
|
|
* |
1058 |
|
|
* @param index index at which to insert the first element from the |
1059 |
|
|
* specified collection |
1060 |
|
|
* @param c elements to be inserted into this Vector |
1061 |
jsr166 |
1.14 |
* @return {@code true} if this Vector changed as a result of the call |
1062 |
jsr166 |
1.18 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
1063 |
|
|
* ({@code index < 0 || index > size()}) |
1064 |
dl |
1.1 |
* @throws NullPointerException if the specified collection is null |
1065 |
|
|
* @since 1.2 |
1066 |
|
|
*/ |
1067 |
|
|
public synchronized boolean addAll(int index, Collection<? extends E> c) { |
1068 |
jsr166 |
1.23 |
if (index < 0 || index > elementCount) |
1069 |
|
|
throw new ArrayIndexOutOfBoundsException(index); |
1070 |
dl |
1.1 |
|
1071 |
|
|
Object[] a = c.toArray(); |
1072 |
jsr166 |
1.30 |
modCount++; |
1073 |
jsr166 |
1.23 |
int numNew = a.length; |
1074 |
jsr166 |
1.30 |
if (numNew == 0) |
1075 |
|
|
return false; |
1076 |
|
|
Object[] elementData = this.elementData; |
1077 |
|
|
final int s = elementCount; |
1078 |
|
|
if (numNew > elementData.length - s) |
1079 |
|
|
elementData = grow(s + numNew); |
1080 |
dl |
1.1 |
|
1081 |
jsr166 |
1.30 |
int numMoved = s - index; |
1082 |
jsr166 |
1.23 |
if (numMoved > 0) |
1083 |
jsr166 |
1.30 |
System.arraycopy(elementData, index, |
1084 |
|
|
elementData, index + numNew, |
1085 |
jsr166 |
1.23 |
numMoved); |
1086 |
dl |
1.1 |
System.arraycopy(a, 0, elementData, index, numNew); |
1087 |
jsr166 |
1.30 |
elementCount = s + numNew; |
1088 |
jsr166 |
1.35 |
// checkInvariants(); |
1089 |
jsr166 |
1.30 |
return true; |
1090 |
dl |
1.1 |
} |
1091 |
|
|
|
1092 |
|
|
/** |
1093 |
|
|
* Compares the specified Object with this Vector for equality. Returns |
1094 |
|
|
* true if and only if the specified Object is also a List, both Lists |
1095 |
|
|
* have the same size, and all corresponding pairs of elements in the two |
1096 |
jsr166 |
1.14 |
* Lists are <em>equal</em>. (Two elements {@code e1} and |
1097 |
jsr166 |
1.30 |
* {@code e2} are <em>equal</em> if {@code Objects.equals(e1, e2)}.) |
1098 |
|
|
* In other words, two Lists are defined to be |
1099 |
dl |
1.1 |
* equal if they contain the same elements in the same order. |
1100 |
|
|
* |
1101 |
|
|
* @param o the Object to be compared for equality with this Vector |
1102 |
|
|
* @return true if the specified Object is equal to this Vector |
1103 |
|
|
*/ |
1104 |
|
|
public synchronized boolean equals(Object o) { |
1105 |
|
|
return super.equals(o); |
1106 |
|
|
} |
1107 |
|
|
|
1108 |
|
|
/** |
1109 |
|
|
* Returns the hash code value for this Vector. |
1110 |
|
|
*/ |
1111 |
|
|
public synchronized int hashCode() { |
1112 |
|
|
return super.hashCode(); |
1113 |
|
|
} |
1114 |
|
|
|
1115 |
|
|
/** |
1116 |
|
|
* Returns a string representation of this Vector, containing |
1117 |
|
|
* the String representation of each element. |
1118 |
|
|
*/ |
1119 |
|
|
public synchronized String toString() { |
1120 |
|
|
return super.toString(); |
1121 |
|
|
} |
1122 |
|
|
|
1123 |
|
|
/** |
1124 |
jsr166 |
1.22 |
* Returns a view of the portion of this List between fromIndex, |
1125 |
|
|
* inclusive, and toIndex, exclusive. (If fromIndex and toIndex are |
1126 |
|
|
* equal, the returned List is empty.) The returned List is backed by this |
1127 |
|
|
* List, so changes in the returned List are reflected in this List, and |
1128 |
|
|
* vice-versa. The returned List supports all of the optional List |
1129 |
|
|
* operations supported by this List. |
1130 |
dl |
1.1 |
* |
1131 |
jsr166 |
1.22 |
* <p>This method eliminates the need for explicit range operations (of |
1132 |
|
|
* the sort that commonly exist for arrays). Any operation that expects |
1133 |
|
|
* a List can be used as a range operation by operating on a subList view |
1134 |
|
|
* instead of a whole List. For example, the following idiom |
1135 |
|
|
* removes a range of elements from a List: |
1136 |
|
|
* <pre> |
1137 |
jsr166 |
1.23 |
* list.subList(from, to).clear(); |
1138 |
jsr166 |
1.22 |
* </pre> |
1139 |
|
|
* Similar idioms may be constructed for indexOf and lastIndexOf, |
1140 |
|
|
* and all of the algorithms in the Collections class can be applied to |
1141 |
|
|
* a subList. |
1142 |
|
|
* |
1143 |
|
|
* <p>The semantics of the List returned by this method become undefined if |
1144 |
|
|
* the backing list (i.e., this List) is <i>structurally modified</i> in |
1145 |
|
|
* any way other than via the returned List. (Structural modifications are |
1146 |
|
|
* those that change the size of the List, or otherwise perturb it in such |
1147 |
|
|
* a fashion that iterations in progress may yield incorrect results.) |
1148 |
|
|
* |
1149 |
|
|
* @param fromIndex low endpoint (inclusive) of the subList |
1150 |
|
|
* @param toIndex high endpoint (exclusive) of the subList |
1151 |
|
|
* @return a view of the specified range within this List |
1152 |
|
|
* @throws IndexOutOfBoundsException if an endpoint index value is out of range |
1153 |
|
|
* {@code (fromIndex < 0 || toIndex > size)} |
1154 |
|
|
* @throws IllegalArgumentException if the endpoint indices are out of order |
1155 |
jsr166 |
1.23 |
* {@code (fromIndex > toIndex)} |
1156 |
jsr166 |
1.22 |
*/ |
1157 |
|
|
public synchronized List<E> subList(int fromIndex, int toIndex) { |
1158 |
|
|
return Collections.synchronizedList(super.subList(fromIndex, toIndex), |
1159 |
|
|
this); |
1160 |
|
|
} |
1161 |
|
|
|
1162 |
|
|
/** |
1163 |
|
|
* Removes from this list all of the elements whose index is between |
1164 |
|
|
* {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. |
1165 |
|
|
* Shifts any succeeding elements to the left (reduces their index). |
1166 |
|
|
* This call shortens the list by {@code (toIndex - fromIndex)} elements. |
1167 |
|
|
* (If {@code toIndex==fromIndex}, this operation has no effect.) |
1168 |
dl |
1.1 |
*/ |
1169 |
|
|
protected synchronized void removeRange(int fromIndex, int toIndex) { |
1170 |
jsr166 |
1.35 |
modCount++; |
1171 |
jsr166 |
1.40 |
shiftTailOverGap(elementData, fromIndex, toIndex); |
1172 |
jsr166 |
1.35 |
// checkInvariants(); |
1173 |
dl |
1.1 |
} |
1174 |
|
|
|
1175 |
jsr166 |
1.40 |
/** Erases the gap from lo to hi, by sliding down following elements. */ |
1176 |
|
|
private void shiftTailOverGap(Object[] es, int lo, int hi) { |
1177 |
|
|
System.arraycopy(es, hi, es, lo, elementCount - hi); |
1178 |
|
|
for (int to = elementCount, i = (elementCount -= hi - lo); i < to; i++) |
1179 |
|
|
es[i] = null; |
1180 |
|
|
} |
1181 |
|
|
|
1182 |
dl |
1.1 |
/** |
1183 |
jsr166 |
1.49 |
* Loads a {@code Vector} instance from a stream |
1184 |
|
|
* (that is, deserializes it). |
1185 |
|
|
* This method performs checks to ensure the consistency |
1186 |
|
|
* of the fields. |
1187 |
|
|
* |
1188 |
|
|
* @param in the stream |
1189 |
|
|
* @throws java.io.IOException if an I/O error occurs |
1190 |
|
|
* @throws ClassNotFoundException if the stream contains data |
1191 |
|
|
* of a non-existing class |
1192 |
|
|
*/ |
1193 |
|
|
private void readObject(ObjectInputStream in) |
1194 |
|
|
throws IOException, ClassNotFoundException { |
1195 |
|
|
ObjectInputStream.GetField gfields = in.readFields(); |
1196 |
|
|
int count = gfields.get("elementCount", 0); |
1197 |
|
|
Object[] data = (Object[])gfields.get("elementData", null); |
1198 |
|
|
if (count < 0 || data == null || count > data.length) { |
1199 |
|
|
throw new StreamCorruptedException("Inconsistent vector internals"); |
1200 |
|
|
} |
1201 |
|
|
elementCount = count; |
1202 |
|
|
elementData = data.clone(); |
1203 |
|
|
} |
1204 |
|
|
|
1205 |
|
|
/** |
1206 |
jsr166 |
1.39 |
* Saves the state of the {@code Vector} instance to a stream |
1207 |
|
|
* (that is, serializes it). |
1208 |
jsr166 |
1.30 |
* This method performs synchronization to ensure the consistency |
1209 |
|
|
* of the serialized data. |
1210 |
jsr166 |
1.39 |
* |
1211 |
|
|
* @param s the stream |
1212 |
|
|
* @throws java.io.IOException if an I/O error occurs |
1213 |
jsr166 |
1.30 |
*/ |
1214 |
|
|
private void writeObject(java.io.ObjectOutputStream s) |
1215 |
|
|
throws java.io.IOException { |
1216 |
|
|
final java.io.ObjectOutputStream.PutField fields = s.putFields(); |
1217 |
|
|
final Object[] data; |
1218 |
|
|
synchronized (this) { |
1219 |
|
|
fields.put("capacityIncrement", capacityIncrement); |
1220 |
|
|
fields.put("elementCount", elementCount); |
1221 |
|
|
data = elementData.clone(); |
1222 |
|
|
} |
1223 |
|
|
fields.put("elementData", data); |
1224 |
|
|
s.writeFields(); |
1225 |
dl |
1.1 |
} |
1226 |
|
|
|
1227 |
|
|
/** |
1228 |
jsr166 |
1.22 |
* Returns a list iterator over the elements in this list (in proper |
1229 |
dl |
1.1 |
* sequence), starting at the specified position in the list. |
1230 |
jsr166 |
1.22 |
* The specified index indicates the first element that would be |
1231 |
|
|
* returned by an initial call to {@link ListIterator#next next}. |
1232 |
|
|
* An initial call to {@link ListIterator#previous previous} would |
1233 |
|
|
* return the element with the specified index minus one. |
1234 |
|
|
* |
1235 |
|
|
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1236 |
dl |
1.1 |
* |
1237 |
|
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
1238 |
|
|
*/ |
1239 |
|
|
public synchronized ListIterator<E> listIterator(int index) { |
1240 |
jsr166 |
1.23 |
if (index < 0 || index > elementCount) |
1241 |
dl |
1.1 |
throw new IndexOutOfBoundsException("Index: "+index); |
1242 |
jsr166 |
1.23 |
return new ListItr(index); |
1243 |
dl |
1.1 |
} |
1244 |
jsr166 |
1.2 |
|
1245 |
dl |
1.1 |
/** |
1246 |
jsr166 |
1.22 |
* Returns a list iterator over the elements in this list (in proper |
1247 |
|
|
* sequence). |
1248 |
|
|
* |
1249 |
|
|
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1250 |
|
|
* |
1251 |
|
|
* @see #listIterator(int) |
1252 |
dl |
1.3 |
*/ |
1253 |
|
|
public synchronized ListIterator<E> listIterator() { |
1254 |
jsr166 |
1.23 |
return new ListItr(0); |
1255 |
dl |
1.3 |
} |
1256 |
|
|
|
1257 |
|
|
/** |
1258 |
dl |
1.1 |
* Returns an iterator over the elements in this list in proper sequence. |
1259 |
|
|
* |
1260 |
jsr166 |
1.22 |
* <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1261 |
|
|
* |
1262 |
dl |
1.1 |
* @return an iterator over the elements in this list in proper sequence |
1263 |
|
|
*/ |
1264 |
|
|
public synchronized Iterator<E> iterator() { |
1265 |
jsr166 |
1.23 |
return new Itr(); |
1266 |
dl |
1.1 |
} |
1267 |
|
|
|
1268 |
|
|
/** |
1269 |
jsr166 |
1.22 |
* An optimized version of AbstractList.Itr |
1270 |
dl |
1.10 |
*/ |
1271 |
jsr166 |
1.22 |
private class Itr implements Iterator<E> { |
1272 |
jsr166 |
1.23 |
int cursor; // index of next element to return |
1273 |
|
|
int lastRet = -1; // index of last element returned; -1 if no such |
1274 |
|
|
int expectedModCount = modCount; |
1275 |
jsr166 |
1.22 |
|
1276 |
jsr166 |
1.23 |
public boolean hasNext() { |
1277 |
jsr166 |
1.22 |
// Racy but within spec, since modifications are checked |
1278 |
|
|
// within or after synchronization in next/previous |
1279 |
|
|
return cursor != elementCount; |
1280 |
jsr166 |
1.23 |
} |
1281 |
|
|
|
1282 |
|
|
public E next() { |
1283 |
|
|
synchronized (Vector.this) { |
1284 |
|
|
checkForComodification(); |
1285 |
|
|
int i = cursor; |
1286 |
|
|
if (i >= elementCount) |
1287 |
|
|
throw new NoSuchElementException(); |
1288 |
|
|
cursor = i + 1; |
1289 |
|
|
return elementData(lastRet = i); |
1290 |
|
|
} |
1291 |
|
|
} |
1292 |
jsr166 |
1.22 |
|
1293 |
jsr166 |
1.23 |
public void remove() { |
1294 |
|
|
if (lastRet == -1) |
1295 |
|
|
throw new IllegalStateException(); |
1296 |
|
|
synchronized (Vector.this) { |
1297 |
|
|
checkForComodification(); |
1298 |
|
|
Vector.this.remove(lastRet); |
1299 |
|
|
expectedModCount = modCount; |
1300 |
dl |
1.10 |
} |
1301 |
jsr166 |
1.23 |
cursor = lastRet; |
1302 |
|
|
lastRet = -1; |
1303 |
|
|
} |
1304 |
|
|
|
1305 |
jsr166 |
1.30 |
@Override |
1306 |
|
|
public void forEachRemaining(Consumer<? super E> action) { |
1307 |
|
|
Objects.requireNonNull(action); |
1308 |
|
|
synchronized (Vector.this) { |
1309 |
|
|
final int size = elementCount; |
1310 |
|
|
int i = cursor; |
1311 |
|
|
if (i >= size) { |
1312 |
|
|
return; |
1313 |
|
|
} |
1314 |
jsr166 |
1.34 |
final Object[] es = elementData; |
1315 |
|
|
if (i >= es.length) |
1316 |
jsr166 |
1.30 |
throw new ConcurrentModificationException(); |
1317 |
jsr166 |
1.34 |
while (i < size && modCount == expectedModCount) |
1318 |
|
|
action.accept(elementAt(es, i++)); |
1319 |
jsr166 |
1.30 |
// update once at end of iteration to reduce heap write traffic |
1320 |
|
|
cursor = i; |
1321 |
|
|
lastRet = i - 1; |
1322 |
|
|
checkForComodification(); |
1323 |
|
|
} |
1324 |
|
|
} |
1325 |
|
|
|
1326 |
jsr166 |
1.23 |
final void checkForComodification() { |
1327 |
|
|
if (modCount != expectedModCount) |
1328 |
|
|
throw new ConcurrentModificationException(); |
1329 |
|
|
} |
1330 |
dl |
1.10 |
} |
1331 |
|
|
|
1332 |
|
|
/** |
1333 |
jsr166 |
1.22 |
* An optimized version of AbstractList.ListItr |
1334 |
dl |
1.1 |
*/ |
1335 |
jsr166 |
1.22 |
final class ListItr extends Itr implements ListIterator<E> { |
1336 |
jsr166 |
1.23 |
ListItr(int index) { |
1337 |
|
|
super(); |
1338 |
|
|
cursor = index; |
1339 |
|
|
} |
1340 |
|
|
|
1341 |
|
|
public boolean hasPrevious() { |
1342 |
|
|
return cursor != 0; |
1343 |
|
|
} |
1344 |
|
|
|
1345 |
|
|
public int nextIndex() { |
1346 |
|
|
return cursor; |
1347 |
|
|
} |
1348 |
|
|
|
1349 |
|
|
public int previousIndex() { |
1350 |
|
|
return cursor - 1; |
1351 |
|
|
} |
1352 |
|
|
|
1353 |
|
|
public E previous() { |
1354 |
|
|
synchronized (Vector.this) { |
1355 |
|
|
checkForComodification(); |
1356 |
|
|
int i = cursor - 1; |
1357 |
|
|
if (i < 0) |
1358 |
|
|
throw new NoSuchElementException(); |
1359 |
|
|
cursor = i; |
1360 |
|
|
return elementData(lastRet = i); |
1361 |
|
|
} |
1362 |
|
|
} |
1363 |
|
|
|
1364 |
|
|
public void set(E e) { |
1365 |
|
|
if (lastRet == -1) |
1366 |
|
|
throw new IllegalStateException(); |
1367 |
|
|
synchronized (Vector.this) { |
1368 |
|
|
checkForComodification(); |
1369 |
|
|
Vector.this.set(lastRet, e); |
1370 |
|
|
} |
1371 |
|
|
} |
1372 |
|
|
|
1373 |
|
|
public void add(E e) { |
1374 |
|
|
int i = cursor; |
1375 |
|
|
synchronized (Vector.this) { |
1376 |
|
|
checkForComodification(); |
1377 |
|
|
Vector.this.add(i, e); |
1378 |
|
|
expectedModCount = modCount; |
1379 |
|
|
} |
1380 |
|
|
cursor = i + 1; |
1381 |
|
|
lastRet = -1; |
1382 |
|
|
} |
1383 |
dl |
1.1 |
} |
1384 |
jsr166 |
1.30 |
|
1385 |
jsr166 |
1.41 |
/** |
1386 |
|
|
* @throws NullPointerException {@inheritDoc} |
1387 |
|
|
*/ |
1388 |
jsr166 |
1.30 |
@Override |
1389 |
|
|
public synchronized void forEach(Consumer<? super E> action) { |
1390 |
|
|
Objects.requireNonNull(action); |
1391 |
|
|
final int expectedModCount = modCount; |
1392 |
jsr166 |
1.34 |
final Object[] es = elementData; |
1393 |
|
|
final int size = elementCount; |
1394 |
|
|
for (int i = 0; modCount == expectedModCount && i < size; i++) |
1395 |
|
|
action.accept(elementAt(es, i)); |
1396 |
|
|
if (modCount != expectedModCount) |
1397 |
jsr166 |
1.30 |
throw new ConcurrentModificationException(); |
1398 |
jsr166 |
1.35 |
// checkInvariants(); |
1399 |
jsr166 |
1.30 |
} |
1400 |
|
|
|
1401 |
jsr166 |
1.41 |
/** |
1402 |
|
|
* @throws NullPointerException {@inheritDoc} |
1403 |
|
|
*/ |
1404 |
jsr166 |
1.30 |
@Override |
1405 |
|
|
public synchronized void replaceAll(UnaryOperator<E> operator) { |
1406 |
|
|
Objects.requireNonNull(operator); |
1407 |
|
|
final int expectedModCount = modCount; |
1408 |
jsr166 |
1.34 |
final Object[] es = elementData; |
1409 |
jsr166 |
1.30 |
final int size = elementCount; |
1410 |
jsr166 |
1.34 |
for (int i = 0; modCount == expectedModCount && i < size; i++) |
1411 |
|
|
es[i] = operator.apply(elementAt(es, i)); |
1412 |
|
|
if (modCount != expectedModCount) |
1413 |
jsr166 |
1.30 |
throw new ConcurrentModificationException(); |
1414 |
jsr166 |
1.35 |
// checkInvariants(); |
1415 |
jsr166 |
1.30 |
} |
1416 |
|
|
|
1417 |
|
|
@SuppressWarnings("unchecked") |
1418 |
|
|
@Override |
1419 |
|
|
public synchronized void sort(Comparator<? super E> c) { |
1420 |
|
|
final int expectedModCount = modCount; |
1421 |
|
|
Arrays.sort((E[]) elementData, 0, elementCount, c); |
1422 |
jsr166 |
1.35 |
if (modCount != expectedModCount) |
1423 |
jsr166 |
1.30 |
throw new ConcurrentModificationException(); |
1424 |
|
|
modCount++; |
1425 |
jsr166 |
1.35 |
// checkInvariants(); |
1426 |
jsr166 |
1.30 |
} |
1427 |
|
|
|
1428 |
|
|
/** |
1429 |
|
|
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> |
1430 |
|
|
* and <em>fail-fast</em> {@link Spliterator} over the elements in this |
1431 |
|
|
* list. |
1432 |
|
|
* |
1433 |
|
|
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED}, |
1434 |
|
|
* {@link Spliterator#SUBSIZED}, and {@link Spliterator#ORDERED}. |
1435 |
|
|
* Overriding implementations should document the reporting of additional |
1436 |
|
|
* characteristic values. |
1437 |
|
|
* |
1438 |
|
|
* @return a {@code Spliterator} over the elements in this list |
1439 |
|
|
* @since 1.8 |
1440 |
|
|
*/ |
1441 |
|
|
@Override |
1442 |
|
|
public Spliterator<E> spliterator() { |
1443 |
jsr166 |
1.37 |
return new VectorSpliterator(null, 0, -1, 0); |
1444 |
jsr166 |
1.30 |
} |
1445 |
|
|
|
1446 |
|
|
/** Similar to ArrayList Spliterator */ |
1447 |
jsr166 |
1.37 |
final class VectorSpliterator implements Spliterator<E> { |
1448 |
jsr166 |
1.30 |
private Object[] array; |
1449 |
|
|
private int index; // current index, modified on advance/split |
1450 |
|
|
private int fence; // -1 until used; then one past last index |
1451 |
|
|
private int expectedModCount; // initialized when fence set |
1452 |
|
|
|
1453 |
jsr166 |
1.43 |
/** Creates new spliterator covering the given range. */ |
1454 |
jsr166 |
1.37 |
VectorSpliterator(Object[] array, int origin, int fence, |
1455 |
jsr166 |
1.30 |
int expectedModCount) { |
1456 |
|
|
this.array = array; |
1457 |
|
|
this.index = origin; |
1458 |
|
|
this.fence = fence; |
1459 |
|
|
this.expectedModCount = expectedModCount; |
1460 |
|
|
} |
1461 |
|
|
|
1462 |
|
|
private int getFence() { // initialize on first use |
1463 |
|
|
int hi; |
1464 |
|
|
if ((hi = fence) < 0) { |
1465 |
jsr166 |
1.37 |
synchronized (Vector.this) { |
1466 |
|
|
array = elementData; |
1467 |
|
|
expectedModCount = modCount; |
1468 |
|
|
hi = fence = elementCount; |
1469 |
jsr166 |
1.30 |
} |
1470 |
|
|
} |
1471 |
|
|
return hi; |
1472 |
|
|
} |
1473 |
|
|
|
1474 |
|
|
public Spliterator<E> trySplit() { |
1475 |
|
|
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1476 |
|
|
return (lo >= mid) ? null : |
1477 |
jsr166 |
1.37 |
new VectorSpliterator(array, lo, index = mid, expectedModCount); |
1478 |
jsr166 |
1.30 |
} |
1479 |
|
|
|
1480 |
|
|
@SuppressWarnings("unchecked") |
1481 |
|
|
public boolean tryAdvance(Consumer<? super E> action) { |
1482 |
jsr166 |
1.44 |
Objects.requireNonNull(action); |
1483 |
jsr166 |
1.30 |
int i; |
1484 |
|
|
if (getFence() > (i = index)) { |
1485 |
|
|
index = i + 1; |
1486 |
|
|
action.accept((E)array[i]); |
1487 |
jsr166 |
1.37 |
if (modCount != expectedModCount) |
1488 |
jsr166 |
1.30 |
throw new ConcurrentModificationException(); |
1489 |
|
|
return true; |
1490 |
|
|
} |
1491 |
|
|
return false; |
1492 |
|
|
} |
1493 |
|
|
|
1494 |
|
|
@SuppressWarnings("unchecked") |
1495 |
|
|
public void forEachRemaining(Consumer<? super E> action) { |
1496 |
jsr166 |
1.44 |
Objects.requireNonNull(action); |
1497 |
jsr166 |
1.38 |
final int hi = getFence(); |
1498 |
|
|
final Object[] a = array; |
1499 |
|
|
int i; |
1500 |
|
|
for (i = index, index = hi; i < hi; i++) |
1501 |
|
|
action.accept((E) a[i]); |
1502 |
|
|
if (modCount != expectedModCount) |
1503 |
|
|
throw new ConcurrentModificationException(); |
1504 |
jsr166 |
1.30 |
} |
1505 |
|
|
|
1506 |
|
|
public long estimateSize() { |
1507 |
|
|
return getFence() - index; |
1508 |
|
|
} |
1509 |
|
|
|
1510 |
|
|
public int characteristics() { |
1511 |
|
|
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED; |
1512 |
|
|
} |
1513 |
|
|
} |
1514 |
jsr166 |
1.35 |
|
1515 |
|
|
void checkInvariants() { |
1516 |
|
|
// assert elementCount >= 0; |
1517 |
|
|
// assert elementCount == elementData.length || elementData[elementCount] == null; |
1518 |
|
|
} |
1519 |
dl |
1.1 |
} |