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; |
670 |
> |
final SequenceLock lock = this.lock; |
671 |
|
for (;;) { |
672 |
|
long seq = lock.awaitAvailability(); |
673 |
|
Object[] items = array; |
692 |
|
} |
693 |
|
|
694 |
|
public boolean isEmpty() { |
686 |
– |
long ignore = lock.getSequence(); |
695 |
|
return count == 0; |
696 |
|
} |
697 |
|
|
698 |
|
public Iterator<E> iterator() { |
699 |
< |
return new Itr(this, 0); |
699 |
> |
return new Itr<E>(this, 0); |
700 |
|
} |
701 |
|
|
702 |
|
public int lastIndexOf(Object o) { |
703 |
< |
SequenceLock lock = this.lock; |
703 |
> |
final SequenceLock lock = this.lock; |
704 |
|
for (;;) { |
705 |
|
long seq = lock.awaitAvailability(); |
706 |
|
Object[] items = array; |
725 |
|
} |
726 |
|
|
727 |
|
public ListIterator<E> listIterator() { |
728 |
< |
return new Itr(this, 0); |
728 |
> |
return new Itr<E>(this, 0); |
729 |
|
} |
730 |
|
|
731 |
|
public ListIterator<E> listIterator(int index) { |
732 |
< |
return new Itr(this, index); |
732 |
> |
return new Itr<E>(this, index); |
733 |
|
} |
734 |
|
|
735 |
|
public E remove(int index) { |
736 |
< |
SequenceLock lock = this.lock; |
729 |
< |
Object oldValue; |
736 |
> |
final SequenceLock lock = this.lock; |
737 |
|
lock.lock(); |
738 |
|
try { |
739 |
|
if (index < 0 || index >= count) |
740 |
|
throw new ArrayIndexOutOfBoundsException(index); |
741 |
< |
oldValue = array[index]; |
741 |
> |
@SuppressWarnings("unchecked") |
742 |
> |
E oldValue = (E) array[index]; |
743 |
|
rawRemoveAt(index); |
744 |
+ |
return oldValue; |
745 |
|
} finally { |
746 |
|
lock.unlock(); |
747 |
|
} |
739 |
– |
return (E)oldValue; |
748 |
|
} |
749 |
|
|
750 |
|
public boolean remove(Object o) { |
751 |
< |
SequenceLock lock = this.lock; |
744 |
< |
boolean removed; |
751 |
> |
final SequenceLock lock = this.lock; |
752 |
|
lock.lock(); |
753 |
|
try { |
754 |
< |
removed = rawRemoveAt(rawIndexOf(o, 0, count)); |
754 |
> |
return rawRemoveAt(rawIndexOf(o, 0, count)); |
755 |
|
} finally { |
756 |
|
lock.unlock(); |
757 |
|
} |
751 |
– |
return removed; |
758 |
|
} |
759 |
|
|
760 |
|
public boolean removeAll(Collection<?> c) { |
766 |
|
} |
767 |
|
|
768 |
|
public E set(int index, E element) { |
769 |
< |
Object oldValue; |
764 |
< |
SequenceLock lock = this.lock; |
769 |
> |
final SequenceLock lock = this.lock; |
770 |
|
lock.lock(); |
771 |
|
try { |
772 |
+ |
Object[] items = array; |
773 |
|
if (index < 0 || index >= count) |
774 |
|
throw new ArrayIndexOutOfBoundsException(index); |
775 |
< |
oldValue = array[index]; |
776 |
< |
array[index] = element; |
775 |
> |
@SuppressWarnings("unchecked") |
776 |
> |
E oldValue = (E) items[index]; |
777 |
> |
items[index] = element; |
778 |
> |
return oldValue; |
779 |
|
} finally { |
780 |
|
lock.unlock(); |
781 |
|
} |
774 |
– |
return (E)oldValue; |
782 |
|
} |
783 |
|
|
784 |
|
public int size() { |
778 |
– |
long ignore = lock.getSequence(); |
785 |
|
return count; |
786 |
|
} |
787 |
|
|
790 |
|
int ssize = toIndex - fromIndex; |
791 |
|
if (fromIndex < 0 || toIndex > c || ssize < 0) |
792 |
|
throw new IndexOutOfBoundsException(); |
793 |
< |
return new ReadMostlyVectorSublist(this, fromIndex, ssize); |
793 |
> |
return new ReadMostlyVectorSublist<E>(this, fromIndex, ssize); |
794 |
|
} |
795 |
|
|
796 |
|
public Object[] toArray() { |
814 |
|
* @return {@code true} if the element was added |
815 |
|
*/ |
816 |
|
public boolean addIfAbsent(E e) { |
817 |
< |
boolean added; |
812 |
< |
SequenceLock lock = this.lock; |
817 |
> |
final SequenceLock lock = this.lock; |
818 |
|
lock.lock(); |
819 |
|
try { |
820 |
|
if (rawIndexOf(e, 0, count) < 0) { |
821 |
|
rawAdd(e); |
822 |
< |
added = true; |
822 |
> |
return true; |
823 |
|
} |
824 |
|
else |
825 |
< |
added = false; |
825 |
> |
return false; |
826 |
|
} finally { |
827 |
|
lock.unlock(); |
828 |
|
} |
824 |
– |
return added; |
829 |
|
} |
830 |
|
|
831 |
|
/** |
847 |
|
lock.lock(); |
848 |
|
try { |
849 |
|
for (int i = 0; i < clen; ++i) { |
850 |
< |
Object e = cs[i]; |
850 |
> |
@SuppressWarnings("unchecked") |
851 |
> |
E e = (E) cs[i]; |
852 |
|
if (rawIndexOf(e, 0, count) < 0) { |
853 |
|
rawAdd(e); |
854 |
|
++added; |
874 |
|
} |
875 |
|
|
876 |
|
static final class SnapshotIterator<E> implements Iterator<E> { |
877 |
< |
final Object[] items; |
878 |
< |
int cursor; |
877 |
> |
private final Object[] items; |
878 |
> |
private int cursor; |
879 |
|
SnapshotIterator(ReadMostlyVector<E> v) { items = v.toArray(); } |
880 |
|
public boolean hasNext() { return cursor < items.length; } |
881 |
+ |
@SuppressWarnings("unchecked") |
882 |
|
public E next() { |
883 |
|
if (cursor < items.length) |
884 |
|
return (E) items[cursor++]; |
891 |
|
|
892 |
|
/** See {@link Vector#firstElement} */ |
893 |
|
public E firstElement() { |
894 |
< |
SequenceLock lock = this.lock; |
894 |
> |
final SequenceLock lock = this.lock; |
895 |
|
for (;;) { |
896 |
|
long seq = lock.awaitAvailability(); |
897 |
|
Object[] items = array; |
899 |
|
int n = count; |
900 |
|
if (n > len) |
901 |
|
continue; |
902 |
< |
Object e; boolean ex; |
903 |
< |
if (n > 0) { |
904 |
< |
e = items[0]; |
899 |
< |
ex = false; |
900 |
< |
} |
901 |
< |
else { |
902 |
< |
e = null; |
903 |
< |
ex = true; |
904 |
< |
} |
902 |
> |
boolean empty = (n == 0); |
903 |
> |
@SuppressWarnings("unchecked") |
904 |
> |
E e = empty ? null : (E) items[0]; |
905 |
|
if (lock.getSequence() == seq) { |
906 |
< |
if (ex) |
906 |
> |
if (empty) |
907 |
|
throw new NoSuchElementException(); |
908 |
|
else |
909 |
< |
return (E)e; |
909 |
> |
return e; |
910 |
|
} |
911 |
|
} |
912 |
|
} |
913 |
|
|
914 |
|
/** See {@link Vector#lastElement} */ |
915 |
|
public E lastElement() { |
916 |
< |
SequenceLock lock = this.lock; |
916 |
> |
final SequenceLock lock = this.lock; |
917 |
|
for (;;) { |
918 |
|
long seq = lock.awaitAvailability(); |
919 |
|
Object[] items = array; |
921 |
|
int n = count; |
922 |
|
if (n > len) |
923 |
|
continue; |
924 |
< |
Object e; boolean ex; |
925 |
< |
if (n > 0) { |
926 |
< |
e = items[n - 1]; |
927 |
< |
ex = false; |
928 |
< |
} |
929 |
< |
else { |
930 |
< |
e = null; |
931 |
< |
ex = true; |
932 |
< |
} |
924 |
> |
boolean empty = (n == 0); |
925 |
> |
@SuppressWarnings("unchecked") |
926 |
> |
E e = empty ? null : (E) items[n - 1]; |
927 |
|
if (lock.getSequence() == seq) { |
928 |
< |
if (ex) |
928 |
> |
if (empty) |
929 |
|
throw new NoSuchElementException(); |
930 |
|
else |
931 |
< |
return (E)e; |
931 |
> |
return e; |
932 |
|
} |
933 |
|
} |
934 |
|
} |
935 |
|
|
936 |
|
/** See {@link Vector#indexOf(Object, int)} */ |
937 |
|
public int indexOf(Object o, int index) { |
938 |
< |
SequenceLock lock = this.lock; |
938 |
> |
final SequenceLock lock = this.lock; |
939 |
|
int idx = 0; |
940 |
|
boolean ex = false; |
941 |
|
long seq = lock.awaitAvailability(); |
966 |
|
|
967 |
|
/** See {@link Vector#lastIndexOf(Object, int)} */ |
968 |
|
public int lastIndexOf(Object o, int index) { |
969 |
< |
SequenceLock lock = this.lock; |
969 |
> |
final SequenceLock lock = this.lock; |
970 |
|
int idx = 0; |
971 |
|
boolean ex = false; |
972 |
|
long seq = lock.awaitAvailability(); |
999 |
|
public void setSize(int newSize) { |
1000 |
|
if (newSize < 0) |
1001 |
|
throw new ArrayIndexOutOfBoundsException(newSize); |
1002 |
< |
SequenceLock lock = this.lock; |
1002 |
> |
final SequenceLock lock = this.lock; |
1003 |
|
lock.lock(); |
1004 |
|
try { |
1005 |
|
int n = count; |
1006 |
|
if (newSize > n) |
1007 |
|
grow(newSize); |
1008 |
|
else { |
1009 |
+ |
Object[] items = array; |
1010 |
|
for (int i = newSize ; i < n ; i++) |
1011 |
< |
array[i] = null; |
1011 |
> |
items[i] = null; |
1012 |
|
} |
1013 |
|
count = newSize; |
1014 |
|
} finally { |
1018 |
|
|
1019 |
|
/** See {@link Vector#copyInto} */ |
1020 |
|
public void copyInto(Object[] anArray) { |
1021 |
< |
SequenceLock lock = this.lock; |
1021 |
> |
final SequenceLock lock = this.lock; |
1022 |
|
lock.lock(); |
1023 |
|
try { |
1024 |
|
System.arraycopy(array, 0, anArray, 0, count); |
1029 |
|
|
1030 |
|
/** See {@link Vector#trimToSize} */ |
1031 |
|
public void trimToSize() { |
1032 |
< |
SequenceLock lock = this.lock; |
1032 |
> |
final SequenceLock lock = this.lock; |
1033 |
|
lock.lock(); |
1034 |
|
try { |
1035 |
< |
if (count < array.length) |
1036 |
< |
array = Arrays.copyOf(array, count); |
1035 |
> |
Object[] items = array; |
1036 |
> |
int n = count; |
1037 |
> |
if (n < items.length) |
1038 |
> |
array = Arrays.copyOf(items, n); |
1039 |
|
} finally { |
1040 |
|
lock.unlock(); |
1041 |
|
} |
1044 |
|
/** See {@link Vector#ensureCapacity} */ |
1045 |
|
public void ensureCapacity(int minCapacity) { |
1046 |
|
if (minCapacity > 0) { |
1047 |
< |
SequenceLock lock = this.lock; |
1047 |
> |
final SequenceLock lock = this.lock; |
1048 |
|
lock.lock(); |
1049 |
|
try { |
1050 |
|
if (minCapacity - array.length > 0) |
1057 |
|
|
1058 |
|
/** See {@link Vector#elements} */ |
1059 |
|
public Enumeration<E> elements() { |
1060 |
< |
return new Itr(this, 0); |
1060 |
> |
return new Itr<E>(this, 0); |
1061 |
|
} |
1062 |
|
|
1063 |
|
/** See {@link Vector#capacity} */ |
1064 |
|
public int capacity() { |
1068 |
– |
long ignore = lock.getSequence(); |
1065 |
|
return array.length; |
1066 |
|
} |
1067 |
|
|
1102 |
|
|
1103 |
|
// other methods |
1104 |
|
|
1105 |
< |
public Object clone() { |
1106 |
< |
SequenceLock lock = this.lock; |
1105 |
> |
public ReadMostlyVector<E> clone() { |
1106 |
> |
final SequenceLock lock = this.lock; |
1107 |
|
Object[] a = null; |
1108 |
|
boolean retry = false; |
1109 |
|
long seq = lock.awaitAvailability(); |
1122 |
|
lock.unlock(); |
1123 |
|
} |
1124 |
|
} |
1125 |
< |
return new ReadMostlyVector(a, n, capacityIncrement); |
1125 |
> |
return new ReadMostlyVector<E>(a, n, capacityIncrement); |
1126 |
|
} |
1127 |
|
|
1128 |
|
private void writeObject(java.io.ObjectOutputStream s) |
1129 |
|
throws java.io.IOException { |
1130 |
< |
SequenceLock lock = this.lock; |
1130 |
> |
final SequenceLock lock = this.lock; |
1131 |
|
lock.lock(); |
1132 |
|
try { |
1133 |
|
s.defaultWriteObject(); |
1140 |
|
final ReadMostlyVector<E> list; |
1141 |
|
final SequenceLock lock; |
1142 |
|
Object[] items; |
1143 |
< |
Object next, prev; |
1143 |
> |
E next, prev; |
1144 |
|
long seq; |
1145 |
|
int cursor; |
1146 |
|
int fence; |
1166 |
|
lock.getSequence() != seq); |
1167 |
|
} |
1168 |
|
|
1169 |
+ |
@SuppressWarnings("unchecked") |
1170 |
|
public boolean hasNext() { |
1171 |
|
boolean valid; |
1172 |
|
int i = cursor; |
1175 |
|
valid = false; |
1176 |
|
break; |
1177 |
|
} |
1178 |
< |
next = items[i]; |
1178 |
> |
next = (E) items[i]; |
1179 |
|
if (lock.getSequence() == seq) { |
1180 |
|
valid = true; |
1181 |
|
break; |
1185 |
|
return validNext = valid; |
1186 |
|
} |
1187 |
|
|
1188 |
+ |
@SuppressWarnings("unchecked") |
1189 |
|
public boolean hasPrevious() { |
1190 |
|
boolean valid; |
1191 |
|
int i = cursor - 1; |
1194 |
|
valid = false; |
1195 |
|
break; |
1196 |
|
} |
1197 |
< |
prev = items[i]; |
1197 |
> |
prev = (E) items[i]; |
1198 |
|
if (lock.getSequence() == seq) { |
1199 |
|
valid = true; |
1200 |
|
break; |
1208 |
|
if (validNext || hasNext()) { |
1209 |
|
validNext = false; |
1210 |
|
lastRet = cursor++; |
1211 |
< |
return (E) next; |
1211 |
> |
return next; |
1212 |
|
} |
1213 |
|
throw new NoSuchElementException(); |
1214 |
|
} |
1217 |
|
if (validPrev || hasPrevious()) { |
1218 |
|
validPrev = false; |
1219 |
|
lastRet = cursor--; |
1220 |
< |
return (E) prev; |
1220 |
> |
return prev; |
1221 |
|
} |
1222 |
|
throw new NoSuchElementException(); |
1223 |
|
} |
1274 |
|
public int previousIndex() { return cursor - 1; } |
1275 |
|
} |
1276 |
|
|
1277 |
< |
static final class ReadMostlyVectorSublist<E> implements List<E>, RandomAccess, java.io.Serializable { |
1277 |
> |
static final class ReadMostlyVectorSublist<E> |
1278 |
> |
implements List<E>, RandomAccess, java.io.Serializable { |
1279 |
> |
static final long serialVersionUID = 3041673470172026059L; |
1280 |
> |
|
1281 |
|
final ReadMostlyVector<E> list; |
1282 |
|
final int offset; |
1283 |
|
volatile int size; |
1284 |
|
|
1285 |
< |
ReadMostlyVectorSublist(ReadMostlyVector<E> list, int offset, int size) { |
1285 |
> |
ReadMostlyVectorSublist(ReadMostlyVector<E> list, |
1286 |
> |
int offset, int size) { |
1287 |
|
this.list = list; |
1288 |
|
this.offset = offset; |
1289 |
|
this.size = size; |
1295 |
|
} |
1296 |
|
|
1297 |
|
public boolean add(E element) { |
1298 |
< |
SequenceLock lock = list.lock; |
1298 |
> |
final SequenceLock lock = list.lock; |
1299 |
|
lock.lock(); |
1300 |
|
try { |
1301 |
|
int c = size; |
1308 |
|
} |
1309 |
|
|
1310 |
|
public void add(int index, E element) { |
1311 |
< |
SequenceLock lock = list.lock; |
1311 |
> |
final SequenceLock lock = list.lock; |
1312 |
|
lock.lock(); |
1313 |
|
try { |
1314 |
|
if (index < 0 || index > size) |
1322 |
|
|
1323 |
|
public boolean addAll(Collection<? extends E> c) { |
1324 |
|
Object[] elements = c.toArray(); |
1325 |
< |
int added; |
1324 |
< |
SequenceLock lock = list.lock; |
1325 |
> |
final SequenceLock lock = list.lock; |
1326 |
|
lock.lock(); |
1327 |
|
try { |
1328 |
|
int s = size; |
1329 |
|
int pc = list.count; |
1330 |
|
list.rawAddAllAt(offset + s, elements); |
1331 |
< |
added = list.count - pc; |
1331 |
> |
int added = list.count - pc; |
1332 |
|
size = s + added; |
1333 |
+ |
return added != 0; |
1334 |
|
} finally { |
1335 |
|
lock.unlock(); |
1336 |
|
} |
1335 |
– |
return added != 0; |
1337 |
|
} |
1338 |
|
|
1339 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
1340 |
|
Object[] elements = c.toArray(); |
1341 |
< |
int added; |
1341 |
< |
SequenceLock lock = list.lock; |
1341 |
> |
final SequenceLock lock = list.lock; |
1342 |
|
lock.lock(); |
1343 |
|
try { |
1344 |
|
int s = size; |
1346 |
|
throw new ArrayIndexOutOfBoundsException(index); |
1347 |
|
int pc = list.count; |
1348 |
|
list.rawAddAllAt(index + offset, elements); |
1349 |
< |
added = list.count - pc; |
1349 |
> |
int added = list.count - pc; |
1350 |
|
size = s + added; |
1351 |
+ |
return added != 0; |
1352 |
|
} finally { |
1353 |
|
lock.unlock(); |
1354 |
|
} |
1354 |
– |
return added != 0; |
1355 |
|
} |
1356 |
|
|
1357 |
|
public void clear() { |
1358 |
< |
SequenceLock lock = list.lock; |
1358 |
> |
final SequenceLock lock = list.lock; |
1359 |
|
lock.lock(); |
1360 |
|
try { |
1361 |
|
list.internalClear(offset, offset + size); |
1392 |
|
} |
1393 |
|
|
1394 |
|
public int indexOf(Object o) { |
1395 |
< |
SequenceLock lock = list.lock; |
1395 |
> |
final SequenceLock lock = list.lock; |
1396 |
|
long seq = lock.awaitAvailability(); |
1397 |
|
Object[] items = list.array; |
1398 |
|
int c = list.count; |
1416 |
|
} |
1417 |
|
|
1418 |
|
public Iterator<E> iterator() { |
1419 |
< |
return new SubItr(this, offset); |
1419 |
> |
return new SubItr<E>(this, offset); |
1420 |
|
} |
1421 |
|
|
1422 |
|
public int lastIndexOf(Object o) { |
1423 |
< |
SequenceLock lock = list.lock; |
1423 |
> |
final SequenceLock lock = list.lock; |
1424 |
|
long seq = lock.awaitAvailability(); |
1425 |
|
Object[] items = list.array; |
1426 |
|
int c = list.count; |
1440 |
|
} |
1441 |
|
|
1442 |
|
public ListIterator<E> listIterator() { |
1443 |
< |
return new SubItr(this, offset); |
1443 |
> |
return new SubItr<E>(this, offset); |
1444 |
|
} |
1445 |
|
|
1446 |
|
public ListIterator<E> listIterator(int index) { |
1447 |
< |
return new SubItr(this, index + offset); |
1447 |
> |
return new SubItr<E>(this, index + offset); |
1448 |
|
} |
1449 |
|
|
1450 |
|
public E remove(int index) { |
1451 |
< |
Object result; |
1452 |
< |
SequenceLock lock = list.lock; |
1451 |
> |
final SequenceLock lock = list.lock; |
1452 |
|
lock.lock(); |
1453 |
|
try { |
1454 |
|
Object[] items = list.array; |
1455 |
|
int i = index + offset; |
1456 |
|
if (index < 0 || index >= size || i >= items.length) |
1457 |
|
throw new ArrayIndexOutOfBoundsException(index); |
1458 |
< |
result = items[i]; |
1458 |
> |
@SuppressWarnings("unchecked") |
1459 |
> |
E result = (E) items[i]; |
1460 |
|
list.rawRemoveAt(i); |
1461 |
|
size--; |
1462 |
+ |
return result; |
1463 |
|
} finally { |
1464 |
|
lock.unlock(); |
1465 |
|
} |
1465 |
– |
return (E)result; |
1466 |
|
} |
1467 |
|
|
1468 |
|
public boolean remove(Object o) { |
1469 |
< |
boolean removed = false; |
1470 |
< |
SequenceLock lock = list.lock; |
1469 |
> |
final SequenceLock lock = list.lock; |
1470 |
|
lock.lock(); |
1471 |
|
try { |
1472 |
|
if (list.rawRemoveAt(list.rawIndexOf(o, offset, |
1473 |
|
offset + size))) { |
1475 |
– |
removed = true; |
1474 |
|
--size; |
1475 |
+ |
return true; |
1476 |
|
} |
1477 |
+ |
else |
1478 |
+ |
return false; |
1479 |
|
} finally { |
1480 |
|
lock.unlock(); |
1481 |
|
} |
1481 |
– |
return removed; |
1482 |
|
} |
1483 |
|
|
1484 |
|
public boolean removeAll(Collection<?> c) { |
1504 |
|
int ssize = toIndex - fromIndex; |
1505 |
|
if (fromIndex < 0 || toIndex > c || ssize < 0) |
1506 |
|
throw new IndexOutOfBoundsException(); |
1507 |
< |
return new ReadMostlyVectorSublist(list, offset+fromIndex, ssize); |
1507 |
> |
return new ReadMostlyVectorSublist<E>(list, offset+fromIndex, ssize); |
1508 |
|
} |
1509 |
|
|
1510 |
|
public Object[] toArray() { |
1526 |
|
final ReadMostlyVector<E> list; |
1527 |
|
final SequenceLock lock; |
1528 |
|
Object[] items; |
1529 |
< |
Object next, prev; |
1529 |
> |
E next, prev; |
1530 |
|
long seq; |
1531 |
|
int cursor; |
1532 |
|
int fence; |
1557 |
|
} while (lock.getSequence() != seq); |
1558 |
|
} |
1559 |
|
|
1560 |
+ |
@SuppressWarnings("unchecked") |
1561 |
|
public boolean hasNext() { |
1562 |
|
boolean valid; |
1563 |
|
int i = cursor; |
1566 |
|
valid = false; |
1567 |
|
break; |
1568 |
|
} |
1569 |
< |
next = items[i]; |
1569 |
> |
next = (E) items[i]; |
1570 |
|
if (lock.getSequence() == seq) { |
1571 |
|
valid = true; |
1572 |
|
break; |
1576 |
|
return validNext = valid; |
1577 |
|
} |
1578 |
|
|
1579 |
+ |
@SuppressWarnings("unchecked") |
1580 |
|
public boolean hasPrevious() { |
1581 |
|
boolean valid; |
1582 |
|
int i = cursor - 1; |
1585 |
|
valid = false; |
1586 |
|
break; |
1587 |
|
} |
1588 |
< |
prev = items[i]; |
1588 |
> |
prev = (E) items[i]; |
1589 |
|
if (lock.getSequence() == seq) { |
1590 |
|
valid = true; |
1591 |
|
break; |
1599 |
|
if (validNext || hasNext()) { |
1600 |
|
validNext = false; |
1601 |
|
lastRet = cursor++; |
1602 |
< |
return (E) next; |
1602 |
> |
return next; |
1603 |
|
} |
1604 |
|
throw new NoSuchElementException(); |
1605 |
|
} |
1608 |
|
if (validPrev || hasPrevious()) { |
1609 |
|
validPrev = false; |
1610 |
|
lastRet = cursor--; |
1611 |
< |
return (E) prev; |
1611 |
> |
return prev; |
1612 |
|
} |
1613 |
|
throw new NoSuchElementException(); |
1614 |
|
} |