19 |
|
* best-effort in the presence of concurrent modifications, and do |
20 |
|
* <em>NOT</em> throw {@link ConcurrentModificationException}. An |
21 |
|
* iterator's {@code next()} method returns consecutive elements as |
22 |
< |
* they appear in the underlying array upon each access. |
22 |
> |
* they appear in the underlying array upon each access. Alternatvely, |
23 |
> |
* method {@link #snapshotIterator} may be used for deterministic |
24 |
> |
* traversals, at the expense of making a copy, and unavailability of |
25 |
> |
* method {@code Iterator.remove}. |
26 |
|
* |
27 |
|
* <p>Otherwise, this class supports all methods, under the same |
28 |
|
* documented specifications, as {@code Vector}. Consult {@link |
853 |
|
return added; |
854 |
|
} |
855 |
|
|
856 |
+ |
/** |
857 |
+ |
* Returns an iterator operating over a snapshot copy of the |
858 |
+ |
* elements of this collection created upon construction of the |
859 |
+ |
* iterator. The iterator does <em>NOT</em> support the |
860 |
+ |
* <tt>remove</tt> method. |
861 |
+ |
* |
862 |
+ |
* @return an iterator over the elements in this list in proper sequence |
863 |
+ |
*/ |
864 |
+ |
public Iterator<E> snapshotIterator() { |
865 |
+ |
return new SnapshotIterator<E>(this); |
866 |
+ |
} |
867 |
+ |
|
868 |
+ |
static final class SnapshotIterator<E> implements Iterator<E> { |
869 |
+ |
final Object[] items; |
870 |
+ |
int cursor; |
871 |
+ |
SnapshotIterator(ReadMostlyVector<E> v) { items = v.toArray(); } |
872 |
+ |
public boolean hasNext() { return cursor < items.length; } |
873 |
+ |
public E next() { |
874 |
+ |
if (cursor < items.length) |
875 |
+ |
return (E) items[cursor++]; |
876 |
+ |
throw new NoSuchElementException(); |
877 |
+ |
} |
878 |
+ |
public void remove() { throw new UnsupportedOperationException() ; } |
879 |
+ |
} |
880 |
+ |
|
881 |
|
// Vector-only methods |
882 |
|
|
883 |
|
/** See {@link Vector#firstElement} */ |