1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* Copyright (c) 1994, 2008, 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. Sun designates this |
8 |
> |
* particular file as subject to the "Classpath" exception as provided |
9 |
> |
* by Sun 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; |
41 |
|
* capacity of a vector before inserting a large number of |
42 |
|
* components; this reduces the amount of incremental reallocation. |
43 |
|
* |
44 |
< |
* <p>The Iterators returned by Vector's iterator and listIterator |
45 |
< |
* methods are <em>fail-fast</em>: if the Vector is structurally modified |
46 |
< |
* at any time after the Iterator is created, in any way except through the |
47 |
< |
* Iterator's own remove or add methods, the Iterator will throw a |
48 |
< |
* ConcurrentModificationException. Thus, in the face of concurrent |
49 |
< |
* modification, the Iterator fails quickly and cleanly, rather than risking |
50 |
< |
* arbitrary, non-deterministic behavior at an undetermined time in the future. |
51 |
< |
* The Enumerations returned by Vector's elements method are <em>not</em> |
52 |
< |
* fail-fast. |
44 |
> |
* <p><a name="fail-fast"/> |
45 |
> |
* The iterators returned by this class's {@link #iterator() iterator} and |
46 |
> |
* {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>: |
47 |
> |
* if the vector is structurally modified at any time after the iterator is |
48 |
> |
* created, in any way except through the iterator's own |
49 |
> |
* {@link ListIterator#remove() remove} or |
50 |
> |
* {@link ListIterator#add(Object) add} methods, the iterator will throw a |
51 |
> |
* {@link ConcurrentModificationException}. Thus, in the face of |
52 |
> |
* concurrent modification, the iterator fails quickly and cleanly, rather |
53 |
> |
* than risking arbitrary, non-deterministic behavior at an undetermined |
54 |
> |
* time in the future. The {@link Enumeration Enumerations} returned by |
55 |
> |
* the {@link #elements() elements} method are <em>not</em> fail-fast. |
56 |
|
* |
57 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
58 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
64 |
|
* |
65 |
|
* <p>As of the Java 2 platform v1.2, this class was retrofitted to |
66 |
|
* implement the {@link List} interface, making it a member of the |
67 |
< |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> Java |
68 |
< |
* Collections Framework</a>. Unlike the new collection |
69 |
< |
* implementations, {@code Vector} is synchronized. |
67 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
68 |
> |
* Java Collections Framework</a>. Unlike the new collection |
69 |
> |
* implementations, {@code Vector} is synchronized. If a thread-safe |
70 |
> |
* implementation is not needed, it is recommended to use {@link |
71 |
> |
* ArrayList} in place of {@code Vector}. |
72 |
|
* |
73 |
|
* @author Lee Boynton |
74 |
|
* @author Jonathan Payne |
52 |
– |
* @version %I%, %G% |
75 |
|
* @see Collection |
54 |
– |
* @see List |
55 |
– |
* @see ArrayList |
76 |
|
* @see LinkedList |
77 |
|
* @since JDK1.0 |
78 |
|
*/ |
124 |
|
* is negative |
125 |
|
*/ |
126 |
|
public Vector(int initialCapacity, int capacityIncrement) { |
127 |
< |
super(); |
127 |
> |
super(); |
128 |
|
if (initialCapacity < 0) |
129 |
|
throw new IllegalArgumentException("Illegal Capacity: "+ |
130 |
|
initialCapacity); |
131 |
< |
this.elementData = new Object[initialCapacity]; |
132 |
< |
this.capacityIncrement = capacityIncrement; |
131 |
> |
this.elementData = new Object[initialCapacity]; |
132 |
> |
this.capacityIncrement = capacityIncrement; |
133 |
|
} |
134 |
|
|
135 |
|
/** |
141 |
|
* is negative |
142 |
|
*/ |
143 |
|
public Vector(int initialCapacity) { |
144 |
< |
this(initialCapacity, 0); |
144 |
> |
this(initialCapacity, 0); |
145 |
|
} |
146 |
|
|
147 |
|
/** |
150 |
|
* zero. |
151 |
|
*/ |
152 |
|
public Vector() { |
153 |
< |
this(10); |
153 |
> |
this(10); |
154 |
|
} |
155 |
|
|
156 |
|
/** |
164 |
|
* @since 1.2 |
165 |
|
*/ |
166 |
|
public Vector(Collection<? extends E> c) { |
167 |
< |
elementData = c.toArray(); |
168 |
< |
elementCount = elementData.length; |
169 |
< |
// c.toArray might (incorrectly) not return Object[] (see 6260652) |
170 |
< |
if (elementData.getClass() != Object[].class) |
171 |
< |
elementData = Arrays.copyOf(elementData, elementCount, Object[].class); |
167 |
> |
elementData = c.toArray(); |
168 |
> |
elementCount = elementData.length; |
169 |
> |
// c.toArray might (incorrectly) not return Object[] (see 6260652) |
170 |
> |
if (elementData.getClass() != Object[].class) |
171 |
> |
elementData = Arrays.copyOf(elementData, elementCount, Object[].class); |
172 |
|
} |
173 |
|
|
174 |
|
/** |
185 |
|
* @see #toArray(Object[]) |
186 |
|
*/ |
187 |
|
public synchronized void copyInto(Object[] anArray) { |
188 |
< |
System.arraycopy(elementData, 0, anArray, 0, elementCount); |
188 |
> |
System.arraycopy(elementData, 0, anArray, 0, elementCount); |
189 |
|
} |
190 |
|
|
191 |
|
/** |
197 |
|
* minimize the storage of a vector. |
198 |
|
*/ |
199 |
|
public synchronized void trimToSize() { |
200 |
< |
modCount++; |
201 |
< |
int oldCapacity = elementData.length; |
202 |
< |
if (elementCount < oldCapacity) { |
200 |
> |
modCount++; |
201 |
> |
int oldCapacity = elementData.length; |
202 |
> |
if (elementCount < oldCapacity) { |
203 |
|
elementData = Arrays.copyOf(elementData, elementCount); |
204 |
< |
} |
204 |
> |
} |
205 |
|
} |
206 |
|
|
207 |
|
/** |
222 |
|
* @param minCapacity the desired minimum capacity |
223 |
|
*/ |
224 |
|
public synchronized void ensureCapacity(int minCapacity) { |
225 |
< |
modCount++; |
226 |
< |
ensureCapacityHelper(minCapacity); |
225 |
> |
modCount++; |
226 |
> |
ensureCapacityHelper(minCapacity); |
227 |
|
} |
228 |
|
|
229 |
|
/** |
235 |
|
* @see #ensureCapacity(int) |
236 |
|
*/ |
237 |
|
private void ensureCapacityHelper(int minCapacity) { |
238 |
< |
int oldCapacity = elementData.length; |
239 |
< |
if (minCapacity > oldCapacity) { |
240 |
< |
Object[] oldData = elementData; |
241 |
< |
int newCapacity = (capacityIncrement > 0) ? |
242 |
< |
(oldCapacity + capacityIncrement) : (oldCapacity * 2); |
243 |
< |
if (newCapacity < minCapacity) { |
244 |
< |
newCapacity = minCapacity; |
245 |
< |
} |
238 |
> |
int oldCapacity = elementData.length; |
239 |
> |
if (minCapacity > oldCapacity) { |
240 |
> |
Object[] oldData = elementData; |
241 |
> |
int newCapacity = (capacityIncrement > 0) ? |
242 |
> |
(oldCapacity + capacityIncrement) : (oldCapacity * 2); |
243 |
> |
if (newCapacity < minCapacity) { |
244 |
> |
newCapacity = minCapacity; |
245 |
> |
} |
246 |
|
elementData = Arrays.copyOf(elementData, newCapacity); |
247 |
< |
} |
247 |
> |
} |
248 |
|
} |
249 |
|
|
250 |
|
/** |
257 |
|
* @throws ArrayIndexOutOfBoundsException if the new size is negative |
258 |
|
*/ |
259 |
|
public synchronized void setSize(int newSize) { |
260 |
< |
modCount++; |
261 |
< |
if (newSize > elementCount) { |
262 |
< |
ensureCapacityHelper(newSize); |
263 |
< |
} else { |
264 |
< |
for (int i = newSize ; i < elementCount ; i++) { |
265 |
< |
elementData[i] = null; |
266 |
< |
} |
267 |
< |
} |
268 |
< |
elementCount = newSize; |
260 |
> |
modCount++; |
261 |
> |
if (newSize > elementCount) { |
262 |
> |
ensureCapacityHelper(newSize); |
263 |
> |
} else { |
264 |
> |
for (int i = newSize ; i < elementCount ; i++) { |
265 |
> |
elementData[i] = null; |
266 |
> |
} |
267 |
> |
} |
268 |
> |
elementCount = newSize; |
269 |
|
} |
270 |
|
|
271 |
|
/** |
276 |
|
* of this vector) |
277 |
|
*/ |
278 |
|
public synchronized int capacity() { |
279 |
< |
return elementData.length; |
279 |
> |
return elementData.length; |
280 |
|
} |
281 |
|
|
282 |
|
/** |
285 |
|
* @return the number of components in this vector |
286 |
|
*/ |
287 |
|
public synchronized int size() { |
288 |
< |
return elementCount; |
288 |
> |
return elementCount; |
289 |
|
} |
290 |
|
|
291 |
|
/** |
296 |
|
* {@code false} otherwise. |
297 |
|
*/ |
298 |
|
public synchronized boolean isEmpty() { |
299 |
< |
return elementCount == 0; |
299 |
> |
return elementCount == 0; |
300 |
|
} |
301 |
|
|
302 |
|
/** |
309 |
|
* @see Iterator |
310 |
|
*/ |
311 |
|
public Enumeration<E> elements() { |
312 |
< |
return new Enumeration<E>() { |
313 |
< |
int count = 0; |
312 |
> |
return new Enumeration<E>() { |
313 |
> |
int count = 0; |
314 |
> |
|
315 |
> |
public boolean hasMoreElements() { |
316 |
> |
return count < elementCount; |
317 |
> |
} |
318 |
|
|
319 |
< |
public boolean hasMoreElements() { |
320 |
< |
return count < elementCount; |
321 |
< |
} |
322 |
< |
|
323 |
< |
public E nextElement() { |
324 |
< |
synchronized (Vector.this) { |
325 |
< |
if (count < elementCount) { |
326 |
< |
return (E)elementData[count++]; |
327 |
< |
} |
304 |
< |
} |
305 |
< |
throw new NoSuchElementException("Vector Enumeration"); |
306 |
< |
} |
307 |
< |
}; |
319 |
> |
public E nextElement() { |
320 |
> |
synchronized (Vector.this) { |
321 |
> |
if (count < elementCount) { |
322 |
> |
return elementData(count++); |
323 |
> |
} |
324 |
> |
} |
325 |
> |
throw new NoSuchElementException("Vector Enumeration"); |
326 |
> |
} |
327 |
> |
}; |
328 |
|
} |
329 |
|
|
330 |
|
/** |
337 |
|
* @return {@code true} if this vector contains the specified element |
338 |
|
*/ |
339 |
|
public boolean contains(Object o) { |
340 |
< |
return indexOf(o, 0) >= 0; |
340 |
> |
return indexOf(o, 0) >= 0; |
341 |
|
} |
342 |
|
|
343 |
|
/** |
352 |
|
* this vector, or -1 if this vector does not contain the element |
353 |
|
*/ |
354 |
|
public int indexOf(Object o) { |
355 |
< |
return indexOf(o, 0); |
355 |
> |
return indexOf(o, 0); |
356 |
|
} |
357 |
|
|
358 |
|
/** |
372 |
|
* @see Object#equals(Object) |
373 |
|
*/ |
374 |
|
public synchronized int indexOf(Object o, int index) { |
375 |
< |
if (o == null) { |
376 |
< |
for (int i = index ; i < elementCount ; i++) |
377 |
< |
if (elementData[i]==null) |
378 |
< |
return i; |
379 |
< |
} else { |
380 |
< |
for (int i = index ; i < elementCount ; i++) |
381 |
< |
if (o.equals(elementData[i])) |
382 |
< |
return i; |
383 |
< |
} |
384 |
< |
return -1; |
375 |
> |
if (o == null) { |
376 |
> |
for (int i = index ; i < elementCount ; i++) |
377 |
> |
if (elementData[i]==null) |
378 |
> |
return i; |
379 |
> |
} else { |
380 |
> |
for (int i = index ; i < elementCount ; i++) |
381 |
> |
if (o.equals(elementData[i])) |
382 |
> |
return i; |
383 |
> |
} |
384 |
> |
return -1; |
385 |
|
} |
386 |
|
|
387 |
|
/** |
396 |
|
* this vector, or -1 if this vector does not contain the element |
397 |
|
*/ |
398 |
|
public synchronized int lastIndexOf(Object o) { |
399 |
< |
return lastIndexOf(o, elementCount-1); |
399 |
> |
return lastIndexOf(o, elementCount-1); |
400 |
|
} |
401 |
|
|
402 |
|
/** |
419 |
|
if (index >= elementCount) |
420 |
|
throw new IndexOutOfBoundsException(index + " >= "+ elementCount); |
421 |
|
|
422 |
< |
if (o == null) { |
423 |
< |
for (int i = index; i >= 0; i--) |
424 |
< |
if (elementData[i]==null) |
425 |
< |
return i; |
426 |
< |
} else { |
427 |
< |
for (int i = index; i >= 0; i--) |
428 |
< |
if (o.equals(elementData[i])) |
429 |
< |
return i; |
430 |
< |
} |
431 |
< |
return -1; |
422 |
> |
if (o == null) { |
423 |
> |
for (int i = index; i >= 0; i--) |
424 |
> |
if (elementData[i]==null) |
425 |
> |
return i; |
426 |
> |
} else { |
427 |
> |
for (int i = index; i >= 0; i--) |
428 |
> |
if (o.equals(elementData[i])) |
429 |
> |
return i; |
430 |
> |
} |
431 |
> |
return -1; |
432 |
|
} |
433 |
|
|
434 |
|
/** |
440 |
|
* @param index an index into this vector |
441 |
|
* @return the component at the specified index |
442 |
|
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
443 |
< |
* ({@code index < 0 || index >= size()}) |
443 |
> |
* ({@code index < 0 || index >= size()}) |
444 |
|
*/ |
445 |
|
public synchronized E elementAt(int index) { |
446 |
< |
if (index >= elementCount) { |
447 |
< |
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
448 |
< |
} |
446 |
> |
if (index >= elementCount) { |
447 |
> |
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
448 |
> |
} |
449 |
|
|
450 |
< |
return (E)elementData[index]; |
450 |
> |
return elementData(index); |
451 |
|
} |
452 |
|
|
453 |
|
/** |
458 |
|
* @throws NoSuchElementException if this vector has no components |
459 |
|
*/ |
460 |
|
public synchronized E firstElement() { |
461 |
< |
if (elementCount == 0) { |
462 |
< |
throw new NoSuchElementException(); |
463 |
< |
} |
464 |
< |
return (E)elementData[0]; |
461 |
> |
if (elementCount == 0) { |
462 |
> |
throw new NoSuchElementException(); |
463 |
> |
} |
464 |
> |
return elementData(0); |
465 |
|
} |
466 |
|
|
467 |
|
/** |
472 |
|
* @throws NoSuchElementException if this vector is empty |
473 |
|
*/ |
474 |
|
public synchronized E lastElement() { |
475 |
< |
if (elementCount == 0) { |
476 |
< |
throw new NoSuchElementException(); |
477 |
< |
} |
478 |
< |
return (E)elementData[elementCount - 1]; |
475 |
> |
if (elementCount == 0) { |
476 |
> |
throw new NoSuchElementException(); |
477 |
> |
} |
478 |
> |
return elementData(elementCount - 1); |
479 |
|
} |
480 |
|
|
481 |
|
/** |
496 |
|
* @param obj what the component is to be set to |
497 |
|
* @param index the specified index |
498 |
|
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
499 |
< |
* ({@code index < 0 || index >= size()}) |
499 |
> |
* ({@code index < 0 || index >= size()}) |
500 |
|
*/ |
501 |
|
public synchronized void setElementAt(E obj, int index) { |
502 |
< |
if (index >= elementCount) { |
503 |
< |
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
504 |
< |
elementCount); |
505 |
< |
} |
506 |
< |
elementData[index] = obj; |
502 |
> |
if (index >= elementCount) { |
503 |
> |
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
504 |
> |
elementCount); |
505 |
> |
} |
506 |
> |
elementData[index] = obj; |
507 |
|
} |
508 |
|
|
509 |
|
/** |
523 |
|
* |
524 |
|
* @param index the index of the object to remove |
525 |
|
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
526 |
< |
* ({@code index < 0 || index >= size()}) |
526 |
> |
* ({@code index < 0 || index >= size()}) |
527 |
|
*/ |
528 |
|
public synchronized void removeElementAt(int index) { |
529 |
< |
modCount++; |
530 |
< |
if (index >= elementCount) { |
531 |
< |
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
532 |
< |
elementCount); |
533 |
< |
} |
534 |
< |
else if (index < 0) { |
535 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
536 |
< |
} |
537 |
< |
int j = elementCount - index - 1; |
538 |
< |
if (j > 0) { |
539 |
< |
System.arraycopy(elementData, index + 1, elementData, index, j); |
540 |
< |
} |
541 |
< |
elementCount--; |
542 |
< |
elementData[elementCount] = null; /* to let gc do its work */ |
529 |
> |
modCount++; |
530 |
> |
if (index >= elementCount) { |
531 |
> |
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
532 |
> |
elementCount); |
533 |
> |
} |
534 |
> |
else if (index < 0) { |
535 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
536 |
> |
} |
537 |
> |
int j = elementCount - index - 1; |
538 |
> |
if (j > 0) { |
539 |
> |
System.arraycopy(elementData, index + 1, elementData, index, j); |
540 |
> |
} |
541 |
> |
elementCount--; |
542 |
> |
elementData[elementCount] = null; /* to let gc do its work */ |
543 |
|
} |
544 |
|
|
545 |
|
/** |
563 |
|
* @param obj the component to insert |
564 |
|
* @param index where to insert the new component |
565 |
|
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
566 |
< |
* ({@code index < 0 || index > size()}) |
566 |
> |
* ({@code index < 0 || index > size()}) |
567 |
|
*/ |
568 |
|
public synchronized void insertElementAt(E obj, int index) { |
569 |
< |
modCount++; |
570 |
< |
if (index > elementCount) { |
571 |
< |
throw new ArrayIndexOutOfBoundsException(index |
572 |
< |
+ " > " + elementCount); |
573 |
< |
} |
574 |
< |
ensureCapacityHelper(elementCount + 1); |
575 |
< |
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index); |
576 |
< |
elementData[index] = obj; |
577 |
< |
elementCount++; |
569 |
> |
modCount++; |
570 |
> |
if (index > elementCount) { |
571 |
> |
throw new ArrayIndexOutOfBoundsException(index |
572 |
> |
+ " > " + elementCount); |
573 |
> |
} |
574 |
> |
ensureCapacityHelper(elementCount + 1); |
575 |
> |
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index); |
576 |
> |
elementData[index] = obj; |
577 |
> |
elementCount++; |
578 |
|
} |
579 |
|
|
580 |
|
/** |
589 |
|
* @param obj the component to be added |
590 |
|
*/ |
591 |
|
public synchronized void addElement(E obj) { |
592 |
< |
modCount++; |
593 |
< |
ensureCapacityHelper(elementCount + 1); |
594 |
< |
elementData[elementCount++] = obj; |
592 |
> |
modCount++; |
593 |
> |
ensureCapacityHelper(elementCount + 1); |
594 |
> |
elementData[elementCount++] = obj; |
595 |
|
} |
596 |
|
|
597 |
|
/** |
610 |
|
* vector; {@code false} otherwise. |
611 |
|
*/ |
612 |
|
public synchronized boolean removeElement(Object obj) { |
613 |
< |
modCount++; |
614 |
< |
int i = indexOf(obj); |
615 |
< |
if (i >= 0) { |
616 |
< |
removeElementAt(i); |
617 |
< |
return true; |
618 |
< |
} |
619 |
< |
return false; |
613 |
> |
modCount++; |
614 |
> |
int i = indexOf(obj); |
615 |
> |
if (i >= 0) { |
616 |
> |
removeElementAt(i); |
617 |
> |
return true; |
618 |
> |
} |
619 |
> |
return false; |
620 |
|
} |
621 |
|
|
622 |
|
/** |
627 |
|
*/ |
628 |
|
public synchronized void removeAllElements() { |
629 |
|
modCount++; |
630 |
< |
// Let gc do its work |
631 |
< |
for (int i = 0; i < elementCount; i++) |
632 |
< |
elementData[i] = null; |
630 |
> |
// Let gc do its work |
631 |
> |
for (int i = 0; i < elementCount; i++) |
632 |
> |
elementData[i] = null; |
633 |
|
|
634 |
< |
elementCount = 0; |
634 |
> |
elementCount = 0; |
635 |
|
} |
636 |
|
|
637 |
|
/** |
642 |
|
* @return a clone of this vector |
643 |
|
*/ |
644 |
|
public synchronized Object clone() { |
645 |
< |
try { |
646 |
< |
Vector<E> v = (Vector<E>) super.clone(); |
647 |
< |
v.elementData = Arrays.copyOf(elementData, elementCount); |
648 |
< |
v.modCount = 0; |
649 |
< |
return v; |
650 |
< |
} catch (CloneNotSupportedException e) { |
651 |
< |
// this shouldn't happen, since we are Cloneable |
652 |
< |
throw new InternalError(); |
653 |
< |
} |
645 |
> |
try { |
646 |
> |
@SuppressWarnings("unchecked") |
647 |
> |
Vector<E> v = (Vector<E>) super.clone(); |
648 |
> |
v.elementData = Arrays.copyOf(elementData, elementCount); |
649 |
> |
v.modCount = 0; |
650 |
> |
return v; |
651 |
> |
} catch (CloneNotSupportedException e) { |
652 |
> |
// this shouldn't happen, since we are Cloneable |
653 |
> |
throw new InternalError(); |
654 |
> |
} |
655 |
|
} |
656 |
|
|
657 |
|
/** |
679 |
|
* does not contain any null elements.) |
680 |
|
* |
681 |
|
* @param a the array into which the elements of the Vector are to |
682 |
< |
* be stored, if it is big enough; otherwise, a new array of the |
683 |
< |
* same runtime type is allocated for this purpose. |
682 |
> |
* be stored, if it is big enough; otherwise, a new array of the |
683 |
> |
* same runtime type is allocated for this purpose. |
684 |
|
* @return an array containing the elements of the Vector |
685 |
|
* @throws ArrayStoreException if the runtime type of a is not a supertype |
686 |
|
* of the runtime type of every element in this Vector |
687 |
|
* @throws NullPointerException if the given array is null |
688 |
|
* @since 1.2 |
689 |
|
*/ |
690 |
+ |
@SuppressWarnings("unchecked") |
691 |
|
public synchronized <T> T[] toArray(T[] a) { |
692 |
|
if (a.length < elementCount) |
693 |
|
return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass()); |
694 |
|
|
695 |
< |
System.arraycopy(elementData, 0, a, 0, elementCount); |
695 |
> |
System.arraycopy(elementData, 0, a, 0, elementCount); |
696 |
|
|
697 |
|
if (a.length > elementCount) |
698 |
|
a[elementCount] = null; |
702 |
|
|
703 |
|
// Positional Access Operations |
704 |
|
|
705 |
+ |
@SuppressWarnings("unchecked") |
706 |
+ |
E elementData(int index) { |
707 |
+ |
return (E) elementData[index]; |
708 |
+ |
} |
709 |
+ |
|
710 |
|
/** |
711 |
|
* Returns the element at the specified position in this Vector. |
712 |
|
* |
717 |
|
* @since 1.2 |
718 |
|
*/ |
719 |
|
public synchronized E get(int index) { |
720 |
< |
if (index >= elementCount) |
721 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
720 |
> |
if (index >= elementCount) |
721 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
722 |
|
|
723 |
< |
return (E)elementData[index]; |
723 |
> |
return elementData(index); |
724 |
|
} |
725 |
|
|
726 |
|
/** |
731 |
|
* @param element element to be stored at the specified position |
732 |
|
* @return the element previously at the specified position |
733 |
|
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
734 |
< |
* ({@code index < 0 || index >= size()}) |
734 |
> |
* ({@code index < 0 || index >= size()}) |
735 |
|
* @since 1.2 |
736 |
|
*/ |
737 |
|
public synchronized E set(int index, E element) { |
738 |
< |
if (index >= elementCount) |
739 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
738 |
> |
if (index >= elementCount) |
739 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
740 |
|
|
741 |
< |
Object oldValue = elementData[index]; |
742 |
< |
elementData[index] = element; |
743 |
< |
return (E)oldValue; |
741 |
> |
E oldValue = elementData(index); |
742 |
> |
elementData[index] = element; |
743 |
> |
return oldValue; |
744 |
|
} |
745 |
|
|
746 |
|
/** |
751 |
|
* @since 1.2 |
752 |
|
*/ |
753 |
|
public synchronized boolean add(E e) { |
754 |
< |
modCount++; |
755 |
< |
ensureCapacityHelper(elementCount + 1); |
756 |
< |
elementData[elementCount++] = e; |
754 |
> |
modCount++; |
755 |
> |
ensureCapacityHelper(elementCount + 1); |
756 |
> |
elementData[elementCount++] = e; |
757 |
|
return true; |
758 |
|
} |
759 |
|
|
799 |
|
* @since 1.2 |
800 |
|
*/ |
801 |
|
public synchronized E remove(int index) { |
802 |
< |
modCount++; |
803 |
< |
if (index >= elementCount) |
804 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
805 |
< |
Object oldValue = elementData[index]; |
806 |
< |
|
807 |
< |
int numMoved = elementCount - index - 1; |
808 |
< |
if (numMoved > 0) |
809 |
< |
System.arraycopy(elementData, index+1, elementData, index, |
810 |
< |
numMoved); |
811 |
< |
elementData[--elementCount] = null; // Let gc do its work |
802 |
> |
modCount++; |
803 |
> |
if (index >= elementCount) |
804 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
805 |
> |
E oldValue = elementData(index); |
806 |
> |
|
807 |
> |
int numMoved = elementCount - index - 1; |
808 |
> |
if (numMoved > 0) |
809 |
> |
System.arraycopy(elementData, index+1, elementData, index, |
810 |
> |
numMoved); |
811 |
> |
elementData[--elementCount] = null; // Let gc do its work |
812 |
|
|
813 |
< |
return (E)oldValue; |
813 |
> |
return oldValue; |
814 |
|
} |
815 |
|
|
816 |
|
/** |
832 |
|
* @param c a collection whose elements will be tested for containment |
833 |
|
* in this Vector |
834 |
|
* @return true if this Vector contains all of the elements in the |
835 |
< |
* specified collection |
835 |
> |
* specified collection |
836 |
|
* @throws NullPointerException if the specified collection is null |
837 |
|
*/ |
838 |
|
public synchronized boolean containsAll(Collection<?> c) { |
853 |
|
* @since 1.2 |
854 |
|
*/ |
855 |
|
public synchronized boolean addAll(Collection<? extends E> c) { |
856 |
< |
modCount++; |
856 |
> |
modCount++; |
857 |
|
Object[] a = c.toArray(); |
858 |
|
int numNew = a.length; |
859 |
< |
ensureCapacityHelper(elementCount + numNew); |
859 |
> |
ensureCapacityHelper(elementCount + numNew); |
860 |
|
System.arraycopy(a, 0, elementData, elementCount, numNew); |
861 |
|
elementCount += numNew; |
862 |
< |
return numNew != 0; |
862 |
> |
return numNew != 0; |
863 |
|
} |
864 |
|
|
865 |
|
/** |
896 |
|
* elements (optional), or if the specified collection is null |
897 |
|
* @since 1.2 |
898 |
|
*/ |
899 |
< |
public synchronized boolean retainAll(Collection<?> c) { |
899 |
> |
public synchronized boolean retainAll(Collection<?> c) { |
900 |
|
return super.retainAll(c); |
901 |
|
} |
902 |
|
|
918 |
|
* @since 1.2 |
919 |
|
*/ |
920 |
|
public synchronized boolean addAll(int index, Collection<? extends E> c) { |
921 |
< |
modCount++; |
922 |
< |
if (index < 0 || index > elementCount) |
923 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
921 |
> |
modCount++; |
922 |
> |
if (index < 0 || index > elementCount) |
923 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
924 |
|
|
925 |
|
Object[] a = c.toArray(); |
926 |
< |
int numNew = a.length; |
927 |
< |
ensureCapacityHelper(elementCount + numNew); |
926 |
> |
int numNew = a.length; |
927 |
> |
ensureCapacityHelper(elementCount + numNew); |
928 |
|
|
929 |
< |
int numMoved = elementCount - index; |
930 |
< |
if (numMoved > 0) |
931 |
< |
System.arraycopy(elementData, index, elementData, index + numNew, |
932 |
< |
numMoved); |
929 |
> |
int numMoved = elementCount - index; |
930 |
> |
if (numMoved > 0) |
931 |
> |
System.arraycopy(elementData, index, elementData, index + numNew, |
932 |
> |
numMoved); |
933 |
|
|
934 |
|
System.arraycopy(a, 0, elementData, index, numNew); |
935 |
< |
elementCount += numNew; |
936 |
< |
return numNew != 0; |
935 |
> |
elementCount += numNew; |
936 |
> |
return numNew != 0; |
937 |
|
} |
938 |
|
|
939 |
|
/** |
968 |
|
} |
969 |
|
|
970 |
|
/** |
971 |
< |
* Removes from this List all of the elements whose index is between |
972 |
< |
* fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding |
973 |
< |
* elements to the left (reduces their index). |
974 |
< |
* This call shortens the Vector by (toIndex - fromIndex) elements. (If |
975 |
< |
* toIndex==fromIndex, this operation has no effect.) |
971 |
> |
* Returns a view of the portion of this List between fromIndex, |
972 |
> |
* inclusive, and toIndex, exclusive. (If fromIndex and toIndex are |
973 |
> |
* equal, the returned List is empty.) The returned List is backed by this |
974 |
> |
* List, so changes in the returned List are reflected in this List, and |
975 |
> |
* vice-versa. The returned List supports all of the optional List |
976 |
> |
* operations supported by this List. |
977 |
|
* |
978 |
< |
* @param fromIndex index of first element to be removed |
979 |
< |
* @param toIndex index after last element to be removed |
978 |
> |
* <p>This method eliminates the need for explicit range operations (of |
979 |
> |
* the sort that commonly exist for arrays). Any operation that expects |
980 |
> |
* a List can be used as a range operation by operating on a subList view |
981 |
> |
* instead of a whole List. For example, the following idiom |
982 |
> |
* removes a range of elements from a List: |
983 |
> |
* <pre> |
984 |
> |
* list.subList(from, to).clear(); |
985 |
> |
* </pre> |
986 |
> |
* Similar idioms may be constructed for indexOf and lastIndexOf, |
987 |
> |
* and all of the algorithms in the Collections class can be applied to |
988 |
> |
* a subList. |
989 |
> |
* |
990 |
> |
* <p>The semantics of the List returned by this method become undefined if |
991 |
> |
* the backing list (i.e., this List) is <i>structurally modified</i> in |
992 |
> |
* any way other than via the returned List. (Structural modifications are |
993 |
> |
* those that change the size of the List, or otherwise perturb it in such |
994 |
> |
* a fashion that iterations in progress may yield incorrect results.) |
995 |
> |
* |
996 |
> |
* @param fromIndex low endpoint (inclusive) of the subList |
997 |
> |
* @param toIndex high endpoint (exclusive) of the subList |
998 |
> |
* @return a view of the specified range within this List |
999 |
> |
* @throws IndexOutOfBoundsException if an endpoint index value is out of range |
1000 |
> |
* {@code (fromIndex < 0 || toIndex > size)} |
1001 |
> |
* @throws IllegalArgumentException if the endpoint indices are out of order |
1002 |
> |
* {@code (fromIndex > toIndex)} |
1003 |
> |
*/ |
1004 |
> |
public synchronized List<E> subList(int fromIndex, int toIndex) { |
1005 |
> |
return Collections.synchronizedList(super.subList(fromIndex, toIndex), |
1006 |
> |
this); |
1007 |
> |
} |
1008 |
> |
|
1009 |
> |
/** |
1010 |
> |
* Removes from this list all of the elements whose index is between |
1011 |
> |
* {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. |
1012 |
> |
* Shifts any succeeding elements to the left (reduces their index). |
1013 |
> |
* This call shortens the list by {@code (toIndex - fromIndex)} elements. |
1014 |
> |
* (If {@code toIndex==fromIndex}, this operation has no effect.) |
1015 |
|
*/ |
1016 |
|
protected synchronized void removeRange(int fromIndex, int toIndex) { |
1017 |
< |
modCount++; |
1018 |
< |
int numMoved = elementCount - toIndex; |
1017 |
> |
modCount++; |
1018 |
> |
int numMoved = elementCount - toIndex; |
1019 |
|
System.arraycopy(elementData, toIndex, elementData, fromIndex, |
1020 |
|
numMoved); |
1021 |
|
|
1022 |
< |
// Let gc do its work |
1023 |
< |
int newElementCount = elementCount - (toIndex-fromIndex); |
1024 |
< |
while (elementCount != newElementCount) |
1025 |
< |
elementData[--elementCount] = null; |
1022 |
> |
// Let gc do its work |
1023 |
> |
int newElementCount = elementCount - (toIndex-fromIndex); |
1024 |
> |
while (elementCount != newElementCount) |
1025 |
> |
elementData[--elementCount] = null; |
1026 |
|
} |
1027 |
|
|
1028 |
|
/** |
1033 |
|
private synchronized void writeObject(java.io.ObjectOutputStream s) |
1034 |
|
throws java.io.IOException |
1035 |
|
{ |
1036 |
< |
s.defaultWriteObject(); |
1036 |
> |
s.defaultWriteObject(); |
1037 |
|
} |
1038 |
|
|
1039 |
|
/** |
1040 |
< |
* Returns a list-iterator of the elements in this list (in proper |
1040 |
> |
* Returns a list iterator over the elements in this list (in proper |
1041 |
|
* sequence), starting at the specified position in the list. |
1042 |
< |
* Obeys the general contract of {@link List#listIterator(int)}. |
1042 |
> |
* The specified index indicates the first element that would be |
1043 |
> |
* returned by an initial call to {@link ListIterator#next next}. |
1044 |
> |
* An initial call to {@link ListIterator#previous previous} would |
1045 |
> |
* return the element with the specified index minus one. |
1046 |
> |
* |
1047 |
> |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1048 |
|
* |
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 |
1049 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
1050 |
|
*/ |
1051 |
|
public synchronized ListIterator<E> listIterator(int index) { |
1052 |
< |
if (index < 0 || index > elementCount) |
1052 |
> |
if (index < 0 || index > elementCount) |
1053 |
|
throw new IndexOutOfBoundsException("Index: "+index); |
1054 |
< |
return new VectorIterator(index, elementCount); |
1054 |
> |
return new ListItr(index); |
1055 |
|
} |
1056 |
|
|
1057 |
|
/** |
1058 |
< |
* {@inheritDoc} |
1058 |
> |
* Returns a list iterator over the elements in this list (in proper |
1059 |
> |
* sequence). |
1060 |
> |
* |
1061 |
> |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1062 |
> |
* |
1063 |
> |
* @see #listIterator(int) |
1064 |
|
*/ |
1065 |
|
public synchronized ListIterator<E> listIterator() { |
1066 |
< |
return new VectorIterator(0, elementCount); |
1066 |
> |
return new ListItr(0); |
1067 |
|
} |
1068 |
|
|
1069 |
|
/** |
1070 |
|
* Returns an iterator over the elements in this list in proper sequence. |
1071 |
|
* |
1072 |
+ |
* <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1073 |
+ |
* |
1074 |
|
* @return an iterator over the elements in this list in proper sequence |
1075 |
|
*/ |
1076 |
|
public synchronized Iterator<E> iterator() { |
1077 |
< |
return new VectorIterator(0, elementCount); |
1077 |
> |
return new Itr(); |
1078 |
|
} |
1079 |
|
|
1080 |
|
/** |
1081 |
< |
* 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. |
1081 |
> |
* An optimized version of AbstractList.Itr |
1082 |
|
*/ |
1083 |
< |
final synchronized Object iteratorGet(int index, int expectedModCount) { |
1084 |
< |
if (modCount == expectedModCount) { |
1085 |
< |
try { |
1086 |
< |
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 |
< |
} |
1083 |
> |
private class Itr implements Iterator<E> { |
1084 |
> |
int cursor; // index of next element to return |
1085 |
> |
int lastRet = -1; // index of last element returned; -1 if no such |
1086 |
> |
int expectedModCount = modCount; |
1087 |
|
|
1088 |
< |
public boolean hasNext() { |
1089 |
< |
return cursor < fence; |
1090 |
< |
} |
1091 |
< |
|
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; |
1088 |
> |
public boolean hasNext() { |
1089 |
> |
// Racy but within spec, since modifications are checked |
1090 |
> |
// within or after synchronization in next/previous |
1091 |
> |
return cursor != elementCount; |
1092 |
|
} |
1093 |
|
|
1094 |
< |
public void set(E e) { |
1095 |
< |
if (lastRet < 0) |
1096 |
< |
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 { |
1094 |
> |
public E next() { |
1095 |
> |
synchronized (Vector.this) { |
1096 |
> |
checkForComodification(); |
1097 |
|
int i = cursor; |
1098 |
< |
Vector.this.add(i, e); |
1098 |
> |
if (i >= elementCount) |
1099 |
> |
throw new NoSuchElementException(); |
1100 |
|
cursor = i + 1; |
1101 |
< |
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 |
< |
/** |
1136 |
< |
* Returns a view of the portion of this List between fromIndex, |
1137 |
< |
* inclusive, and toIndex, exclusive. (If fromIndex and toIndex are |
1138 |
< |
* equal, the returned List is empty.) The returned List is backed by this |
1139 |
< |
* List, so changes in the returned List are reflected in this List, and |
1140 |
< |
* vice-versa. The returned List supports all of the optional List |
1141 |
< |
* operations supported by this List. |
1142 |
< |
* |
1143 |
< |
* <p>This method eliminates the need for explicit range operations (of |
1144 |
< |
* the sort that commonly exist for arrays). Any operation that expects |
1145 |
< |
* a List can be used as a range operation by operating on a subList view |
1146 |
< |
* instead of a whole List. For example, the following idiom |
1147 |
< |
* removes a range of elements from a List: |
1148 |
< |
* <pre> |
1149 |
< |
* list.subList(from, to).clear(); |
1150 |
< |
* </pre> |
1151 |
< |
* Similar idioms may be constructed for indexOf and lastIndexOf, |
1152 |
< |
* and all of the algorithms in the Collections class can be applied to |
1153 |
< |
* a subList. |
1154 |
< |
* |
1155 |
< |
* <p>The semantics of the List returned by this method become undefined if |
1156 |
< |
* the backing list (i.e., this List) is <i>structurally modified</i> in |
1157 |
< |
* any way other than via the returned List. (Structural modifications are |
1158 |
< |
* those that change the size of the List, or otherwise perturb it in such |
1159 |
< |
* a fashion that iterations in progress may yield incorrect results.) |
1160 |
< |
* |
1161 |
< |
* @param fromIndex low endpoint (inclusive) of the subList |
1162 |
< |
* @param toIndex high endpoint (exclusive) of the subList |
1163 |
< |
* @return a view of the specified range within this List |
1164 |
< |
* @throws IndexOutOfBoundsException if an endpoint index value is out of range |
1165 |
< |
* {@code (fromIndex < 0 || toIndex > size)} |
1166 |
< |
* @throws IllegalArgumentException if the endpoint indices are out of order |
1167 |
< |
* {@code (fromIndex > toIndex)} |
1168 |
< |
*/ |
1169 |
< |
public synchronized List<E> subList(int fromIndex, int toIndex) { |
1170 |
< |
return new VectorSubList(this, this, fromIndex, fromIndex, toIndex); |
1171 |
< |
} |
1172 |
< |
|
1173 |
< |
/** |
1174 |
< |
* This class specializes the AbstractList version of SubList to |
1175 |
< |
* avoid the double-indirection penalty that would arise using a |
1176 |
< |
* synchronized wrapper, as well as to avoid some unnecessary |
1177 |
< |
* checks in sublist iterators. |
1178 |
< |
*/ |
1179 |
< |
private static final class VectorSubList<E> extends AbstractList<E> implements RandomAccess { |
1180 |
< |
final Vector<E> base; // base list |
1181 |
< |
final AbstractList<E> parent; // Creating list |
1182 |
< |
final int baseOffset; // index wrt Vector |
1183 |
< |
final int parentOffset; // index wrt parent |
1184 |
< |
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 |
< |
} |
1221 |
< |
|
1222 |
< |
public E get(int index) { |
1223 |
< |
synchronized(base) { |
1224 |
< |
if (index < 0 || index >= length) |
1225 |
< |
throw indexError(index); |
1226 |
< |
if (base.modCount != modCount) |
1227 |
< |
throw new ConcurrentModificationException(); |
1228 |
< |
return base.get(index + baseOffset); |
1229 |
< |
} |
1230 |
< |
} |
1231 |
< |
|
1232 |
< |
public int size() { |
1233 |
< |
synchronized(base) { |
1234 |
< |
if (base.modCount != modCount) |
1235 |
< |
throw new ConcurrentModificationException(); |
1236 |
< |
return length; |
1237 |
< |
} |
1238 |
< |
} |
1239 |
< |
|
1240 |
< |
public void add(int index, E element) { |
1241 |
< |
synchronized(base) { |
1242 |
< |
if (index < 0 || index > length) |
1243 |
< |
throw indexError(index); |
1244 |
< |
if (base.modCount != modCount) |
1245 |
< |
throw new ConcurrentModificationException(); |
1246 |
< |
parent.add(index + parentOffset, element); |
1247 |
< |
length++; |
1248 |
< |
modCount = base.modCount; |
1101 |
> |
return elementData(lastRet = i); |
1102 |
|
} |
1103 |
|
} |
1104 |
|
|
1105 |
< |
public E remove(int index) { |
1106 |
< |
synchronized(base) { |
1107 |
< |
if (index < 0 || index >= length) |
1108 |
< |
throw indexError(index); |
1109 |
< |
if (base.modCount != modCount) |
1110 |
< |
throw new ConcurrentModificationException(); |
1111 |
< |
E result = parent.remove(index + parentOffset); |
1259 |
< |
length--; |
1260 |
< |
modCount = base.modCount; |
1261 |
< |
return result; |
1105 |
> |
public void remove() { |
1106 |
> |
if (lastRet == -1) |
1107 |
> |
throw new IllegalStateException(); |
1108 |
> |
synchronized (Vector.this) { |
1109 |
> |
checkForComodification(); |
1110 |
> |
Vector.this.remove(lastRet); |
1111 |
> |
expectedModCount = modCount; |
1112 |
|
} |
1113 |
+ |
cursor = lastRet; |
1114 |
+ |
lastRet = -1; |
1115 |
|
} |
1116 |
|
|
1117 |
< |
protected void removeRange(int fromIndex, int toIndex) { |
1118 |
< |
synchronized(base) { |
1119 |
< |
if (base.modCount != modCount) |
1268 |
< |
throw new ConcurrentModificationException(); |
1269 |
< |
parent.removeRange(fromIndex + parentOffset, |
1270 |
< |
toIndex + parentOffset); |
1271 |
< |
length -= (toIndex-fromIndex); |
1272 |
< |
modCount = base.modCount; |
1273 |
< |
} |
1274 |
< |
} |
1275 |
< |
|
1276 |
< |
public boolean addAll(Collection<? extends E> c) { |
1277 |
< |
return addAll(length, c); |
1278 |
< |
} |
1279 |
< |
|
1280 |
< |
public boolean addAll(int index, Collection<? extends E> c) { |
1281 |
< |
synchronized(base) { |
1282 |
< |
if (index < 0 || index > length) |
1283 |
< |
throw indexError(index); |
1284 |
< |
int cSize = c.size(); |
1285 |
< |
if (cSize==0) |
1286 |
< |
return false; |
1287 |
< |
|
1288 |
< |
if (base.modCount != modCount) |
1289 |
< |
throw new ConcurrentModificationException(); |
1290 |
< |
parent.addAll(parentOffset + index, c); |
1291 |
< |
modCount = base.modCount; |
1292 |
< |
length += cSize; |
1293 |
< |
return true; |
1294 |
< |
} |
1295 |
< |
} |
1296 |
< |
|
1297 |
< |
public boolean equals(Object o) { |
1298 |
< |
synchronized(base) {return super.equals(o);} |
1299 |
< |
} |
1300 |
< |
|
1301 |
< |
public int hashCode() { |
1302 |
< |
synchronized(base) {return super.hashCode();} |
1303 |
< |
} |
1304 |
< |
|
1305 |
< |
public int indexOf(Object o) { |
1306 |
< |
synchronized(base) {return super.indexOf(o);} |
1307 |
< |
} |
1308 |
< |
|
1309 |
< |
public int lastIndexOf(Object o) { |
1310 |
< |
synchronized(base) {return super.lastIndexOf(o);} |
1117 |
> |
final void checkForComodification() { |
1118 |
> |
if (modCount != expectedModCount) |
1119 |
> |
throw new ConcurrentModificationException(); |
1120 |
|
} |
1121 |
+ |
} |
1122 |
|
|
1123 |
< |
public List<E> subList(int fromIndex, int toIndex) { |
1124 |
< |
return new VectorSubList(base, this, fromIndex + baseOffset, |
1125 |
< |
fromIndex, toIndex); |
1123 |
> |
/** |
1124 |
> |
* An optimized version of AbstractList.ListItr |
1125 |
> |
*/ |
1126 |
> |
final class ListItr extends Itr implements ListIterator<E> { |
1127 |
> |
ListItr(int index) { |
1128 |
> |
super(); |
1129 |
> |
cursor = index; |
1130 |
|
} |
1131 |
|
|
1132 |
< |
public Iterator<E> iterator() { |
1133 |
< |
synchronized(base) { |
1320 |
< |
return new VectorSubListIterator(this, 0); |
1321 |
< |
} |
1132 |
> |
public boolean hasPrevious() { |
1133 |
> |
return cursor != 0; |
1134 |
|
} |
1135 |
|
|
1136 |
< |
public synchronized ListIterator<E> listIterator() { |
1137 |
< |
synchronized(base) { |
1326 |
< |
return new VectorSubListIterator(this, 0); |
1327 |
< |
} |
1136 |
> |
public int nextIndex() { |
1137 |
> |
return cursor; |
1138 |
|
} |
1139 |
|
|
1140 |
< |
public ListIterator<E> listIterator(int index) { |
1141 |
< |
synchronized(base) { |
1332 |
< |
if (index < 0 || index > length) |
1333 |
< |
throw indexError(index); |
1334 |
< |
return new VectorSubListIterator(this, index); |
1335 |
< |
} |
1140 |
> |
public int previousIndex() { |
1141 |
> |
return cursor - 1; |
1142 |
|
} |
1143 |
|
|
1144 |
< |
/** |
1145 |
< |
* Same idea as VectorIterator, except routing structural |
1146 |
< |
* change operations through the sublist. |
1341 |
< |
*/ |
1342 |
< |
private static final class VectorSubListIterator<E> implements ListIterator<E> { |
1343 |
< |
final Vector<E> base; // base list |
1344 |
< |
final VectorSubList<E> outer; // Sublist creating this iteraor |
1345 |
< |
final int offset; // cursor offset wrt base |
1346 |
< |
int cursor; // Current index |
1347 |
< |
int fence; // Upper bound on cursor |
1348 |
< |
int lastRet; // Index of returned element, or -1 |
1349 |
< |
int expectedModCount; // Expected modCount of base Vector |
1350 |
< |
|
1351 |
< |
VectorSubListIterator(VectorSubList<E> list, int index) { |
1352 |
< |
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 |
< |
} |
1364 |
< |
|
1365 |
< |
public boolean hasPrevious() { |
1366 |
< |
return cursor > 0; |
1367 |
< |
} |
1368 |
< |
|
1369 |
< |
public int nextIndex() { |
1370 |
< |
return cursor; |
1371 |
< |
} |
1372 |
< |
|
1373 |
< |
public int previousIndex() { |
1374 |
< |
return cursor - 1; |
1375 |
< |
} |
1376 |
< |
|
1377 |
< |
public E next() { |
1378 |
< |
int i = cursor; |
1379 |
< |
if (cursor >= fence) |
1380 |
< |
throw new NoSuchElementException(); |
1381 |
< |
Object next = base.iteratorGet(i + offset, expectedModCount); |
1382 |
< |
lastRet = i; |
1383 |
< |
cursor = i + 1; |
1384 |
< |
return (E)next; |
1385 |
< |
} |
1386 |
< |
|
1387 |
< |
public E previous() { |
1144 |
> |
public E previous() { |
1145 |
> |
synchronized (Vector.this) { |
1146 |
> |
checkForComodification(); |
1147 |
|
int i = cursor - 1; |
1148 |
|
if (i < 0) |
1149 |
|
throw new NoSuchElementException(); |
1391 |
– |
Object prev = base.iteratorGet(i + offset, expectedModCount); |
1392 |
– |
lastRet = i; |
1150 |
|
cursor = i; |
1151 |
< |
return (E)prev; |
1395 |
< |
} |
1396 |
< |
|
1397 |
< |
public void set(E e) { |
1398 |
< |
if (lastRet < 0) |
1399 |
< |
throw new IllegalStateException(); |
1400 |
< |
if (base.modCount != expectedModCount) |
1401 |
< |
throw new ConcurrentModificationException(); |
1402 |
< |
try { |
1403 |
< |
outer.set(lastRet, e); |
1404 |
< |
expectedModCount = base.modCount; |
1405 |
< |
} catch (IndexOutOfBoundsException ex) { |
1406 |
< |
throw new ConcurrentModificationException(); |
1407 |
< |
} |
1151 |
> |
return elementData(lastRet = i); |
1152 |
|
} |
1153 |
+ |
} |
1154 |
|
|
1155 |
< |
public void remove() { |
1156 |
< |
int i = lastRet; |
1157 |
< |
if (i < 0) |
1158 |
< |
throw new IllegalStateException(); |
1159 |
< |
if (base.modCount != expectedModCount) |
1160 |
< |
throw new ConcurrentModificationException(); |
1416 |
< |
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 |
< |
} |
1155 |
> |
public void set(E e) { |
1156 |
> |
if (lastRet == -1) |
1157 |
> |
throw new IllegalStateException(); |
1158 |
> |
synchronized (Vector.this) { |
1159 |
> |
checkForComodification(); |
1160 |
> |
Vector.this.set(lastRet, e); |
1161 |
|
} |
1162 |
+ |
} |
1163 |
|
|
1164 |
< |
public void add(E e) { |
1165 |
< |
if (base.modCount != expectedModCount) |
1166 |
< |
throw new ConcurrentModificationException(); |
1167 |
< |
try { |
1168 |
< |
int i = cursor; |
1169 |
< |
outer.add(i, e); |
1434 |
< |
cursor = i + 1; |
1435 |
< |
lastRet = -1; |
1436 |
< |
fence = outer.length; |
1437 |
< |
expectedModCount = base.modCount; |
1438 |
< |
} catch (IndexOutOfBoundsException ex) { |
1439 |
< |
throw new ConcurrentModificationException(); |
1440 |
< |
} |
1164 |
> |
public void add(E e) { |
1165 |
> |
int i = cursor; |
1166 |
> |
synchronized (Vector.this) { |
1167 |
> |
checkForComodification(); |
1168 |
> |
Vector.this.add(i, e); |
1169 |
> |
expectedModCount = modCount; |
1170 |
|
} |
1171 |
+ |
cursor = i + 1; |
1172 |
+ |
lastRet = -1; |
1173 |
|
} |
1174 |
|
} |
1175 |
|
} |
1445 |
– |
|
1446 |
– |
|
1447 |
– |
|