1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved. |
3 |
> |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
5 |
< |
* Copyright 2007 Sun Microsystems, Inc. All rights reserved. |
6 |
< |
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
5 |
> |
* 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 |
> |
* published by the Free Software Foundation. Oracle designates this |
8 |
> |
* particular file as subject to the "Classpath" exception as provided |
9 |
> |
* by Oracle in the LICENSE file that accompanied this code. |
10 |
> |
* |
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 |
> |
* 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 |
|
*/ |
25 |
|
|
26 |
|
package java.util; |
27 |
|
|
28 |
+ |
import java.io.IOException; |
29 |
+ |
import java.io.ObjectInputStream; |
30 |
+ |
import java.io.StreamCorruptedException; |
31 |
+ |
import java.util.function.Consumer; |
32 |
+ |
import java.util.function.Predicate; |
33 |
+ |
import java.util.function.UnaryOperator; |
34 |
+ |
|
35 |
+ |
import jdk.internal.util.ArraysSupport; |
36 |
+ |
|
37 |
|
/** |
38 |
|
* The {@code Vector} class implements a growable array of |
39 |
|
* objects. Like an array, it contains components that can be |
50 |
|
* capacity of a vector before inserting a large number of |
51 |
|
* components; this reduces the amount of incremental reallocation. |
52 |
|
* |
53 |
< |
* <p>The Iterators returned by Vector's iterator and listIterator |
54 |
< |
* methods are <em>fail-fast</em>: if the Vector is structurally modified |
55 |
< |
* at any time after the Iterator is created, in any way except through the |
56 |
< |
* Iterator's own remove or add methods, the Iterator will throw a |
57 |
< |
* ConcurrentModificationException. Thus, in the face of concurrent |
58 |
< |
* modification, the Iterator fails quickly and cleanly, rather than risking |
59 |
< |
* arbitrary, non-deterministic behavior at an undetermined time in the future. |
60 |
< |
* The Enumerations returned by Vector's elements method are <em>not</em> |
61 |
< |
* fail-fast. |
53 |
> |
* <p id="fail-fast"> |
54 |
> |
* The iterators returned by this class's {@link #iterator() iterator} and |
55 |
> |
* {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>: |
56 |
> |
* if the vector is structurally modified at any time after the iterator is |
57 |
> |
* created, in any way except through the iterator's own |
58 |
> |
* {@link ListIterator#remove() remove} or |
59 |
> |
* {@link ListIterator#add(Object) add} methods, the iterator will throw a |
60 |
> |
* {@link ConcurrentModificationException}. Thus, in the face of |
61 |
> |
* concurrent modification, the iterator fails quickly and cleanly, rather |
62 |
> |
* than risking arbitrary, non-deterministic behavior at an undetermined |
63 |
> |
* time in the future. The {@link Enumeration Enumerations} returned by |
64 |
> |
* the {@link #elements() elements} method are <em>not</em> fail-fast; if the |
65 |
> |
* Vector is structurally modified at any time after the enumeration is |
66 |
> |
* created then the results of enumerating are undefined. |
67 |
|
* |
68 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
69 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
75 |
|
* |
76 |
|
* <p>As of the Java 2 platform v1.2, this class was retrofitted to |
77 |
|
* implement the {@link List} interface, making it a member of the |
78 |
< |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> Java |
79 |
< |
* Collections Framework</a>. Unlike the new collection |
80 |
< |
* implementations, {@code Vector} is synchronized. |
78 |
> |
* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework"> |
79 |
> |
* Java Collections Framework</a>. Unlike the new collection |
80 |
> |
* implementations, {@code Vector} is synchronized. If a thread-safe |
81 |
> |
* implementation is not needed, it is recommended to use {@link |
82 |
> |
* ArrayList} in place of {@code Vector}. |
83 |
> |
* |
84 |
> |
* @param <E> Type of component elements |
85 |
|
* |
86 |
|
* @author Lee Boynton |
87 |
|
* @author Jonathan Payne |
52 |
– |
* @version %I%, %G% |
88 |
|
* @see Collection |
54 |
– |
* @see List |
55 |
– |
* @see ArrayList |
89 |
|
* @see LinkedList |
90 |
< |
* @since JDK1.0 |
90 |
> |
* @since 1.0 |
91 |
|
*/ |
92 |
|
public class Vector<E> |
93 |
|
extends AbstractList<E> |
102 |
|
* |
103 |
|
* @serial |
104 |
|
*/ |
105 |
+ |
@SuppressWarnings("serial") // Conditionally serializable |
106 |
|
protected Object[] elementData; |
107 |
|
|
108 |
|
/** |
125 |
|
protected int capacityIncrement; |
126 |
|
|
127 |
|
/** use serialVersionUID from JDK 1.0.2 for interoperability */ |
128 |
+ |
// OPENJDK @java.io.Serial |
129 |
|
private static final long serialVersionUID = -2767605614048989439L; |
130 |
|
|
131 |
|
/** |
139 |
|
* is negative |
140 |
|
*/ |
141 |
|
public Vector(int initialCapacity, int capacityIncrement) { |
142 |
< |
super(); |
142 |
> |
super(); |
143 |
|
if (initialCapacity < 0) |
144 |
|
throw new IllegalArgumentException("Illegal Capacity: "+ |
145 |
|
initialCapacity); |
146 |
< |
this.elementData = new Object[initialCapacity]; |
147 |
< |
this.capacityIncrement = capacityIncrement; |
146 |
> |
this.elementData = new Object[initialCapacity]; |
147 |
> |
this.capacityIncrement = capacityIncrement; |
148 |
|
} |
149 |
|
|
150 |
|
/** |
156 |
|
* is negative |
157 |
|
*/ |
158 |
|
public Vector(int initialCapacity) { |
159 |
< |
this(initialCapacity, 0); |
159 |
> |
this(initialCapacity, 0); |
160 |
|
} |
161 |
|
|
162 |
|
/** |
165 |
|
* zero. |
166 |
|
*/ |
167 |
|
public Vector() { |
168 |
< |
this(10); |
168 |
> |
this(10); |
169 |
|
} |
170 |
|
|
171 |
|
/** |
179 |
|
* @since 1.2 |
180 |
|
*/ |
181 |
|
public Vector(Collection<? extends E> c) { |
182 |
< |
elementData = c.toArray(); |
183 |
< |
elementCount = elementData.length; |
184 |
< |
// c.toArray might (incorrectly) not return Object[] (see 6260652) |
185 |
< |
if (elementData.getClass() != Object[].class) |
186 |
< |
elementData = Arrays.copyOf(elementData, elementCount, Object[].class); |
182 |
> |
Object[] a = c.toArray(); |
183 |
> |
elementCount = a.length; |
184 |
> |
if (c.getClass() == ArrayList.class) { |
185 |
> |
elementData = a; |
186 |
> |
} else { |
187 |
> |
elementData = Arrays.copyOf(a, elementCount, Object[].class); |
188 |
> |
} |
189 |
|
} |
190 |
|
|
191 |
|
/** |
202 |
|
* @see #toArray(Object[]) |
203 |
|
*/ |
204 |
|
public synchronized void copyInto(Object[] anArray) { |
205 |
< |
System.arraycopy(elementData, 0, anArray, 0, elementCount); |
205 |
> |
System.arraycopy(elementData, 0, anArray, 0, elementCount); |
206 |
|
} |
207 |
|
|
208 |
|
/** |
214 |
|
* minimize the storage of a vector. |
215 |
|
*/ |
216 |
|
public synchronized void trimToSize() { |
217 |
< |
modCount++; |
218 |
< |
int oldCapacity = elementData.length; |
219 |
< |
if (elementCount < oldCapacity) { |
217 |
> |
modCount++; |
218 |
> |
int oldCapacity = elementData.length; |
219 |
> |
if (elementCount < oldCapacity) { |
220 |
|
elementData = Arrays.copyOf(elementData, elementCount); |
221 |
< |
} |
221 |
> |
} |
222 |
|
} |
223 |
|
|
224 |
|
/** |
239 |
|
* @param minCapacity the desired minimum capacity |
240 |
|
*/ |
241 |
|
public synchronized void ensureCapacity(int minCapacity) { |
242 |
< |
modCount++; |
243 |
< |
ensureCapacityHelper(minCapacity); |
242 |
> |
if (minCapacity > 0) { |
243 |
> |
modCount++; |
244 |
> |
if (minCapacity > elementData.length) |
245 |
> |
grow(minCapacity); |
246 |
> |
} |
247 |
|
} |
248 |
|
|
249 |
|
/** |
250 |
< |
* This implements the unsynchronized semantics of ensureCapacity. |
251 |
< |
* Synchronized methods in this class can internally call this |
252 |
< |
* method for ensuring capacity without incurring the cost of an |
253 |
< |
* extra synchronization. |
254 |
< |
* |
255 |
< |
* @see #ensureCapacity(int) |
256 |
< |
*/ |
257 |
< |
private void ensureCapacityHelper(int minCapacity) { |
258 |
< |
int oldCapacity = elementData.length; |
259 |
< |
if (minCapacity > oldCapacity) { |
260 |
< |
Object[] oldData = elementData; |
261 |
< |
int newCapacity = (capacityIncrement > 0) ? |
262 |
< |
(oldCapacity + capacityIncrement) : (oldCapacity * 2); |
263 |
< |
if (newCapacity < minCapacity) { |
264 |
< |
newCapacity = minCapacity; |
265 |
< |
} |
266 |
< |
elementData = Arrays.copyOf(elementData, newCapacity); |
227 |
< |
} |
250 |
> |
* Increases the capacity to ensure that it can hold at least the |
251 |
> |
* number of elements specified by the minimum capacity argument. |
252 |
> |
* |
253 |
> |
* @param minCapacity the desired minimum capacity |
254 |
> |
* @throws OutOfMemoryError if minCapacity is less than zero |
255 |
> |
*/ |
256 |
> |
private Object[] grow(int minCapacity) { |
257 |
> |
int oldCapacity = elementData.length; |
258 |
> |
int newCapacity = ArraysSupport.newLength(oldCapacity, |
259 |
> |
minCapacity - oldCapacity, /* minimum growth */ |
260 |
> |
capacityIncrement > 0 ? capacityIncrement : oldCapacity |
261 |
> |
/* preferred growth */); |
262 |
> |
return elementData = Arrays.copyOf(elementData, newCapacity); |
263 |
> |
} |
264 |
> |
|
265 |
> |
private Object[] grow() { |
266 |
> |
return grow(elementCount + 1); |
267 |
|
} |
268 |
|
|
269 |
|
/** |
276 |
|
* @throws ArrayIndexOutOfBoundsException if the new size is negative |
277 |
|
*/ |
278 |
|
public synchronized void setSize(int newSize) { |
279 |
< |
modCount++; |
280 |
< |
if (newSize > elementCount) { |
281 |
< |
ensureCapacityHelper(newSize); |
282 |
< |
} else { |
283 |
< |
for (int i = newSize ; i < elementCount ; i++) { |
284 |
< |
elementData[i] = null; |
285 |
< |
} |
247 |
< |
} |
248 |
< |
elementCount = newSize; |
279 |
> |
modCount++; |
280 |
> |
if (newSize > elementData.length) |
281 |
> |
grow(newSize); |
282 |
> |
final Object[] es = elementData; |
283 |
> |
for (int to = elementCount, i = newSize; i < to; i++) |
284 |
> |
es[i] = null; |
285 |
> |
elementCount = newSize; |
286 |
|
} |
287 |
|
|
288 |
|
/** |
293 |
|
* of this vector) |
294 |
|
*/ |
295 |
|
public synchronized int capacity() { |
296 |
< |
return elementData.length; |
296 |
> |
return elementData.length; |
297 |
|
} |
298 |
|
|
299 |
|
/** |
302 |
|
* @return the number of components in this vector |
303 |
|
*/ |
304 |
|
public synchronized int size() { |
305 |
< |
return elementCount; |
305 |
> |
return elementCount; |
306 |
|
} |
307 |
|
|
308 |
|
/** |
313 |
|
* {@code false} otherwise. |
314 |
|
*/ |
315 |
|
public synchronized boolean isEmpty() { |
316 |
< |
return elementCount == 0; |
316 |
> |
return elementCount == 0; |
317 |
|
} |
318 |
|
|
319 |
|
/** |
320 |
|
* Returns an enumeration of the components of this vector. The |
321 |
|
* returned {@code Enumeration} object will generate all items in |
322 |
|
* this vector. The first item generated is the item at index {@code 0}, |
323 |
< |
* then the item at index {@code 1}, and so on. |
323 |
> |
* then the item at index {@code 1}, and so on. If the vector is |
324 |
> |
* structurally modified while enumerating over the elements then the |
325 |
> |
* results of enumerating are undefined. |
326 |
|
* |
327 |
|
* @return an enumeration of the components of this vector |
328 |
|
* @see Iterator |
329 |
|
*/ |
330 |
|
public Enumeration<E> elements() { |
331 |
< |
return new Enumeration<E>() { |
332 |
< |
int count = 0; |
331 |
> |
return new Enumeration<E>() { |
332 |
> |
int count = 0; |
333 |
|
|
334 |
< |
public boolean hasMoreElements() { |
335 |
< |
return count < elementCount; |
336 |
< |
} |
337 |
< |
|
338 |
< |
public E nextElement() { |
339 |
< |
synchronized (Vector.this) { |
340 |
< |
if (count < elementCount) { |
341 |
< |
return (E)elementData[count++]; |
342 |
< |
} |
343 |
< |
} |
344 |
< |
throw new NoSuchElementException("Vector Enumeration"); |
345 |
< |
} |
346 |
< |
}; |
334 |
> |
public boolean hasMoreElements() { |
335 |
> |
return count < elementCount; |
336 |
> |
} |
337 |
> |
|
338 |
> |
public E nextElement() { |
339 |
> |
synchronized (Vector.this) { |
340 |
> |
if (count < elementCount) { |
341 |
> |
return elementData(count++); |
342 |
> |
} |
343 |
> |
} |
344 |
> |
throw new NoSuchElementException("Vector Enumeration"); |
345 |
> |
} |
346 |
> |
}; |
347 |
|
} |
348 |
|
|
349 |
|
/** |
350 |
|
* Returns {@code true} if this vector contains the specified element. |
351 |
|
* More formally, returns {@code true} if and only if this vector |
352 |
|
* contains at least one element {@code e} such that |
353 |
< |
* <tt>(o==null ? e==null : o.equals(e))</tt>. |
353 |
> |
* {@code Objects.equals(o, e)}. |
354 |
|
* |
355 |
|
* @param o element whose presence in this vector is to be tested |
356 |
|
* @return {@code true} if this vector contains the specified element |
357 |
|
*/ |
358 |
|
public boolean contains(Object o) { |
359 |
< |
return indexOf(o, 0) >= 0; |
359 |
> |
return indexOf(o, 0) >= 0; |
360 |
|
} |
361 |
|
|
362 |
|
/** |
363 |
|
* Returns the index of the first occurrence of the specified element |
364 |
|
* in this vector, or -1 if this vector does not contain the element. |
365 |
|
* More formally, returns the lowest index {@code i} such that |
366 |
< |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
366 |
> |
* {@code Objects.equals(o, get(i))}, |
367 |
|
* or -1 if there is no such index. |
368 |
|
* |
369 |
|
* @param o element to search for |
371 |
|
* this vector, or -1 if this vector does not contain the element |
372 |
|
*/ |
373 |
|
public int indexOf(Object o) { |
374 |
< |
return indexOf(o, 0); |
374 |
> |
return indexOf(o, 0); |
375 |
|
} |
376 |
|
|
377 |
|
/** |
379 |
|
* this vector, searching forwards from {@code index}, or returns -1 if |
380 |
|
* the element is not found. |
381 |
|
* More formally, returns the lowest index {@code i} such that |
382 |
< |
* <tt>(i >= index && (o==null ? get(i)==null : o.equals(get(i))))</tt>, |
382 |
> |
* {@code (i >= index && Objects.equals(o, get(i)))}, |
383 |
|
* or -1 if there is no such index. |
384 |
|
* |
385 |
|
* @param o element to search for |
391 |
|
* @see Object#equals(Object) |
392 |
|
*/ |
393 |
|
public synchronized int indexOf(Object o, int index) { |
394 |
< |
if (o == null) { |
395 |
< |
for (int i = index ; i < elementCount ; i++) |
396 |
< |
if (elementData[i]==null) |
397 |
< |
return i; |
398 |
< |
} else { |
399 |
< |
for (int i = index ; i < elementCount ; i++) |
400 |
< |
if (o.equals(elementData[i])) |
401 |
< |
return i; |
402 |
< |
} |
403 |
< |
return -1; |
394 |
> |
if (o == null) { |
395 |
> |
for (int i = index ; i < elementCount ; i++) |
396 |
> |
if (elementData[i]==null) |
397 |
> |
return i; |
398 |
> |
} else { |
399 |
> |
for (int i = index ; i < elementCount ; i++) |
400 |
> |
if (o.equals(elementData[i])) |
401 |
> |
return i; |
402 |
> |
} |
403 |
> |
return -1; |
404 |
|
} |
405 |
|
|
406 |
|
/** |
407 |
|
* Returns the index of the last occurrence of the specified element |
408 |
|
* in this vector, or -1 if this vector does not contain the element. |
409 |
|
* More formally, returns the highest index {@code i} such that |
410 |
< |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
410 |
> |
* {@code Objects.equals(o, get(i))}, |
411 |
|
* or -1 if there is no such index. |
412 |
|
* |
413 |
|
* @param o element to search for |
415 |
|
* this vector, or -1 if this vector does not contain the element |
416 |
|
*/ |
417 |
|
public synchronized int lastIndexOf(Object o) { |
418 |
< |
return lastIndexOf(o, elementCount-1); |
418 |
> |
return lastIndexOf(o, elementCount-1); |
419 |
|
} |
420 |
|
|
421 |
|
/** |
423 |
|
* this vector, searching backwards from {@code index}, or returns -1 if |
424 |
|
* the element is not found. |
425 |
|
* More formally, returns the highest index {@code i} such that |
426 |
< |
* <tt>(i <= index && (o==null ? get(i)==null : o.equals(get(i))))</tt>, |
426 |
> |
* {@code (i <= index && Objects.equals(o, get(i)))}, |
427 |
|
* or -1 if there is no such index. |
428 |
|
* |
429 |
|
* @param o element to search for |
438 |
|
if (index >= elementCount) |
439 |
|
throw new IndexOutOfBoundsException(index + " >= "+ elementCount); |
440 |
|
|
441 |
< |
if (o == null) { |
442 |
< |
for (int i = index; i >= 0; i--) |
443 |
< |
if (elementData[i]==null) |
444 |
< |
return i; |
445 |
< |
} else { |
446 |
< |
for (int i = index; i >= 0; i--) |
447 |
< |
if (o.equals(elementData[i])) |
448 |
< |
return i; |
449 |
< |
} |
450 |
< |
return -1; |
441 |
> |
if (o == null) { |
442 |
> |
for (int i = index; i >= 0; i--) |
443 |
> |
if (elementData[i]==null) |
444 |
> |
return i; |
445 |
> |
} else { |
446 |
> |
for (int i = index; i >= 0; i--) |
447 |
> |
if (o.equals(elementData[i])) |
448 |
> |
return i; |
449 |
> |
} |
450 |
> |
return -1; |
451 |
|
} |
452 |
|
|
453 |
|
/** |
459 |
|
* @param index an index into this vector |
460 |
|
* @return the component at the specified index |
461 |
|
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
462 |
< |
* ({@code index < 0 || index >= size()}) |
462 |
> |
* ({@code index < 0 || index >= size()}) |
463 |
|
*/ |
464 |
|
public synchronized E elementAt(int index) { |
465 |
< |
if (index >= elementCount) { |
466 |
< |
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
467 |
< |
} |
465 |
> |
if (index >= elementCount) { |
466 |
> |
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
467 |
> |
} |
468 |
|
|
469 |
< |
return (E)elementData[index]; |
469 |
> |
return elementData(index); |
470 |
|
} |
471 |
|
|
472 |
|
/** |
477 |
|
* @throws NoSuchElementException if this vector has no components |
478 |
|
*/ |
479 |
|
public synchronized E firstElement() { |
480 |
< |
if (elementCount == 0) { |
481 |
< |
throw new NoSuchElementException(); |
482 |
< |
} |
483 |
< |
return (E)elementData[0]; |
480 |
> |
if (elementCount == 0) { |
481 |
> |
throw new NoSuchElementException(); |
482 |
> |
} |
483 |
> |
return elementData(0); |
484 |
|
} |
485 |
|
|
486 |
|
/** |
487 |
|
* Returns the last component of the vector. |
488 |
|
* |
489 |
|
* @return the last component of the vector, i.e., the component at index |
490 |
< |
* <code>size() - 1</code>. |
490 |
> |
* {@code size() - 1} |
491 |
|
* @throws NoSuchElementException if this vector is empty |
492 |
|
*/ |
493 |
|
public synchronized E lastElement() { |
494 |
< |
if (elementCount == 0) { |
495 |
< |
throw new NoSuchElementException(); |
496 |
< |
} |
497 |
< |
return (E)elementData[elementCount - 1]; |
494 |
> |
if (elementCount == 0) { |
495 |
> |
throw new NoSuchElementException(); |
496 |
> |
} |
497 |
> |
return elementData(elementCount - 1); |
498 |
|
} |
499 |
|
|
500 |
|
/** |
515 |
|
* @param obj what the component is to be set to |
516 |
|
* @param index the specified index |
517 |
|
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
518 |
< |
* ({@code index < 0 || index >= size()}) |
518 |
> |
* ({@code index < 0 || index >= size()}) |
519 |
|
*/ |
520 |
|
public synchronized void setElementAt(E obj, int index) { |
521 |
< |
if (index >= elementCount) { |
522 |
< |
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
523 |
< |
elementCount); |
524 |
< |
} |
525 |
< |
elementData[index] = obj; |
521 |
> |
if (index >= elementCount) { |
522 |
> |
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
523 |
> |
elementCount); |
524 |
> |
} |
525 |
> |
elementData[index] = obj; |
526 |
|
} |
527 |
|
|
528 |
|
/** |
542 |
|
* |
543 |
|
* @param index the index of the object to remove |
544 |
|
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
545 |
< |
* ({@code index < 0 || index >= size()}) |
545 |
> |
* ({@code index < 0 || index >= size()}) |
546 |
|
*/ |
547 |
|
public synchronized void removeElementAt(int index) { |
548 |
< |
modCount++; |
549 |
< |
if (index >= elementCount) { |
550 |
< |
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
551 |
< |
elementCount); |
552 |
< |
} |
553 |
< |
else if (index < 0) { |
554 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
555 |
< |
} |
556 |
< |
int j = elementCount - index - 1; |
557 |
< |
if (j > 0) { |
558 |
< |
System.arraycopy(elementData, index + 1, elementData, index, j); |
559 |
< |
} |
560 |
< |
elementCount--; |
561 |
< |
elementData[elementCount] = null; /* to let gc do its work */ |
548 |
> |
if (index >= elementCount) { |
549 |
> |
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
550 |
> |
elementCount); |
551 |
> |
} |
552 |
> |
else if (index < 0) { |
553 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
554 |
> |
} |
555 |
> |
int j = elementCount - index - 1; |
556 |
> |
if (j > 0) { |
557 |
> |
System.arraycopy(elementData, index + 1, elementData, index, j); |
558 |
> |
} |
559 |
> |
modCount++; |
560 |
> |
elementCount--; |
561 |
> |
elementData[elementCount] = null; /* to let gc do its work */ |
562 |
> |
// checkInvariants(); |
563 |
|
} |
564 |
|
|
565 |
|
/** |
583 |
|
* @param obj the component to insert |
584 |
|
* @param index where to insert the new component |
585 |
|
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
586 |
< |
* ({@code index < 0 || index > size()}) |
586 |
> |
* ({@code index < 0 || index > size()}) |
587 |
|
*/ |
588 |
|
public synchronized void insertElementAt(E obj, int index) { |
589 |
< |
modCount++; |
590 |
< |
if (index > elementCount) { |
591 |
< |
throw new ArrayIndexOutOfBoundsException(index |
592 |
< |
+ " > " + elementCount); |
593 |
< |
} |
594 |
< |
ensureCapacityHelper(elementCount + 1); |
595 |
< |
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index); |
596 |
< |
elementData[index] = obj; |
597 |
< |
elementCount++; |
589 |
> |
if (index > elementCount) { |
590 |
> |
throw new ArrayIndexOutOfBoundsException(index |
591 |
> |
+ " > " + elementCount); |
592 |
> |
} |
593 |
> |
modCount++; |
594 |
> |
final int s = elementCount; |
595 |
> |
Object[] elementData = this.elementData; |
596 |
> |
if (s == elementData.length) |
597 |
> |
elementData = grow(); |
598 |
> |
System.arraycopy(elementData, index, |
599 |
> |
elementData, index + 1, |
600 |
> |
s - index); |
601 |
> |
elementData[index] = obj; |
602 |
> |
elementCount = s + 1; |
603 |
|
} |
604 |
|
|
605 |
|
/** |
614 |
|
* @param obj the component to be added |
615 |
|
*/ |
616 |
|
public synchronized void addElement(E obj) { |
617 |
< |
modCount++; |
618 |
< |
ensureCapacityHelper(elementCount + 1); |
574 |
< |
elementData[elementCount++] = obj; |
617 |
> |
modCount++; |
618 |
> |
add(obj, elementData, elementCount); |
619 |
|
} |
620 |
|
|
621 |
|
/** |
634 |
|
* vector; {@code false} otherwise. |
635 |
|
*/ |
636 |
|
public synchronized boolean removeElement(Object obj) { |
637 |
< |
modCount++; |
638 |
< |
int i = indexOf(obj); |
639 |
< |
if (i >= 0) { |
640 |
< |
removeElementAt(i); |
641 |
< |
return true; |
642 |
< |
} |
643 |
< |
return false; |
637 |
> |
modCount++; |
638 |
> |
int i = indexOf(obj); |
639 |
> |
if (i >= 0) { |
640 |
> |
removeElementAt(i); |
641 |
> |
return true; |
642 |
> |
} |
643 |
> |
return false; |
644 |
|
} |
645 |
|
|
646 |
|
/** |
650 |
|
* method (which is part of the {@link List} interface). |
651 |
|
*/ |
652 |
|
public synchronized void removeAllElements() { |
653 |
+ |
final Object[] es = elementData; |
654 |
+ |
for (int to = elementCount, i = elementCount = 0; i < to; i++) |
655 |
+ |
es[i] = null; |
656 |
|
modCount++; |
610 |
– |
// Let gc do its work |
611 |
– |
for (int i = 0; i < elementCount; i++) |
612 |
– |
elementData[i] = null; |
613 |
– |
|
614 |
– |
elementCount = 0; |
657 |
|
} |
658 |
|
|
659 |
|
/** |
664 |
|
* @return a clone of this vector |
665 |
|
*/ |
666 |
|
public synchronized Object clone() { |
667 |
< |
try { |
668 |
< |
Vector<E> v = (Vector<E>) super.clone(); |
669 |
< |
v.elementData = Arrays.copyOf(elementData, elementCount); |
670 |
< |
v.modCount = 0; |
671 |
< |
return v; |
672 |
< |
} catch (CloneNotSupportedException e) { |
673 |
< |
// this shouldn't happen, since we are Cloneable |
674 |
< |
throw new InternalError(); |
675 |
< |
} |
667 |
> |
try { |
668 |
> |
@SuppressWarnings("unchecked") |
669 |
> |
Vector<E> v = (Vector<E>) super.clone(); |
670 |
> |
v.elementData = Arrays.copyOf(elementData, elementCount); |
671 |
> |
v.modCount = 0; |
672 |
> |
return v; |
673 |
> |
} catch (CloneNotSupportedException e) { |
674 |
> |
// this shouldn't happen, since we are Cloneable |
675 |
> |
throw new InternalError(e); |
676 |
> |
} |
677 |
|
} |
678 |
|
|
679 |
|
/** |
700 |
|
* of the Vector <em>only</em> if the caller knows that the Vector |
701 |
|
* does not contain any null elements.) |
702 |
|
* |
703 |
+ |
* @param <T> type of array elements. The same type as {@code <E>} or a |
704 |
+ |
* supertype of {@code <E>}. |
705 |
|
* @param a the array into which the elements of the Vector are to |
706 |
< |
* be stored, if it is big enough; otherwise, a new array of the |
707 |
< |
* same runtime type is allocated for this purpose. |
706 |
> |
* be stored, if it is big enough; otherwise, a new array of the |
707 |
> |
* same runtime type is allocated for this purpose. |
708 |
|
* @return an array containing the elements of the Vector |
709 |
< |
* @throws ArrayStoreException if the runtime type of a is not a supertype |
710 |
< |
* of the runtime type of every element in this Vector |
709 |
> |
* @throws ArrayStoreException if the runtime type of a, {@code <T>}, is not |
710 |
> |
* a supertype of the runtime type, {@code <E>}, of every element in this |
711 |
> |
* Vector |
712 |
|
* @throws NullPointerException if the given array is null |
713 |
|
* @since 1.2 |
714 |
|
*/ |
715 |
+ |
@SuppressWarnings("unchecked") |
716 |
|
public synchronized <T> T[] toArray(T[] a) { |
717 |
|
if (a.length < elementCount) |
718 |
|
return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass()); |
719 |
|
|
720 |
< |
System.arraycopy(elementData, 0, a, 0, elementCount); |
720 |
> |
System.arraycopy(elementData, 0, a, 0, elementCount); |
721 |
|
|
722 |
|
if (a.length > elementCount) |
723 |
|
a[elementCount] = null; |
727 |
|
|
728 |
|
// Positional Access Operations |
729 |
|
|
730 |
+ |
@SuppressWarnings("unchecked") |
731 |
+ |
E elementData(int index) { |
732 |
+ |
return (E) elementData[index]; |
733 |
+ |
} |
734 |
+ |
|
735 |
+ |
@SuppressWarnings("unchecked") |
736 |
+ |
static <E> E elementAt(Object[] es, int index) { |
737 |
+ |
return (E) es[index]; |
738 |
+ |
} |
739 |
+ |
|
740 |
|
/** |
741 |
|
* Returns the element at the specified position in this Vector. |
742 |
|
* |
747 |
|
* @since 1.2 |
748 |
|
*/ |
749 |
|
public synchronized E get(int index) { |
750 |
< |
if (index >= elementCount) |
751 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
750 |
> |
if (index >= elementCount) |
751 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
752 |
|
|
753 |
< |
return (E)elementData[index]; |
753 |
> |
return elementData(index); |
754 |
|
} |
755 |
|
|
756 |
|
/** |
761 |
|
* @param element element to be stored at the specified position |
762 |
|
* @return the element previously at the specified position |
763 |
|
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
764 |
< |
* ({@code index < 0 || index >= size()}) |
764 |
> |
* ({@code index < 0 || index >= size()}) |
765 |
|
* @since 1.2 |
766 |
|
*/ |
767 |
|
public synchronized E set(int index, E element) { |
768 |
< |
if (index >= elementCount) |
769 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
768 |
> |
if (index >= elementCount) |
769 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
770 |
|
|
771 |
< |
Object oldValue = elementData[index]; |
772 |
< |
elementData[index] = element; |
773 |
< |
return (E)oldValue; |
771 |
> |
E oldValue = elementData(index); |
772 |
> |
elementData[index] = element; |
773 |
> |
return oldValue; |
774 |
> |
} |
775 |
> |
|
776 |
> |
/** |
777 |
> |
* This helper method split out from add(E) to keep method |
778 |
> |
* bytecode size under 35 (the -XX:MaxInlineSize default value), |
779 |
> |
* which helps when add(E) is called in a C1-compiled loop. |
780 |
> |
*/ |
781 |
> |
private void add(E e, Object[] elementData, int s) { |
782 |
> |
if (s == elementData.length) |
783 |
> |
elementData = grow(); |
784 |
> |
elementData[s] = e; |
785 |
> |
elementCount = s + 1; |
786 |
> |
// checkInvariants(); |
787 |
|
} |
788 |
|
|
789 |
|
/** |
794 |
|
* @since 1.2 |
795 |
|
*/ |
796 |
|
public synchronized boolean add(E e) { |
797 |
< |
modCount++; |
798 |
< |
ensureCapacityHelper(elementCount + 1); |
729 |
< |
elementData[elementCount++] = e; |
797 |
> |
modCount++; |
798 |
> |
add(e, elementData, elementCount); |
799 |
|
return true; |
800 |
|
} |
801 |
|
|
803 |
|
* Removes the first occurrence of the specified element in this Vector |
804 |
|
* If the Vector does not contain the element, it is unchanged. More |
805 |
|
* formally, removes the element with the lowest index i such that |
806 |
< |
* {@code (o==null ? get(i)==null : o.equals(get(i)))} (if such |
806 |
> |
* {@code Objects.equals(o, get(i))} (if such |
807 |
|
* an element exists). |
808 |
|
* |
809 |
|
* @param o element to be removed from this Vector, if present |
834 |
|
* Shifts any subsequent elements to the left (subtracts one from their |
835 |
|
* indices). Returns the element that was removed from the Vector. |
836 |
|
* |
768 |
– |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
769 |
– |
* ({@code index < 0 || index >= size()}) |
837 |
|
* @param index the index of the element to be removed |
838 |
|
* @return element that was removed |
839 |
+ |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
840 |
+ |
* ({@code index < 0 || index >= size()}) |
841 |
|
* @since 1.2 |
842 |
|
*/ |
843 |
|
public synchronized E remove(int index) { |
844 |
< |
modCount++; |
845 |
< |
if (index >= elementCount) |
846 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
847 |
< |
Object oldValue = elementData[index]; |
779 |
< |
|
780 |
< |
int numMoved = elementCount - index - 1; |
781 |
< |
if (numMoved > 0) |
782 |
< |
System.arraycopy(elementData, index+1, elementData, index, |
783 |
< |
numMoved); |
784 |
< |
elementData[--elementCount] = null; // Let gc do its work |
844 |
> |
modCount++; |
845 |
> |
if (index >= elementCount) |
846 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
847 |
> |
E oldValue = elementData(index); |
848 |
|
|
849 |
< |
return (E)oldValue; |
849 |
> |
int numMoved = elementCount - index - 1; |
850 |
> |
if (numMoved > 0) |
851 |
> |
System.arraycopy(elementData, index+1, elementData, index, |
852 |
> |
numMoved); |
853 |
> |
elementData[--elementCount] = null; // Let gc do its work |
854 |
> |
|
855 |
> |
// checkInvariants(); |
856 |
> |
return oldValue; |
857 |
|
} |
858 |
|
|
859 |
|
/** |
875 |
|
* @param c a collection whose elements will be tested for containment |
876 |
|
* in this Vector |
877 |
|
* @return true if this Vector contains all of the elements in the |
878 |
< |
* specified collection |
878 |
> |
* specified collection |
879 |
|
* @throws NullPointerException if the specified collection is null |
880 |
|
*/ |
881 |
|
public synchronized boolean containsAll(Collection<?> c) { |
895 |
|
* @throws NullPointerException if the specified collection is null |
896 |
|
* @since 1.2 |
897 |
|
*/ |
898 |
< |
public synchronized boolean addAll(Collection<? extends E> c) { |
829 |
< |
modCount++; |
898 |
> |
public boolean addAll(Collection<? extends E> c) { |
899 |
|
Object[] a = c.toArray(); |
900 |
+ |
modCount++; |
901 |
|
int numNew = a.length; |
902 |
< |
ensureCapacityHelper(elementCount + numNew); |
903 |
< |
System.arraycopy(a, 0, elementData, elementCount, numNew); |
904 |
< |
elementCount += numNew; |
905 |
< |
return numNew != 0; |
902 |
> |
if (numNew == 0) |
903 |
> |
return false; |
904 |
> |
synchronized (this) { |
905 |
> |
Object[] elementData = this.elementData; |
906 |
> |
final int s = elementCount; |
907 |
> |
if (numNew > elementData.length - s) |
908 |
> |
elementData = grow(s + numNew); |
909 |
> |
System.arraycopy(a, 0, elementData, s, numNew); |
910 |
> |
elementCount = s + numNew; |
911 |
> |
// checkInvariants(); |
912 |
> |
return true; |
913 |
> |
} |
914 |
|
} |
915 |
|
|
916 |
|
/** |
921 |
|
* @return true if this Vector changed as a result of the call |
922 |
|
* @throws ClassCastException if the types of one or more elements |
923 |
|
* in this vector are incompatible with the specified |
924 |
< |
* collection (optional) |
924 |
> |
* collection |
925 |
> |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
926 |
|
* @throws NullPointerException if this vector contains one or more null |
927 |
|
* elements and the specified collection does not support null |
928 |
< |
* elements (optional), or if the specified collection is null |
928 |
> |
* elements |
929 |
> |
* (<a href="Collection.html#optional-restrictions">optional</a>), |
930 |
> |
* or if the specified collection is null |
931 |
|
* @since 1.2 |
932 |
|
*/ |
933 |
< |
public synchronized boolean removeAll(Collection<?> c) { |
934 |
< |
return super.removeAll(c); |
933 |
> |
public boolean removeAll(Collection<?> c) { |
934 |
> |
Objects.requireNonNull(c); |
935 |
> |
return bulkRemove(e -> c.contains(e)); |
936 |
|
} |
937 |
|
|
938 |
|
/** |
945 |
|
* @return true if this Vector changed as a result of the call |
946 |
|
* @throws ClassCastException if the types of one or more elements |
947 |
|
* in this vector are incompatible with the specified |
948 |
< |
* collection (optional) |
948 |
> |
* collection |
949 |
> |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
950 |
|
* @throws NullPointerException if this vector contains one or more null |
951 |
|
* elements and the specified collection does not support null |
952 |
< |
* elements (optional), or if the specified collection is null |
952 |
> |
* elements |
953 |
> |
* (<a href="Collection.html#optional-restrictions">optional</a>), |
954 |
> |
* or if the specified collection is null |
955 |
|
* @since 1.2 |
956 |
|
*/ |
957 |
< |
public synchronized boolean retainAll(Collection<?> c) { |
958 |
< |
return super.retainAll(c); |
957 |
> |
public boolean retainAll(Collection<?> c) { |
958 |
> |
Objects.requireNonNull(c); |
959 |
> |
return bulkRemove(e -> !c.contains(e)); |
960 |
> |
} |
961 |
> |
|
962 |
> |
/** |
963 |
> |
* @throws NullPointerException {@inheritDoc} |
964 |
> |
*/ |
965 |
> |
@Override |
966 |
> |
public boolean removeIf(Predicate<? super E> filter) { |
967 |
> |
Objects.requireNonNull(filter); |
968 |
> |
return bulkRemove(filter); |
969 |
> |
} |
970 |
> |
|
971 |
> |
// A tiny bit set implementation |
972 |
> |
|
973 |
> |
private static long[] nBits(int n) { |
974 |
> |
return new long[((n - 1) >> 6) + 1]; |
975 |
> |
} |
976 |
> |
private static void setBit(long[] bits, int i) { |
977 |
> |
bits[i >> 6] |= 1L << i; |
978 |
> |
} |
979 |
> |
private static boolean isClear(long[] bits, int i) { |
980 |
> |
return (bits[i >> 6] & (1L << i)) == 0; |
981 |
> |
} |
982 |
> |
|
983 |
> |
private synchronized boolean bulkRemove(Predicate<? super E> filter) { |
984 |
> |
int expectedModCount = modCount; |
985 |
> |
final Object[] es = elementData; |
986 |
> |
final int end = elementCount; |
987 |
> |
int i; |
988 |
> |
// Optimize for initial run of survivors |
989 |
> |
for (i = 0; i < end && !filter.test(elementAt(es, i)); i++) |
990 |
> |
; |
991 |
> |
// Tolerate predicates that reentrantly access the collection for |
992 |
> |
// read (but writers still get CME), so traverse once to find |
993 |
> |
// elements to delete, a second pass to physically expunge. |
994 |
> |
if (i < end) { |
995 |
> |
final int beg = i; |
996 |
> |
final long[] deathRow = nBits(end - beg); |
997 |
> |
deathRow[0] = 1L; // set bit 0 |
998 |
> |
for (i = beg + 1; i < end; i++) |
999 |
> |
if (filter.test(elementAt(es, i))) |
1000 |
> |
setBit(deathRow, i - beg); |
1001 |
> |
if (modCount != expectedModCount) |
1002 |
> |
throw new ConcurrentModificationException(); |
1003 |
> |
modCount++; |
1004 |
> |
int w = beg; |
1005 |
> |
for (i = beg; i < end; i++) |
1006 |
> |
if (isClear(deathRow, i - beg)) |
1007 |
> |
es[w++] = es[i]; |
1008 |
> |
for (i = elementCount = w; i < end; i++) |
1009 |
> |
es[i] = null; |
1010 |
> |
// checkInvariants(); |
1011 |
> |
return true; |
1012 |
> |
} else { |
1013 |
> |
if (modCount != expectedModCount) |
1014 |
> |
throw new ConcurrentModificationException(); |
1015 |
> |
// checkInvariants(); |
1016 |
> |
return false; |
1017 |
> |
} |
1018 |
|
} |
1019 |
|
|
1020 |
|
/** |
1035 |
|
* @since 1.2 |
1036 |
|
*/ |
1037 |
|
public synchronized boolean addAll(int index, Collection<? extends E> c) { |
1038 |
< |
modCount++; |
1039 |
< |
if (index < 0 || index > elementCount) |
896 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
1038 |
> |
if (index < 0 || index > elementCount) |
1039 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
1040 |
|
|
1041 |
|
Object[] a = c.toArray(); |
1042 |
< |
int numNew = a.length; |
1043 |
< |
ensureCapacityHelper(elementCount + numNew); |
1044 |
< |
|
1045 |
< |
int numMoved = elementCount - index; |
1046 |
< |
if (numMoved > 0) |
1047 |
< |
System.arraycopy(elementData, index, elementData, index + numNew, |
1048 |
< |
numMoved); |
1049 |
< |
|
1042 |
> |
modCount++; |
1043 |
> |
int numNew = a.length; |
1044 |
> |
if (numNew == 0) |
1045 |
> |
return false; |
1046 |
> |
Object[] elementData = this.elementData; |
1047 |
> |
final int s = elementCount; |
1048 |
> |
if (numNew > elementData.length - s) |
1049 |
> |
elementData = grow(s + numNew); |
1050 |
> |
|
1051 |
> |
int numMoved = s - index; |
1052 |
> |
if (numMoved > 0) |
1053 |
> |
System.arraycopy(elementData, index, |
1054 |
> |
elementData, index + numNew, |
1055 |
> |
numMoved); |
1056 |
|
System.arraycopy(a, 0, elementData, index, numNew); |
1057 |
< |
elementCount += numNew; |
1058 |
< |
return numNew != 0; |
1057 |
> |
elementCount = s + numNew; |
1058 |
> |
// checkInvariants(); |
1059 |
> |
return true; |
1060 |
|
} |
1061 |
|
|
1062 |
|
/** |
1064 |
|
* true if and only if the specified Object is also a List, both Lists |
1065 |
|
* have the same size, and all corresponding pairs of elements in the two |
1066 |
|
* Lists are <em>equal</em>. (Two elements {@code e1} and |
1067 |
< |
* {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null : |
1068 |
< |
* e1.equals(e2))}.) In other words, two Lists are defined to be |
1067 |
> |
* {@code e2} are <em>equal</em> if {@code Objects.equals(e1, e2)}.) |
1068 |
> |
* In other words, two Lists are defined to be |
1069 |
|
* equal if they contain the same elements in the same order. |
1070 |
|
* |
1071 |
|
* @param o the Object to be compared for equality with this Vector |
1091 |
|
} |
1092 |
|
|
1093 |
|
/** |
944 |
– |
* Removes from this List all of the elements whose index is between |
945 |
– |
* fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding |
946 |
– |
* elements to the left (reduces their index). |
947 |
– |
* This call shortens the Vector by (toIndex - fromIndex) elements. (If |
948 |
– |
* toIndex==fromIndex, this operation has no effect.) |
949 |
– |
* |
950 |
– |
* @param fromIndex index of first element to be removed |
951 |
– |
* @param toIndex index after last element to be removed |
952 |
– |
*/ |
953 |
– |
protected synchronized void removeRange(int fromIndex, int toIndex) { |
954 |
– |
modCount++; |
955 |
– |
int numMoved = elementCount - toIndex; |
956 |
– |
System.arraycopy(elementData, toIndex, elementData, fromIndex, |
957 |
– |
numMoved); |
958 |
– |
|
959 |
– |
// Let gc do its work |
960 |
– |
int newElementCount = elementCount - (toIndex-fromIndex); |
961 |
– |
while (elementCount != newElementCount) |
962 |
– |
elementData[--elementCount] = null; |
963 |
– |
} |
964 |
– |
|
965 |
– |
/** |
966 |
– |
* Save the state of the {@code Vector} instance to a stream (that |
967 |
– |
* is, serialize it). This method is present merely for synchronization. |
968 |
– |
* It just calls the default writeObject method. |
969 |
– |
*/ |
970 |
– |
private synchronized void writeObject(java.io.ObjectOutputStream s) |
971 |
– |
throws java.io.IOException |
972 |
– |
{ |
973 |
– |
s.defaultWriteObject(); |
974 |
– |
} |
975 |
– |
|
976 |
– |
/** |
977 |
– |
* Returns a list-iterator of the elements in this list (in proper |
978 |
– |
* sequence), starting at the specified position in the list. |
979 |
– |
* Obeys the general contract of {@link List#listIterator(int)}. |
980 |
– |
* |
981 |
– |
* <p>The list-iterator is <i>fail-fast</i>: if the list is structurally |
982 |
– |
* modified at any time after the Iterator is created, in any way except |
983 |
– |
* through the list-iterator's own {@code remove} or {@code add} |
984 |
– |
* methods, the list-iterator will throw a |
985 |
– |
* {@code ConcurrentModificationException}. Thus, in the face of |
986 |
– |
* concurrent modification, the iterator fails quickly and cleanly, rather |
987 |
– |
* than risking arbitrary, non-deterministic behavior at an undetermined |
988 |
– |
* time in the future. |
989 |
– |
* |
990 |
– |
* @param index index of the first element to be returned from the |
991 |
– |
* list-iterator (by a call to {@link ListIterator#next}) |
992 |
– |
* @return a list-iterator of the elements in this list (in proper |
993 |
– |
* sequence), starting at the specified position in the list |
994 |
– |
* @throws IndexOutOfBoundsException {@inheritDoc} |
995 |
– |
*/ |
996 |
– |
public synchronized ListIterator<E> listIterator(int index) { |
997 |
– |
if (index < 0 || index > elementCount) |
998 |
– |
throw new IndexOutOfBoundsException("Index: "+index); |
999 |
– |
return new VectorIterator(index, elementCount); |
1000 |
– |
} |
1001 |
– |
|
1002 |
– |
/** |
1003 |
– |
* {@inheritDoc} |
1004 |
– |
*/ |
1005 |
– |
public synchronized ListIterator<E> listIterator() { |
1006 |
– |
return new VectorIterator(0, elementCount); |
1007 |
– |
} |
1008 |
– |
|
1009 |
– |
/** |
1010 |
– |
* Returns an iterator over the elements in this list in proper sequence. |
1011 |
– |
* |
1012 |
– |
* @return an iterator over the elements in this list in proper sequence |
1013 |
– |
*/ |
1014 |
– |
public synchronized Iterator<E> iterator() { |
1015 |
– |
return new VectorIterator(0, elementCount); |
1016 |
– |
} |
1017 |
– |
|
1018 |
– |
/** |
1019 |
– |
* Helper method to access array elements under synchronization by |
1020 |
– |
* iterators. The caller performs index check with respect to |
1021 |
– |
* expected bounds, so errors accessing the element are reported |
1022 |
– |
* as ConcurrentModificationExceptions. |
1023 |
– |
*/ |
1024 |
– |
final synchronized Object iteratorGet(int index, int expectedModCount) { |
1025 |
– |
if (modCount == expectedModCount) { |
1026 |
– |
try { |
1027 |
– |
return elementData[index]; |
1028 |
– |
} catch(IndexOutOfBoundsException fallThrough) { |
1029 |
– |
} |
1030 |
– |
} |
1031 |
– |
throw new ConcurrentModificationException(); |
1032 |
– |
} |
1033 |
– |
|
1034 |
– |
/** |
1035 |
– |
* Streamlined specialization of AbstractList version of iterator. |
1036 |
– |
* Locally performs bounds checks, but relies on outer Vector |
1037 |
– |
* to access elements under synchronization. |
1038 |
– |
*/ |
1039 |
– |
private final class VectorIterator implements ListIterator<E> { |
1040 |
– |
int cursor; // Index of next element to return; |
1041 |
– |
int fence; // Upper bound on cursor (cache of size()) |
1042 |
– |
int lastRet; // Index of last element, or -1 if no such |
1043 |
– |
int expectedModCount; // To check for CME |
1044 |
– |
|
1045 |
– |
VectorIterator(int index, int fence) { |
1046 |
– |
this.cursor = index; |
1047 |
– |
this.fence = fence; |
1048 |
– |
this.lastRet = -1; |
1049 |
– |
this.expectedModCount = Vector.this.modCount; |
1050 |
– |
} |
1051 |
– |
|
1052 |
– |
public boolean hasNext() { |
1053 |
– |
return cursor < fence; |
1054 |
– |
} |
1055 |
– |
|
1056 |
– |
public boolean hasPrevious() { |
1057 |
– |
return cursor > 0; |
1058 |
– |
} |
1059 |
– |
|
1060 |
– |
public int nextIndex() { |
1061 |
– |
return cursor; |
1062 |
– |
} |
1063 |
– |
|
1064 |
– |
public int previousIndex() { |
1065 |
– |
return cursor - 1; |
1066 |
– |
} |
1067 |
– |
|
1068 |
– |
public E next() { |
1069 |
– |
int i = cursor; |
1070 |
– |
if (i >= fence) |
1071 |
– |
throw new NoSuchElementException(); |
1072 |
– |
Object next = Vector.this.iteratorGet(i, expectedModCount); |
1073 |
– |
lastRet = i; |
1074 |
– |
cursor = i + 1; |
1075 |
– |
return (E)next; |
1076 |
– |
} |
1077 |
– |
|
1078 |
– |
public E previous() { |
1079 |
– |
int i = cursor - 1; |
1080 |
– |
if (i < 0) |
1081 |
– |
throw new NoSuchElementException(); |
1082 |
– |
Object prev = Vector.this.iteratorGet(i, expectedModCount); |
1083 |
– |
lastRet = i; |
1084 |
– |
cursor = i; |
1085 |
– |
return (E)prev; |
1086 |
– |
} |
1087 |
– |
|
1088 |
– |
public void set(E e) { |
1089 |
– |
if (lastRet < 0) |
1090 |
– |
throw new IllegalStateException(); |
1091 |
– |
if (Vector.this.modCount != expectedModCount) |
1092 |
– |
throw new ConcurrentModificationException(); |
1093 |
– |
try { |
1094 |
– |
Vector.this.set(lastRet, e); |
1095 |
– |
expectedModCount = Vector.this.modCount; |
1096 |
– |
} catch (IndexOutOfBoundsException ex) { |
1097 |
– |
throw new ConcurrentModificationException(); |
1098 |
– |
} |
1099 |
– |
} |
1100 |
– |
|
1101 |
– |
public void remove() { |
1102 |
– |
int i = lastRet; |
1103 |
– |
if (i < 0) |
1104 |
– |
throw new IllegalStateException(); |
1105 |
– |
if (Vector.this.modCount != expectedModCount) |
1106 |
– |
throw new ConcurrentModificationException(); |
1107 |
– |
try { |
1108 |
– |
Vector.this.remove(i); |
1109 |
– |
if (i < cursor) |
1110 |
– |
cursor--; |
1111 |
– |
lastRet = -1; |
1112 |
– |
fence = Vector.this.size(); |
1113 |
– |
expectedModCount = Vector.this.modCount; |
1114 |
– |
} catch (IndexOutOfBoundsException ex) { |
1115 |
– |
throw new ConcurrentModificationException(); |
1116 |
– |
} |
1117 |
– |
} |
1118 |
– |
|
1119 |
– |
public void add(E e) { |
1120 |
– |
if (Vector.this.modCount != expectedModCount) |
1121 |
– |
throw new ConcurrentModificationException(); |
1122 |
– |
try { |
1123 |
– |
int i = cursor; |
1124 |
– |
Vector.this.add(i, e); |
1125 |
– |
cursor = i + 1; |
1126 |
– |
lastRet = -1; |
1127 |
– |
fence = Vector.this.size(); |
1128 |
– |
expectedModCount = Vector.this.modCount; |
1129 |
– |
} catch (IndexOutOfBoundsException ex) { |
1130 |
– |
throw new ConcurrentModificationException(); |
1131 |
– |
} |
1132 |
– |
} |
1133 |
– |
} |
1134 |
– |
|
1135 |
– |
/** |
1094 |
|
* Returns a view of the portion of this List between fromIndex, |
1095 |
|
* inclusive, and toIndex, exclusive. (If fromIndex and toIndex are |
1096 |
|
* equal, the returned List is empty.) The returned List is backed by this |
1099 |
|
* operations supported by this List. |
1100 |
|
* |
1101 |
|
* <p>This method eliminates the need for explicit range operations (of |
1102 |
< |
* the sort that commonly exist for arrays). Any operation that expects |
1102 |
> |
* the sort that commonly exist for arrays). Any operation that expects |
1103 |
|
* a List can be used as a range operation by operating on a subList view |
1104 |
|
* instead of a whole List. For example, the following idiom |
1105 |
|
* removes a range of elements from a List: |
1106 |
|
* <pre> |
1107 |
< |
* list.subList(from, to).clear(); |
1107 |
> |
* list.subList(from, to).clear(); |
1108 |
|
* </pre> |
1109 |
|
* Similar idioms may be constructed for indexOf and lastIndexOf, |
1110 |
|
* and all of the algorithms in the Collections class can be applied to |
1122 |
|
* @throws IndexOutOfBoundsException if an endpoint index value is out of range |
1123 |
|
* {@code (fromIndex < 0 || toIndex > size)} |
1124 |
|
* @throws IllegalArgumentException if the endpoint indices are out of order |
1125 |
< |
* {@code (fromIndex > toIndex)} |
1125 |
> |
* {@code (fromIndex > toIndex)} |
1126 |
|
*/ |
1127 |
|
public synchronized List<E> subList(int fromIndex, int toIndex) { |
1128 |
< |
return new VectorSubList(this, this, fromIndex, fromIndex, toIndex); |
1128 |
> |
return Collections.synchronizedList(super.subList(fromIndex, toIndex), |
1129 |
> |
this); |
1130 |
|
} |
1131 |
|
|
1132 |
|
/** |
1133 |
< |
* This class specializes the AbstractList version of SubList to |
1134 |
< |
* avoid the double-indirection penalty that would arise using a |
1135 |
< |
* synchronized wrapper, as well as to avoid some unnecessary |
1136 |
< |
* checks in sublist iterators. |
1137 |
< |
*/ |
1138 |
< |
private static final class VectorSubList<E> extends AbstractList<E> implements RandomAccess { |
1139 |
< |
final Vector<E> base; // base list |
1140 |
< |
final AbstractList<E> parent; // Creating list |
1141 |
< |
final int baseOffset; // index wrt Vector |
1142 |
< |
final int parentOffset; // index wrt parent |
1143 |
< |
int length; // length of sublist |
1185 |
< |
|
1186 |
< |
VectorSubList(Vector<E> base, AbstractList<E> parent, int baseOffset, |
1187 |
< |
int fromIndex, int toIndex) { |
1188 |
< |
if (fromIndex < 0) |
1189 |
< |
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); |
1190 |
< |
if (toIndex > parent.size()) |
1191 |
< |
throw new IndexOutOfBoundsException("toIndex = " + toIndex); |
1192 |
< |
if (fromIndex > toIndex) |
1193 |
< |
throw new IllegalArgumentException("fromIndex(" + fromIndex + |
1194 |
< |
") > toIndex(" + toIndex + ")"); |
1195 |
< |
|
1196 |
< |
this.base = base; |
1197 |
< |
this.parent = parent; |
1198 |
< |
this.baseOffset = baseOffset; |
1199 |
< |
this.parentOffset = fromIndex; |
1200 |
< |
this.length = toIndex - fromIndex; |
1201 |
< |
modCount = base.modCount; |
1202 |
< |
} |
1203 |
< |
|
1204 |
< |
/** |
1205 |
< |
* Returns an IndexOutOfBoundsException with nicer message |
1206 |
< |
*/ |
1207 |
< |
private IndexOutOfBoundsException indexError(int index) { |
1208 |
< |
return new IndexOutOfBoundsException("Index: " + index + |
1209 |
< |
", Size: " + length); |
1210 |
< |
} |
1211 |
< |
|
1212 |
< |
public E set(int index, E element) { |
1213 |
< |
synchronized(base) { |
1214 |
< |
if (index < 0 || index >= length) |
1215 |
< |
throw indexError(index); |
1216 |
< |
if (base.modCount != modCount) |
1217 |
< |
throw new ConcurrentModificationException(); |
1218 |
< |
return base.set(index + baseOffset, element); |
1219 |
< |
} |
1220 |
< |
} |
1133 |
> |
* Removes from this list all of the elements whose index is between |
1134 |
> |
* {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. |
1135 |
> |
* Shifts any succeeding elements to the left (reduces their index). |
1136 |
> |
* This call shortens the list by {@code (toIndex - fromIndex)} elements. |
1137 |
> |
* (If {@code toIndex==fromIndex}, this operation has no effect.) |
1138 |
> |
*/ |
1139 |
> |
protected synchronized void removeRange(int fromIndex, int toIndex) { |
1140 |
> |
modCount++; |
1141 |
> |
shiftTailOverGap(elementData, fromIndex, toIndex); |
1142 |
> |
// checkInvariants(); |
1143 |
> |
} |
1144 |
|
|
1145 |
< |
public E get(int index) { |
1146 |
< |
synchronized(base) { |
1147 |
< |
if (index < 0 || index >= length) |
1148 |
< |
throw indexError(index); |
1149 |
< |
if (base.modCount != modCount) |
1150 |
< |
throw new ConcurrentModificationException(); |
1228 |
< |
return base.get(index + baseOffset); |
1229 |
< |
} |
1230 |
< |
} |
1145 |
> |
/** Erases the gap from lo to hi, by sliding down following elements. */ |
1146 |
> |
private void shiftTailOverGap(Object[] es, int lo, int hi) { |
1147 |
> |
System.arraycopy(es, hi, es, lo, elementCount - hi); |
1148 |
> |
for (int to = elementCount, i = (elementCount -= hi - lo); i < to; i++) |
1149 |
> |
es[i] = null; |
1150 |
> |
} |
1151 |
|
|
1152 |
< |
public int size() { |
1153 |
< |
synchronized(base) { |
1154 |
< |
if (base.modCount != modCount) |
1155 |
< |
throw new ConcurrentModificationException(); |
1156 |
< |
return length; |
1157 |
< |
} |
1152 |
> |
/** |
1153 |
> |
* Loads a {@code Vector} instance from a stream |
1154 |
> |
* (that is, deserializes it). |
1155 |
> |
* This method performs checks to ensure the consistency |
1156 |
> |
* of the fields. |
1157 |
> |
* |
1158 |
> |
* @param in the stream |
1159 |
> |
* @throws java.io.IOException if an I/O error occurs |
1160 |
> |
* @throws ClassNotFoundException if the stream contains data |
1161 |
> |
* of a non-existing class |
1162 |
> |
*/ |
1163 |
> |
// OPENJDK @java.io.Serial |
1164 |
> |
private void readObject(ObjectInputStream in) |
1165 |
> |
throws IOException, ClassNotFoundException { |
1166 |
> |
ObjectInputStream.GetField gfields = in.readFields(); |
1167 |
> |
int count = gfields.get("elementCount", 0); |
1168 |
> |
Object[] data = (Object[])gfields.get("elementData", null); |
1169 |
> |
if (count < 0 || data == null || count > data.length) { |
1170 |
> |
throw new StreamCorruptedException("Inconsistent vector internals"); |
1171 |
|
} |
1172 |
+ |
elementCount = count; |
1173 |
+ |
elementData = data.clone(); |
1174 |
+ |
} |
1175 |
|
|
1176 |
< |
public void add(int index, E element) { |
1177 |
< |
synchronized(base) { |
1178 |
< |
if (index < 0 || index > length) |
1179 |
< |
throw indexError(index); |
1180 |
< |
if (base.modCount != modCount) |
1181 |
< |
throw new ConcurrentModificationException(); |
1182 |
< |
parent.add(index + parentOffset, element); |
1183 |
< |
length++; |
1184 |
< |
modCount = base.modCount; |
1185 |
< |
} |
1176 |
> |
/** |
1177 |
> |
* Saves the state of the {@code Vector} instance to a stream |
1178 |
> |
* (that is, serializes it). |
1179 |
> |
* This method performs synchronization to ensure the consistency |
1180 |
> |
* of the serialized data. |
1181 |
> |
* |
1182 |
> |
* @param s the stream |
1183 |
> |
* @throws java.io.IOException if an I/O error occurs |
1184 |
> |
*/ |
1185 |
> |
// OPENJDK @java.io.Serial |
1186 |
> |
private void writeObject(java.io.ObjectOutputStream s) |
1187 |
> |
throws java.io.IOException { |
1188 |
> |
final java.io.ObjectOutputStream.PutField fields = s.putFields(); |
1189 |
> |
final Object[] data; |
1190 |
> |
synchronized (this) { |
1191 |
> |
fields.put("capacityIncrement", capacityIncrement); |
1192 |
> |
fields.put("elementCount", elementCount); |
1193 |
> |
data = elementData.clone(); |
1194 |
|
} |
1195 |
+ |
fields.put("elementData", data); |
1196 |
+ |
s.writeFields(); |
1197 |
+ |
} |
1198 |
|
|
1199 |
< |
public E remove(int index) { |
1200 |
< |
synchronized(base) { |
1201 |
< |
if (index < 0 || index >= length) |
1202 |
< |
throw indexError(index); |
1203 |
< |
if (base.modCount != modCount) |
1204 |
< |
throw new ConcurrentModificationException(); |
1205 |
< |
E result = parent.remove(index + parentOffset); |
1206 |
< |
length--; |
1207 |
< |
modCount = base.modCount; |
1208 |
< |
return result; |
1209 |
< |
} |
1199 |
> |
/** |
1200 |
> |
* Returns a list iterator over the elements in this list (in proper |
1201 |
> |
* sequence), starting at the specified position in the list. |
1202 |
> |
* The specified index indicates the first element that would be |
1203 |
> |
* returned by an initial call to {@link ListIterator#next next}. |
1204 |
> |
* An initial call to {@link ListIterator#previous previous} would |
1205 |
> |
* return the element with the specified index minus one. |
1206 |
> |
* |
1207 |
> |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1208 |
> |
* |
1209 |
> |
* @throws IndexOutOfBoundsException {@inheritDoc} |
1210 |
> |
*/ |
1211 |
> |
public synchronized ListIterator<E> listIterator(int index) { |
1212 |
> |
if (index < 0 || index > elementCount) |
1213 |
> |
throw new IndexOutOfBoundsException("Index: "+index); |
1214 |
> |
return new ListItr(index); |
1215 |
> |
} |
1216 |
> |
|
1217 |
> |
/** |
1218 |
> |
* Returns a list iterator over the elements in this list (in proper |
1219 |
> |
* sequence). |
1220 |
> |
* |
1221 |
> |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1222 |
> |
* |
1223 |
> |
* @see #listIterator(int) |
1224 |
> |
*/ |
1225 |
> |
public synchronized ListIterator<E> listIterator() { |
1226 |
> |
return new ListItr(0); |
1227 |
> |
} |
1228 |
> |
|
1229 |
> |
/** |
1230 |
> |
* Returns an iterator over the elements in this list in proper sequence. |
1231 |
> |
* |
1232 |
> |
* <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1233 |
> |
* |
1234 |
> |
* @return an iterator over the elements in this list in proper sequence |
1235 |
> |
*/ |
1236 |
> |
public synchronized Iterator<E> iterator() { |
1237 |
> |
return new Itr(); |
1238 |
> |
} |
1239 |
> |
|
1240 |
> |
/** |
1241 |
> |
* An optimized version of AbstractList.Itr |
1242 |
> |
*/ |
1243 |
> |
private class Itr implements Iterator<E> { |
1244 |
> |
int cursor; // index of next element to return |
1245 |
> |
int lastRet = -1; // index of last element returned; -1 if no such |
1246 |
> |
int expectedModCount = modCount; |
1247 |
> |
|
1248 |
> |
public boolean hasNext() { |
1249 |
> |
// Racy but within spec, since modifications are checked |
1250 |
> |
// within or after synchronization in next/previous |
1251 |
> |
return cursor != elementCount; |
1252 |
|
} |
1253 |
|
|
1254 |
< |
protected void removeRange(int fromIndex, int toIndex) { |
1255 |
< |
synchronized(base) { |
1256 |
< |
if (base.modCount != modCount) |
1257 |
< |
throw new ConcurrentModificationException(); |
1258 |
< |
parent.removeRange(fromIndex + parentOffset, |
1259 |
< |
toIndex + parentOffset); |
1260 |
< |
length -= (toIndex-fromIndex); |
1261 |
< |
modCount = base.modCount; |
1254 |
> |
public E next() { |
1255 |
> |
synchronized (Vector.this) { |
1256 |
> |
checkForComodification(); |
1257 |
> |
int i = cursor; |
1258 |
> |
if (i >= elementCount) |
1259 |
> |
throw new NoSuchElementException(); |
1260 |
> |
cursor = i + 1; |
1261 |
> |
return elementData(lastRet = i); |
1262 |
|
} |
1263 |
|
} |
1264 |
|
|
1265 |
< |
public boolean addAll(Collection<? extends E> c) { |
1266 |
< |
return addAll(length, c); |
1265 |
> |
public void remove() { |
1266 |
> |
if (lastRet == -1) |
1267 |
> |
throw new IllegalStateException(); |
1268 |
> |
synchronized (Vector.this) { |
1269 |
> |
checkForComodification(); |
1270 |
> |
Vector.this.remove(lastRet); |
1271 |
> |
expectedModCount = modCount; |
1272 |
> |
} |
1273 |
> |
cursor = lastRet; |
1274 |
> |
lastRet = -1; |
1275 |
|
} |
1276 |
|
|
1277 |
< |
public boolean addAll(int index, Collection<? extends E> c) { |
1278 |
< |
synchronized(base) { |
1279 |
< |
if (index < 0 || index > length) |
1280 |
< |
throw indexError(index); |
1281 |
< |
int cSize = c.size(); |
1282 |
< |
if (cSize==0) |
1283 |
< |
return false; |
1284 |
< |
|
1285 |
< |
if (base.modCount != modCount) |
1277 |
> |
@Override |
1278 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
1279 |
> |
Objects.requireNonNull(action); |
1280 |
> |
synchronized (Vector.this) { |
1281 |
> |
final int size = elementCount; |
1282 |
> |
int i = cursor; |
1283 |
> |
if (i >= size) { |
1284 |
> |
return; |
1285 |
> |
} |
1286 |
> |
final Object[] es = elementData; |
1287 |
> |
if (i >= es.length) |
1288 |
|
throw new ConcurrentModificationException(); |
1289 |
< |
parent.addAll(parentOffset + index, c); |
1290 |
< |
modCount = base.modCount; |
1291 |
< |
length += cSize; |
1292 |
< |
return true; |
1289 |
> |
while (i < size && modCount == expectedModCount) |
1290 |
> |
action.accept(elementAt(es, i++)); |
1291 |
> |
// update once at end of iteration to reduce heap write traffic |
1292 |
> |
cursor = i; |
1293 |
> |
lastRet = i - 1; |
1294 |
> |
checkForComodification(); |
1295 |
|
} |
1296 |
|
} |
1297 |
|
|
1298 |
< |
public boolean equals(Object o) { |
1299 |
< |
synchronized(base) {return super.equals(o);} |
1298 |
> |
final void checkForComodification() { |
1299 |
> |
if (modCount != expectedModCount) |
1300 |
> |
throw new ConcurrentModificationException(); |
1301 |
|
} |
1302 |
+ |
} |
1303 |
|
|
1304 |
< |
public int hashCode() { |
1305 |
< |
synchronized(base) {return super.hashCode();} |
1304 |
> |
/** |
1305 |
> |
* An optimized version of AbstractList.ListItr |
1306 |
> |
*/ |
1307 |
> |
final class ListItr extends Itr implements ListIterator<E> { |
1308 |
> |
ListItr(int index) { |
1309 |
> |
super(); |
1310 |
> |
cursor = index; |
1311 |
|
} |
1312 |
|
|
1313 |
< |
public int indexOf(Object o) { |
1314 |
< |
synchronized(base) {return super.indexOf(o);} |
1313 |
> |
public boolean hasPrevious() { |
1314 |
> |
return cursor != 0; |
1315 |
|
} |
1316 |
|
|
1317 |
< |
public int lastIndexOf(Object o) { |
1318 |
< |
synchronized(base) {return super.lastIndexOf(o);} |
1317 |
> |
public int nextIndex() { |
1318 |
> |
return cursor; |
1319 |
|
} |
1320 |
|
|
1321 |
< |
public List<E> subList(int fromIndex, int toIndex) { |
1322 |
< |
return new VectorSubList(base, this, fromIndex + baseOffset, |
1315 |
< |
fromIndex, toIndex); |
1321 |
> |
public int previousIndex() { |
1322 |
> |
return cursor - 1; |
1323 |
|
} |
1324 |
|
|
1325 |
< |
public Iterator<E> iterator() { |
1326 |
< |
synchronized(base) { |
1327 |
< |
return new VectorSubListIterator(this, 0); |
1325 |
> |
public E previous() { |
1326 |
> |
synchronized (Vector.this) { |
1327 |
> |
checkForComodification(); |
1328 |
> |
int i = cursor - 1; |
1329 |
> |
if (i < 0) |
1330 |
> |
throw new NoSuchElementException(); |
1331 |
> |
cursor = i; |
1332 |
> |
return elementData(lastRet = i); |
1333 |
|
} |
1334 |
|
} |
1335 |
|
|
1336 |
< |
public synchronized ListIterator<E> listIterator() { |
1337 |
< |
synchronized(base) { |
1338 |
< |
return new VectorSubListIterator(this, 0); |
1336 |
> |
public void set(E e) { |
1337 |
> |
if (lastRet == -1) |
1338 |
> |
throw new IllegalStateException(); |
1339 |
> |
synchronized (Vector.this) { |
1340 |
> |
checkForComodification(); |
1341 |
> |
Vector.this.set(lastRet, e); |
1342 |
|
} |
1343 |
|
} |
1344 |
|
|
1345 |
< |
public ListIterator<E> listIterator(int index) { |
1346 |
< |
synchronized(base) { |
1347 |
< |
if (index < 0 || index > length) |
1348 |
< |
throw indexError(index); |
1349 |
< |
return new VectorSubListIterator(this, index); |
1345 |
> |
public void add(E e) { |
1346 |
> |
int i = cursor; |
1347 |
> |
synchronized (Vector.this) { |
1348 |
> |
checkForComodification(); |
1349 |
> |
Vector.this.add(i, e); |
1350 |
> |
expectedModCount = modCount; |
1351 |
|
} |
1352 |
+ |
cursor = i + 1; |
1353 |
+ |
lastRet = -1; |
1354 |
|
} |
1355 |
+ |
} |
1356 |
|
|
1357 |
< |
/** |
1358 |
< |
* Same idea as VectorIterator, except routing structural |
1359 |
< |
* change operations through the sublist. |
1360 |
< |
*/ |
1361 |
< |
private static final class VectorSubListIterator<E> implements ListIterator<E> { |
1362 |
< |
final Vector<E> base; // base list |
1363 |
< |
final VectorSubList<E> outer; // Sublist creating this iteraor |
1364 |
< |
final int offset; // cursor offset wrt base |
1365 |
< |
int cursor; // Current index |
1366 |
< |
int fence; // Upper bound on cursor |
1367 |
< |
int lastRet; // Index of returned element, or -1 |
1368 |
< |
int expectedModCount; // Expected modCount of base Vector |
1369 |
< |
|
1370 |
< |
VectorSubListIterator(VectorSubList<E> list, int index) { |
1371 |
< |
this.lastRet = -1; |
1353 |
< |
this.cursor = index; |
1354 |
< |
this.outer = list; |
1355 |
< |
this.offset = list.baseOffset; |
1356 |
< |
this.fence = list.length; |
1357 |
< |
this.base = list.base; |
1358 |
< |
this.expectedModCount = base.modCount; |
1359 |
< |
} |
1360 |
< |
|
1361 |
< |
public boolean hasNext() { |
1362 |
< |
return cursor < fence; |
1363 |
< |
} |
1357 |
> |
/** |
1358 |
> |
* @throws NullPointerException {@inheritDoc} |
1359 |
> |
*/ |
1360 |
> |
@Override |
1361 |
> |
public synchronized void forEach(Consumer<? super E> action) { |
1362 |
> |
Objects.requireNonNull(action); |
1363 |
> |
final int expectedModCount = modCount; |
1364 |
> |
final Object[] es = elementData; |
1365 |
> |
final int size = elementCount; |
1366 |
> |
for (int i = 0; modCount == expectedModCount && i < size; i++) |
1367 |
> |
action.accept(elementAt(es, i)); |
1368 |
> |
if (modCount != expectedModCount) |
1369 |
> |
throw new ConcurrentModificationException(); |
1370 |
> |
// checkInvariants(); |
1371 |
> |
} |
1372 |
|
|
1373 |
< |
public boolean hasPrevious() { |
1374 |
< |
return cursor > 0; |
1375 |
< |
} |
1373 |
> |
/** |
1374 |
> |
* @throws NullPointerException {@inheritDoc} |
1375 |
> |
*/ |
1376 |
> |
@Override |
1377 |
> |
public synchronized void replaceAll(UnaryOperator<E> operator) { |
1378 |
> |
Objects.requireNonNull(operator); |
1379 |
> |
final int expectedModCount = modCount; |
1380 |
> |
final Object[] es = elementData; |
1381 |
> |
final int size = elementCount; |
1382 |
> |
for (int i = 0; modCount == expectedModCount && i < size; i++) |
1383 |
> |
es[i] = operator.apply(elementAt(es, i)); |
1384 |
> |
if (modCount != expectedModCount) |
1385 |
> |
throw new ConcurrentModificationException(); |
1386 |
> |
// TODO(8203662): remove increment of modCount from ... |
1387 |
> |
modCount++; |
1388 |
> |
// checkInvariants(); |
1389 |
> |
} |
1390 |
|
|
1391 |
< |
public int nextIndex() { |
1392 |
< |
return cursor; |
1393 |
< |
} |
1391 |
> |
@SuppressWarnings("unchecked") |
1392 |
> |
@Override |
1393 |
> |
public synchronized void sort(Comparator<? super E> c) { |
1394 |
> |
final int expectedModCount = modCount; |
1395 |
> |
Arrays.sort((E[]) elementData, 0, elementCount, c); |
1396 |
> |
if (modCount != expectedModCount) |
1397 |
> |
throw new ConcurrentModificationException(); |
1398 |
> |
modCount++; |
1399 |
> |
// checkInvariants(); |
1400 |
> |
} |
1401 |
|
|
1402 |
< |
public int previousIndex() { |
1403 |
< |
return cursor - 1; |
1404 |
< |
} |
1402 |
> |
/** |
1403 |
> |
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> |
1404 |
> |
* and <em>fail-fast</em> {@link Spliterator} over the elements in this |
1405 |
> |
* list. |
1406 |
> |
* |
1407 |
> |
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED}, |
1408 |
> |
* {@link Spliterator#SUBSIZED}, and {@link Spliterator#ORDERED}. |
1409 |
> |
* Overriding implementations should document the reporting of additional |
1410 |
> |
* characteristic values. |
1411 |
> |
* |
1412 |
> |
* @return a {@code Spliterator} over the elements in this list |
1413 |
> |
* @since 1.8 |
1414 |
> |
*/ |
1415 |
> |
@Override |
1416 |
> |
public Spliterator<E> spliterator() { |
1417 |
> |
return new VectorSpliterator(null, 0, -1, 0); |
1418 |
> |
} |
1419 |
|
|
1420 |
< |
public E next() { |
1421 |
< |
int i = cursor; |
1422 |
< |
if (cursor >= fence) |
1423 |
< |
throw new NoSuchElementException(); |
1424 |
< |
Object next = base.iteratorGet(i + offset, expectedModCount); |
1425 |
< |
lastRet = i; |
1383 |
< |
cursor = i + 1; |
1384 |
< |
return (E)next; |
1385 |
< |
} |
1420 |
> |
/** Similar to ArrayList Spliterator */ |
1421 |
> |
final class VectorSpliterator implements Spliterator<E> { |
1422 |
> |
private Object[] array; |
1423 |
> |
private int index; // current index, modified on advance/split |
1424 |
> |
private int fence; // -1 until used; then one past last index |
1425 |
> |
private int expectedModCount; // initialized when fence set |
1426 |
|
|
1427 |
< |
public E previous() { |
1428 |
< |
int i = cursor - 1; |
1429 |
< |
if (i < 0) |
1430 |
< |
throw new NoSuchElementException(); |
1431 |
< |
Object prev = base.iteratorGet(i + offset, expectedModCount); |
1432 |
< |
lastRet = i; |
1433 |
< |
cursor = i; |
1434 |
< |
return (E)prev; |
1395 |
< |
} |
1427 |
> |
/** Creates new spliterator covering the given range. */ |
1428 |
> |
VectorSpliterator(Object[] array, int origin, int fence, |
1429 |
> |
int expectedModCount) { |
1430 |
> |
this.array = array; |
1431 |
> |
this.index = origin; |
1432 |
> |
this.fence = fence; |
1433 |
> |
this.expectedModCount = expectedModCount; |
1434 |
> |
} |
1435 |
|
|
1436 |
< |
public void set(E e) { |
1437 |
< |
if (lastRet < 0) |
1438 |
< |
throw new IllegalStateException(); |
1439 |
< |
if (base.modCount != expectedModCount) |
1440 |
< |
throw new ConcurrentModificationException(); |
1441 |
< |
try { |
1442 |
< |
outer.set(lastRet, e); |
1404 |
< |
expectedModCount = base.modCount; |
1405 |
< |
} catch (IndexOutOfBoundsException ex) { |
1406 |
< |
throw new ConcurrentModificationException(); |
1436 |
> |
private int getFence() { // initialize on first use |
1437 |
> |
int hi; |
1438 |
> |
if ((hi = fence) < 0) { |
1439 |
> |
synchronized (Vector.this) { |
1440 |
> |
array = elementData; |
1441 |
> |
expectedModCount = modCount; |
1442 |
> |
hi = fence = elementCount; |
1443 |
|
} |
1444 |
|
} |
1445 |
+ |
return hi; |
1446 |
+ |
} |
1447 |
|
|
1448 |
< |
public void remove() { |
1449 |
< |
int i = lastRet; |
1450 |
< |
if (i < 0) |
1451 |
< |
throw new IllegalStateException(); |
1452 |
< |
if (base.modCount != expectedModCount) |
1448 |
> |
public Spliterator<E> trySplit() { |
1449 |
> |
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1450 |
> |
return (lo >= mid) ? null : |
1451 |
> |
new VectorSpliterator(array, lo, index = mid, expectedModCount); |
1452 |
> |
} |
1453 |
> |
|
1454 |
> |
@SuppressWarnings("unchecked") |
1455 |
> |
public boolean tryAdvance(Consumer<? super E> action) { |
1456 |
> |
Objects.requireNonNull(action); |
1457 |
> |
int i; |
1458 |
> |
if (getFence() > (i = index)) { |
1459 |
> |
index = i + 1; |
1460 |
> |
action.accept((E)array[i]); |
1461 |
> |
if (modCount != expectedModCount) |
1462 |
|
throw new ConcurrentModificationException(); |
1463 |
< |
try { |
1417 |
< |
outer.remove(i); |
1418 |
< |
if (i < cursor) |
1419 |
< |
cursor--; |
1420 |
< |
lastRet = -1; |
1421 |
< |
fence = outer.length; |
1422 |
< |
expectedModCount = base.modCount; |
1423 |
< |
} catch (IndexOutOfBoundsException ex) { |
1424 |
< |
throw new ConcurrentModificationException(); |
1425 |
< |
} |
1463 |
> |
return true; |
1464 |
|
} |
1465 |
+ |
return false; |
1466 |
+ |
} |
1467 |
|
|
1468 |
< |
public void add(E e) { |
1469 |
< |
if (base.modCount != expectedModCount) |
1470 |
< |
throw new ConcurrentModificationException(); |
1471 |
< |
try { |
1472 |
< |
int i = cursor; |
1473 |
< |
outer.add(i, e); |
1474 |
< |
cursor = i + 1; |
1475 |
< |
lastRet = -1; |
1476 |
< |
fence = outer.length; |
1477 |
< |
expectedModCount = base.modCount; |
1438 |
< |
} catch (IndexOutOfBoundsException ex) { |
1439 |
< |
throw new ConcurrentModificationException(); |
1440 |
< |
} |
1441 |
< |
} |
1468 |
> |
@SuppressWarnings("unchecked") |
1469 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
1470 |
> |
Objects.requireNonNull(action); |
1471 |
> |
final int hi = getFence(); |
1472 |
> |
final Object[] a = array; |
1473 |
> |
int i; |
1474 |
> |
for (i = index, index = hi; i < hi; i++) |
1475 |
> |
action.accept((E) a[i]); |
1476 |
> |
if (modCount != expectedModCount) |
1477 |
> |
throw new ConcurrentModificationException(); |
1478 |
|
} |
1443 |
– |
} |
1444 |
– |
} |
1479 |
|
|
1480 |
+ |
public long estimateSize() { |
1481 |
+ |
return getFence() - index; |
1482 |
+ |
} |
1483 |
|
|
1484 |
+ |
public int characteristics() { |
1485 |
+ |
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED; |
1486 |
+ |
} |
1487 |
+ |
} |
1488 |
|
|
1489 |
+ |
void checkInvariants() { |
1490 |
+ |
// assert elementCount >= 0; |
1491 |
+ |
// assert elementCount == elementData.length || elementData[elementCount] == null; |
1492 |
+ |
} |
1493 |
+ |
} |