ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/extra/ReadMostlyVector.java
(Generate patch)

Comparing jsr166/src/jsr166e/extra/ReadMostlyVector.java (file contents):
Revision 1.4 by dl, Sat Jul 16 15:05:04 2011 UTC vs.
Revision 1.5 by dl, Sat Jul 16 16:05:32 2011 UTC

# Line 19 | Line 19 | import java.util.*;
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
# Line 850 | Line 853 | public class ReadMostlyVector<E> impleme
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} */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines