--- jsr166/src/jsr166e/extra/ReadMostlyVector.java 2011/07/16 15:05:04 1.4
+++ jsr166/src/jsr166e/extra/ReadMostlyVector.java 2011/07/16 16:05:32 1.5
@@ -19,7 +19,10 @@ import java.util.*;
* best-effort in the presence of concurrent modifications, and do
* NOT throw {@link ConcurrentModificationException}. An
* iterator's {@code next()} method returns consecutive elements as
- * they appear in the underlying array upon each access.
+ * they appear in the underlying array upon each access. Alternatvely,
+ * method {@link #snapshotIterator} may be used for deterministic
+ * traversals, at the expense of making a copy, and unavailability of
+ * method {@code Iterator.remove}.
*
*
Otherwise, this class supports all methods, under the same
* documented specifications, as {@code Vector}. Consult {@link
@@ -850,6 +853,31 @@ public class ReadMostlyVector impleme
return added;
}
+ /**
+ * Returns an iterator operating over a snapshot copy of the
+ * elements of this collection created upon construction of the
+ * iterator. The iterator does NOT support the
+ * remove method.
+ *
+ * @return an iterator over the elements in this list in proper sequence
+ */
+ public Iterator snapshotIterator() {
+ return new SnapshotIterator(this);
+ }
+
+ static final class SnapshotIterator implements Iterator {
+ final Object[] items;
+ int cursor;
+ SnapshotIterator(ReadMostlyVector v) { items = v.toArray(); }
+ public boolean hasNext() { return cursor < items.length; }
+ public E next() {
+ if (cursor < items.length)
+ return (E) items[cursor++];
+ throw new NoSuchElementException();
+ }
+ public void remove() { throw new UnsupportedOperationException() ; }
+ }
+
// Vector-only methods
/** See {@link Vector#firstElement} */