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 |
161 |
|
Object e = items[i]; |
162 |
|
if (lock.getSequence() != seq) |
163 |
|
break; |
164 |
< |
if (x == null? e == null : x.equals(e)) |
164 |
> |
if ((x == null) ? e == null : x.equals(e)) |
165 |
|
return i; |
166 |
|
} |
167 |
|
return -1; |
171 |
|
Object[] items = array; |
172 |
|
for (int i = index; i < fence; ++i) { |
173 |
|
Object e = items[i]; |
174 |
< |
if (x == null? e == null : x.equals(e)) |
174 |
> |
if ((x == null) ? e == null : x.equals(e)) |
175 |
|
return i; |
176 |
|
} |
177 |
|
return -1; |
183 |
|
Object e = items[i]; |
184 |
|
if (lock.getSequence() != seq) |
185 |
|
break; |
186 |
< |
if (x == null? e == null : x.equals(e)) |
186 |
> |
if ((x == null) ? e == null : x.equals(e)) |
187 |
|
return i; |
188 |
|
} |
189 |
|
return -1; |
193 |
|
Object[] items = array; |
194 |
|
for (int i = index; i >= origin; --i) { |
195 |
|
Object e = items[i]; |
196 |
< |
if (x == null? e == null : x.equals(e)) |
196 |
> |
if ((x == null) ? e == null : x.equals(e)) |
197 |
|
return i; |
198 |
|
} |
199 |
|
return -1; |
335 |
|
else { |
336 |
|
contained = true; |
337 |
|
for (Object e : c) { |
338 |
< |
int idx = (locked? |
338 |
> |
int idx = (locked ? |
339 |
|
rawIndexOf(e, origin, fence) : |
340 |
|
validatedIndexOf(e, items, origin, |
341 |
|
fence, seq)); |
659 |
|
|
660 |
|
public int indexOf(Object o) { |
661 |
|
SequenceLock lock = this.lock; |
662 |
< |
long seq = lock.awaitAvailability(); |
663 |
< |
Object[] items = array; |
664 |
< |
int n = count; |
665 |
< |
if (n <= items.length) { |
666 |
< |
boolean valid = true; |
667 |
< |
for (int i = 0; i < n; ++i) { |
668 |
< |
Object e = items[i]; |
669 |
< |
if (lock.getSequence() == seq) { |
670 |
< |
if (o == null? e == null : o.equals(e)) |
662 |
> |
for (;;) { |
663 |
> |
long seq = lock.awaitAvailability(); |
664 |
> |
Object[] items = array; |
665 |
> |
int n = count; |
666 |
> |
if (n <= items.length) { |
667 |
> |
for (int i = 0; i < n; ++i) { |
668 |
> |
Object e = items[i]; |
669 |
> |
if (lock.getSequence() != seq) { |
670 |
> |
lock.lock(); |
671 |
> |
try { |
672 |
> |
return rawIndexOf(o, 0, count); |
673 |
> |
} finally { |
674 |
> |
lock.unlock(); |
675 |
> |
} |
676 |
> |
} |
677 |
> |
else if ((o == null) ? e == null : o.equals(e)) |
678 |
|
return i; |
679 |
|
} |
670 |
– |
else { |
671 |
– |
valid = false; |
672 |
– |
break; |
673 |
– |
} |
674 |
– |
} |
675 |
– |
if (valid) |
680 |
|
return -1; |
681 |
< |
} |
678 |
< |
lock.lock(); |
679 |
< |
try { |
680 |
< |
return rawIndexOf(o, 0, count); |
681 |
< |
} finally { |
682 |
< |
lock.unlock(); |
681 |
> |
} |
682 |
|
} |
683 |
|
} |
684 |
|
|
693 |
|
|
694 |
|
public int lastIndexOf(Object o) { |
695 |
|
SequenceLock lock = this.lock; |
696 |
< |
long seq = lock.awaitAvailability(); |
697 |
< |
Object[] items = array; |
698 |
< |
int n = count; |
699 |
< |
if (n <= items.length) { |
700 |
< |
int idx = validatedLastIndexOf(o, items, n - 1, 0, seq); |
701 |
< |
if (lock.getSequence() == seq) |
702 |
< |
return idx; |
703 |
< |
} |
704 |
< |
lock.lock(); |
705 |
< |
try { |
706 |
< |
return rawLastIndexOf(o, count - 1, 0); |
707 |
< |
} finally { |
708 |
< |
lock.unlock(); |
696 |
> |
for (;;) { |
697 |
> |
long seq = lock.awaitAvailability(); |
698 |
> |
Object[] items = array; |
699 |
> |
int n = count; |
700 |
> |
if (n <= items.length) { |
701 |
> |
for (int i = n - 1; i >= 0; --i) { |
702 |
> |
Object e = items[i]; |
703 |
> |
if (lock.getSequence() != seq) { |
704 |
> |
lock.lock(); |
705 |
> |
try { |
706 |
> |
return rawLastIndexOf(o, 0, count); |
707 |
> |
} finally { |
708 |
> |
lock.unlock(); |
709 |
> |
} |
710 |
> |
} |
711 |
> |
else if ((o == null) ? e == null : o.equals(e)) |
712 |
> |
return i; |
713 |
> |
} |
714 |
> |
return -1; |
715 |
> |
} |
716 |
|
} |
717 |
|
} |
718 |
|
|
856 |
|
return added; |
857 |
|
} |
858 |
|
|
859 |
+ |
/** |
860 |
+ |
* Returns an iterator operating over a snapshot copy of the |
861 |
+ |
* elements of this collection created upon construction of the |
862 |
+ |
* iterator. The iterator does <em>NOT</em> support the |
863 |
+ |
* <tt>remove</tt> method. |
864 |
+ |
* |
865 |
+ |
* @return an iterator over the elements in this list in proper sequence |
866 |
+ |
*/ |
867 |
+ |
public Iterator<E> snapshotIterator() { |
868 |
+ |
return new SnapshotIterator<E>(this); |
869 |
+ |
} |
870 |
+ |
|
871 |
+ |
static final class SnapshotIterator<E> implements Iterator<E> { |
872 |
+ |
final Object[] items; |
873 |
+ |
int cursor; |
874 |
+ |
SnapshotIterator(ReadMostlyVector<E> v) { items = v.toArray(); } |
875 |
+ |
public boolean hasNext() { return cursor < items.length; } |
876 |
+ |
public E next() { |
877 |
+ |
if (cursor < items.length) |
878 |
+ |
return (E) items[cursor++]; |
879 |
+ |
throw new NoSuchElementException(); |
880 |
+ |
} |
881 |
+ |
public void remove() { throw new UnsupportedOperationException() ; } |
882 |
+ |
} |
883 |
+ |
|
884 |
|
// Vector-only methods |
885 |
|
|
886 |
|
/** See {@link Vector#firstElement} */ |