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> |
137 |
|
* is negative |
138 |
|
*/ |
139 |
|
public Vector(int initialCapacity, int capacityIncrement) { |
140 |
< |
super(); |
140 |
> |
super(); |
141 |
|
if (initialCapacity < 0) |
142 |
|
throw new IllegalArgumentException("Illegal Capacity: "+ |
143 |
|
initialCapacity); |
144 |
< |
this.elementData = new Object[initialCapacity]; |
145 |
< |
this.capacityIncrement = capacityIncrement; |
144 |
> |
this.elementData = new Object[initialCapacity]; |
145 |
> |
this.capacityIncrement = capacityIncrement; |
146 |
|
} |
147 |
|
|
148 |
|
/** |
154 |
|
* is negative |
155 |
|
*/ |
156 |
|
public Vector(int initialCapacity) { |
157 |
< |
this(initialCapacity, 0); |
157 |
> |
this(initialCapacity, 0); |
158 |
|
} |
159 |
|
|
160 |
|
/** |
163 |
|
* zero. |
164 |
|
*/ |
165 |
|
public Vector() { |
166 |
< |
this(10); |
166 |
> |
this(10); |
167 |
|
} |
168 |
|
|
169 |
|
/** |
177 |
|
* @since 1.2 |
178 |
|
*/ |
179 |
|
public Vector(Collection<? extends E> c) { |
180 |
< |
elementData = c.toArray(); |
181 |
< |
elementCount = elementData.length; |
182 |
< |
// c.toArray might (incorrectly) not return Object[] (see 6260652) |
183 |
< |
if (elementData.getClass() != Object[].class) |
184 |
< |
elementData = Arrays.copyOf(elementData, elementCount, Object[].class); |
180 |
> |
elementData = c.toArray(); |
181 |
> |
elementCount = elementData.length; |
182 |
> |
// defend against c.toArray (incorrectly) not returning Object[] |
183 |
> |
// (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652) |
184 |
> |
if (elementData.getClass() != Object[].class) |
185 |
> |
elementData = Arrays.copyOf(elementData, elementCount, Object[].class); |
186 |
|
} |
187 |
|
|
188 |
|
/** |
199 |
|
* @see #toArray(Object[]) |
200 |
|
*/ |
201 |
|
public synchronized void copyInto(Object[] anArray) { |
202 |
< |
System.arraycopy(elementData, 0, anArray, 0, elementCount); |
202 |
> |
System.arraycopy(elementData, 0, anArray, 0, elementCount); |
203 |
|
} |
204 |
|
|
205 |
|
/** |
211 |
|
* minimize the storage of a vector. |
212 |
|
*/ |
213 |
|
public synchronized void trimToSize() { |
214 |
< |
modCount++; |
215 |
< |
int oldCapacity = elementData.length; |
216 |
< |
if (elementCount < oldCapacity) { |
214 |
> |
modCount++; |
215 |
> |
int oldCapacity = elementData.length; |
216 |
> |
if (elementCount < oldCapacity) { |
217 |
|
elementData = Arrays.copyOf(elementData, elementCount); |
218 |
< |
} |
218 |
> |
} |
219 |
|
} |
220 |
|
|
221 |
|
/** |
236 |
|
* @param minCapacity the desired minimum capacity |
237 |
|
*/ |
238 |
|
public synchronized void ensureCapacity(int minCapacity) { |
239 |
< |
modCount++; |
240 |
< |
ensureCapacityHelper(minCapacity); |
239 |
> |
if (minCapacity > 0) { |
240 |
> |
modCount++; |
241 |
> |
if (minCapacity > elementData.length) |
242 |
> |
grow(minCapacity); |
243 |
> |
} |
244 |
|
} |
245 |
|
|
246 |
|
/** |
247 |
< |
* This implements the unsynchronized semantics of ensureCapacity. |
248 |
< |
* Synchronized methods in this class can internally call this |
249 |
< |
* method for ensuring capacity without incurring the cost of an |
250 |
< |
* extra synchronization. |
251 |
< |
* |
252 |
< |
* @see #ensureCapacity(int) |
253 |
< |
*/ |
254 |
< |
private void ensureCapacityHelper(int minCapacity) { |
255 |
< |
int oldCapacity = elementData.length; |
256 |
< |
if (minCapacity > oldCapacity) { |
257 |
< |
Object[] oldData = elementData; |
258 |
< |
int newCapacity = (capacityIncrement > 0) ? |
259 |
< |
(oldCapacity + capacityIncrement) : (oldCapacity * 2); |
260 |
< |
if (newCapacity < minCapacity) { |
261 |
< |
newCapacity = minCapacity; |
262 |
< |
} |
263 |
< |
elementData = Arrays.copyOf(elementData, newCapacity); |
227 |
< |
} |
247 |
> |
* Increases the capacity to ensure that it can hold at least the |
248 |
> |
* number of elements specified by the minimum capacity argument. |
249 |
> |
* |
250 |
> |
* @param minCapacity the desired minimum capacity |
251 |
> |
* @throws OutOfMemoryError if minCapacity is less than zero |
252 |
> |
*/ |
253 |
> |
private Object[] grow(int minCapacity) { |
254 |
> |
int oldCapacity = elementData.length; |
255 |
> |
int newCapacity = ArraysSupport.newLength(oldCapacity, |
256 |
> |
minCapacity - oldCapacity, /* minimum growth */ |
257 |
> |
capacityIncrement > 0 ? capacityIncrement : oldCapacity |
258 |
> |
/* preferred growth */); |
259 |
> |
return elementData = Arrays.copyOf(elementData, newCapacity); |
260 |
> |
} |
261 |
> |
|
262 |
> |
private Object[] grow() { |
263 |
> |
return grow(elementCount + 1); |
264 |
|
} |
265 |
|
|
266 |
|
/** |
273 |
|
* @throws ArrayIndexOutOfBoundsException if the new size is negative |
274 |
|
*/ |
275 |
|
public synchronized void setSize(int newSize) { |
276 |
< |
modCount++; |
277 |
< |
if (newSize > elementCount) { |
278 |
< |
ensureCapacityHelper(newSize); |
279 |
< |
} else { |
280 |
< |
for (int i = newSize ; i < elementCount ; i++) { |
281 |
< |
elementData[i] = null; |
282 |
< |
} |
247 |
< |
} |
248 |
< |
elementCount = newSize; |
276 |
> |
modCount++; |
277 |
> |
if (newSize > elementData.length) |
278 |
> |
grow(newSize); |
279 |
> |
final Object[] es = elementData; |
280 |
> |
for (int to = elementCount, i = newSize; i < to; i++) |
281 |
> |
es[i] = null; |
282 |
> |
elementCount = newSize; |
283 |
|
} |
284 |
|
|
285 |
|
/** |
290 |
|
* of this vector) |
291 |
|
*/ |
292 |
|
public synchronized int capacity() { |
293 |
< |
return elementData.length; |
293 |
> |
return elementData.length; |
294 |
|
} |
295 |
|
|
296 |
|
/** |
299 |
|
* @return the number of components in this vector |
300 |
|
*/ |
301 |
|
public synchronized int size() { |
302 |
< |
return elementCount; |
302 |
> |
return elementCount; |
303 |
|
} |
304 |
|
|
305 |
|
/** |
310 |
|
* {@code false} otherwise. |
311 |
|
*/ |
312 |
|
public synchronized boolean isEmpty() { |
313 |
< |
return elementCount == 0; |
313 |
> |
return elementCount == 0; |
314 |
|
} |
315 |
|
|
316 |
|
/** |
317 |
|
* Returns an enumeration of the components of this vector. The |
318 |
|
* returned {@code Enumeration} object will generate all items in |
319 |
|
* this vector. The first item generated is the item at index {@code 0}, |
320 |
< |
* then the item at index {@code 1}, and so on. |
320 |
> |
* then the item at index {@code 1}, and so on. If the vector is |
321 |
> |
* structurally modified while enumerating over the elements then the |
322 |
> |
* results of enumerating are undefined. |
323 |
|
* |
324 |
|
* @return an enumeration of the components of this vector |
325 |
|
* @see Iterator |
326 |
|
*/ |
327 |
|
public Enumeration<E> elements() { |
328 |
< |
return new Enumeration<E>() { |
329 |
< |
int count = 0; |
328 |
> |
return new Enumeration<E>() { |
329 |
> |
int count = 0; |
330 |
> |
|
331 |
> |
public boolean hasMoreElements() { |
332 |
> |
return count < elementCount; |
333 |
> |
} |
334 |
|
|
335 |
< |
public boolean hasMoreElements() { |
336 |
< |
return count < elementCount; |
337 |
< |
} |
338 |
< |
|
339 |
< |
public E nextElement() { |
340 |
< |
synchronized (Vector.this) { |
341 |
< |
if (count < elementCount) { |
342 |
< |
return (E)elementData[count++]; |
343 |
< |
} |
304 |
< |
} |
305 |
< |
throw new NoSuchElementException("Vector Enumeration"); |
306 |
< |
} |
307 |
< |
}; |
335 |
> |
public E nextElement() { |
336 |
> |
synchronized (Vector.this) { |
337 |
> |
if (count < elementCount) { |
338 |
> |
return elementData(count++); |
339 |
> |
} |
340 |
> |
} |
341 |
> |
throw new NoSuchElementException("Vector Enumeration"); |
342 |
> |
} |
343 |
> |
}; |
344 |
|
} |
345 |
|
|
346 |
|
/** |
347 |
|
* Returns {@code true} if this vector contains the specified element. |
348 |
|
* More formally, returns {@code true} if and only if this vector |
349 |
|
* contains at least one element {@code e} such that |
350 |
< |
* <tt>(o==null ? e==null : o.equals(e))</tt>. |
350 |
> |
* {@code Objects.equals(o, e)}. |
351 |
|
* |
352 |
|
* @param o element whose presence in this vector is to be tested |
353 |
|
* @return {@code true} if this vector contains the specified element |
354 |
|
*/ |
355 |
|
public boolean contains(Object o) { |
356 |
< |
return indexOf(o, 0) >= 0; |
356 |
> |
return indexOf(o, 0) >= 0; |
357 |
|
} |
358 |
|
|
359 |
|
/** |
360 |
|
* Returns the index of the first occurrence of the specified element |
361 |
|
* in this vector, or -1 if this vector does not contain the element. |
362 |
|
* More formally, returns the lowest index {@code i} such that |
363 |
< |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
363 |
> |
* {@code Objects.equals(o, get(i))}, |
364 |
|
* or -1 if there is no such index. |
365 |
|
* |
366 |
|
* @param o element to search for |
368 |
|
* this vector, or -1 if this vector does not contain the element |
369 |
|
*/ |
370 |
|
public int indexOf(Object o) { |
371 |
< |
return indexOf(o, 0); |
371 |
> |
return indexOf(o, 0); |
372 |
|
} |
373 |
|
|
374 |
|
/** |
376 |
|
* this vector, searching forwards from {@code index}, or returns -1 if |
377 |
|
* the element is not found. |
378 |
|
* More formally, returns the lowest index {@code i} such that |
379 |
< |
* <tt>(i >= index && (o==null ? get(i)==null : o.equals(get(i))))</tt>, |
379 |
> |
* {@code (i >= index && Objects.equals(o, get(i)))}, |
380 |
|
* or -1 if there is no such index. |
381 |
|
* |
382 |
|
* @param o element to search for |
388 |
|
* @see Object#equals(Object) |
389 |
|
*/ |
390 |
|
public synchronized int indexOf(Object o, int index) { |
391 |
< |
if (o == null) { |
392 |
< |
for (int i = index ; i < elementCount ; i++) |
393 |
< |
if (elementData[i]==null) |
394 |
< |
return i; |
395 |
< |
} else { |
396 |
< |
for (int i = index ; i < elementCount ; i++) |
397 |
< |
if (o.equals(elementData[i])) |
398 |
< |
return i; |
399 |
< |
} |
400 |
< |
return -1; |
391 |
> |
if (o == null) { |
392 |
> |
for (int i = index ; i < elementCount ; i++) |
393 |
> |
if (elementData[i]==null) |
394 |
> |
return i; |
395 |
> |
} else { |
396 |
> |
for (int i = index ; i < elementCount ; i++) |
397 |
> |
if (o.equals(elementData[i])) |
398 |
> |
return i; |
399 |
> |
} |
400 |
> |
return -1; |
401 |
|
} |
402 |
|
|
403 |
|
/** |
404 |
|
* Returns the index of the last occurrence of the specified element |
405 |
|
* in this vector, or -1 if this vector does not contain the element. |
406 |
|
* More formally, returns the highest index {@code i} such that |
407 |
< |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
407 |
> |
* {@code Objects.equals(o, get(i))}, |
408 |
|
* or -1 if there is no such index. |
409 |
|
* |
410 |
|
* @param o element to search for |
412 |
|
* this vector, or -1 if this vector does not contain the element |
413 |
|
*/ |
414 |
|
public synchronized int lastIndexOf(Object o) { |
415 |
< |
return lastIndexOf(o, elementCount-1); |
415 |
> |
return lastIndexOf(o, elementCount-1); |
416 |
|
} |
417 |
|
|
418 |
|
/** |
420 |
|
* this vector, searching backwards from {@code index}, or returns -1 if |
421 |
|
* the element is not found. |
422 |
|
* More formally, returns the highest index {@code i} such that |
423 |
< |
* <tt>(i <= index && (o==null ? get(i)==null : o.equals(get(i))))</tt>, |
423 |
> |
* {@code (i <= index && Objects.equals(o, get(i)))}, |
424 |
|
* or -1 if there is no such index. |
425 |
|
* |
426 |
|
* @param o element to search for |
435 |
|
if (index >= elementCount) |
436 |
|
throw new IndexOutOfBoundsException(index + " >= "+ elementCount); |
437 |
|
|
438 |
< |
if (o == null) { |
439 |
< |
for (int i = index; i >= 0; i--) |
440 |
< |
if (elementData[i]==null) |
441 |
< |
return i; |
442 |
< |
} else { |
443 |
< |
for (int i = index; i >= 0; i--) |
444 |
< |
if (o.equals(elementData[i])) |
445 |
< |
return i; |
446 |
< |
} |
447 |
< |
return -1; |
438 |
> |
if (o == null) { |
439 |
> |
for (int i = index; i >= 0; i--) |
440 |
> |
if (elementData[i]==null) |
441 |
> |
return i; |
442 |
> |
} else { |
443 |
> |
for (int i = index; i >= 0; i--) |
444 |
> |
if (o.equals(elementData[i])) |
445 |
> |
return i; |
446 |
> |
} |
447 |
> |
return -1; |
448 |
|
} |
449 |
|
|
450 |
|
/** |
456 |
|
* @param index an index into this vector |
457 |
|
* @return the component at the specified index |
458 |
|
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
459 |
< |
* ({@code index < 0 || index >= size()}) |
459 |
> |
* ({@code index < 0 || index >= size()}) |
460 |
|
*/ |
461 |
|
public synchronized E elementAt(int index) { |
462 |
< |
if (index >= elementCount) { |
463 |
< |
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
464 |
< |
} |
462 |
> |
if (index >= elementCount) { |
463 |
> |
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
464 |
> |
} |
465 |
|
|
466 |
< |
return (E)elementData[index]; |
466 |
> |
return elementData(index); |
467 |
|
} |
468 |
|
|
469 |
|
/** |
474 |
|
* @throws NoSuchElementException if this vector has no components |
475 |
|
*/ |
476 |
|
public synchronized E firstElement() { |
477 |
< |
if (elementCount == 0) { |
478 |
< |
throw new NoSuchElementException(); |
479 |
< |
} |
480 |
< |
return (E)elementData[0]; |
477 |
> |
if (elementCount == 0) { |
478 |
> |
throw new NoSuchElementException(); |
479 |
> |
} |
480 |
> |
return elementData(0); |
481 |
|
} |
482 |
|
|
483 |
|
/** |
484 |
|
* Returns the last component of the vector. |
485 |
|
* |
486 |
|
* @return the last component of the vector, i.e., the component at index |
487 |
< |
* <code>size() - 1</code>. |
487 |
> |
* {@code size() - 1} |
488 |
|
* @throws NoSuchElementException if this vector is empty |
489 |
|
*/ |
490 |
|
public synchronized E lastElement() { |
491 |
< |
if (elementCount == 0) { |
492 |
< |
throw new NoSuchElementException(); |
493 |
< |
} |
494 |
< |
return (E)elementData[elementCount - 1]; |
491 |
> |
if (elementCount == 0) { |
492 |
> |
throw new NoSuchElementException(); |
493 |
> |
} |
494 |
> |
return elementData(elementCount - 1); |
495 |
|
} |
496 |
|
|
497 |
|
/** |
512 |
|
* @param obj what the component is to be set to |
513 |
|
* @param index the specified index |
514 |
|
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
515 |
< |
* ({@code index < 0 || index >= size()}) |
515 |
> |
* ({@code index < 0 || index >= size()}) |
516 |
|
*/ |
517 |
|
public synchronized void setElementAt(E obj, int index) { |
518 |
< |
if (index >= elementCount) { |
519 |
< |
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
520 |
< |
elementCount); |
521 |
< |
} |
522 |
< |
elementData[index] = obj; |
518 |
> |
if (index >= elementCount) { |
519 |
> |
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
520 |
> |
elementCount); |
521 |
> |
} |
522 |
> |
elementData[index] = obj; |
523 |
|
} |
524 |
|
|
525 |
|
/** |
539 |
|
* |
540 |
|
* @param index the index of the object to remove |
541 |
|
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
542 |
< |
* ({@code index < 0 || index >= size()}) |
542 |
> |
* ({@code index < 0 || index >= size()}) |
543 |
|
*/ |
544 |
|
public synchronized void removeElementAt(int index) { |
545 |
< |
modCount++; |
546 |
< |
if (index >= elementCount) { |
547 |
< |
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
548 |
< |
elementCount); |
549 |
< |
} |
550 |
< |
else if (index < 0) { |
551 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
552 |
< |
} |
553 |
< |
int j = elementCount - index - 1; |
554 |
< |
if (j > 0) { |
555 |
< |
System.arraycopy(elementData, index + 1, elementData, index, j); |
556 |
< |
} |
557 |
< |
elementCount--; |
558 |
< |
elementData[elementCount] = null; /* to let gc do its work */ |
545 |
> |
if (index >= elementCount) { |
546 |
> |
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
547 |
> |
elementCount); |
548 |
> |
} |
549 |
> |
else if (index < 0) { |
550 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
551 |
> |
} |
552 |
> |
int j = elementCount - index - 1; |
553 |
> |
if (j > 0) { |
554 |
> |
System.arraycopy(elementData, index + 1, elementData, index, j); |
555 |
> |
} |
556 |
> |
modCount++; |
557 |
> |
elementCount--; |
558 |
> |
elementData[elementCount] = null; /* to let gc do its work */ |
559 |
> |
// checkInvariants(); |
560 |
|
} |
561 |
|
|
562 |
|
/** |
580 |
|
* @param obj the component to insert |
581 |
|
* @param index where to insert the new component |
582 |
|
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
583 |
< |
* ({@code index < 0 || index > size()}) |
583 |
> |
* ({@code index < 0 || index > size()}) |
584 |
|
*/ |
585 |
|
public synchronized void insertElementAt(E obj, int index) { |
586 |
< |
modCount++; |
587 |
< |
if (index > elementCount) { |
588 |
< |
throw new ArrayIndexOutOfBoundsException(index |
589 |
< |
+ " > " + elementCount); |
590 |
< |
} |
591 |
< |
ensureCapacityHelper(elementCount + 1); |
592 |
< |
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index); |
593 |
< |
elementData[index] = obj; |
594 |
< |
elementCount++; |
586 |
> |
if (index > elementCount) { |
587 |
> |
throw new ArrayIndexOutOfBoundsException(index |
588 |
> |
+ " > " + elementCount); |
589 |
> |
} |
590 |
> |
modCount++; |
591 |
> |
final int s = elementCount; |
592 |
> |
Object[] elementData = this.elementData; |
593 |
> |
if (s == elementData.length) |
594 |
> |
elementData = grow(); |
595 |
> |
System.arraycopy(elementData, index, |
596 |
> |
elementData, index + 1, |
597 |
> |
s - index); |
598 |
> |
elementData[index] = obj; |
599 |
> |
elementCount = s + 1; |
600 |
|
} |
601 |
|
|
602 |
|
/** |
611 |
|
* @param obj the component to be added |
612 |
|
*/ |
613 |
|
public synchronized void addElement(E obj) { |
614 |
< |
modCount++; |
615 |
< |
ensureCapacityHelper(elementCount + 1); |
574 |
< |
elementData[elementCount++] = obj; |
614 |
> |
modCount++; |
615 |
> |
add(obj, elementData, elementCount); |
616 |
|
} |
617 |
|
|
618 |
|
/** |
631 |
|
* vector; {@code false} otherwise. |
632 |
|
*/ |
633 |
|
public synchronized boolean removeElement(Object obj) { |
634 |
< |
modCount++; |
635 |
< |
int i = indexOf(obj); |
636 |
< |
if (i >= 0) { |
637 |
< |
removeElementAt(i); |
638 |
< |
return true; |
639 |
< |
} |
640 |
< |
return false; |
634 |
> |
modCount++; |
635 |
> |
int i = indexOf(obj); |
636 |
> |
if (i >= 0) { |
637 |
> |
removeElementAt(i); |
638 |
> |
return true; |
639 |
> |
} |
640 |
> |
return false; |
641 |
|
} |
642 |
|
|
643 |
|
/** |
647 |
|
* method (which is part of the {@link List} interface). |
648 |
|
*/ |
649 |
|
public synchronized void removeAllElements() { |
650 |
+ |
final Object[] es = elementData; |
651 |
+ |
for (int to = elementCount, i = elementCount = 0; i < to; i++) |
652 |
+ |
es[i] = null; |
653 |
|
modCount++; |
610 |
– |
// Let gc do its work |
611 |
– |
for (int i = 0; i < elementCount; i++) |
612 |
– |
elementData[i] = null; |
613 |
– |
|
614 |
– |
elementCount = 0; |
654 |
|
} |
655 |
|
|
656 |
|
/** |
661 |
|
* @return a clone of this vector |
662 |
|
*/ |
663 |
|
public synchronized Object clone() { |
664 |
< |
try { |
665 |
< |
Vector<E> v = (Vector<E>) super.clone(); |
666 |
< |
v.elementData = Arrays.copyOf(elementData, elementCount); |
667 |
< |
v.modCount = 0; |
668 |
< |
return v; |
669 |
< |
} catch (CloneNotSupportedException e) { |
670 |
< |
// this shouldn't happen, since we are Cloneable |
671 |
< |
throw new InternalError(); |
672 |
< |
} |
664 |
> |
try { |
665 |
> |
@SuppressWarnings("unchecked") |
666 |
> |
Vector<E> v = (Vector<E>) super.clone(); |
667 |
> |
v.elementData = Arrays.copyOf(elementData, elementCount); |
668 |
> |
v.modCount = 0; |
669 |
> |
return v; |
670 |
> |
} catch (CloneNotSupportedException e) { |
671 |
> |
// this shouldn't happen, since we are Cloneable |
672 |
> |
throw new InternalError(e); |
673 |
> |
} |
674 |
|
} |
675 |
|
|
676 |
|
/** |
697 |
|
* of the Vector <em>only</em> if the caller knows that the Vector |
698 |
|
* does not contain any null elements.) |
699 |
|
* |
700 |
+ |
* @param <T> type of array elements. The same type as {@code <E>} or a |
701 |
+ |
* supertype of {@code <E>}. |
702 |
|
* @param a the array into which the elements of the Vector are to |
703 |
< |
* be stored, if it is big enough; otherwise, a new array of the |
704 |
< |
* same runtime type is allocated for this purpose. |
703 |
> |
* be stored, if it is big enough; otherwise, a new array of the |
704 |
> |
* same runtime type is allocated for this purpose. |
705 |
|
* @return an array containing the elements of the Vector |
706 |
< |
* @throws ArrayStoreException if the runtime type of a is not a supertype |
707 |
< |
* of the runtime type of every element in this Vector |
706 |
> |
* @throws ArrayStoreException if the runtime type of a, {@code <T>}, is not |
707 |
> |
* a supertype of the runtime type, {@code <E>}, of every element in this |
708 |
> |
* Vector |
709 |
|
* @throws NullPointerException if the given array is null |
710 |
|
* @since 1.2 |
711 |
|
*/ |
712 |
+ |
@SuppressWarnings("unchecked") |
713 |
|
public synchronized <T> T[] toArray(T[] a) { |
714 |
|
if (a.length < elementCount) |
715 |
|
return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass()); |
716 |
|
|
717 |
< |
System.arraycopy(elementData, 0, a, 0, elementCount); |
717 |
> |
System.arraycopy(elementData, 0, a, 0, elementCount); |
718 |
|
|
719 |
|
if (a.length > elementCount) |
720 |
|
a[elementCount] = null; |
724 |
|
|
725 |
|
// Positional Access Operations |
726 |
|
|
727 |
+ |
@SuppressWarnings("unchecked") |
728 |
+ |
E elementData(int index) { |
729 |
+ |
return (E) elementData[index]; |
730 |
+ |
} |
731 |
+ |
|
732 |
+ |
@SuppressWarnings("unchecked") |
733 |
+ |
static <E> E elementAt(Object[] es, int index) { |
734 |
+ |
return (E) es[index]; |
735 |
+ |
} |
736 |
+ |
|
737 |
|
/** |
738 |
|
* Returns the element at the specified position in this Vector. |
739 |
|
* |
744 |
|
* @since 1.2 |
745 |
|
*/ |
746 |
|
public synchronized E get(int index) { |
747 |
< |
if (index >= elementCount) |
748 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
747 |
> |
if (index >= elementCount) |
748 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
749 |
|
|
750 |
< |
return (E)elementData[index]; |
750 |
> |
return elementData(index); |
751 |
|
} |
752 |
|
|
753 |
|
/** |
758 |
|
* @param element element to be stored at the specified position |
759 |
|
* @return the element previously at the specified position |
760 |
|
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
761 |
< |
* ({@code index < 0 || index >= size()}) |
761 |
> |
* ({@code index < 0 || index >= size()}) |
762 |
|
* @since 1.2 |
763 |
|
*/ |
764 |
|
public synchronized E set(int index, E element) { |
765 |
< |
if (index >= elementCount) |
766 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
765 |
> |
if (index >= elementCount) |
766 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
767 |
|
|
768 |
< |
Object oldValue = elementData[index]; |
769 |
< |
elementData[index] = element; |
770 |
< |
return (E)oldValue; |
768 |
> |
E oldValue = elementData(index); |
769 |
> |
elementData[index] = element; |
770 |
> |
return oldValue; |
771 |
> |
} |
772 |
> |
|
773 |
> |
/** |
774 |
> |
* This helper method split out from add(E) to keep method |
775 |
> |
* bytecode size under 35 (the -XX:MaxInlineSize default value), |
776 |
> |
* which helps when add(E) is called in a C1-compiled loop. |
777 |
> |
*/ |
778 |
> |
private void add(E e, Object[] elementData, int s) { |
779 |
> |
if (s == elementData.length) |
780 |
> |
elementData = grow(); |
781 |
> |
elementData[s] = e; |
782 |
> |
elementCount = s + 1; |
783 |
> |
// checkInvariants(); |
784 |
|
} |
785 |
|
|
786 |
|
/** |
791 |
|
* @since 1.2 |
792 |
|
*/ |
793 |
|
public synchronized boolean add(E e) { |
794 |
< |
modCount++; |
795 |
< |
ensureCapacityHelper(elementCount + 1); |
729 |
< |
elementData[elementCount++] = e; |
794 |
> |
modCount++; |
795 |
> |
add(e, elementData, elementCount); |
796 |
|
return true; |
797 |
|
} |
798 |
|
|
800 |
|
* Removes the first occurrence of the specified element in this Vector |
801 |
|
* If the Vector does not contain the element, it is unchanged. More |
802 |
|
* formally, removes the element with the lowest index i such that |
803 |
< |
* {@code (o==null ? get(i)==null : o.equals(get(i)))} (if such |
803 |
> |
* {@code Objects.equals(o, get(i))} (if such |
804 |
|
* an element exists). |
805 |
|
* |
806 |
|
* @param o element to be removed from this Vector, if present |
831 |
|
* Shifts any subsequent elements to the left (subtracts one from their |
832 |
|
* indices). Returns the element that was removed from the Vector. |
833 |
|
* |
768 |
– |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
769 |
– |
* ({@code index < 0 || index >= size()}) |
834 |
|
* @param index the index of the element to be removed |
835 |
|
* @return element that was removed |
836 |
+ |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
837 |
+ |
* ({@code index < 0 || index >= size()}) |
838 |
|
* @since 1.2 |
839 |
|
*/ |
840 |
|
public synchronized E remove(int index) { |
841 |
< |
modCount++; |
842 |
< |
if (index >= elementCount) |
843 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
844 |
< |
Object oldValue = elementData[index]; |
845 |
< |
|
846 |
< |
int numMoved = elementCount - index - 1; |
847 |
< |
if (numMoved > 0) |
848 |
< |
System.arraycopy(elementData, index+1, elementData, index, |
849 |
< |
numMoved); |
850 |
< |
elementData[--elementCount] = null; // Let gc do its work |
841 |
> |
modCount++; |
842 |
> |
if (index >= elementCount) |
843 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
844 |
> |
E oldValue = elementData(index); |
845 |
> |
|
846 |
> |
int numMoved = elementCount - index - 1; |
847 |
> |
if (numMoved > 0) |
848 |
> |
System.arraycopy(elementData, index+1, elementData, index, |
849 |
> |
numMoved); |
850 |
> |
elementData[--elementCount] = null; // Let gc do its work |
851 |
|
|
852 |
< |
return (E)oldValue; |
852 |
> |
// checkInvariants(); |
853 |
> |
return oldValue; |
854 |
|
} |
855 |
|
|
856 |
|
/** |
872 |
|
* @param c a collection whose elements will be tested for containment |
873 |
|
* in this Vector |
874 |
|
* @return true if this Vector contains all of the elements in the |
875 |
< |
* specified collection |
875 |
> |
* specified collection |
876 |
|
* @throws NullPointerException if the specified collection is null |
877 |
|
*/ |
878 |
|
public synchronized boolean containsAll(Collection<?> c) { |
892 |
|
* @throws NullPointerException if the specified collection is null |
893 |
|
* @since 1.2 |
894 |
|
*/ |
895 |
< |
public synchronized boolean addAll(Collection<? extends E> c) { |
829 |
< |
modCount++; |
895 |
> |
public boolean addAll(Collection<? extends E> c) { |
896 |
|
Object[] a = c.toArray(); |
897 |
+ |
modCount++; |
898 |
|
int numNew = a.length; |
899 |
< |
ensureCapacityHelper(elementCount + numNew); |
900 |
< |
System.arraycopy(a, 0, elementData, elementCount, numNew); |
901 |
< |
elementCount += numNew; |
902 |
< |
return numNew != 0; |
899 |
> |
if (numNew == 0) |
900 |
> |
return false; |
901 |
> |
synchronized (this) { |
902 |
> |
Object[] elementData = this.elementData; |
903 |
> |
final int s = elementCount; |
904 |
> |
if (numNew > elementData.length - s) |
905 |
> |
elementData = grow(s + numNew); |
906 |
> |
System.arraycopy(a, 0, elementData, s, numNew); |
907 |
> |
elementCount = s + numNew; |
908 |
> |
// checkInvariants(); |
909 |
> |
return true; |
910 |
> |
} |
911 |
|
} |
912 |
|
|
913 |
|
/** |
918 |
|
* @return true if this Vector changed as a result of the call |
919 |
|
* @throws ClassCastException if the types of one or more elements |
920 |
|
* in this vector are incompatible with the specified |
921 |
< |
* collection (optional) |
921 |
> |
* collection |
922 |
> |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
923 |
|
* @throws NullPointerException if this vector contains one or more null |
924 |
|
* elements and the specified collection does not support null |
925 |
< |
* elements (optional), or if the specified collection is null |
925 |
> |
* elements |
926 |
> |
* (<a href="Collection.html#optional-restrictions">optional</a>), |
927 |
> |
* or if the specified collection is null |
928 |
|
* @since 1.2 |
929 |
|
*/ |
930 |
< |
public synchronized boolean removeAll(Collection<?> c) { |
931 |
< |
return super.removeAll(c); |
930 |
> |
public boolean removeAll(Collection<?> c) { |
931 |
> |
Objects.requireNonNull(c); |
932 |
> |
return bulkRemove(e -> c.contains(e)); |
933 |
|
} |
934 |
|
|
935 |
|
/** |
942 |
|
* @return true if this Vector changed as a result of the call |
943 |
|
* @throws ClassCastException if the types of one or more elements |
944 |
|
* in this vector are incompatible with the specified |
945 |
< |
* collection (optional) |
945 |
> |
* collection |
946 |
> |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
947 |
|
* @throws NullPointerException if this vector contains one or more null |
948 |
|
* elements and the specified collection does not support null |
949 |
< |
* elements (optional), or if the specified collection is null |
949 |
> |
* elements |
950 |
> |
* (<a href="Collection.html#optional-restrictions">optional</a>), |
951 |
> |
* or if the specified collection is null |
952 |
|
* @since 1.2 |
953 |
|
*/ |
954 |
< |
public synchronized boolean retainAll(Collection<?> c) { |
955 |
< |
return super.retainAll(c); |
954 |
> |
public boolean retainAll(Collection<?> c) { |
955 |
> |
Objects.requireNonNull(c); |
956 |
> |
return bulkRemove(e -> !c.contains(e)); |
957 |
> |
} |
958 |
> |
|
959 |
> |
/** |
960 |
> |
* @throws NullPointerException {@inheritDoc} |
961 |
> |
*/ |
962 |
> |
@Override |
963 |
> |
public boolean removeIf(Predicate<? super E> filter) { |
964 |
> |
Objects.requireNonNull(filter); |
965 |
> |
return bulkRemove(filter); |
966 |
> |
} |
967 |
> |
|
968 |
> |
// A tiny bit set implementation |
969 |
> |
|
970 |
> |
private static long[] nBits(int n) { |
971 |
> |
return new long[((n - 1) >> 6) + 1]; |
972 |
> |
} |
973 |
> |
private static void setBit(long[] bits, int i) { |
974 |
> |
bits[i >> 6] |= 1L << i; |
975 |
> |
} |
976 |
> |
private static boolean isClear(long[] bits, int i) { |
977 |
> |
return (bits[i >> 6] & (1L << i)) == 0; |
978 |
> |
} |
979 |
> |
|
980 |
> |
private synchronized boolean bulkRemove(Predicate<? super E> filter) { |
981 |
> |
int expectedModCount = modCount; |
982 |
> |
final Object[] es = elementData; |
983 |
> |
final int end = elementCount; |
984 |
> |
int i; |
985 |
> |
// Optimize for initial run of survivors |
986 |
> |
for (i = 0; i < end && !filter.test(elementAt(es, i)); i++) |
987 |
> |
; |
988 |
> |
// Tolerate predicates that reentrantly access the collection for |
989 |
> |
// read (but writers still get CME), so traverse once to find |
990 |
> |
// elements to delete, a second pass to physically expunge. |
991 |
> |
if (i < end) { |
992 |
> |
final int beg = i; |
993 |
> |
final long[] deathRow = nBits(end - beg); |
994 |
> |
deathRow[0] = 1L; // set bit 0 |
995 |
> |
for (i = beg + 1; i < end; i++) |
996 |
> |
if (filter.test(elementAt(es, i))) |
997 |
> |
setBit(deathRow, i - beg); |
998 |
> |
if (modCount != expectedModCount) |
999 |
> |
throw new ConcurrentModificationException(); |
1000 |
> |
modCount++; |
1001 |
> |
int w = beg; |
1002 |
> |
for (i = beg; i < end; i++) |
1003 |
> |
if (isClear(deathRow, i - beg)) |
1004 |
> |
es[w++] = es[i]; |
1005 |
> |
for (i = elementCount = w; i < end; i++) |
1006 |
> |
es[i] = null; |
1007 |
> |
// checkInvariants(); |
1008 |
> |
return true; |
1009 |
> |
} else { |
1010 |
> |
if (modCount != expectedModCount) |
1011 |
> |
throw new ConcurrentModificationException(); |
1012 |
> |
// checkInvariants(); |
1013 |
> |
return false; |
1014 |
> |
} |
1015 |
|
} |
1016 |
|
|
1017 |
|
/** |
1032 |
|
* @since 1.2 |
1033 |
|
*/ |
1034 |
|
public synchronized boolean addAll(int index, Collection<? extends E> c) { |
1035 |
< |
modCount++; |
1036 |
< |
if (index < 0 || index > elementCount) |
896 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
1035 |
> |
if (index < 0 || index > elementCount) |
1036 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
1037 |
|
|
1038 |
|
Object[] a = c.toArray(); |
1039 |
< |
int numNew = a.length; |
1040 |
< |
ensureCapacityHelper(elementCount + numNew); |
1041 |
< |
|
1042 |
< |
int numMoved = elementCount - index; |
1043 |
< |
if (numMoved > 0) |
1044 |
< |
System.arraycopy(elementData, index, elementData, index + numNew, |
1045 |
< |
numMoved); |
1046 |
< |
|
1039 |
> |
modCount++; |
1040 |
> |
int numNew = a.length; |
1041 |
> |
if (numNew == 0) |
1042 |
> |
return false; |
1043 |
> |
Object[] elementData = this.elementData; |
1044 |
> |
final int s = elementCount; |
1045 |
> |
if (numNew > elementData.length - s) |
1046 |
> |
elementData = grow(s + numNew); |
1047 |
> |
|
1048 |
> |
int numMoved = s - index; |
1049 |
> |
if (numMoved > 0) |
1050 |
> |
System.arraycopy(elementData, index, |
1051 |
> |
elementData, index + numNew, |
1052 |
> |
numMoved); |
1053 |
|
System.arraycopy(a, 0, elementData, index, numNew); |
1054 |
< |
elementCount += numNew; |
1055 |
< |
return numNew != 0; |
1054 |
> |
elementCount = s + numNew; |
1055 |
> |
// checkInvariants(); |
1056 |
> |
return true; |
1057 |
|
} |
1058 |
|
|
1059 |
|
/** |
1061 |
|
* true if and only if the specified Object is also a List, both Lists |
1062 |
|
* have the same size, and all corresponding pairs of elements in the two |
1063 |
|
* Lists are <em>equal</em>. (Two elements {@code e1} and |
1064 |
< |
* {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null : |
1065 |
< |
* e1.equals(e2))}.) In other words, two Lists are defined to be |
1064 |
> |
* {@code e2} are <em>equal</em> if {@code Objects.equals(e1, e2)}.) |
1065 |
> |
* In other words, two Lists are defined to be |
1066 |
|
* equal if they contain the same elements in the same order. |
1067 |
|
* |
1068 |
|
* @param o the Object to be compared for equality with this Vector |
1088 |
|
} |
1089 |
|
|
1090 |
|
/** |
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 |
– |
/** |
1091 |
|
* Returns a view of the portion of this List between fromIndex, |
1092 |
|
* inclusive, and toIndex, exclusive. (If fromIndex and toIndex are |
1093 |
|
* equal, the returned List is empty.) The returned List is backed by this |
1096 |
|
* operations supported by this List. |
1097 |
|
* |
1098 |
|
* <p>This method eliminates the need for explicit range operations (of |
1099 |
< |
* the sort that commonly exist for arrays). Any operation that expects |
1099 |
> |
* the sort that commonly exist for arrays). Any operation that expects |
1100 |
|
* a List can be used as a range operation by operating on a subList view |
1101 |
|
* instead of a whole List. For example, the following idiom |
1102 |
|
* removes a range of elements from a List: |
1103 |
|
* <pre> |
1104 |
< |
* list.subList(from, to).clear(); |
1104 |
> |
* list.subList(from, to).clear(); |
1105 |
|
* </pre> |
1106 |
|
* Similar idioms may be constructed for indexOf and lastIndexOf, |
1107 |
|
* and all of the algorithms in the Collections class can be applied to |
1119 |
|
* @throws IndexOutOfBoundsException if an endpoint index value is out of range |
1120 |
|
* {@code (fromIndex < 0 || toIndex > size)} |
1121 |
|
* @throws IllegalArgumentException if the endpoint indices are out of order |
1122 |
< |
* {@code (fromIndex > toIndex)} |
1122 |
> |
* {@code (fromIndex > toIndex)} |
1123 |
|
*/ |
1124 |
|
public synchronized List<E> subList(int fromIndex, int toIndex) { |
1125 |
< |
return new VectorSubList(this, this, fromIndex, fromIndex, toIndex); |
1125 |
> |
return Collections.synchronizedList(super.subList(fromIndex, toIndex), |
1126 |
> |
this); |
1127 |
|
} |
1128 |
|
|
1129 |
|
/** |
1130 |
< |
* This class specializes the AbstractList version of SubList to |
1131 |
< |
* avoid the double-indirection penalty that would arise using a |
1132 |
< |
* synchronized wrapper, as well as to avoid some unnecessary |
1133 |
< |
* checks in sublist iterators. |
1134 |
< |
*/ |
1135 |
< |
private static final class VectorSubList<E> extends AbstractList<E> implements RandomAccess { |
1136 |
< |
final Vector<E> base; // base list |
1137 |
< |
final AbstractList<E> parent; // Creating list |
1138 |
< |
final int baseOffset; // index wrt Vector |
1139 |
< |
final int parentOffset; // index wrt parent |
1140 |
< |
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 |
< |
} |
1130 |
> |
* Removes from this list all of the elements whose index is between |
1131 |
> |
* {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. |
1132 |
> |
* Shifts any succeeding elements to the left (reduces their index). |
1133 |
> |
* This call shortens the list by {@code (toIndex - fromIndex)} elements. |
1134 |
> |
* (If {@code toIndex==fromIndex}, this operation has no effect.) |
1135 |
> |
*/ |
1136 |
> |
protected synchronized void removeRange(int fromIndex, int toIndex) { |
1137 |
> |
modCount++; |
1138 |
> |
shiftTailOverGap(elementData, fromIndex, toIndex); |
1139 |
> |
// checkInvariants(); |
1140 |
> |
} |
1141 |
|
|
1142 |
< |
public E get(int index) { |
1143 |
< |
synchronized(base) { |
1144 |
< |
if (index < 0 || index >= length) |
1145 |
< |
throw indexError(index); |
1146 |
< |
if (base.modCount != modCount) |
1147 |
< |
throw new ConcurrentModificationException(); |
1228 |
< |
return base.get(index + baseOffset); |
1229 |
< |
} |
1230 |
< |
} |
1142 |
> |
/** Erases the gap from lo to hi, by sliding down following elements. */ |
1143 |
> |
private void shiftTailOverGap(Object[] es, int lo, int hi) { |
1144 |
> |
System.arraycopy(es, hi, es, lo, elementCount - hi); |
1145 |
> |
for (int to = elementCount, i = (elementCount -= hi - lo); i < to; i++) |
1146 |
> |
es[i] = null; |
1147 |
> |
} |
1148 |
|
|
1149 |
< |
public int size() { |
1150 |
< |
synchronized(base) { |
1151 |
< |
if (base.modCount != modCount) |
1152 |
< |
throw new ConcurrentModificationException(); |
1153 |
< |
return length; |
1154 |
< |
} |
1149 |
> |
/** |
1150 |
> |
* Loads a {@code Vector} instance from a stream |
1151 |
> |
* (that is, deserializes it). |
1152 |
> |
* This method performs checks to ensure the consistency |
1153 |
> |
* of the fields. |
1154 |
> |
* |
1155 |
> |
* @param in the stream |
1156 |
> |
* @throws java.io.IOException if an I/O error occurs |
1157 |
> |
* @throws ClassNotFoundException if the stream contains data |
1158 |
> |
* of a non-existing class |
1159 |
> |
*/ |
1160 |
> |
private void readObject(ObjectInputStream in) |
1161 |
> |
throws IOException, ClassNotFoundException { |
1162 |
> |
ObjectInputStream.GetField gfields = in.readFields(); |
1163 |
> |
int count = gfields.get("elementCount", 0); |
1164 |
> |
Object[] data = (Object[])gfields.get("elementData", null); |
1165 |
> |
if (count < 0 || data == null || count > data.length) { |
1166 |
> |
throw new StreamCorruptedException("Inconsistent vector internals"); |
1167 |
|
} |
1168 |
+ |
elementCount = count; |
1169 |
+ |
elementData = data.clone(); |
1170 |
+ |
} |
1171 |
|
|
1172 |
< |
public void add(int index, E element) { |
1173 |
< |
synchronized(base) { |
1174 |
< |
if (index < 0 || index > length) |
1175 |
< |
throw indexError(index); |
1176 |
< |
if (base.modCount != modCount) |
1177 |
< |
throw new ConcurrentModificationException(); |
1178 |
< |
parent.add(index + parentOffset, element); |
1179 |
< |
length++; |
1180 |
< |
modCount = base.modCount; |
1181 |
< |
} |
1172 |
> |
/** |
1173 |
> |
* Saves the state of the {@code Vector} instance to a stream |
1174 |
> |
* (that is, serializes it). |
1175 |
> |
* This method performs synchronization to ensure the consistency |
1176 |
> |
* of the serialized data. |
1177 |
> |
* |
1178 |
> |
* @param s the stream |
1179 |
> |
* @throws java.io.IOException if an I/O error occurs |
1180 |
> |
*/ |
1181 |
> |
private void writeObject(java.io.ObjectOutputStream s) |
1182 |
> |
throws java.io.IOException { |
1183 |
> |
final java.io.ObjectOutputStream.PutField fields = s.putFields(); |
1184 |
> |
final Object[] data; |
1185 |
> |
synchronized (this) { |
1186 |
> |
fields.put("capacityIncrement", capacityIncrement); |
1187 |
> |
fields.put("elementCount", elementCount); |
1188 |
> |
data = elementData.clone(); |
1189 |
|
} |
1190 |
+ |
fields.put("elementData", data); |
1191 |
+ |
s.writeFields(); |
1192 |
+ |
} |
1193 |
|
|
1194 |
< |
public E remove(int index) { |
1195 |
< |
synchronized(base) { |
1196 |
< |
if (index < 0 || index >= length) |
1197 |
< |
throw indexError(index); |
1198 |
< |
if (base.modCount != modCount) |
1199 |
< |
throw new ConcurrentModificationException(); |
1200 |
< |
E result = parent.remove(index + parentOffset); |
1201 |
< |
length--; |
1202 |
< |
modCount = base.modCount; |
1203 |
< |
return result; |
1204 |
< |
} |
1194 |
> |
/** |
1195 |
> |
* Returns a list iterator over the elements in this list (in proper |
1196 |
> |
* sequence), starting at the specified position in the list. |
1197 |
> |
* The specified index indicates the first element that would be |
1198 |
> |
* returned by an initial call to {@link ListIterator#next next}. |
1199 |
> |
* An initial call to {@link ListIterator#previous previous} would |
1200 |
> |
* return the element with the specified index minus one. |
1201 |
> |
* |
1202 |
> |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1203 |
> |
* |
1204 |
> |
* @throws IndexOutOfBoundsException {@inheritDoc} |
1205 |
> |
*/ |
1206 |
> |
public synchronized ListIterator<E> listIterator(int index) { |
1207 |
> |
if (index < 0 || index > elementCount) |
1208 |
> |
throw new IndexOutOfBoundsException("Index: "+index); |
1209 |
> |
return new ListItr(index); |
1210 |
> |
} |
1211 |
> |
|
1212 |
> |
/** |
1213 |
> |
* Returns a list iterator over the elements in this list (in proper |
1214 |
> |
* sequence). |
1215 |
> |
* |
1216 |
> |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1217 |
> |
* |
1218 |
> |
* @see #listIterator(int) |
1219 |
> |
*/ |
1220 |
> |
public synchronized ListIterator<E> listIterator() { |
1221 |
> |
return new ListItr(0); |
1222 |
> |
} |
1223 |
> |
|
1224 |
> |
/** |
1225 |
> |
* Returns an iterator over the elements in this list in proper sequence. |
1226 |
> |
* |
1227 |
> |
* <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1228 |
> |
* |
1229 |
> |
* @return an iterator over the elements in this list in proper sequence |
1230 |
> |
*/ |
1231 |
> |
public synchronized Iterator<E> iterator() { |
1232 |
> |
return new Itr(); |
1233 |
> |
} |
1234 |
> |
|
1235 |
> |
/** |
1236 |
> |
* An optimized version of AbstractList.Itr |
1237 |
> |
*/ |
1238 |
> |
private class Itr implements Iterator<E> { |
1239 |
> |
int cursor; // index of next element to return |
1240 |
> |
int lastRet = -1; // index of last element returned; -1 if no such |
1241 |
> |
int expectedModCount = modCount; |
1242 |
> |
|
1243 |
> |
public boolean hasNext() { |
1244 |
> |
// Racy but within spec, since modifications are checked |
1245 |
> |
// within or after synchronization in next/previous |
1246 |
> |
return cursor != elementCount; |
1247 |
|
} |
1248 |
|
|
1249 |
< |
protected void removeRange(int fromIndex, int toIndex) { |
1250 |
< |
synchronized(base) { |
1251 |
< |
if (base.modCount != modCount) |
1252 |
< |
throw new ConcurrentModificationException(); |
1253 |
< |
parent.removeRange(fromIndex + parentOffset, |
1254 |
< |
toIndex + parentOffset); |
1255 |
< |
length -= (toIndex-fromIndex); |
1256 |
< |
modCount = base.modCount; |
1249 |
> |
public E next() { |
1250 |
> |
synchronized (Vector.this) { |
1251 |
> |
checkForComodification(); |
1252 |
> |
int i = cursor; |
1253 |
> |
if (i >= elementCount) |
1254 |
> |
throw new NoSuchElementException(); |
1255 |
> |
cursor = i + 1; |
1256 |
> |
return elementData(lastRet = i); |
1257 |
|
} |
1258 |
|
} |
1259 |
|
|
1260 |
< |
public boolean addAll(Collection<? extends E> c) { |
1261 |
< |
return addAll(length, c); |
1260 |
> |
public void remove() { |
1261 |
> |
if (lastRet == -1) |
1262 |
> |
throw new IllegalStateException(); |
1263 |
> |
synchronized (Vector.this) { |
1264 |
> |
checkForComodification(); |
1265 |
> |
Vector.this.remove(lastRet); |
1266 |
> |
expectedModCount = modCount; |
1267 |
> |
} |
1268 |
> |
cursor = lastRet; |
1269 |
> |
lastRet = -1; |
1270 |
|
} |
1271 |
|
|
1272 |
< |
public boolean addAll(int index, Collection<? extends E> c) { |
1273 |
< |
synchronized(base) { |
1274 |
< |
if (index < 0 || index > length) |
1275 |
< |
throw indexError(index); |
1276 |
< |
int cSize = c.size(); |
1277 |
< |
if (cSize==0) |
1278 |
< |
return false; |
1279 |
< |
|
1280 |
< |
if (base.modCount != modCount) |
1272 |
> |
@Override |
1273 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
1274 |
> |
Objects.requireNonNull(action); |
1275 |
> |
synchronized (Vector.this) { |
1276 |
> |
final int size = elementCount; |
1277 |
> |
int i = cursor; |
1278 |
> |
if (i >= size) { |
1279 |
> |
return; |
1280 |
> |
} |
1281 |
> |
final Object[] es = elementData; |
1282 |
> |
if (i >= es.length) |
1283 |
|
throw new ConcurrentModificationException(); |
1284 |
< |
parent.addAll(parentOffset + index, c); |
1285 |
< |
modCount = base.modCount; |
1286 |
< |
length += cSize; |
1287 |
< |
return true; |
1284 |
> |
while (i < size && modCount == expectedModCount) |
1285 |
> |
action.accept(elementAt(es, i++)); |
1286 |
> |
// update once at end of iteration to reduce heap write traffic |
1287 |
> |
cursor = i; |
1288 |
> |
lastRet = i - 1; |
1289 |
> |
checkForComodification(); |
1290 |
|
} |
1291 |
|
} |
1292 |
|
|
1293 |
< |
public boolean equals(Object o) { |
1294 |
< |
synchronized(base) {return super.equals(o);} |
1293 |
> |
final void checkForComodification() { |
1294 |
> |
if (modCount != expectedModCount) |
1295 |
> |
throw new ConcurrentModificationException(); |
1296 |
|
} |
1297 |
+ |
} |
1298 |
|
|
1299 |
< |
public int hashCode() { |
1300 |
< |
synchronized(base) {return super.hashCode();} |
1299 |
> |
/** |
1300 |
> |
* An optimized version of AbstractList.ListItr |
1301 |
> |
*/ |
1302 |
> |
final class ListItr extends Itr implements ListIterator<E> { |
1303 |
> |
ListItr(int index) { |
1304 |
> |
super(); |
1305 |
> |
cursor = index; |
1306 |
|
} |
1307 |
|
|
1308 |
< |
public int indexOf(Object o) { |
1309 |
< |
synchronized(base) {return super.indexOf(o);} |
1308 |
> |
public boolean hasPrevious() { |
1309 |
> |
return cursor != 0; |
1310 |
|
} |
1311 |
|
|
1312 |
< |
public int lastIndexOf(Object o) { |
1313 |
< |
synchronized(base) {return super.lastIndexOf(o);} |
1312 |
> |
public int nextIndex() { |
1313 |
> |
return cursor; |
1314 |
|
} |
1315 |
|
|
1316 |
< |
public List<E> subList(int fromIndex, int toIndex) { |
1317 |
< |
return new VectorSubList(base, this, fromIndex + baseOffset, |
1315 |
< |
fromIndex, toIndex); |
1316 |
> |
public int previousIndex() { |
1317 |
> |
return cursor - 1; |
1318 |
|
} |
1319 |
|
|
1320 |
< |
public Iterator<E> iterator() { |
1321 |
< |
synchronized(base) { |
1322 |
< |
return new VectorSubListIterator(this, 0); |
1320 |
> |
public E previous() { |
1321 |
> |
synchronized (Vector.this) { |
1322 |
> |
checkForComodification(); |
1323 |
> |
int i = cursor - 1; |
1324 |
> |
if (i < 0) |
1325 |
> |
throw new NoSuchElementException(); |
1326 |
> |
cursor = i; |
1327 |
> |
return elementData(lastRet = i); |
1328 |
|
} |
1329 |
|
} |
1330 |
|
|
1331 |
< |
public synchronized ListIterator<E> listIterator() { |
1332 |
< |
synchronized(base) { |
1333 |
< |
return new VectorSubListIterator(this, 0); |
1331 |
> |
public void set(E e) { |
1332 |
> |
if (lastRet == -1) |
1333 |
> |
throw new IllegalStateException(); |
1334 |
> |
synchronized (Vector.this) { |
1335 |
> |
checkForComodification(); |
1336 |
> |
Vector.this.set(lastRet, e); |
1337 |
|
} |
1338 |
|
} |
1339 |
|
|
1340 |
< |
public ListIterator<E> listIterator(int index) { |
1341 |
< |
synchronized(base) { |
1342 |
< |
if (index < 0 || index > length) |
1343 |
< |
throw indexError(index); |
1344 |
< |
return new VectorSubListIterator(this, index); |
1340 |
> |
public void add(E e) { |
1341 |
> |
int i = cursor; |
1342 |
> |
synchronized (Vector.this) { |
1343 |
> |
checkForComodification(); |
1344 |
> |
Vector.this.add(i, e); |
1345 |
> |
expectedModCount = modCount; |
1346 |
|
} |
1347 |
+ |
cursor = i + 1; |
1348 |
+ |
lastRet = -1; |
1349 |
|
} |
1350 |
+ |
} |
1351 |
|
|
1352 |
< |
/** |
1353 |
< |
* Same idea as VectorIterator, except routing structural |
1354 |
< |
* change operations through the sublist. |
1355 |
< |
*/ |
1356 |
< |
private static final class VectorSubListIterator<E> implements ListIterator<E> { |
1357 |
< |
final Vector<E> base; // base list |
1358 |
< |
final VectorSubList<E> outer; // Sublist creating this iteraor |
1359 |
< |
final int offset; // cursor offset wrt base |
1360 |
< |
int cursor; // Current index |
1361 |
< |
int fence; // Upper bound on cursor |
1362 |
< |
int lastRet; // Index of returned element, or -1 |
1363 |
< |
int expectedModCount; // Expected modCount of base Vector |
1364 |
< |
|
1365 |
< |
VectorSubListIterator(VectorSubList<E> list, int index) { |
1366 |
< |
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 |
< |
} |
1352 |
> |
/** |
1353 |
> |
* @throws NullPointerException {@inheritDoc} |
1354 |
> |
*/ |
1355 |
> |
@Override |
1356 |
> |
public synchronized void forEach(Consumer<? super E> action) { |
1357 |
> |
Objects.requireNonNull(action); |
1358 |
> |
final int expectedModCount = modCount; |
1359 |
> |
final Object[] es = elementData; |
1360 |
> |
final int size = elementCount; |
1361 |
> |
for (int i = 0; modCount == expectedModCount && i < size; i++) |
1362 |
> |
action.accept(elementAt(es, i)); |
1363 |
> |
if (modCount != expectedModCount) |
1364 |
> |
throw new ConcurrentModificationException(); |
1365 |
> |
// checkInvariants(); |
1366 |
> |
} |
1367 |
|
|
1368 |
< |
public boolean hasPrevious() { |
1369 |
< |
return cursor > 0; |
1370 |
< |
} |
1368 |
> |
/** |
1369 |
> |
* @throws NullPointerException {@inheritDoc} |
1370 |
> |
*/ |
1371 |
> |
@Override |
1372 |
> |
public synchronized void replaceAll(UnaryOperator<E> operator) { |
1373 |
> |
Objects.requireNonNull(operator); |
1374 |
> |
final int expectedModCount = modCount; |
1375 |
> |
final Object[] es = elementData; |
1376 |
> |
final int size = elementCount; |
1377 |
> |
for (int i = 0; modCount == expectedModCount && i < size; i++) |
1378 |
> |
es[i] = operator.apply(elementAt(es, i)); |
1379 |
> |
if (modCount != expectedModCount) |
1380 |
> |
throw new ConcurrentModificationException(); |
1381 |
> |
// TODO(8203662): remove increment of modCount from ... |
1382 |
> |
modCount++; |
1383 |
> |
// checkInvariants(); |
1384 |
> |
} |
1385 |
|
|
1386 |
< |
public int nextIndex() { |
1387 |
< |
return cursor; |
1388 |
< |
} |
1386 |
> |
@SuppressWarnings("unchecked") |
1387 |
> |
@Override |
1388 |
> |
public synchronized void sort(Comparator<? super E> c) { |
1389 |
> |
final int expectedModCount = modCount; |
1390 |
> |
Arrays.sort((E[]) elementData, 0, elementCount, c); |
1391 |
> |
if (modCount != expectedModCount) |
1392 |
> |
throw new ConcurrentModificationException(); |
1393 |
> |
modCount++; |
1394 |
> |
// checkInvariants(); |
1395 |
> |
} |
1396 |
|
|
1397 |
< |
public int previousIndex() { |
1398 |
< |
return cursor - 1; |
1399 |
< |
} |
1397 |
> |
/** |
1398 |
> |
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> |
1399 |
> |
* and <em>fail-fast</em> {@link Spliterator} over the elements in this |
1400 |
> |
* list. |
1401 |
> |
* |
1402 |
> |
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED}, |
1403 |
> |
* {@link Spliterator#SUBSIZED}, and {@link Spliterator#ORDERED}. |
1404 |
> |
* Overriding implementations should document the reporting of additional |
1405 |
> |
* characteristic values. |
1406 |
> |
* |
1407 |
> |
* @return a {@code Spliterator} over the elements in this list |
1408 |
> |
* @since 1.8 |
1409 |
> |
*/ |
1410 |
> |
@Override |
1411 |
> |
public Spliterator<E> spliterator() { |
1412 |
> |
return new VectorSpliterator(null, 0, -1, 0); |
1413 |
> |
} |
1414 |
|
|
1415 |
< |
public E next() { |
1416 |
< |
int i = cursor; |
1417 |
< |
if (cursor >= fence) |
1418 |
< |
throw new NoSuchElementException(); |
1419 |
< |
Object next = base.iteratorGet(i + offset, expectedModCount); |
1420 |
< |
lastRet = i; |
1383 |
< |
cursor = i + 1; |
1384 |
< |
return (E)next; |
1385 |
< |
} |
1415 |
> |
/** Similar to ArrayList Spliterator */ |
1416 |
> |
final class VectorSpliterator implements Spliterator<E> { |
1417 |
> |
private Object[] array; |
1418 |
> |
private int index; // current index, modified on advance/split |
1419 |
> |
private int fence; // -1 until used; then one past last index |
1420 |
> |
private int expectedModCount; // initialized when fence set |
1421 |
|
|
1422 |
< |
public E previous() { |
1423 |
< |
int i = cursor - 1; |
1424 |
< |
if (i < 0) |
1425 |
< |
throw new NoSuchElementException(); |
1426 |
< |
Object prev = base.iteratorGet(i + offset, expectedModCount); |
1427 |
< |
lastRet = i; |
1428 |
< |
cursor = i; |
1429 |
< |
return (E)prev; |
1395 |
< |
} |
1422 |
> |
/** Creates new spliterator covering the given range. */ |
1423 |
> |
VectorSpliterator(Object[] array, int origin, int fence, |
1424 |
> |
int expectedModCount) { |
1425 |
> |
this.array = array; |
1426 |
> |
this.index = origin; |
1427 |
> |
this.fence = fence; |
1428 |
> |
this.expectedModCount = expectedModCount; |
1429 |
> |
} |
1430 |
|
|
1431 |
< |
public void set(E e) { |
1432 |
< |
if (lastRet < 0) |
1433 |
< |
throw new IllegalStateException(); |
1434 |
< |
if (base.modCount != expectedModCount) |
1435 |
< |
throw new ConcurrentModificationException(); |
1436 |
< |
try { |
1437 |
< |
outer.set(lastRet, e); |
1404 |
< |
expectedModCount = base.modCount; |
1405 |
< |
} catch (IndexOutOfBoundsException ex) { |
1406 |
< |
throw new ConcurrentModificationException(); |
1431 |
> |
private int getFence() { // initialize on first use |
1432 |
> |
int hi; |
1433 |
> |
if ((hi = fence) < 0) { |
1434 |
> |
synchronized (Vector.this) { |
1435 |
> |
array = elementData; |
1436 |
> |
expectedModCount = modCount; |
1437 |
> |
hi = fence = elementCount; |
1438 |
|
} |
1439 |
|
} |
1440 |
+ |
return hi; |
1441 |
+ |
} |
1442 |
|
|
1443 |
< |
public void remove() { |
1444 |
< |
int i = lastRet; |
1445 |
< |
if (i < 0) |
1446 |
< |
throw new IllegalStateException(); |
1447 |
< |
if (base.modCount != expectedModCount) |
1443 |
> |
public Spliterator<E> trySplit() { |
1444 |
> |
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1445 |
> |
return (lo >= mid) ? null : |
1446 |
> |
new VectorSpliterator(array, lo, index = mid, expectedModCount); |
1447 |
> |
} |
1448 |
> |
|
1449 |
> |
@SuppressWarnings("unchecked") |
1450 |
> |
public boolean tryAdvance(Consumer<? super E> action) { |
1451 |
> |
Objects.requireNonNull(action); |
1452 |
> |
int i; |
1453 |
> |
if (getFence() > (i = index)) { |
1454 |
> |
index = i + 1; |
1455 |
> |
action.accept((E)array[i]); |
1456 |
> |
if (modCount != expectedModCount) |
1457 |
|
throw new ConcurrentModificationException(); |
1458 |
< |
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 |
< |
} |
1458 |
> |
return true; |
1459 |
|
} |
1460 |
+ |
return false; |
1461 |
+ |
} |
1462 |
|
|
1463 |
< |
public void add(E e) { |
1464 |
< |
if (base.modCount != expectedModCount) |
1465 |
< |
throw new ConcurrentModificationException(); |
1466 |
< |
try { |
1467 |
< |
int i = cursor; |
1468 |
< |
outer.add(i, e); |
1469 |
< |
cursor = i + 1; |
1470 |
< |
lastRet = -1; |
1471 |
< |
fence = outer.length; |
1472 |
< |
expectedModCount = base.modCount; |
1438 |
< |
} catch (IndexOutOfBoundsException ex) { |
1439 |
< |
throw new ConcurrentModificationException(); |
1440 |
< |
} |
1441 |
< |
} |
1463 |
> |
@SuppressWarnings("unchecked") |
1464 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
1465 |
> |
Objects.requireNonNull(action); |
1466 |
> |
final int hi = getFence(); |
1467 |
> |
final Object[] a = array; |
1468 |
> |
int i; |
1469 |
> |
for (i = index, index = hi; i < hi; i++) |
1470 |
> |
action.accept((E) a[i]); |
1471 |
> |
if (modCount != expectedModCount) |
1472 |
> |
throw new ConcurrentModificationException(); |
1473 |
|
} |
1443 |
– |
} |
1444 |
– |
} |
1474 |
|
|
1475 |
+ |
public long estimateSize() { |
1476 |
+ |
return getFence() - index; |
1477 |
+ |
} |
1478 |
|
|
1479 |
+ |
public int characteristics() { |
1480 |
+ |
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED; |
1481 |
+ |
} |
1482 |
+ |
} |
1483 |
|
|
1484 |
+ |
void checkInvariants() { |
1485 |
+ |
// assert elementCount >= 0; |
1486 |
+ |
// assert elementCount == elementData.length || elementData[elementCount] == null; |
1487 |
+ |
} |
1488 |
+ |
} |