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. Alternatvely, |
22 |
> |
* they appear in the underlying array upon each access. Alternatively, |
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}. |
32 |
|
* |
33 |
|
* @author Doug Lea |
34 |
|
*/ |
35 |
< |
public class ReadMostlyVector<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable { |
35 |
> |
public class ReadMostlyVector<E> |
36 |
> |
implements List<E>, RandomAccess, Cloneable, java.io.Serializable { |
37 |
|
private static final long serialVersionUID = 8673264195747942595L; |
38 |
|
|
39 |
|
/* |
58 |
|
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
59 |
|
|
60 |
|
// fields are non-private to simpify nested class access |
61 |
< |
Object[] array; |
61 |
> |
volatile Object[] array; |
62 |
|
final SequenceLock lock; |
63 |
< |
int count; |
63 |
> |
volatile int count; |
64 |
|
final int capacityIncrement; |
65 |
|
|
66 |
|
/** |
130 |
|
} |
131 |
|
|
132 |
|
// For explanation, see CopyOnWriteArrayList |
133 |
< |
final void grow(int minCapacity) { |
133 |
> |
final Object[] grow(int minCapacity) { |
134 |
|
int oldCapacity = array.length; |
135 |
|
int newCapacity = oldCapacity + ((capacityIncrement > 0) ? |
136 |
|
capacityIncrement : oldCapacity); |
138 |
|
newCapacity = minCapacity; |
139 |
|
if (newCapacity - MAX_ARRAY_SIZE > 0) |
140 |
|
newCapacity = hugeCapacity(minCapacity); |
141 |
< |
array = Arrays.copyOf(array, newCapacity); |
141 |
> |
return array = Arrays.copyOf(array, newCapacity); |
142 |
|
} |
143 |
|
|
144 |
|
static int hugeCapacity(int minCapacity) { |
200 |
|
return -1; |
201 |
|
} |
202 |
|
|
203 |
< |
final void rawAdd(Object e) { |
203 |
> |
final void rawAdd(E e) { |
204 |
|
int n = count; |
205 |
< |
if (n >= array.length) |
206 |
< |
grow(n + 1); |
207 |
< |
array[n] = e; |
205 |
> |
Object[] items = array; |
206 |
> |
if (n >= items.length) |
207 |
> |
items = grow(n + 1); |
208 |
> |
items[n] = e; |
209 |
|
count = n + 1; |
210 |
|
} |
211 |
|
|
212 |
< |
final void rawAddAt(int index, Object e) { |
212 |
> |
final void rawAddAt(int index, E e) { |
213 |
|
int n = count; |
214 |
+ |
Object[] items = array; |
215 |
|
if (index > n) |
216 |
|
throw new ArrayIndexOutOfBoundsException(index); |
217 |
< |
if (n >= array.length) |
218 |
< |
grow(n + 1); |
217 |
> |
if (n >= items.length) |
218 |
> |
items = grow(n + 1); |
219 |
|
if (index < n) |
220 |
< |
System.arraycopy(array, index, array, index + 1, n - index); |
221 |
< |
array[index] = e; |
220 |
> |
System.arraycopy(items, index, items, index + 1, n - index); |
221 |
> |
items[index] = e; |
222 |
|
count = n + 1; |
223 |
|
} |
224 |
|
|
225 |
|
final boolean rawAddAllAt(int index, Object[] elements) { |
226 |
|
int n = count; |
227 |
+ |
Object[] items = array; |
228 |
|
if (index < 0 || index > n) |
229 |
|
throw new ArrayIndexOutOfBoundsException(index); |
230 |
|
int len = elements.length; |
231 |
|
if (len == 0) |
232 |
|
return false; |
233 |
|
int newCount = n + len; |
234 |
< |
if (newCount >= array.length) |
235 |
< |
grow(newCount); |
236 |
< |
int mv = count - index; |
234 |
> |
if (newCount >= items.length) |
235 |
> |
items = grow(newCount); |
236 |
> |
int mv = n - index; |
237 |
|
if (mv > 0) |
238 |
< |
System.arraycopy(array, index, array, index + len, mv); |
239 |
< |
System.arraycopy(elements, 0, array, index, len); |
238 |
> |
System.arraycopy(items, index, items, index + len, mv); |
239 |
> |
System.arraycopy(elements, 0, items, index, len); |
240 |
|
count = newCount; |
241 |
|
return true; |
242 |
|
} |
243 |
|
|
244 |
|
final boolean rawRemoveAt(int index) { |
245 |
|
int n = count - 1; |
246 |
+ |
Object[] items = array; |
247 |
|
if (index < 0 || index > n) |
248 |
|
return false; |
249 |
|
int mv = n - index; |
250 |
|
if (mv > 0) |
251 |
< |
System.arraycopy(array, index + 1, array, index, mv); |
252 |
< |
array[n] = null; |
251 |
> |
System.arraycopy(items, index + 1, items, index, mv); |
252 |
> |
items[n] = null; |
253 |
|
count = n; |
254 |
|
return true; |
255 |
|
} |
262 |
|
* field under lock. |
263 |
|
*/ |
264 |
|
final boolean internalRemoveAll(Collection<?> c, int origin, int bound) { |
265 |
< |
SequenceLock lock = this.lock; |
265 |
> |
final SequenceLock lock = this.lock; |
266 |
|
boolean removed = false; |
267 |
|
lock.lock(); |
268 |
|
try { |
281 |
|
} |
282 |
|
|
283 |
|
final boolean internalRetainAll(Collection<?> c, int origin, int bound) { |
284 |
< |
SequenceLock lock = this.lock; |
284 |
> |
final SequenceLock lock = this.lock; |
285 |
|
boolean removed = false; |
286 |
|
if (c != this) { |
287 |
|
lock.lock(); |
288 |
|
try { |
289 |
+ |
Object[] items = array; |
290 |
|
int i = origin; |
291 |
|
int n = count; |
292 |
|
int fence = bound < 0 || bound > n ? n : bound; |
293 |
|
while (i >= 0 && i < fence) { |
294 |
< |
if (c.contains(array[i])) |
294 |
> |
if (c.contains(items[i])) |
295 |
|
++i; |
296 |
|
else { |
297 |
|
--fence; |
298 |
< |
int mv = --count - i; |
298 |
> |
int mv = --n - i; |
299 |
|
if (mv > 0) |
300 |
< |
System.arraycopy(array, i + 1, array, i, mv); |
295 |
< |
removed = true; |
300 |
> |
System.arraycopy(items, i + 1, items, i, mv); |
301 |
|
} |
302 |
|
} |
303 |
+ |
if (count != n) { |
304 |
+ |
count = n; |
305 |
+ |
removed = true; |
306 |
+ |
} |
307 |
|
} finally { |
308 |
|
lock.unlock(); |
309 |
|
} |
315 |
|
int n = count; |
316 |
|
int fence = bound < 0 || bound > n ? n : bound; |
317 |
|
if (origin >= 0 && origin < fence) { |
318 |
+ |
Object[] items = array; |
319 |
|
int removed = fence - origin; |
320 |
|
int newCount = n - removed; |
321 |
|
int mv = n - (origin + removed); |
322 |
|
if (mv > 0) |
323 |
< |
System.arraycopy(array, origin + removed, array, origin, mv); |
323 |
> |
System.arraycopy(items, origin + removed, items, origin, mv); |
324 |
|
for (int i = n; i < newCount; ++i) |
325 |
< |
array[i] = null; |
325 |
> |
items[i] = null; |
326 |
|
count = newCount; |
327 |
|
} |
328 |
|
} |
329 |
|
|
330 |
|
final boolean internalContainsAll(Collection<?> c, int origin, int bound) { |
331 |
< |
SequenceLock lock = this.lock; |
331 |
> |
final SequenceLock lock = this.lock; |
332 |
|
boolean contained; |
333 |
|
boolean locked = false; |
334 |
|
try { |
335 |
|
for (;;) { |
336 |
|
long seq = lock.awaitAvailability(); |
337 |
+ |
int n = count; |
338 |
|
Object[] items = array; |
339 |
|
int len = items.length; |
329 |
– |
int n = count; |
340 |
|
if (n > len) |
341 |
|
continue; |
342 |
|
int fence = bound < 0 || bound > n ? n : bound; |
368 |
|
} |
369 |
|
|
370 |
|
final boolean internalEquals(List<?> list, int origin, int bound) { |
371 |
< |
SequenceLock lock = this.lock; |
371 |
> |
final SequenceLock lock = this.lock; |
372 |
|
boolean locked = false; |
373 |
|
boolean equal; |
374 |
|
try { |
409 |
|
} |
410 |
|
|
411 |
|
final int internalHashCode(int origin, int bound) { |
412 |
< |
SequenceLock lock = this.lock; |
412 |
> |
final SequenceLock lock = this.lock; |
413 |
|
int hash; |
414 |
|
boolean locked = false; |
415 |
|
try { |
417 |
|
hash = 1; |
418 |
|
long seq = lock.awaitAvailability(); |
419 |
|
Object[] items = array; |
410 |
– |
int len = items.length; |
420 |
|
int n = count; |
421 |
+ |
int len = items.length; |
422 |
|
if (n > len) |
423 |
|
continue; |
424 |
|
int fence = bound < 0 || bound > n ? n : bound; |
441 |
|
} |
442 |
|
|
443 |
|
final String internalToString(int origin, int bound) { |
444 |
< |
SequenceLock lock = this.lock; |
444 |
> |
final SequenceLock lock = this.lock; |
445 |
|
String ret; |
446 |
|
boolean locked = false; |
447 |
|
try { |
448 |
|
outer:for (;;) { |
449 |
|
long seq = lock.awaitAvailability(); |
450 |
|
Object[] items = array; |
441 |
– |
int len = items.length; |
451 |
|
int n = count; |
452 |
+ |
int len = items.length; |
453 |
|
if (n > len) |
454 |
|
continue; |
455 |
|
int fence = bound < 0 || bound > n ? n : bound; |
488 |
|
|
489 |
|
final Object[] internalToArray(int origin, int bound) { |
490 |
|
Object[] result; |
491 |
< |
SequenceLock lock = this.lock; |
491 |
> |
final SequenceLock lock = this.lock; |
492 |
|
boolean locked = false; |
493 |
|
try { |
494 |
|
for (;;) { |
495 |
|
result = null; |
496 |
|
long seq = lock.awaitAvailability(); |
497 |
|
Object[] items = array; |
488 |
– |
int len = items.length; |
498 |
|
int n = count; |
499 |
+ |
int len = items.length; |
500 |
|
if (n > len) |
501 |
|
continue; |
502 |
|
int fence = bound < 0 || bound > n ? n : bound; |
515 |
|
return result; |
516 |
|
} |
517 |
|
|
518 |
+ |
@SuppressWarnings("unchecked") |
519 |
|
final <T> T[] internalToArray(T[] a, int origin, int bound) { |
520 |
|
int alen = a.length; |
521 |
|
T[] result; |
522 |
< |
SequenceLock lock = this.lock; |
522 |
> |
final SequenceLock lock = this.lock; |
523 |
|
boolean locked = false; |
524 |
|
try { |
525 |
|
for (;;) { |
526 |
|
long seq = lock.awaitAvailability(); |
527 |
|
Object[] items = array; |
517 |
– |
int len = items.length; |
528 |
|
int n = count; |
529 |
+ |
int len = items.length; |
530 |
|
if (n > len) |
531 |
|
continue; |
532 |
|
int fence = bound < 0 || bound > n ? n : bound; |
535 |
|
rlen = 0; |
536 |
|
if (origin < 0 || alen >= rlen) { |
537 |
|
if (rlen > 0) |
538 |
< |
System.arraycopy(array, 0, a, origin, rlen); |
538 |
> |
System.arraycopy(items, 0, a, origin, rlen); |
539 |
|
if (alen > rlen) |
540 |
|
a[rlen] = null; |
541 |
|
result = a; |
542 |
|
} |
543 |
|
else |
544 |
< |
result = (T[]) Arrays.copyOfRange(array, origin, |
544 |
> |
result = (T[]) Arrays.copyOfRange(items, origin, |
545 |
|
fence, a.getClass()); |
546 |
|
if (lock.getSequence() == seq) |
547 |
|
break; |
558 |
|
// public List methods |
559 |
|
|
560 |
|
public boolean add(E e) { |
561 |
< |
SequenceLock lock = this.lock; |
561 |
> |
final SequenceLock lock = this.lock; |
562 |
|
lock.lock(); |
563 |
|
try { |
564 |
|
rawAdd(e); |
569 |
|
} |
570 |
|
|
571 |
|
public void add(int index, E element) { |
572 |
< |
SequenceLock lock = this.lock; |
572 |
> |
final SequenceLock lock = this.lock; |
573 |
|
lock.lock(); |
574 |
|
try { |
575 |
|
rawAddAt(index, element); |
583 |
|
int len = elements.length; |
584 |
|
if (len == 0) |
585 |
|
return false; |
586 |
< |
SequenceLock lock = this.lock; |
586 |
> |
final SequenceLock lock = this.lock; |
587 |
|
lock.lock(); |
588 |
|
try { |
589 |
< |
int newCount = count + len; |
590 |
< |
if (newCount >= array.length) |
591 |
< |
grow(newCount); |
592 |
< |
System.arraycopy(elements, 0, array, count, len); |
589 |
> |
Object[] items = array; |
590 |
> |
int n = count; |
591 |
> |
int newCount = n + len; |
592 |
> |
if (newCount >= items.length) |
593 |
> |
items = grow(newCount); |
594 |
> |
System.arraycopy(elements, 0, items, n, len); |
595 |
|
count = newCount; |
596 |
|
} finally { |
597 |
|
lock.unlock(); |
600 |
|
} |
601 |
|
|
602 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
603 |
< |
SequenceLock lock = this.lock; |
603 |
> |
final SequenceLock lock = this.lock; |
604 |
|
boolean ret; |
605 |
|
Object[] elements = c.toArray(); |
606 |
|
lock.lock(); |
613 |
|
} |
614 |
|
|
615 |
|
public void clear() { |
616 |
< |
SequenceLock lock = this.lock; |
616 |
> |
final SequenceLock lock = this.lock; |
617 |
|
lock.lock(); |
618 |
|
try { |
619 |
< |
for (int i = 0; i < count; i++) |
620 |
< |
array[i] = null; |
619 |
> |
int n = count; |
620 |
> |
Object[] items = array; |
621 |
> |
for (int i = 0; i < n; i++) |
622 |
> |
items[i] = null; |
623 |
|
count = 0; |
624 |
|
} finally { |
625 |
|
lock.unlock(); |
643 |
|
} |
644 |
|
|
645 |
|
public E get(int index) { |
646 |
< |
SequenceLock lock = this.lock; |
646 |
> |
final SequenceLock lock = this.lock; |
647 |
|
for (;;) { |
648 |
|
long seq = lock.awaitAvailability(); |
634 |
– |
Object[] items = array; |
649 |
|
int n = count; |
650 |
+ |
Object[] items = array; |
651 |
|
if (n > items.length) |
652 |
|
continue; |
653 |
< |
Object e; boolean ex; |
654 |
< |
if (index < 0 || index >= n) { |
655 |
< |
e = null; |
641 |
< |
ex = true; |
642 |
< |
} |
643 |
< |
else { |
644 |
< |
e = items[index]; |
645 |
< |
ex = false; |
646 |
< |
} |
653 |
> |
boolean outOfBounds = (index < 0 || index >= n); |
654 |
> |
@SuppressWarnings("unchecked") |
655 |
> |
E e = outOfBounds ? null : (E) items[index]; |
656 |
|
if (lock.getSequence() == seq) { |
657 |
< |
if (ex) |
657 |
> |
if (outOfBounds) |
658 |
|
throw new ArrayIndexOutOfBoundsException(index); |
659 |
|
else |
660 |
< |
return (E)e; |
660 |
> |
return e; |
661 |
|
} |
662 |
|
} |
663 |
|
} |
667 |
|
} |
668 |
|
|
669 |
|
public int indexOf(Object o) { |
670 |
< |
SequenceLock lock = this.lock; |
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 |
< |
} |
680 |
< |
return -1; |
681 |
< |
} |
682 |
< |
} |
670 |
> |
return indexOf(o, 0); |
671 |
|
} |
672 |
|
|
673 |
|
public boolean isEmpty() { |
686 |
– |
long ignore = lock.getSequence(); |
674 |
|
return count == 0; |
675 |
|
} |
676 |
|
|
679 |
|
} |
680 |
|
|
681 |
|
public int lastIndexOf(Object o) { |
682 |
< |
SequenceLock lock = this.lock; |
682 |
> |
final SequenceLock lock = this.lock; |
683 |
|
for (;;) { |
684 |
|
long seq = lock.awaitAvailability(); |
685 |
|
Object[] items = array; |
712 |
|
} |
713 |
|
|
714 |
|
public E remove(int index) { |
715 |
< |
SequenceLock lock = this.lock; |
729 |
< |
Object oldValue; |
715 |
> |
final SequenceLock lock = this.lock; |
716 |
|
lock.lock(); |
717 |
|
try { |
718 |
|
if (index < 0 || index >= count) |
719 |
|
throw new ArrayIndexOutOfBoundsException(index); |
720 |
< |
oldValue = array[index]; |
720 |
> |
@SuppressWarnings("unchecked") |
721 |
> |
E oldValue = (E) array[index]; |
722 |
|
rawRemoveAt(index); |
723 |
+ |
return oldValue; |
724 |
|
} finally { |
725 |
|
lock.unlock(); |
726 |
|
} |
739 |
– |
return (E)oldValue; |
727 |
|
} |
728 |
|
|
729 |
|
public boolean remove(Object o) { |
730 |
< |
SequenceLock lock = this.lock; |
744 |
< |
boolean removed; |
730 |
> |
final SequenceLock lock = this.lock; |
731 |
|
lock.lock(); |
732 |
|
try { |
733 |
< |
removed = rawRemoveAt(rawIndexOf(o, 0, count)); |
733 |
> |
return rawRemoveAt(rawIndexOf(o, 0, count)); |
734 |
|
} finally { |
735 |
|
lock.unlock(); |
736 |
|
} |
751 |
– |
return removed; |
737 |
|
} |
738 |
|
|
739 |
|
public boolean removeAll(Collection<?> c) { |
745 |
|
} |
746 |
|
|
747 |
|
public E set(int index, E element) { |
748 |
< |
Object oldValue; |
764 |
< |
SequenceLock lock = this.lock; |
748 |
> |
final SequenceLock lock = this.lock; |
749 |
|
lock.lock(); |
750 |
|
try { |
751 |
+ |
Object[] items = array; |
752 |
|
if (index < 0 || index >= count) |
753 |
|
throw new ArrayIndexOutOfBoundsException(index); |
754 |
< |
oldValue = array[index]; |
755 |
< |
array[index] = element; |
754 |
> |
@SuppressWarnings("unchecked") |
755 |
> |
E oldValue = (E) items[index]; |
756 |
> |
items[index] = element; |
757 |
> |
return oldValue; |
758 |
|
} finally { |
759 |
|
lock.unlock(); |
760 |
|
} |
774 |
– |
return (E)oldValue; |
761 |
|
} |
762 |
|
|
763 |
|
public int size() { |
778 |
– |
long ignore = lock.getSequence(); |
764 |
|
return count; |
765 |
|
} |
766 |
|
|
793 |
|
* @return {@code true} if the element was added |
794 |
|
*/ |
795 |
|
public boolean addIfAbsent(E e) { |
796 |
< |
boolean added; |
812 |
< |
SequenceLock lock = this.lock; |
796 |
> |
final SequenceLock lock = this.lock; |
797 |
|
lock.lock(); |
798 |
|
try { |
799 |
|
if (rawIndexOf(e, 0, count) < 0) { |
800 |
|
rawAdd(e); |
801 |
< |
added = true; |
801 |
> |
return true; |
802 |
|
} |
803 |
|
else |
804 |
< |
added = false; |
804 |
> |
return false; |
805 |
|
} finally { |
806 |
|
lock.unlock(); |
807 |
|
} |
824 |
– |
return added; |
808 |
|
} |
809 |
|
|
810 |
|
/** |
826 |
|
lock.lock(); |
827 |
|
try { |
828 |
|
for (int i = 0; i < clen; ++i) { |
829 |
< |
Object e = cs[i]; |
829 |
> |
@SuppressWarnings("unchecked") |
830 |
> |
E e = (E) cs[i]; |
831 |
|
if (rawIndexOf(e, 0, count) < 0) { |
832 |
|
rawAdd(e); |
833 |
|
++added; |
853 |
|
} |
854 |
|
|
855 |
|
static final class SnapshotIterator<E> implements Iterator<E> { |
856 |
< |
final Object[] items; |
857 |
< |
int cursor; |
856 |
> |
private final Object[] items; |
857 |
> |
private int cursor; |
858 |
|
SnapshotIterator(ReadMostlyVector<E> v) { items = v.toArray(); } |
859 |
|
public boolean hasNext() { return cursor < items.length; } |
860 |
+ |
@SuppressWarnings("unchecked") |
861 |
|
public E next() { |
862 |
|
if (cursor < items.length) |
863 |
|
return (E) items[cursor++]; |
870 |
|
|
871 |
|
/** See {@link Vector#firstElement} */ |
872 |
|
public E firstElement() { |
873 |
< |
SequenceLock lock = this.lock; |
873 |
> |
final SequenceLock lock = this.lock; |
874 |
|
for (;;) { |
875 |
|
long seq = lock.awaitAvailability(); |
876 |
|
Object[] items = array; |
878 |
|
int n = count; |
879 |
|
if (n > len) |
880 |
|
continue; |
881 |
< |
Object e; boolean ex; |
882 |
< |
if (n > 0) { |
883 |
< |
e = items[0]; |
899 |
< |
ex = false; |
900 |
< |
} |
901 |
< |
else { |
902 |
< |
e = null; |
903 |
< |
ex = true; |
904 |
< |
} |
881 |
> |
boolean empty = (n == 0); |
882 |
> |
@SuppressWarnings("unchecked") |
883 |
> |
E e = empty ? null : (E) items[0]; |
884 |
|
if (lock.getSequence() == seq) { |
885 |
< |
if (ex) |
885 |
> |
if (empty) |
886 |
|
throw new NoSuchElementException(); |
887 |
|
else |
888 |
< |
return (E)e; |
888 |
> |
return e; |
889 |
|
} |
890 |
|
} |
891 |
|
} |
892 |
|
|
893 |
|
/** See {@link Vector#lastElement} */ |
894 |
|
public E lastElement() { |
895 |
< |
SequenceLock lock = this.lock; |
895 |
> |
final SequenceLock lock = this.lock; |
896 |
|
for (;;) { |
897 |
|
long seq = lock.awaitAvailability(); |
898 |
|
Object[] items = array; |
900 |
|
int n = count; |
901 |
|
if (n > len) |
902 |
|
continue; |
903 |
< |
Object e; boolean ex; |
904 |
< |
if (n > 0) { |
905 |
< |
e = items[n - 1]; |
927 |
< |
ex = false; |
928 |
< |
} |
929 |
< |
else { |
930 |
< |
e = null; |
931 |
< |
ex = true; |
932 |
< |
} |
903 |
> |
boolean empty = (n == 0); |
904 |
> |
@SuppressWarnings("unchecked") |
905 |
> |
E e = empty ? null : (E) items[n - 1]; |
906 |
|
if (lock.getSequence() == seq) { |
907 |
< |
if (ex) |
907 |
> |
if (empty) |
908 |
|
throw new NoSuchElementException(); |
909 |
|
else |
910 |
< |
return (E)e; |
910 |
> |
return e; |
911 |
|
} |
912 |
|
} |
913 |
|
} |
914 |
|
|
915 |
|
/** See {@link Vector#indexOf(Object, int)} */ |
916 |
|
public int indexOf(Object o, int index) { |
917 |
< |
SequenceLock lock = this.lock; |
917 |
> |
final SequenceLock lock = this.lock; |
918 |
|
int idx = 0; |
919 |
|
boolean ex = false; |
920 |
|
long seq = lock.awaitAvailability(); |
933 |
|
if (index < 0) |
934 |
|
ex = true; |
935 |
|
else |
936 |
< |
idx = rawIndexOf(o, 0, count); |
936 |
> |
idx = rawIndexOf(o, index, count); |
937 |
|
} finally { |
938 |
|
lock.unlock(); |
939 |
|
} |
945 |
|
|
946 |
|
/** See {@link Vector#lastIndexOf(Object, int)} */ |
947 |
|
public int lastIndexOf(Object o, int index) { |
948 |
< |
SequenceLock lock = this.lock; |
948 |
> |
final SequenceLock lock = this.lock; |
949 |
|
int idx = 0; |
950 |
|
boolean ex = false; |
951 |
|
long seq = lock.awaitAvailability(); |
978 |
|
public void setSize(int newSize) { |
979 |
|
if (newSize < 0) |
980 |
|
throw new ArrayIndexOutOfBoundsException(newSize); |
981 |
< |
SequenceLock lock = this.lock; |
981 |
> |
final SequenceLock lock = this.lock; |
982 |
|
lock.lock(); |
983 |
|
try { |
984 |
|
int n = count; |
985 |
|
if (newSize > n) |
986 |
|
grow(newSize); |
987 |
|
else { |
988 |
+ |
Object[] items = array; |
989 |
|
for (int i = newSize ; i < n ; i++) |
990 |
< |
array[i] = null; |
990 |
> |
items[i] = null; |
991 |
|
} |
992 |
|
count = newSize; |
993 |
|
} finally { |
997 |
|
|
998 |
|
/** See {@link Vector#copyInto} */ |
999 |
|
public void copyInto(Object[] anArray) { |
1000 |
< |
SequenceLock lock = this.lock; |
1000 |
> |
final SequenceLock lock = this.lock; |
1001 |
|
lock.lock(); |
1002 |
|
try { |
1003 |
|
System.arraycopy(array, 0, anArray, 0, count); |
1008 |
|
|
1009 |
|
/** See {@link Vector#trimToSize} */ |
1010 |
|
public void trimToSize() { |
1011 |
< |
SequenceLock lock = this.lock; |
1011 |
> |
final SequenceLock lock = this.lock; |
1012 |
|
lock.lock(); |
1013 |
|
try { |
1014 |
< |
if (count < array.length) |
1015 |
< |
array = Arrays.copyOf(array, count); |
1014 |
> |
Object[] items = array; |
1015 |
> |
int n = count; |
1016 |
> |
if (n < items.length) |
1017 |
> |
array = Arrays.copyOf(items, n); |
1018 |
|
} finally { |
1019 |
|
lock.unlock(); |
1020 |
|
} |
1023 |
|
/** See {@link Vector#ensureCapacity} */ |
1024 |
|
public void ensureCapacity(int minCapacity) { |
1025 |
|
if (minCapacity > 0) { |
1026 |
< |
SequenceLock lock = this.lock; |
1026 |
> |
final SequenceLock lock = this.lock; |
1027 |
|
lock.lock(); |
1028 |
|
try { |
1029 |
|
if (minCapacity - array.length > 0) |
1041 |
|
|
1042 |
|
/** See {@link Vector#capacity} */ |
1043 |
|
public int capacity() { |
1068 |
– |
long ignore = lock.getSequence(); |
1044 |
|
return array.length; |
1045 |
|
} |
1046 |
|
|
1082 |
|
// other methods |
1083 |
|
|
1084 |
|
public ReadMostlyVector<E> clone() { |
1085 |
< |
SequenceLock lock = this.lock; |
1085 |
> |
final SequenceLock lock = this.lock; |
1086 |
|
Object[] a = null; |
1087 |
|
boolean retry = false; |
1088 |
|
long seq = lock.awaitAvailability(); |
1106 |
|
|
1107 |
|
private void writeObject(java.io.ObjectOutputStream s) |
1108 |
|
throws java.io.IOException { |
1109 |
< |
SequenceLock lock = this.lock; |
1109 |
> |
final SequenceLock lock = this.lock; |
1110 |
|
lock.lock(); |
1111 |
|
try { |
1112 |
|
s.defaultWriteObject(); |
1115 |
|
} |
1116 |
|
} |
1117 |
|
|
1118 |
< |
static final class Itr<E> implements ListIterator<E>, Enumeration<E> { |
1118 |
> |
static final class Itr<E> implements ListIterator<E>, Enumeration<E> { |
1119 |
|
final ReadMostlyVector<E> list; |
1120 |
|
final SequenceLock lock; |
1121 |
|
Object[] items; |
1122 |
< |
Object next, prev; |
1122 |
> |
E next, prev; |
1123 |
|
long seq; |
1124 |
|
int cursor; |
1125 |
|
int fence; |
1145 |
|
lock.getSequence() != seq); |
1146 |
|
} |
1147 |
|
|
1148 |
+ |
@SuppressWarnings("unchecked") |
1149 |
|
public boolean hasNext() { |
1150 |
|
boolean valid; |
1151 |
|
int i = cursor; |
1154 |
|
valid = false; |
1155 |
|
break; |
1156 |
|
} |
1157 |
< |
next = items[i]; |
1157 |
> |
next = (E) items[i]; |
1158 |
|
if (lock.getSequence() == seq) { |
1159 |
|
valid = true; |
1160 |
|
break; |
1164 |
|
return validNext = valid; |
1165 |
|
} |
1166 |
|
|
1167 |
+ |
@SuppressWarnings("unchecked") |
1168 |
|
public boolean hasPrevious() { |
1169 |
|
boolean valid; |
1170 |
|
int i = cursor - 1; |
1173 |
|
valid = false; |
1174 |
|
break; |
1175 |
|
} |
1176 |
< |
prev = items[i]; |
1176 |
> |
prev = (E) items[i]; |
1177 |
|
if (lock.getSequence() == seq) { |
1178 |
|
valid = true; |
1179 |
|
break; |
1187 |
|
if (validNext || hasNext()) { |
1188 |
|
validNext = false; |
1189 |
|
lastRet = cursor++; |
1190 |
< |
return (E) next; |
1190 |
> |
return next; |
1191 |
|
} |
1192 |
|
throw new NoSuchElementException(); |
1193 |
|
} |
1196 |
|
if (validPrev || hasPrevious()) { |
1197 |
|
validPrev = false; |
1198 |
|
lastRet = cursor--; |
1199 |
< |
return (E) prev; |
1199 |
> |
return prev; |
1200 |
|
} |
1201 |
|
throw new NoSuchElementException(); |
1202 |
|
} |
1253 |
|
public int previousIndex() { return cursor - 1; } |
1254 |
|
} |
1255 |
|
|
1256 |
< |
static final class ReadMostlyVectorSublist<E> implements List<E>, RandomAccess, java.io.Serializable { |
1256 |
> |
static final class ReadMostlyVectorSublist<E> |
1257 |
> |
implements List<E>, RandomAccess, java.io.Serializable { |
1258 |
> |
static final long serialVersionUID = 3041673470172026059L; |
1259 |
> |
|
1260 |
|
final ReadMostlyVector<E> list; |
1261 |
|
final int offset; |
1262 |
|
volatile int size; |
1263 |
|
|
1264 |
< |
ReadMostlyVectorSublist(ReadMostlyVector<E> list, int offset, int size) { |
1264 |
> |
ReadMostlyVectorSublist(ReadMostlyVector<E> list, |
1265 |
> |
int offset, int size) { |
1266 |
|
this.list = list; |
1267 |
|
this.offset = offset; |
1268 |
|
this.size = size; |
1274 |
|
} |
1275 |
|
|
1276 |
|
public boolean add(E element) { |
1277 |
< |
SequenceLock lock = list.lock; |
1277 |
> |
final SequenceLock lock = list.lock; |
1278 |
|
lock.lock(); |
1279 |
|
try { |
1280 |
|
int c = size; |
1287 |
|
} |
1288 |
|
|
1289 |
|
public void add(int index, E element) { |
1290 |
< |
SequenceLock lock = list.lock; |
1290 |
> |
final SequenceLock lock = list.lock; |
1291 |
|
lock.lock(); |
1292 |
|
try { |
1293 |
|
if (index < 0 || index > size) |
1301 |
|
|
1302 |
|
public boolean addAll(Collection<? extends E> c) { |
1303 |
|
Object[] elements = c.toArray(); |
1304 |
< |
int added; |
1324 |
< |
SequenceLock lock = list.lock; |
1304 |
> |
final SequenceLock lock = list.lock; |
1305 |
|
lock.lock(); |
1306 |
|
try { |
1307 |
|
int s = size; |
1308 |
|
int pc = list.count; |
1309 |
|
list.rawAddAllAt(offset + s, elements); |
1310 |
< |
added = list.count - pc; |
1310 |
> |
int added = list.count - pc; |
1311 |
|
size = s + added; |
1312 |
+ |
return added != 0; |
1313 |
|
} finally { |
1314 |
|
lock.unlock(); |
1315 |
|
} |
1335 |
– |
return added != 0; |
1316 |
|
} |
1317 |
|
|
1318 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
1319 |
|
Object[] elements = c.toArray(); |
1320 |
< |
int added; |
1341 |
< |
SequenceLock lock = list.lock; |
1320 |
> |
final SequenceLock lock = list.lock; |
1321 |
|
lock.lock(); |
1322 |
|
try { |
1323 |
|
int s = size; |
1325 |
|
throw new ArrayIndexOutOfBoundsException(index); |
1326 |
|
int pc = list.count; |
1327 |
|
list.rawAddAllAt(index + offset, elements); |
1328 |
< |
added = list.count - pc; |
1328 |
> |
int added = list.count - pc; |
1329 |
|
size = s + added; |
1330 |
+ |
return added != 0; |
1331 |
|
} finally { |
1332 |
|
lock.unlock(); |
1333 |
|
} |
1354 |
– |
return added != 0; |
1334 |
|
} |
1335 |
|
|
1336 |
|
public void clear() { |
1337 |
< |
SequenceLock lock = list.lock; |
1337 |
> |
final SequenceLock lock = list.lock; |
1338 |
|
lock.lock(); |
1339 |
|
try { |
1340 |
|
list.internalClear(offset, offset + size); |
1371 |
|
} |
1372 |
|
|
1373 |
|
public int indexOf(Object o) { |
1374 |
< |
SequenceLock lock = list.lock; |
1374 |
> |
final SequenceLock lock = list.lock; |
1375 |
|
long seq = lock.awaitAvailability(); |
1376 |
|
Object[] items = list.array; |
1377 |
|
int c = list.count; |
1399 |
|
} |
1400 |
|
|
1401 |
|
public int lastIndexOf(Object o) { |
1402 |
< |
SequenceLock lock = list.lock; |
1402 |
> |
final SequenceLock lock = list.lock; |
1403 |
|
long seq = lock.awaitAvailability(); |
1404 |
|
Object[] items = list.array; |
1405 |
|
int c = list.count; |
1427 |
|
} |
1428 |
|
|
1429 |
|
public E remove(int index) { |
1430 |
< |
Object result; |
1452 |
< |
SequenceLock lock = list.lock; |
1430 |
> |
final SequenceLock lock = list.lock; |
1431 |
|
lock.lock(); |
1432 |
|
try { |
1433 |
|
Object[] items = list.array; |
1434 |
|
int i = index + offset; |
1435 |
|
if (index < 0 || index >= size || i >= items.length) |
1436 |
|
throw new ArrayIndexOutOfBoundsException(index); |
1437 |
< |
result = items[i]; |
1437 |
> |
@SuppressWarnings("unchecked") |
1438 |
> |
E result = (E) items[i]; |
1439 |
|
list.rawRemoveAt(i); |
1440 |
|
size--; |
1441 |
+ |
return result; |
1442 |
|
} finally { |
1443 |
|
lock.unlock(); |
1444 |
|
} |
1465 |
– |
return (E)result; |
1445 |
|
} |
1446 |
|
|
1447 |
|
public boolean remove(Object o) { |
1448 |
< |
boolean removed = false; |
1470 |
< |
SequenceLock lock = list.lock; |
1448 |
> |
final SequenceLock lock = list.lock; |
1449 |
|
lock.lock(); |
1450 |
|
try { |
1451 |
|
if (list.rawRemoveAt(list.rawIndexOf(o, offset, |
1452 |
|
offset + size))) { |
1475 |
– |
removed = true; |
1453 |
|
--size; |
1454 |
+ |
return true; |
1455 |
|
} |
1456 |
+ |
else |
1457 |
+ |
return false; |
1458 |
|
} finally { |
1459 |
|
lock.unlock(); |
1460 |
|
} |
1481 |
– |
return removed; |
1461 |
|
} |
1462 |
|
|
1463 |
|
public boolean removeAll(Collection<?> c) { |
1505 |
|
final ReadMostlyVector<E> list; |
1506 |
|
final SequenceLock lock; |
1507 |
|
Object[] items; |
1508 |
< |
Object next, prev; |
1508 |
> |
E next, prev; |
1509 |
|
long seq; |
1510 |
|
int cursor; |
1511 |
|
int fence; |
1536 |
|
} while (lock.getSequence() != seq); |
1537 |
|
} |
1538 |
|
|
1539 |
+ |
@SuppressWarnings("unchecked") |
1540 |
|
public boolean hasNext() { |
1541 |
|
boolean valid; |
1542 |
|
int i = cursor; |
1545 |
|
valid = false; |
1546 |
|
break; |
1547 |
|
} |
1548 |
< |
next = items[i]; |
1548 |
> |
next = (E) items[i]; |
1549 |
|
if (lock.getSequence() == seq) { |
1550 |
|
valid = true; |
1551 |
|
break; |
1555 |
|
return validNext = valid; |
1556 |
|
} |
1557 |
|
|
1558 |
+ |
@SuppressWarnings("unchecked") |
1559 |
|
public boolean hasPrevious() { |
1560 |
|
boolean valid; |
1561 |
|
int i = cursor - 1; |
1564 |
|
valid = false; |
1565 |
|
break; |
1566 |
|
} |
1567 |
< |
prev = items[i]; |
1567 |
> |
prev = (E) items[i]; |
1568 |
|
if (lock.getSequence() == seq) { |
1569 |
|
valid = true; |
1570 |
|
break; |
1578 |
|
if (validNext || hasNext()) { |
1579 |
|
validNext = false; |
1580 |
|
lastRet = cursor++; |
1581 |
< |
return (E) next; |
1581 |
> |
return next; |
1582 |
|
} |
1583 |
|
throw new NoSuchElementException(); |
1584 |
|
} |
1587 |
|
if (validPrev || hasPrevious()) { |
1588 |
|
validPrev = false; |
1589 |
|
lastRet = cursor--; |
1590 |
< |
return (E) prev; |
1590 |
> |
return prev; |
1591 |
|
} |
1592 |
|
throw new NoSuchElementException(); |
1593 |
|
} |