1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
< |
* Expert Group. Adapted and released under explicit permission |
3 |
> |
* Expert Group. Adapted and released under explicit permission |
4 |
|
* from JDK1.2 ArrayList.java which carries the following copyright: |
5 |
|
* |
6 |
|
* Copyright 1997 by Sun Microsystems, Inc., |
64 |
|
* |
65 |
|
*/ |
66 |
|
public CopyOnWriteArrayList() { |
67 |
< |
array_ = new E[0]; |
67 |
> |
array_ = (E[]) new Object[0]; |
68 |
|
} |
69 |
|
|
70 |
|
/** |
74 |
|
* @param c the collection of initially held elements |
75 |
|
*/ |
76 |
|
public CopyOnWriteArrayList(Collection<E> c) { |
77 |
< |
array_ = new E[c.size()]; |
77 |
> |
array_ = (E[]) new Object[c.size()]; |
78 |
|
Iterator<E> i = c.iterator(); |
79 |
|
int size = 0; |
80 |
|
while (i.hasNext()) |
103 |
|
* the list. |
104 |
|
**/ |
105 |
|
private synchronized void copyIn(E[] toCopyIn, int first, int n) { |
106 |
< |
array_ = new E[n]; |
106 |
> |
array_ = (E[]) new Object[n]; |
107 |
|
System.arraycopy(toCopyIn, first, array_, 0, n); |
108 |
|
} |
109 |
|
|
260 |
|
try { |
261 |
|
E[] elementData = array(); |
262 |
|
CopyOnWriteArrayList<E> v = (CopyOnWriteArrayList)super.clone(); |
263 |
< |
v.array_ = new E[elementData.length]; |
263 |
> |
v.array_ = (E[]) new Object[elementData.length]; |
264 |
|
System.arraycopy(elementData, 0, v.array_, 0, elementData.length); |
265 |
|
return v; |
266 |
|
} catch (CloneNotSupportedException e) { |
351 |
|
boolean same = (oldValue == element || |
352 |
|
(element != null && element.equals(oldValue))); |
353 |
|
if (!same) { |
354 |
< |
E[] newArray = new E[len]; |
354 |
> |
E[] newArray = (E[]) new Object[len]; |
355 |
|
System.arraycopy(array_, 0, newArray, 0, len); |
356 |
|
newArray[index] = element; |
357 |
|
array_ = newArray; |
367 |
|
*/ |
368 |
|
public synchronized boolean add(E element) { |
369 |
|
int len = array_.length; |
370 |
< |
E[] newArray = new E[len+1]; |
370 |
> |
E[] newArray = (E[]) new Object[len+1]; |
371 |
|
System.arraycopy(array_, 0, newArray, 0, len); |
372 |
|
newArray[len] = element; |
373 |
|
array_ = newArray; |
389 |
|
if (index > len || index < 0) |
390 |
|
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+len); |
391 |
|
|
392 |
< |
E[] newArray = new E[len+1]; |
392 |
> |
E[] newArray = (E[]) new Object[len+1]; |
393 |
|
System.arraycopy(array_, 0, newArray, 0, index); |
394 |
|
newArray[index] = element; |
395 |
|
System.arraycopy(array_, index, newArray, index+1, len - index); |
409 |
|
int len = array_.length; |
410 |
|
rangeCheck(index, len); |
411 |
|
E oldValue = array_[index]; |
412 |
< |
E[] newArray = new E[len-1]; |
412 |
> |
E[] newArray = (E[]) new Object[len-1]; |
413 |
|
System.arraycopy(array_, 0, newArray, 0, index); |
414 |
|
int numMoved = len - index - 1; |
415 |
|
if (numMoved > 0) |
438 |
|
// This wins in the normal case of element being present |
439 |
|
|
440 |
|
int newlen = len-1; |
441 |
< |
E[] newArray = new E[newlen]; |
441 |
> |
E[] newArray = (E[]) new Object[newlen]; |
442 |
|
|
443 |
|
for (int i = 0; i < newlen; ++i) { |
444 |
|
if (element == array_[i] || |
486 |
|
|
487 |
|
int numMoved = len - toIndex; |
488 |
|
int newlen = len - (toIndex-fromIndex); |
489 |
< |
E[] newArray = new E[newlen]; |
489 |
> |
E[] newArray = (E[]) new Object[newlen]; |
490 |
|
System.arraycopy(array_, 0, newArray, 0, fromIndex); |
491 |
|
System.arraycopy(array_, toIndex, newArray, fromIndex, numMoved); |
492 |
|
array_ = newArray; |
504 |
|
// Copy while checking if already present. |
505 |
|
// This wins in the most common case where it is not present |
506 |
|
int len = array_.length; |
507 |
< |
E[] newArray = new E[len + 1]; |
507 |
> |
E[] newArray = (E[]) new Object[len + 1]; |
508 |
|
for (int i = 0; i < len; ++i) { |
509 |
|
if (element == array_[i] || |
510 |
|
(element != null && element.equals(array_[i]))) |
528 |
|
* @param c the collection |
529 |
|
* @return true if all elements are contained |
530 |
|
*/ |
531 |
< |
public <T> boolean containsAll(Collection<T> c) { |
531 |
> |
public boolean containsAll(Collection<?> c) { |
532 |
|
E[] elementData = array(); |
533 |
|
int len = elementData.length; |
534 |
< |
Iterator<T> e = c.iterator(); |
534 |
> |
Iterator e = c.iterator(); |
535 |
|
while (e.hasNext()) |
536 |
< |
if (indexOf(e.next(), elementData, len) < 0) |
536 |
> |
if (indexOf((E) e.next(), elementData, len) < 0) |
537 |
|
return false; |
538 |
|
|
539 |
|
return true; |
549 |
|
* @param c the collection |
550 |
|
* @return true if this Collection changed as a result of the call. |
551 |
|
*/ |
552 |
< |
public synchronized <T> boolean removeAll(Collection<T> c) { |
552 |
> |
public synchronized boolean removeAll(Collection<?> c) { |
553 |
|
E[] elementData = array_; |
554 |
|
int len = elementData.length; |
555 |
|
if (len == 0) return false; |
556 |
|
|
557 |
|
// temp array holds those elements we know we want to keep |
558 |
< |
E[] temp = new E[len]; |
558 |
> |
E[] temp = (E[]) new Object[len]; |
559 |
|
int newlen = 0; |
560 |
|
for (int i = 0; i < len; ++i) { |
561 |
|
E element = elementData[i]; |
567 |
|
if (newlen == len) return false; |
568 |
|
|
569 |
|
// copy temp as new array |
570 |
< |
E[] newArray = new E[newlen]; |
570 |
> |
E[] newArray = (E[]) new Object[newlen]; |
571 |
|
System.arraycopy(temp, 0, newArray, 0, newlen); |
572 |
|
array_ = newArray; |
573 |
|
return true; |
581 |
|
* @param c the collection |
582 |
|
* @return true if this Collection changed as a result of the call. |
583 |
|
*/ |
584 |
< |
public synchronized <T> boolean retainAll(Collection<T> c) { |
584 |
> |
public synchronized boolean retainAll(Collection<?> c) { |
585 |
|
E[] elementData = array_; |
586 |
|
int len = elementData.length; |
587 |
|
if (len == 0) return false; |
588 |
|
|
589 |
< |
E[] temp = new E[len]; |
589 |
> |
E[] temp = (E[]) new Object[len]; |
590 |
|
int newlen = 0; |
591 |
|
for (int i = 0; i < len; ++i) { |
592 |
|
E element = elementData[i]; |
597 |
|
|
598 |
|
if (newlen == len) return false; |
599 |
|
|
600 |
< |
E[] newArray = new E[newlen]; |
600 |
> |
E[] newArray = (E[]) new Object[newlen]; |
601 |
|
System.arraycopy(temp, 0, newArray, 0, newlen); |
602 |
|
array_ = newArray; |
603 |
|
return true; |
612 |
|
* @param c elements to be added into this list. |
613 |
|
* @return the number of elements added |
614 |
|
*/ |
615 |
< |
public synchronized <T extends E> int addAllAbsent(Collection<T> c) { |
615 |
> |
public synchronized int addAllAbsent(Collection<? extends E> c) { |
616 |
|
int numNew = c.size(); |
617 |
|
if (numNew == 0) return 0; |
618 |
|
|
619 |
|
E[] elementData = array_; |
620 |
|
int len = elementData.length; |
621 |
|
|
622 |
< |
E[] temp = new E[numNew]; |
622 |
> |
E[] temp = (E[]) new Object[numNew]; |
623 |
|
int added = 0; |
624 |
< |
Iterator<T> e = c.iterator(); |
624 |
> |
Iterator e = c.iterator(); |
625 |
|
while (e.hasNext()) { |
626 |
< |
E element = e.next(); |
626 |
> |
E element = (E) e.next(); |
627 |
|
if (indexOf(element, elementData, len) < 0) { |
628 |
|
if (indexOf(element, temp, added) < 0) { |
629 |
|
temp[added++] = element; |
633 |
|
|
634 |
|
if (added == 0) return 0; |
635 |
|
|
636 |
< |
E[] newArray = new E[len+added]; |
636 |
> |
E[] newArray = (E[]) new Object[len+added]; |
637 |
|
System.arraycopy(elementData, 0, newArray, 0, len); |
638 |
|
System.arraycopy(temp, 0, newArray, len, added); |
639 |
|
array_ = newArray; |
645 |
|
* |
646 |
|
*/ |
647 |
|
public synchronized void clear() { |
648 |
< |
array_ = new E[0]; |
648 |
> |
array_ = (E[]) new Object[0]; |
649 |
|
} |
650 |
|
|
651 |
|
/** |
656 |
|
* @param c elements to be inserted into this list. |
657 |
|
* @return true if any elements are added |
658 |
|
*/ |
659 |
< |
public synchronized <T extends E> boolean addAll(Collection<T> c) { |
659 |
> |
public synchronized boolean addAll(Collection<? extends E> c) { |
660 |
|
int numNew = c.size(); |
661 |
|
if (numNew == 0) return false; |
662 |
|
|
663 |
|
int len = array_.length; |
664 |
< |
E[] newArray = new E[len+numNew]; |
664 |
> |
E[] newArray = (E[]) new Object[len+numNew]; |
665 |
|
System.arraycopy(array_, 0, newArray, 0, len); |
666 |
< |
Iterator<T> e = c.iterator(); |
666 |
> |
Iterator e = c.iterator(); |
667 |
|
for (int i=0; i<numNew; i++) |
668 |
< |
newArray[len++] = e.next(); |
668 |
> |
newArray[len++] = (E) e.next(); |
669 |
|
array_ = newArray; |
670 |
|
|
671 |
|
return true; |
686 |
|
* < 0 || index > size()). |
687 |
|
* @return true if any elements are added |
688 |
|
*/ |
689 |
< |
public synchronized <T extends E> boolean addAll(int index, Collection<T> c) { |
689 |
> |
public synchronized boolean addAll(int index, Collection<? extends E> c) { |
690 |
|
int len = array_.length; |
691 |
|
if (index > len || index < 0) |
692 |
|
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+len); |
694 |
|
int numNew = c.size(); |
695 |
|
if (numNew == 0) return false; |
696 |
|
|
697 |
< |
E[] newArray = new E[len+numNew]; |
697 |
> |
E[] newArray = (E[]) new Object[len+numNew]; |
698 |
|
System.arraycopy(array_, 0, newArray, 0, len); |
699 |
|
int numMoved = len - index; |
700 |
|
if (numMoved > 0) |
701 |
|
System.arraycopy(array_, index, newArray, index + numNew, numMoved); |
702 |
< |
Iterator<T> e = c.iterator(); |
702 |
> |
Iterator e = c.iterator(); |
703 |
|
for (int i=0; i<numNew; i++) |
704 |
< |
newArray[index++] = e.next(); |
704 |
> |
newArray[index++] = (E) e.next(); |
705 |
|
array_ = newArray; |
706 |
|
|
707 |
|
return true; |
751 |
|
|
752 |
|
// Read in array length and allocate array |
753 |
|
int arrayLength = s.readInt(); |
754 |
< |
E[] elementData = new E[arrayLength]; |
754 |
> |
E[] elementData = (E[]) new Object[arrayLength]; |
755 |
|
|
756 |
|
// Read in all elements in the proper order. |
757 |
|
for (int i=0; i<elementData.length; i++) |
1082 |
|
public Iterator<E> iterator() { |
1083 |
|
synchronized(l) { |
1084 |
|
checkForComodification(); |
1085 |
< |
return new COWSubListIterator(0); |
1085 |
> |
return new COWSubListIterator(l, 0, offset, size); |
1086 |
|
} |
1087 |
|
} |
1088 |
|
|
1091 |
|
checkForComodification(); |
1092 |
|
if (index<0 || index>size) |
1093 |
|
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); |
1094 |
< |
return new COWSubListIterator(index); |
1094 |
> |
return new COWSubListIterator(l, index, offset, size); |
1095 |
|
} |
1096 |
|
} |
1097 |
|
|
1098 |
< |
private class COWSubListIterator implements ListIterator<E> { |
1099 |
< |
private final ListIterator<E> i; |
1100 |
< |
private final int index; |
1101 |
< |
private COWSubListIterator(int index) { |
1102 |
< |
this.index = index; |
1103 |
< |
i = l.listIterator(index+offset); |
1098 |
> |
public List<E> subList(int fromIndex, int toIndex) { |
1099 |
> |
synchronized(l) { |
1100 |
> |
checkForComodification(); |
1101 |
> |
if (fromIndex<0 || toIndex>size) |
1102 |
> |
throw new IndexOutOfBoundsException(); |
1103 |
> |
return new COWSubList<E>(l, fromIndex+offset, toIndex+offset); |
1104 |
|
} |
1105 |
+ |
} |
1106 |
|
|
1107 |
< |
public boolean hasNext() { |
1107 |
< |
return nextIndex() < size; |
1108 |
< |
} |
1107 |
> |
} |
1108 |
|
|
1110 |
– |
public E next() { |
1111 |
– |
if (hasNext()) |
1112 |
– |
return i.next(); |
1113 |
– |
else |
1114 |
– |
throw new NoSuchElementException(); |
1115 |
– |
} |
1109 |
|
|
1110 |
< |
public boolean hasPrevious() { |
1111 |
< |
return previousIndex() >= 0; |
1112 |
< |
} |
1110 |
> |
private static class COWSubListIterator<E> implements ListIterator<E> { |
1111 |
> |
private final ListIterator<E> i; |
1112 |
> |
private final int index; |
1113 |
> |
private final int offset; |
1114 |
> |
private final int size; |
1115 |
> |
private COWSubListIterator(List<E> l, int index, int offset, int size) { |
1116 |
> |
this.index = index; |
1117 |
> |
this.offset = offset; |
1118 |
> |
this.size = size; |
1119 |
> |
i = l.listIterator(index+offset); |
1120 |
> |
} |
1121 |
|
|
1122 |
< |
public E previous() { |
1123 |
< |
if (hasPrevious()) |
1124 |
< |
return i.previous(); |
1124 |
< |
else |
1125 |
< |
throw new NoSuchElementException(); |
1126 |
< |
} |
1122 |
> |
public boolean hasNext() { |
1123 |
> |
return nextIndex() < size; |
1124 |
> |
} |
1125 |
|
|
1126 |
< |
public int nextIndex() { |
1127 |
< |
return i.nextIndex() - offset; |
1128 |
< |
} |
1126 |
> |
public E next() { |
1127 |
> |
if (hasNext()) |
1128 |
> |
return i.next(); |
1129 |
> |
else |
1130 |
> |
throw new NoSuchElementException(); |
1131 |
> |
} |
1132 |
|
|
1133 |
< |
public int previousIndex() { |
1134 |
< |
return i.previousIndex() - offset; |
1135 |
< |
} |
1133 |
> |
public boolean hasPrevious() { |
1134 |
> |
return previousIndex() >= 0; |
1135 |
> |
} |
1136 |
|
|
1137 |
< |
public void remove() { |
1138 |
< |
throw new UnsupportedOperationException(); |
1139 |
< |
} |
1137 |
> |
public E previous() { |
1138 |
> |
if (hasPrevious()) |
1139 |
> |
return i.previous(); |
1140 |
> |
else |
1141 |
> |
throw new NoSuchElementException(); |
1142 |
> |
} |
1143 |
|
|
1144 |
< |
public void set(E o) { |
1145 |
< |
throw new UnsupportedOperationException(); |
1146 |
< |
} |
1144 |
> |
public int nextIndex() { |
1145 |
> |
return i.nextIndex() - offset; |
1146 |
> |
} |
1147 |
|
|
1148 |
< |
public void add(E o) { |
1149 |
< |
throw new UnsupportedOperationException(); |
1146 |
< |
} |
1148 |
> |
public int previousIndex() { |
1149 |
> |
return i.previousIndex() - offset; |
1150 |
|
} |
1151 |
|
|
1152 |
+ |
public void remove() { |
1153 |
+ |
throw new UnsupportedOperationException(); |
1154 |
+ |
} |
1155 |
|
|
1156 |
< |
public List<E> subList(int fromIndex, int toIndex) { |
1157 |
< |
synchronized(l) { |
1152 |
< |
checkForComodification(); |
1153 |
< |
if (fromIndex<0 || toIndex>size) |
1154 |
< |
throw new IndexOutOfBoundsException(); |
1155 |
< |
return new COWSubList<E>(l, fromIndex+offset, toIndex+offset); |
1156 |
< |
} |
1156 |
> |
public void set(E o) { |
1157 |
> |
throw new UnsupportedOperationException(); |
1158 |
|
} |
1159 |
|
|
1160 |
+ |
public void add(E o) { |
1161 |
+ |
throw new UnsupportedOperationException(); |
1162 |
+ |
} |
1163 |
|
} |
1164 |
|
|
1165 |
|
} |