1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* Copyright 1994-2007 Sun Microsystems, Inc. All Rights Reserved. |
3 |
> |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
5 |
< |
* Copyright 2006 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
22 |
> |
* CA 95054 USA or visit www.sun.com if you need additional information or |
23 |
> |
* have any 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 |
254 |
|
* the vector. If the new size is less than the current size, all |
255 |
|
* components at index {@code newSize} and greater are discarded. |
256 |
|
* |
257 |
< |
* @param newSize the new size of this vector |
258 |
< |
* @throws ArrayIndexOutOfBoundsException if new size is negative |
257 |
> |
* @param newSize the new size of this vector |
258 |
> |
* @throws ArrayIndexOutOfBoundsException if the new size is negative |
259 |
|
*/ |
260 |
|
public synchronized void setSize(int newSize) { |
261 |
|
modCount++; |
320 |
|
public E nextElement() { |
321 |
|
synchronized (Vector.this) { |
322 |
|
if (count < elementCount) { |
323 |
< |
return (E)elementData[count++]; |
323 |
> |
return elementData(count++); |
324 |
|
} |
325 |
|
} |
326 |
|
throw new NoSuchElementException("Vector Enumeration"); |
448 |
|
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
449 |
|
} |
450 |
|
|
451 |
< |
return (E)elementData[index]; |
451 |
> |
return elementData(index); |
452 |
|
} |
453 |
|
|
454 |
|
/** |
462 |
|
if (elementCount == 0) { |
463 |
|
throw new NoSuchElementException(); |
464 |
|
} |
465 |
< |
return (E)elementData[0]; |
465 |
> |
return elementData(0); |
466 |
|
} |
467 |
|
|
468 |
|
/** |
476 |
|
if (elementCount == 0) { |
477 |
|
throw new NoSuchElementException(); |
478 |
|
} |
479 |
< |
return (E)elementData[elementCount - 1]; |
479 |
> |
return elementData(elementCount - 1); |
480 |
|
} |
481 |
|
|
482 |
|
/** |
483 |
|
* Sets the component at the specified {@code index} of this |
484 |
|
* vector to be the specified object. The previous component at that |
485 |
< |
* position is discarded.<p> |
485 |
> |
* position is discarded. |
486 |
|
* |
487 |
< |
* The index must be a value greater than or equal to {@code 0} |
488 |
< |
* and less than the current size of the vector. <p> |
487 |
> |
* <p>The index must be a value greater than or equal to {@code 0} |
488 |
> |
* and less than the current size of the vector. |
489 |
|
* |
490 |
< |
* This method is identical in functionality to the set method |
491 |
< |
* (which is part of the List interface). Note that the set method reverses |
492 |
< |
* the order of the parameters, to more closely match array usage. Note |
493 |
< |
* also that the set method returns the old value that was stored at the |
494 |
< |
* specified position. |
490 |
> |
* <p>This method is identical in functionality to the |
491 |
> |
* {@link #set(int, Object) set(int, E)} |
492 |
> |
* method (which is part of the {@link List} interface). Note that the |
493 |
> |
* {@code set} method reverses the order of the parameters, to more closely |
494 |
> |
* match array usage. Note also that the {@code set} method returns the |
495 |
> |
* old value that was stored at the specified position. |
496 |
|
* |
497 |
|
* @param obj what the component is to be set to |
498 |
|
* @param index the specified index |
499 |
< |
* @throws ArrayIndexOutOfBoundsException if the index was invalid |
500 |
< |
* @see #size() |
479 |
< |
* @see List |
480 |
< |
* @see #set(int, java.lang.Object) |
499 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
500 |
> |
* ({@code index < 0 || index >= size()}) |
501 |
|
*/ |
502 |
|
public synchronized void setElementAt(E obj, int index) { |
503 |
|
if (index >= elementCount) { |
517 |
|
* <p>The index must be a value greater than or equal to {@code 0} |
518 |
|
* and less than the current size of the vector. |
519 |
|
* |
520 |
< |
* <p>This method is identical in functionality to the remove method |
521 |
< |
* (which is part of the List interface). Note that the remove method |
522 |
< |
* returns the old value that was stored at the specified position. |
520 |
> |
* <p>This method is identical in functionality to the {@link #remove(int)} |
521 |
> |
* method (which is part of the {@link List} interface). Note that the |
522 |
> |
* {@code remove} method returns the old value that was stored at the |
523 |
> |
* specified position. |
524 |
|
* |
525 |
|
* @param index the index of the object to remove |
526 |
< |
* @exception ArrayIndexOutOfBoundsException if the index was invalid |
527 |
< |
* @see #size() |
507 |
< |
* @see #remove(int) |
508 |
< |
* @see List |
526 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
527 |
> |
* ({@code index < 0 || index >= size()}) |
528 |
|
*/ |
529 |
|
public synchronized void removeElementAt(int index) { |
530 |
|
modCount++; |
555 |
|
* index is equal to the current size of the vector, the new element |
556 |
|
* is appended to the Vector.) |
557 |
|
* |
558 |
< |
* <p>This method is identical in functionality to the add(Object, int) method |
559 |
< |
* (which is part of the List interface). Note that the add method reverses |
560 |
< |
* the order of the parameters, to more closely match array usage. |
558 |
> |
* <p>This method is identical in functionality to the |
559 |
> |
* {@link #add(int, Object) add(int, E)} |
560 |
> |
* method (which is part of the {@link List} interface). Note that the |
561 |
> |
* {@code add} method reverses the order of the parameters, to more closely |
562 |
> |
* match array usage. |
563 |
|
* |
564 |
|
* @param obj the component to insert |
565 |
|
* @param index where to insert the new component |
566 |
< |
* @exception ArrayIndexOutOfBoundsException if the index was invalid |
567 |
< |
* @see #size() |
547 |
< |
* @see #add(int, Object) |
548 |
< |
* @see List |
566 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
567 |
> |
* ({@code index < 0 || index > size()}) |
568 |
|
*/ |
569 |
|
public synchronized void insertElementAt(E obj, int index) { |
570 |
|
modCount++; |
581 |
|
/** |
582 |
|
* Adds the specified component to the end of this vector, |
583 |
|
* increasing its size by one. The capacity of this vector is |
584 |
< |
* increased if its size becomes greater than its capacity. <p> |
584 |
> |
* increased if its size becomes greater than its capacity. |
585 |
|
* |
586 |
< |
* This method is identical in functionality to the add(Object) method |
587 |
< |
* (which is part of the List interface). |
586 |
> |
* <p>This method is identical in functionality to the |
587 |
> |
* {@link #add(Object) add(E)} |
588 |
> |
* method (which is part of the {@link List} interface). |
589 |
|
* |
590 |
|
* @param obj the component to be added |
571 |
– |
* @see #add(Object) |
572 |
– |
* @see List |
591 |
|
*/ |
592 |
|
public synchronized void addElement(E obj) { |
593 |
|
modCount++; |
600 |
|
* from this vector. If the object is found in this vector, each |
601 |
|
* component in the vector with an index greater or equal to the |
602 |
|
* object's index is shifted downward to have an index one smaller |
603 |
< |
* than the value it had previously.<p> |
603 |
> |
* than the value it had previously. |
604 |
|
* |
605 |
< |
* This method is identical in functionality to the remove(Object) |
606 |
< |
* method (which is part of the List interface). |
605 |
> |
* <p>This method is identical in functionality to the |
606 |
> |
* {@link #remove(Object)} method (which is part of the |
607 |
> |
* {@link List} interface). |
608 |
|
* |
609 |
|
* @param obj the component to be removed |
610 |
|
* @return {@code true} if the argument was a component of this |
611 |
|
* vector; {@code false} otherwise. |
593 |
– |
* @see List#remove(Object) |
594 |
– |
* @see List |
612 |
|
*/ |
613 |
|
public synchronized boolean removeElement(Object obj) { |
614 |
|
modCount++; |
621 |
|
} |
622 |
|
|
623 |
|
/** |
624 |
< |
* Removes all components from this vector and sets its size to zero.<p> |
608 |
< |
* |
609 |
< |
* This method is identical in functionality to the clear method |
610 |
< |
* (which is part of the List interface). |
624 |
> |
* Removes all components from this vector and sets its size to zero. |
625 |
|
* |
626 |
< |
* @see #clear |
627 |
< |
* @see List |
626 |
> |
* <p>This method is identical in functionality to the {@link #clear} |
627 |
> |
* method (which is part of the {@link List} interface). |
628 |
|
*/ |
629 |
|
public synchronized void removeAllElements() { |
630 |
|
modCount++; |
644 |
|
*/ |
645 |
|
public synchronized Object clone() { |
646 |
|
try { |
647 |
< |
Vector<E> v = (Vector<E>) super.clone(); |
647 |
> |
@SuppressWarnings("unchecked") |
648 |
> |
Vector<E> v = (Vector<E>) super.clone(); |
649 |
|
v.elementData = Arrays.copyOf(elementData, elementCount); |
650 |
|
v.modCount = 0; |
651 |
|
return v; |
670 |
|
* correct order; the runtime type of the returned array is that of the |
671 |
|
* specified array. If the Vector fits in the specified array, it is |
672 |
|
* returned therein. Otherwise, a new array is allocated with the runtime |
673 |
< |
* type of the specified array and the size of this Vector.<p> |
673 |
> |
* type of the specified array and the size of this Vector. |
674 |
|
* |
675 |
< |
* If the Vector fits in the specified array with room to spare |
675 |
> |
* <p>If the Vector fits in the specified array with room to spare |
676 |
|
* (i.e., the array has more elements than the Vector), |
677 |
|
* the element in the array immediately following the end of the |
678 |
|
* Vector is set to null. (This is useful in determining the length |
683 |
|
* be stored, if it is big enough; otherwise, a new array of the |
684 |
|
* same runtime type is allocated for this purpose. |
685 |
|
* @return an array containing the elements of the Vector |
686 |
< |
* @exception ArrayStoreException the runtime type of a is not a supertype |
686 |
> |
* @throws ArrayStoreException if the runtime type of a is not a supertype |
687 |
|
* of the runtime type of every element in this Vector |
688 |
|
* @throws NullPointerException if the given array is null |
689 |
|
* @since 1.2 |
690 |
|
*/ |
691 |
+ |
@SuppressWarnings("unchecked") |
692 |
|
public synchronized <T> T[] toArray(T[] a) { |
693 |
|
if (a.length < elementCount) |
694 |
|
return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass()); |
703 |
|
|
704 |
|
// Positional Access Operations |
705 |
|
|
706 |
+ |
@SuppressWarnings("unchecked") |
707 |
+ |
E elementData(int index) { |
708 |
+ |
return (E) elementData[index]; |
709 |
+ |
} |
710 |
+ |
|
711 |
|
/** |
712 |
|
* Returns the element at the specified position in this Vector. |
713 |
|
* |
714 |
|
* @param index index of the element to return |
715 |
|
* @return object at the specified index |
716 |
< |
* @exception ArrayIndexOutOfBoundsException index is out of range (index |
717 |
< |
* < 0 || index >= size()) |
716 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
717 |
> |
* ({@code index < 0 || index >= size()}) |
718 |
|
* @since 1.2 |
719 |
|
*/ |
720 |
|
public synchronized E get(int index) { |
721 |
|
if (index >= elementCount) |
722 |
|
throw new ArrayIndexOutOfBoundsException(index); |
723 |
|
|
724 |
< |
return (E)elementData[index]; |
724 |
> |
return elementData(index); |
725 |
|
} |
726 |
|
|
727 |
|
/** |
731 |
|
* @param index index of the element to replace |
732 |
|
* @param element element to be stored at the specified position |
733 |
|
* @return the element previously at the specified position |
734 |
< |
* @exception ArrayIndexOutOfBoundsException index out of range |
735 |
< |
* (index < 0 || index >= size()) |
734 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
735 |
> |
* ({@code index < 0 || index >= size()}) |
736 |
|
* @since 1.2 |
737 |
|
*/ |
738 |
|
public synchronized E set(int index, E element) { |
739 |
|
if (index >= elementCount) |
740 |
|
throw new ArrayIndexOutOfBoundsException(index); |
741 |
|
|
742 |
< |
Object oldValue = elementData[index]; |
742 |
> |
E oldValue = elementData(index); |
743 |
|
elementData[index] = element; |
744 |
< |
return (E)oldValue; |
744 |
> |
return oldValue; |
745 |
|
} |
746 |
|
|
747 |
|
/** |
780 |
|
* |
781 |
|
* @param index index at which the specified element is to be inserted |
782 |
|
* @param element element to be inserted |
783 |
< |
* @exception ArrayIndexOutOfBoundsException index is out of range |
784 |
< |
* (index < 0 || index > size()) |
783 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
784 |
> |
* ({@code index < 0 || index > size()}) |
785 |
|
* @since 1.2 |
786 |
|
*/ |
787 |
|
public void add(int index, E element) { |
793 |
|
* Shifts any subsequent elements to the left (subtracts one from their |
794 |
|
* indices). Returns the element that was removed from the Vector. |
795 |
|
* |
796 |
< |
* @exception ArrayIndexOutOfBoundsException index out of range (index |
797 |
< |
* < 0 || index >= size()) |
796 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
797 |
> |
* ({@code index < 0 || index >= size()}) |
798 |
|
* @param index the index of the element to be removed |
799 |
|
* @return element that was removed |
800 |
|
* @since 1.2 |
803 |
|
modCount++; |
804 |
|
if (index >= elementCount) |
805 |
|
throw new ArrayIndexOutOfBoundsException(index); |
806 |
< |
Object oldValue = elementData[index]; |
806 |
> |
E oldValue = elementData(index); |
807 |
|
|
808 |
|
int numMoved = elementCount - index - 1; |
809 |
|
if (numMoved > 0) |
811 |
|
numMoved); |
812 |
|
elementData[--elementCount] = null; // Let gc do its work |
813 |
|
|
814 |
< |
return (E)oldValue; |
814 |
> |
return oldValue; |
815 |
|
} |
816 |
|
|
817 |
|
/** |
913 |
|
* specified collection |
914 |
|
* @param c elements to be inserted into this Vector |
915 |
|
* @return {@code true} if this Vector changed as a result of the call |
916 |
< |
* @exception ArrayIndexOutOfBoundsException index out of range (index |
917 |
< |
* < 0 || index > size()) |
916 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
917 |
> |
* ({@code index < 0 || index > size()}) |
918 |
|
* @throws NullPointerException if the specified collection is null |
919 |
|
* @since 1.2 |
920 |
|
*/ |
969 |
|
} |
970 |
|
|
971 |
|
/** |
972 |
< |
* Removes from this List all of the elements whose index is between |
973 |
< |
* fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding |
974 |
< |
* elements to the left (reduces their index). |
975 |
< |
* This call shortens the Vector by (toIndex - fromIndex) elements. (If |
976 |
< |
* toIndex==fromIndex, this operation has no effect.) |
972 |
> |
* Returns a view of the portion of this List between fromIndex, |
973 |
> |
* inclusive, and toIndex, exclusive. (If fromIndex and toIndex are |
974 |
> |
* equal, the returned List is empty.) The returned List is backed by this |
975 |
> |
* List, so changes in the returned List are reflected in this List, and |
976 |
> |
* vice-versa. The returned List supports all of the optional List |
977 |
> |
* operations supported by this List. |
978 |
|
* |
979 |
< |
* @param fromIndex index of first element to be removed |
980 |
< |
* @param toIndex index after last element to be removed |
979 |
> |
* <p>This method eliminates the need for explicit range operations (of |
980 |
> |
* the sort that commonly exist for arrays). Any operation that expects |
981 |
> |
* a List can be used as a range operation by operating on a subList view |
982 |
> |
* instead of a whole List. For example, the following idiom |
983 |
> |
* removes a range of elements from a List: |
984 |
> |
* <pre> |
985 |
> |
* list.subList(from, to).clear(); |
986 |
> |
* </pre> |
987 |
> |
* Similar idioms may be constructed for indexOf and lastIndexOf, |
988 |
> |
* and all of the algorithms in the Collections class can be applied to |
989 |
> |
* a subList. |
990 |
> |
* |
991 |
> |
* <p>The semantics of the List returned by this method become undefined if |
992 |
> |
* the backing list (i.e., this List) is <i>structurally modified</i> in |
993 |
> |
* any way other than via the returned List. (Structural modifications are |
994 |
> |
* those that change the size of the List, or otherwise perturb it in such |
995 |
> |
* a fashion that iterations in progress may yield incorrect results.) |
996 |
> |
* |
997 |
> |
* @param fromIndex low endpoint (inclusive) of the subList |
998 |
> |
* @param toIndex high endpoint (exclusive) of the subList |
999 |
> |
* @return a view of the specified range within this List |
1000 |
> |
* @throws IndexOutOfBoundsException if an endpoint index value is out of range |
1001 |
> |
* {@code (fromIndex < 0 || toIndex > size)} |
1002 |
> |
* @throws IllegalArgumentException if the endpoint indices are out of order |
1003 |
> |
* {@code (fromIndex > toIndex)} |
1004 |
> |
*/ |
1005 |
> |
public synchronized List<E> subList(int fromIndex, int toIndex) { |
1006 |
> |
return Collections.synchronizedList(super.subList(fromIndex, toIndex), |
1007 |
> |
this); |
1008 |
> |
} |
1009 |
> |
|
1010 |
> |
/** |
1011 |
> |
* Removes from this list all of the elements whose index is between |
1012 |
> |
* {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. |
1013 |
> |
* Shifts any succeeding elements to the left (reduces their index). |
1014 |
> |
* This call shortens the list by {@code (toIndex - fromIndex)} elements. |
1015 |
> |
* (If {@code toIndex==fromIndex}, this operation has no effect.) |
1016 |
|
*/ |
1017 |
|
protected synchronized void removeRange(int fromIndex, int toIndex) { |
1018 |
|
modCount++; |
1038 |
|
} |
1039 |
|
|
1040 |
|
/** |
1041 |
< |
* Returns a list-iterator of the elements in this list (in proper |
1041 |
> |
* Returns a list iterator over the elements in this list (in proper |
1042 |
|
* sequence), starting at the specified position in the list. |
1043 |
< |
* Obeys the general contract of {@link List#listIterator(int)}. |
1043 |
> |
* The specified index indicates the first element that would be |
1044 |
> |
* returned by an initial call to {@link ListIterator#next next}. |
1045 |
> |
* An initial call to {@link ListIterator#previous previous} would |
1046 |
> |
* return the element with the specified index minus one. |
1047 |
> |
* |
1048 |
> |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1049 |
|
* |
988 |
– |
* <p>The list-iterator is <i>fail-fast</i>: if the list is structurally |
989 |
– |
* modified at any time after the Iterator is created, in any way except |
990 |
– |
* through the list-iterator's own {@code remove} or {@code add} |
991 |
– |
* methods, the list-iterator will throw a |
992 |
– |
* {@code ConcurrentModificationException}. Thus, in the face of |
993 |
– |
* concurrent modification, the iterator fails quickly and cleanly, rather |
994 |
– |
* than risking arbitrary, non-deterministic behavior at an undetermined |
995 |
– |
* time in the future. |
996 |
– |
* |
997 |
– |
* @param index index of the first element to be returned from the |
998 |
– |
* list-iterator (by a call to {@link ListIterator#next}) |
999 |
– |
* @return a list-iterator of the elements in this list (in proper |
1000 |
– |
* sequence), starting at the specified position in the list |
1050 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
1051 |
|
*/ |
1052 |
|
public synchronized ListIterator<E> listIterator(int index) { |
1053 |
|
if (index < 0 || index > elementCount) |
1054 |
|
throw new IndexOutOfBoundsException("Index: "+index); |
1055 |
< |
return new VectorIterator(index, elementCount); |
1055 |
> |
return new ListItr(index); |
1056 |
|
} |
1057 |
|
|
1058 |
|
/** |
1059 |
< |
* {@inheritDoc} |
1059 |
> |
* Returns a list iterator over the elements in this list (in proper |
1060 |
> |
* sequence). |
1061 |
> |
* |
1062 |
> |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1063 |
> |
* |
1064 |
> |
* @see #listIterator(int) |
1065 |
|
*/ |
1066 |
|
public synchronized ListIterator<E> listIterator() { |
1067 |
< |
return new VectorIterator(0, elementCount); |
1067 |
> |
return new ListItr(0); |
1068 |
|
} |
1069 |
|
|
1070 |
|
/** |
1071 |
|
* Returns an iterator over the elements in this list in proper sequence. |
1072 |
|
* |
1073 |
+ |
* <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1074 |
+ |
* |
1075 |
|
* @return an iterator over the elements in this list in proper sequence |
1076 |
|
*/ |
1077 |
|
public synchronized Iterator<E> iterator() { |
1078 |
< |
return new VectorIterator(0, elementCount); |
1078 |
> |
return new Itr(); |
1079 |
|
} |
1080 |
|
|
1081 |
|
/** |
1082 |
< |
* Helper method to access array elements under synchronization by |
1027 |
< |
* iterators. The caller performs index check with respect to |
1028 |
< |
* expected bounds, so errors accessing the element are reported |
1029 |
< |
* as ConcurrentModificationExceptions. |
1082 |
> |
* An optimized version of AbstractList.Itr |
1083 |
|
*/ |
1084 |
< |
final synchronized Object iteratorGet(int index, int expectedModCount) { |
1085 |
< |
if (modCount == expectedModCount) { |
1086 |
< |
try { |
1087 |
< |
return elementData[index]; |
1088 |
< |
} catch(IndexOutOfBoundsException fallThrough) { |
1084 |
> |
private class Itr implements Iterator<E> { |
1085 |
> |
int cursor; // index of next element to return |
1086 |
> |
int lastRet = -1; // index of last element returned; -1 if no such |
1087 |
> |
int expectedModCount = modCount; |
1088 |
> |
|
1089 |
> |
public boolean hasNext() { |
1090 |
> |
// Racy but within spec, since modifications are checked |
1091 |
> |
// within or after synchronization in next/previous |
1092 |
> |
return cursor != elementCount; |
1093 |
> |
} |
1094 |
> |
|
1095 |
> |
public E next() { |
1096 |
> |
synchronized (Vector.this) { |
1097 |
> |
checkForComodification(); |
1098 |
> |
int i = cursor; |
1099 |
> |
if (i >= elementCount) |
1100 |
> |
throw new NoSuchElementException(); |
1101 |
> |
cursor = i + 1; |
1102 |
> |
return elementData(lastRet = i); |
1103 |
> |
} |
1104 |
> |
} |
1105 |
> |
|
1106 |
> |
public void remove() { |
1107 |
> |
if (lastRet == -1) |
1108 |
> |
throw new IllegalStateException(); |
1109 |
> |
synchronized (Vector.this) { |
1110 |
> |
checkForComodification(); |
1111 |
> |
Vector.this.remove(lastRet); |
1112 |
> |
expectedModCount = modCount; |
1113 |
|
} |
1114 |
< |
} |
1115 |
< |
throw new ConcurrentModificationException(); |
1114 |
> |
cursor = lastRet; |
1115 |
> |
lastRet = -1; |
1116 |
> |
} |
1117 |
> |
|
1118 |
> |
final void checkForComodification() { |
1119 |
> |
if (modCount != expectedModCount) |
1120 |
> |
throw new ConcurrentModificationException(); |
1121 |
> |
} |
1122 |
|
} |
1123 |
|
|
1124 |
|
/** |
1125 |
< |
* Streamlined specialization of AbstractList version of iterator. |
1043 |
< |
* Locally perfroms bounds checks, but relies on outer Vector |
1044 |
< |
* to access elements under synchronization. |
1125 |
> |
* An optimized version of AbstractList.ListItr |
1126 |
|
*/ |
1127 |
< |
private final class VectorIterator implements ListIterator<E> { |
1128 |
< |
int cursor; // Index of next element to return; |
1129 |
< |
int fence; // Upper bound on cursor (cache of size()) |
1130 |
< |
int lastRet; // Index of last element, or -1 if no such |
1050 |
< |
int expectedModCount; // To check for CME |
1051 |
< |
|
1052 |
< |
VectorIterator(int index, int fence) { |
1053 |
< |
this.cursor = index; |
1054 |
< |
this.fence = fence; |
1055 |
< |
this.lastRet = -1; |
1056 |
< |
this.expectedModCount = Vector.this.modCount; |
1057 |
< |
} |
1058 |
< |
|
1059 |
< |
public boolean hasNext() { |
1060 |
< |
return cursor < fence; |
1127 |
> |
final class ListItr extends Itr implements ListIterator<E> { |
1128 |
> |
ListItr(int index) { |
1129 |
> |
super(); |
1130 |
> |
cursor = index; |
1131 |
|
} |
1132 |
|
|
1133 |
|
public boolean hasPrevious() { |
1134 |
< |
return cursor > 0; |
1134 |
> |
return cursor != 0; |
1135 |
|
} |
1136 |
|
|
1137 |
|
public int nextIndex() { |
1142 |
|
return cursor - 1; |
1143 |
|
} |
1144 |
|
|
1145 |
< |
public E next() { |
1146 |
< |
int i = cursor; |
1147 |
< |
if (i >= fence) |
1148 |
< |
throw new NoSuchElementException(); |
1149 |
< |
Object next = Vector.this.iteratorGet(i, expectedModCount); |
1150 |
< |
lastRet = i; |
1151 |
< |
cursor = i + 1; |
1152 |
< |
return (E)next; |
1153 |
< |
} |
1084 |
< |
|
1085 |
< |
public E previous() { |
1086 |
< |
int i = cursor - 1; |
1087 |
< |
if (i < 0) |
1088 |
< |
throw new NoSuchElementException(); |
1089 |
< |
Object prev = Vector.this.iteratorGet(i, expectedModCount); |
1090 |
< |
lastRet = i; |
1091 |
< |
cursor = i; |
1092 |
< |
return (E)prev; |
1145 |
> |
public E previous() { |
1146 |
> |
synchronized (Vector.this) { |
1147 |
> |
checkForComodification(); |
1148 |
> |
int i = cursor - 1; |
1149 |
> |
if (i < 0) |
1150 |
> |
throw new NoSuchElementException(); |
1151 |
> |
cursor = i; |
1152 |
> |
return elementData(lastRet = i); |
1153 |
> |
} |
1154 |
|
} |
1155 |
|
|
1156 |
|
public void set(E e) { |
1157 |
< |
if (lastRet < 0) |
1157 |
> |
if (lastRet == -1) |
1158 |
|
throw new IllegalStateException(); |
1159 |
< |
if (Vector.this.modCount != expectedModCount) |
1160 |
< |
throw new ConcurrentModificationException(); |
1161 |
< |
try { |
1101 |
< |
Vector.this.set(lastRet, e); |
1102 |
< |
expectedModCount = Vector.this.modCount; |
1103 |
< |
} catch (IndexOutOfBoundsException ex) { |
1104 |
< |
throw new ConcurrentModificationException(); |
1105 |
< |
} |
1106 |
< |
} |
1107 |
< |
|
1108 |
< |
public void remove() { |
1109 |
< |
int i = lastRet; |
1110 |
< |
if (i < 0) |
1111 |
< |
throw new IllegalStateException(); |
1112 |
< |
if (Vector.this.modCount != expectedModCount) |
1113 |
< |
throw new ConcurrentModificationException(); |
1114 |
< |
try { |
1115 |
< |
Vector.this.remove(i); |
1116 |
< |
if (i < cursor) |
1117 |
< |
cursor--; |
1118 |
< |
lastRet = -1; |
1119 |
< |
fence = Vector.this.size(); |
1120 |
< |
expectedModCount = Vector.this.modCount; |
1121 |
< |
} catch (IndexOutOfBoundsException ex) { |
1122 |
< |
throw new ConcurrentModificationException(); |
1159 |
> |
synchronized (Vector.this) { |
1160 |
> |
checkForComodification(); |
1161 |
> |
Vector.this.set(lastRet, e); |
1162 |
|
} |
1163 |
|
} |
1164 |
|
|
1165 |
|
public void add(E e) { |
1166 |
< |
if (Vector.this.modCount != expectedModCount) |
1167 |
< |
throw new ConcurrentModificationException(); |
1168 |
< |
try { |
1169 |
< |
int i = cursor; |
1170 |
< |
Vector.this.add(i, e); |
1132 |
< |
cursor = i + 1; |
1133 |
< |
lastRet = -1; |
1134 |
< |
fence = Vector.this.size(); |
1135 |
< |
expectedModCount = Vector.this.modCount; |
1136 |
< |
} catch (IndexOutOfBoundsException ex) { |
1137 |
< |
throw new ConcurrentModificationException(); |
1166 |
> |
int i = cursor; |
1167 |
> |
synchronized (Vector.this) { |
1168 |
> |
checkForComodification(); |
1169 |
> |
Vector.this.add(i, e); |
1170 |
> |
expectedModCount = modCount; |
1171 |
|
} |
1172 |
+ |
cursor = i + 1; |
1173 |
+ |
lastRet = -1; |
1174 |
|
} |
1175 |
|
} |
1141 |
– |
|
1142 |
– |
/** |
1143 |
– |
* Returns a view of the portion of this List between fromIndex, |
1144 |
– |
* inclusive, and toIndex, exclusive. (If fromIndex and toIndex are |
1145 |
– |
* equal, the returned List is empty.) The returned List is backed by this |
1146 |
– |
* List, so changes in the returned List are reflected in this List, and |
1147 |
– |
* vice-versa. The returned List supports all of the optional List |
1148 |
– |
* operations supported by this List.<p> |
1149 |
– |
* |
1150 |
– |
* This method eliminates the need for explicit range operations (of |
1151 |
– |
* the sort that commonly exist for arrays). Any operation that expects |
1152 |
– |
* a List can be used as a range operation by operating on a subList view |
1153 |
– |
* instead of a whole List. For example, the following idiom |
1154 |
– |
* removes a range of elements from a List: |
1155 |
– |
* <pre> |
1156 |
– |
* list.subList(from, to).clear(); |
1157 |
– |
* </pre> |
1158 |
– |
* Similar idioms may be constructed for indexOf and lastIndexOf, |
1159 |
– |
* and all of the algorithms in the Collections class can be applied to |
1160 |
– |
* a subList.<p> |
1161 |
– |
* |
1162 |
– |
* The semantics of the List returned by this method become undefined if |
1163 |
– |
* the backing list (i.e., this List) is <i>structurally modified</i> in |
1164 |
– |
* any way other than via the returned List. (Structural modifications are |
1165 |
– |
* those that change the size of the List, or otherwise perturb it in such |
1166 |
– |
* a fashion that iterations in progress may yield incorrect results.) |
1167 |
– |
* |
1168 |
– |
* @param fromIndex low endpoint (inclusive) of the subList |
1169 |
– |
* @param toIndex high endpoint (exclusive) of the subList |
1170 |
– |
* @return a view of the specified range within this List |
1171 |
– |
* @throws IndexOutOfBoundsException endpoint index value out of range |
1172 |
– |
* <code>(fromIndex < 0 || toIndex > size)</code> |
1173 |
– |
* @throws IllegalArgumentException endpoint indices out of order |
1174 |
– |
* <code>(fromIndex > toIndex)</code> |
1175 |
– |
*/ |
1176 |
– |
public synchronized List<E> subList(int fromIndex, int toIndex) { |
1177 |
– |
return new VectorSubList(this, this, fromIndex, fromIndex, toIndex); |
1178 |
– |
} |
1179 |
– |
|
1180 |
– |
/** |
1181 |
– |
* This class specializes the AbstractList version of SubList to |
1182 |
– |
* avoid the double-indirection penalty that would arise using a |
1183 |
– |
* synchronized wrapper, as well as to avoid some unnecessary |
1184 |
– |
* checks in sublist iterators. |
1185 |
– |
*/ |
1186 |
– |
private static final class VectorSubList<E> extends AbstractList<E> implements RandomAccess { |
1187 |
– |
final Vector<E> base; // base list |
1188 |
– |
final AbstractList<E> parent; // Creating list |
1189 |
– |
final int baseOffset; // index wrt Vector |
1190 |
– |
final int parentOffset; // index wrt parent |
1191 |
– |
int length; // length of sublist |
1192 |
– |
|
1193 |
– |
VectorSubList(Vector<E> base, AbstractList<E> parent, int baseOffset, |
1194 |
– |
int fromIndex, int toIndex) { |
1195 |
– |
if (fromIndex < 0) |
1196 |
– |
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); |
1197 |
– |
if (toIndex > parent.size()) |
1198 |
– |
throw new IndexOutOfBoundsException("toIndex = " + toIndex); |
1199 |
– |
if (fromIndex > toIndex) |
1200 |
– |
throw new IllegalArgumentException("fromIndex(" + fromIndex + |
1201 |
– |
") > toIndex(" + toIndex + ")"); |
1202 |
– |
|
1203 |
– |
this.base = base; |
1204 |
– |
this.parent = parent; |
1205 |
– |
this.baseOffset = baseOffset; |
1206 |
– |
this.parentOffset = fromIndex; |
1207 |
– |
this.length = toIndex - fromIndex; |
1208 |
– |
modCount = base.modCount; |
1209 |
– |
} |
1210 |
– |
|
1211 |
– |
/** |
1212 |
– |
* Returns an IndexOutOfBoundsException with nicer message |
1213 |
– |
*/ |
1214 |
– |
private IndexOutOfBoundsException indexError(int index) { |
1215 |
– |
return new IndexOutOfBoundsException("Index: " + index + |
1216 |
– |
", Size: " + length); |
1217 |
– |
} |
1218 |
– |
|
1219 |
– |
public E set(int index, E element) { |
1220 |
– |
synchronized(base) { |
1221 |
– |
if (index < 0 || index >= length) |
1222 |
– |
throw indexError(index); |
1223 |
– |
if (base.modCount != modCount) |
1224 |
– |
throw new ConcurrentModificationException(); |
1225 |
– |
return base.set(index + baseOffset, element); |
1226 |
– |
} |
1227 |
– |
} |
1228 |
– |
|
1229 |
– |
public E get(int index) { |
1230 |
– |
synchronized(base) { |
1231 |
– |
if (index < 0 || index >= length) |
1232 |
– |
throw indexError(index); |
1233 |
– |
if (base.modCount != modCount) |
1234 |
– |
throw new ConcurrentModificationException(); |
1235 |
– |
return base.get(index + baseOffset); |
1236 |
– |
} |
1237 |
– |
} |
1238 |
– |
|
1239 |
– |
public int size() { |
1240 |
– |
synchronized(base) { |
1241 |
– |
if (base.modCount != modCount) |
1242 |
– |
throw new ConcurrentModificationException(); |
1243 |
– |
return length; |
1244 |
– |
} |
1245 |
– |
} |
1246 |
– |
|
1247 |
– |
public void add(int index, E element) { |
1248 |
– |
synchronized(base) { |
1249 |
– |
if (index < 0 || index > length) |
1250 |
– |
throw indexError(index); |
1251 |
– |
if (base.modCount != modCount) |
1252 |
– |
throw new ConcurrentModificationException(); |
1253 |
– |
parent.add(index + parentOffset, element); |
1254 |
– |
length++; |
1255 |
– |
modCount = base.modCount; |
1256 |
– |
} |
1257 |
– |
} |
1258 |
– |
|
1259 |
– |
public E remove(int index) { |
1260 |
– |
synchronized(base) { |
1261 |
– |
if (index < 0 || index >= length) |
1262 |
– |
throw indexError(index); |
1263 |
– |
if (base.modCount != modCount) |
1264 |
– |
throw new ConcurrentModificationException(); |
1265 |
– |
E result = parent.remove(index + parentOffset); |
1266 |
– |
length--; |
1267 |
– |
modCount = base.modCount; |
1268 |
– |
return result; |
1269 |
– |
} |
1270 |
– |
} |
1271 |
– |
|
1272 |
– |
protected void removeRange(int fromIndex, int toIndex) { |
1273 |
– |
synchronized(base) { |
1274 |
– |
if (base.modCount != modCount) |
1275 |
– |
throw new ConcurrentModificationException(); |
1276 |
– |
parent.removeRange(fromIndex + parentOffset, |
1277 |
– |
toIndex + parentOffset); |
1278 |
– |
length -= (toIndex-fromIndex); |
1279 |
– |
modCount = base.modCount; |
1280 |
– |
} |
1281 |
– |
} |
1282 |
– |
|
1283 |
– |
public boolean addAll(Collection<? extends E> c) { |
1284 |
– |
return addAll(length, c); |
1285 |
– |
} |
1286 |
– |
|
1287 |
– |
public boolean addAll(int index, Collection<? extends E> c) { |
1288 |
– |
synchronized(base) { |
1289 |
– |
if (index < 0 || index > length) |
1290 |
– |
throw indexError(index); |
1291 |
– |
int cSize = c.size(); |
1292 |
– |
if (cSize==0) |
1293 |
– |
return false; |
1294 |
– |
|
1295 |
– |
if (base.modCount != modCount) |
1296 |
– |
throw new ConcurrentModificationException(); |
1297 |
– |
parent.addAll(parentOffset + index, c); |
1298 |
– |
modCount = base.modCount; |
1299 |
– |
length += cSize; |
1300 |
– |
return true; |
1301 |
– |
} |
1302 |
– |
} |
1303 |
– |
|
1304 |
– |
public boolean equals(Object o) { |
1305 |
– |
synchronized(base) {return super.equals(o);} |
1306 |
– |
} |
1307 |
– |
|
1308 |
– |
public int hashCode() { |
1309 |
– |
synchronized(base) {return super.hashCode();} |
1310 |
– |
} |
1311 |
– |
|
1312 |
– |
public int indexOf(Object o) { |
1313 |
– |
synchronized(base) {return super.indexOf(o);} |
1314 |
– |
} |
1315 |
– |
|
1316 |
– |
public int lastIndexOf(Object o) { |
1317 |
– |
synchronized(base) {return super.lastIndexOf(o);} |
1318 |
– |
} |
1319 |
– |
|
1320 |
– |
public List<E> subList(int fromIndex, int toIndex) { |
1321 |
– |
return new VectorSubList(base, this, fromIndex + baseOffset, |
1322 |
– |
fromIndex, toIndex); |
1323 |
– |
} |
1324 |
– |
|
1325 |
– |
public Iterator<E> iterator() { |
1326 |
– |
synchronized(base) { |
1327 |
– |
return new VectorSubListIterator(this, 0); |
1328 |
– |
} |
1329 |
– |
} |
1330 |
– |
|
1331 |
– |
public synchronized ListIterator<E> listIterator() { |
1332 |
– |
synchronized(base) { |
1333 |
– |
return new VectorSubListIterator(this, 0); |
1334 |
– |
} |
1335 |
– |
} |
1336 |
– |
|
1337 |
– |
public ListIterator<E> listIterator(int index) { |
1338 |
– |
synchronized(base) { |
1339 |
– |
if (index < 0 || index > length) |
1340 |
– |
throw indexError(index); |
1341 |
– |
return new VectorSubListIterator(this, index); |
1342 |
– |
} |
1343 |
– |
} |
1344 |
– |
|
1345 |
– |
/** |
1346 |
– |
* Same idea as VectorIterator, except routing structural |
1347 |
– |
* change operations through the sublist. |
1348 |
– |
*/ |
1349 |
– |
private static final class VectorSubListIterator<E> implements ListIterator<E> { |
1350 |
– |
final Vector<E> base; // base list |
1351 |
– |
final VectorSubList<E> outer; // Sublist creating this iteraor |
1352 |
– |
final int offset; // cursor offset wrt base |
1353 |
– |
int cursor; // Current index |
1354 |
– |
int fence; // Upper bound on cursor |
1355 |
– |
int lastRet; // Index of returned element, or -1 |
1356 |
– |
int expectedModCount; // Expected modCount of base Vector |
1357 |
– |
|
1358 |
– |
VectorSubListIterator(VectorSubList<E> list, int index) { |
1359 |
– |
this.lastRet = -1; |
1360 |
– |
this.cursor = index; |
1361 |
– |
this.outer = list; |
1362 |
– |
this.offset = list.baseOffset; |
1363 |
– |
this.fence = list.length; |
1364 |
– |
this.base = list.base; |
1365 |
– |
this.expectedModCount = base.modCount; |
1366 |
– |
} |
1367 |
– |
|
1368 |
– |
public boolean hasNext() { |
1369 |
– |
return cursor < fence; |
1370 |
– |
} |
1371 |
– |
|
1372 |
– |
public boolean hasPrevious() { |
1373 |
– |
return cursor > 0; |
1374 |
– |
} |
1375 |
– |
|
1376 |
– |
public int nextIndex() { |
1377 |
– |
return cursor; |
1378 |
– |
} |
1379 |
– |
|
1380 |
– |
public int previousIndex() { |
1381 |
– |
return cursor - 1; |
1382 |
– |
} |
1383 |
– |
|
1384 |
– |
public E next() { |
1385 |
– |
int i = cursor; |
1386 |
– |
if (cursor >= fence) |
1387 |
– |
throw new NoSuchElementException(); |
1388 |
– |
Object next = base.iteratorGet(i + offset, expectedModCount); |
1389 |
– |
lastRet = i; |
1390 |
– |
cursor = i + 1; |
1391 |
– |
return (E)next; |
1392 |
– |
} |
1393 |
– |
|
1394 |
– |
public E previous() { |
1395 |
– |
int i = cursor - 1; |
1396 |
– |
if (i < 0) |
1397 |
– |
throw new NoSuchElementException(); |
1398 |
– |
Object prev = base.iteratorGet(i + offset, expectedModCount); |
1399 |
– |
lastRet = i; |
1400 |
– |
cursor = i; |
1401 |
– |
return (E)prev; |
1402 |
– |
} |
1403 |
– |
|
1404 |
– |
public void set(E e) { |
1405 |
– |
if (lastRet < 0) |
1406 |
– |
throw new IllegalStateException(); |
1407 |
– |
if (base.modCount != expectedModCount) |
1408 |
– |
throw new ConcurrentModificationException(); |
1409 |
– |
try { |
1410 |
– |
outer.set(lastRet, e); |
1411 |
– |
expectedModCount = base.modCount; |
1412 |
– |
} catch (IndexOutOfBoundsException ex) { |
1413 |
– |
throw new ConcurrentModificationException(); |
1414 |
– |
} |
1415 |
– |
} |
1416 |
– |
|
1417 |
– |
public void remove() { |
1418 |
– |
int i = lastRet; |
1419 |
– |
if (i < 0) |
1420 |
– |
throw new IllegalStateException(); |
1421 |
– |
if (base.modCount != expectedModCount) |
1422 |
– |
throw new ConcurrentModificationException(); |
1423 |
– |
try { |
1424 |
– |
outer.remove(i); |
1425 |
– |
if (i < cursor) |
1426 |
– |
cursor--; |
1427 |
– |
lastRet = -1; |
1428 |
– |
fence = outer.length; |
1429 |
– |
expectedModCount = base.modCount; |
1430 |
– |
} catch (IndexOutOfBoundsException ex) { |
1431 |
– |
throw new ConcurrentModificationException(); |
1432 |
– |
} |
1433 |
– |
} |
1434 |
– |
|
1435 |
– |
public void add(E e) { |
1436 |
– |
if (base.modCount != expectedModCount) |
1437 |
– |
throw new ConcurrentModificationException(); |
1438 |
– |
try { |
1439 |
– |
int i = cursor; |
1440 |
– |
outer.add(i, e); |
1441 |
– |
cursor = i + 1; |
1442 |
– |
lastRet = -1; |
1443 |
– |
fence = outer.length; |
1444 |
– |
expectedModCount = base.modCount; |
1445 |
– |
} catch (IndexOutOfBoundsException ex) { |
1446 |
– |
throw new ConcurrentModificationException(); |
1447 |
– |
} |
1448 |
– |
} |
1449 |
– |
} |
1450 |
– |
} |
1176 |
|
} |
1452 |
– |
|
1453 |
– |
|
1454 |
– |
|