57 |
|
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
58 |
|
|
59 |
|
// fields are non-private to simpify nested class access |
60 |
< |
Object[] array; |
60 |
> |
volatile Object[] array; |
61 |
|
final SequenceLock lock; |
62 |
< |
int count; |
62 |
> |
volatile int count; |
63 |
|
final int capacityIncrement; |
64 |
|
|
65 |
|
/** |
201 |
|
|
202 |
|
final void rawAdd(Object e) { |
203 |
|
int n = count; |
204 |
< |
if (n >= array.length) |
204 |
> |
Object[] items = array; |
205 |
> |
if (n < items.length) |
206 |
> |
items[n] = e; |
207 |
> |
else { |
208 |
|
grow(n + 1); |
209 |
< |
array[n] = e; |
209 |
> |
array[n] = e; |
210 |
> |
} |
211 |
|
count = n + 1; |
212 |
|
} |
213 |
|
|
217 |
|
throw new ArrayIndexOutOfBoundsException(index); |
218 |
|
if (n >= array.length) |
219 |
|
grow(n + 1); |
220 |
+ |
Object[] items = array; |
221 |
|
if (index < n) |
222 |
< |
System.arraycopy(array, index, array, index + 1, n - index); |
223 |
< |
array[index] = e; |
222 |
> |
System.arraycopy(items, index, items, index + 1, n - index); |
223 |
> |
items[index] = e; |
224 |
|
count = n + 1; |
225 |
|
} |
226 |
|
|
234 |
|
int newCount = n + len; |
235 |
|
if (newCount >= array.length) |
236 |
|
grow(newCount); |
237 |
< |
int mv = count - index; |
237 |
> |
Object[] items = array; |
238 |
> |
int mv = n - index; |
239 |
|
if (mv > 0) |
240 |
< |
System.arraycopy(array, index, array, index + len, mv); |
241 |
< |
System.arraycopy(elements, 0, array, index, len); |
240 |
> |
System.arraycopy(items, index, items, index + len, mv); |
241 |
> |
System.arraycopy(elements, 0, items, index, len); |
242 |
|
count = newCount; |
243 |
|
return true; |
244 |
|
} |
245 |
|
|
246 |
|
final boolean rawRemoveAt(int index) { |
247 |
+ |
Object[] items = array; |
248 |
|
int n = count - 1; |
249 |
|
if (index < 0 || index > n) |
250 |
|
return false; |
251 |
|
int mv = n - index; |
252 |
|
if (mv > 0) |
253 |
< |
System.arraycopy(array, index + 1, array, index, mv); |
254 |
< |
array[n] = null; |
253 |
> |
System.arraycopy(items, index + 1, items, index, mv); |
254 |
> |
items[n] = null; |
255 |
|
count = n; |
256 |
|
return true; |
257 |
|
} |
288 |
|
if (c != this) { |
289 |
|
lock.lock(); |
290 |
|
try { |
291 |
+ |
Object[] items = array; |
292 |
|
int i = origin; |
293 |
|
int n = count; |
294 |
|
int fence = bound < 0 || bound > n ? n : bound; |
295 |
|
while (i >= 0 && i < fence) { |
296 |
< |
if (c.contains(array[i])) |
296 |
> |
if (c.contains(items[i])) |
297 |
|
++i; |
298 |
|
else { |
299 |
|
--fence; |
300 |
|
int mv = --count - i; |
301 |
|
if (mv > 0) |
302 |
< |
System.arraycopy(array, i + 1, array, i, mv); |
302 |
> |
System.arraycopy(items, i + 1, items, i, mv); |
303 |
|
removed = true; |
304 |
|
} |
305 |
|
} |
311 |
|
} |
312 |
|
|
313 |
|
final void internalClear(int origin, int bound) { |
314 |
+ |
Object[] items = array; |
315 |
|
int n = count; |
316 |
|
int fence = bound < 0 || bound > n ? n : bound; |
317 |
|
if (origin >= 0 && origin < fence) { |
319 |
|
int newCount = n - removed; |
320 |
|
int mv = n - (origin + removed); |
321 |
|
if (mv > 0) |
322 |
< |
System.arraycopy(array, origin + removed, array, origin, mv); |
322 |
> |
System.arraycopy(items, origin + removed, items, origin, mv); |
323 |
|
for (int i = n; i < newCount; ++i) |
324 |
< |
array[i] = null; |
324 |
> |
items[i] = null; |
325 |
|
count = newCount; |
326 |
|
} |
327 |
|
} |
533 |
|
rlen = 0; |
534 |
|
if (origin < 0 || alen >= rlen) { |
535 |
|
if (rlen > 0) |
536 |
< |
System.arraycopy(array, 0, a, origin, rlen); |
536 |
> |
System.arraycopy(items, 0, a, origin, rlen); |
537 |
|
if (alen > rlen) |
538 |
|
a[rlen] = null; |
539 |
|
result = a; |
540 |
|
} |
541 |
|
else |
542 |
< |
result = (T[]) Arrays.copyOfRange(array, origin, |
542 |
> |
result = (T[]) Arrays.copyOfRange(items, origin, |
543 |
|
fence, a.getClass()); |
544 |
|
if (lock.getSequence() == seq) |
545 |
|
break; |
612 |
|
SequenceLock lock = this.lock; |
613 |
|
lock.lock(); |
614 |
|
try { |
615 |
+ |
Object[] items = array; |
616 |
|
for (int i = 0; i < count; i++) |
617 |
< |
array[i] = null; |
617 |
> |
items[i] = null; |
618 |
|
count = 0; |
619 |
|
} finally { |
620 |
|
lock.unlock(); |
673 |
|
long seq = lock.awaitAvailability(); |
674 |
|
Object[] items = array; |
675 |
|
int n = count; |
676 |
< |
if (n <= items.length) { |
676 |
> |
if (n <= items.length) { |
677 |
|
for (int i = 0; i < n; ++i) { |
678 |
|
Object e = items[i]; |
679 |
|
if (lock.getSequence() != seq) { |
693 |
|
} |
694 |
|
|
695 |
|
public boolean isEmpty() { |
686 |
– |
long ignore = lock.getSequence(); |
696 |
|
return count == 0; |
697 |
|
} |
698 |
|
|
699 |
|
public Iterator<E> iterator() { |
700 |
< |
return new Itr(this, 0); |
700 |
> |
return new Itr<E>(this, 0); |
701 |
|
} |
702 |
|
|
703 |
|
public int lastIndexOf(Object o) { |
706 |
|
long seq = lock.awaitAvailability(); |
707 |
|
Object[] items = array; |
708 |
|
int n = count; |
709 |
< |
if (n <= items.length) { |
709 |
> |
if (n <= items.length) { |
710 |
|
for (int i = n - 1; i >= 0; --i) { |
711 |
|
Object e = items[i]; |
712 |
|
if (lock.getSequence() != seq) { |
726 |
|
} |
727 |
|
|
728 |
|
public ListIterator<E> listIterator() { |
729 |
< |
return new Itr(this, 0); |
729 |
> |
return new Itr<E>(this, 0); |
730 |
|
} |
731 |
|
|
732 |
|
public ListIterator<E> listIterator(int index) { |
733 |
< |
return new Itr(this, index); |
733 |
> |
return new Itr<E>(this, index); |
734 |
|
} |
735 |
|
|
736 |
|
public E remove(int index) { |
773 |
|
SequenceLock lock = this.lock; |
774 |
|
lock.lock(); |
775 |
|
try { |
776 |
+ |
Object[] items = array; |
777 |
|
if (index < 0 || index >= count) |
778 |
|
throw new ArrayIndexOutOfBoundsException(index); |
779 |
< |
oldValue = array[index]; |
780 |
< |
array[index] = element; |
779 |
> |
oldValue = items[index]; |
780 |
> |
items[index] = element; |
781 |
|
} finally { |
782 |
|
lock.unlock(); |
783 |
|
} |
785 |
|
} |
786 |
|
|
787 |
|
public int size() { |
778 |
– |
long ignore = lock.getSequence(); |
788 |
|
return count; |
789 |
|
} |
790 |
|
|
793 |
|
int ssize = toIndex - fromIndex; |
794 |
|
if (fromIndex < 0 || toIndex > c || ssize < 0) |
795 |
|
throw new IndexOutOfBoundsException(); |
796 |
< |
return new ReadMostlyVectorSublist(this, fromIndex, ssize); |
796 |
> |
return new ReadMostlyVectorSublist<E>(this, fromIndex, ssize); |
797 |
|
} |
798 |
|
|
799 |
|
public Object[] toArray() { |
814 |
|
* Append the element if not present. |
815 |
|
* |
816 |
|
* @param e element to be added to this list, if absent |
817 |
< |
* @return <tt>true</tt> if the element was added |
817 |
> |
* @return {@code true} if the element was added |
818 |
|
*/ |
819 |
|
public boolean addIfAbsent(E e) { |
820 |
|
boolean added; |
869 |
|
* Returns an iterator operating over a snapshot copy of the |
870 |
|
* elements of this collection created upon construction of the |
871 |
|
* iterator. The iterator does <em>NOT</em> support the |
872 |
< |
* <tt>remove</tt> method. |
872 |
> |
* {@code remove} method. |
873 |
|
* |
874 |
|
* @return an iterator over the elements in this list in proper sequence |
875 |
|
*/ |
1021 |
|
if (newSize > n) |
1022 |
|
grow(newSize); |
1023 |
|
else { |
1024 |
+ |
Object[] items = array; |
1025 |
|
for (int i = newSize ; i < n ; i++) |
1026 |
< |
array[i] = null; |
1026 |
> |
items[i] = null; |
1027 |
|
} |
1028 |
|
count = newSize; |
1029 |
|
} finally { |
1047 |
|
SequenceLock lock = this.lock; |
1048 |
|
lock.lock(); |
1049 |
|
try { |
1050 |
< |
if (count < array.length) |
1051 |
< |
array = Arrays.copyOf(array, count); |
1050 |
> |
Object[] items = array; |
1051 |
> |
if (count < items.length) |
1052 |
> |
array = Arrays.copyOf(items, count); |
1053 |
|
} finally { |
1054 |
|
lock.unlock(); |
1055 |
|
} |
1071 |
|
|
1072 |
|
/** See {@link Vector#elements} */ |
1073 |
|
public Enumeration<E> elements() { |
1074 |
< |
return new Itr(this, 0); |
1074 |
> |
return new Itr<E>(this, 0); |
1075 |
|
} |
1076 |
|
|
1077 |
|
/** See {@link Vector#capacity} */ |
1078 |
|
public int capacity() { |
1068 |
– |
long ignore = lock.getSequence(); |
1079 |
|
return array.length; |
1080 |
|
} |
1081 |
|
|
1116 |
|
|
1117 |
|
// other methods |
1118 |
|
|
1119 |
< |
public Object clone() { |
1119 |
> |
public ReadMostlyVector<E> clone() { |
1120 |
|
SequenceLock lock = this.lock; |
1121 |
|
Object[] a = null; |
1122 |
|
boolean retry = false; |
1136 |
|
lock.unlock(); |
1137 |
|
} |
1138 |
|
} |
1139 |
< |
return new ReadMostlyVector(a, n, capacityIncrement); |
1139 |
> |
return new ReadMostlyVector<E>(a, n, capacityIncrement); |
1140 |
|
} |
1141 |
|
|
1142 |
|
private void writeObject(java.io.ObjectOutputStream s) |
1426 |
|
} |
1427 |
|
|
1428 |
|
public Iterator<E> iterator() { |
1429 |
< |
return new SubItr(this, offset); |
1429 |
> |
return new SubItr<E>(this, offset); |
1430 |
|
} |
1431 |
|
|
1432 |
|
public int lastIndexOf(Object o) { |
1450 |
|
} |
1451 |
|
|
1452 |
|
public ListIterator<E> listIterator() { |
1453 |
< |
return new SubItr(this, offset); |
1453 |
> |
return new SubItr<E>(this, offset); |
1454 |
|
} |
1455 |
|
|
1456 |
|
public ListIterator<E> listIterator(int index) { |
1457 |
< |
return new SubItr(this, index + offset); |
1457 |
> |
return new SubItr<E>(this, index + offset); |
1458 |
|
} |
1459 |
|
|
1460 |
|
public E remove(int index) { |
1514 |
|
int ssize = toIndex - fromIndex; |
1515 |
|
if (fromIndex < 0 || toIndex > c || ssize < 0) |
1516 |
|
throw new IndexOutOfBoundsException(); |
1517 |
< |
return new ReadMostlyVectorSublist(list, offset+fromIndex, ssize); |
1517 |
> |
return new ReadMostlyVectorSublist<E>(list, offset+fromIndex, ssize); |
1518 |
|
} |
1519 |
|
|
1520 |
|
public Object[] toArray() { |