Sublist for CopyOnWriteArrayList.
This class extends AbstractList merely for convenience, to
avoid having to define addAll, etc. This doesn't hurt, but
is wasteful. This class does not need or use modCount
mechanics in AbstractList, but does need to check for
concurrent modification using similar mechanics. On each
operation, the array that we expect the backing list to use
is checked and updated. Since we do this for all of the
base operations invoked by those defined in AbstractList,
all is well. While inefficient, this is not worth
improving. The kinds of list operations inherited from
AbstractList are already so slow on COW sublists that
adding a bit more space/time doesn't seem even noticeable.
| Method from java.util.concurrent.CopyOnWriteArrayList$COWSubList Detail: |
public void add(int index,
E element) {
final ReentrantLock lock = l.lock;
lock.lock();
try {
checkForComodification();
if (index< 0 || index >size)
throw new IndexOutOfBoundsException();
l.add(index+offset, element);
expectedArray = l.getArray();
size++;
} finally {
lock.unlock();
}
}
|
public void clear() {
final ReentrantLock lock = l.lock;
lock.lock();
try {
checkForComodification();
l.removeRange(offset, offset+size);
expectedArray = l.getArray();
size = 0;
} finally {
lock.unlock();
}
}
|
public E get(int index) {
final ReentrantLock lock = l.lock;
lock.lock();
try {
rangeCheck(index);
checkForComodification();
return l.get(index+offset);
} finally {
lock.unlock();
}
}
|
public Iterator iterator() {
final ReentrantLock lock = l.lock;
lock.lock();
try {
checkForComodification();
return new COWSubListIterator< E >(l, 0, offset, size);
} finally {
lock.unlock();
}
}
|
public ListIterator listIterator(int index) {
final ReentrantLock lock = l.lock;
lock.lock();
try {
checkForComodification();
if (index< 0 || index >size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
return new COWSubListIterator< E >(l, index, offset, size);
} finally {
lock.unlock();
}
}
|
public E remove(int index) {
final ReentrantLock lock = l.lock;
lock.lock();
try {
rangeCheck(index);
checkForComodification();
E result = l.remove(index+offset);
expectedArray = l.getArray();
size--;
return result;
} finally {
lock.unlock();
}
}
|
public boolean remove(Object o) {
int index = indexOf(o);
if (index == -1)
return false;
remove(index);
return true;
}
|
public E set(int index,
E element) {
final ReentrantLock lock = l.lock;
lock.lock();
try {
rangeCheck(index);
checkForComodification();
E x = l.set(index+offset, element);
expectedArray = l.getArray();
return x;
} finally {
lock.unlock();
}
}
|
public int size() {
final ReentrantLock lock = l.lock;
lock.lock();
try {
checkForComodification();
return size;
} finally {
lock.unlock();
}
}
|
public List subList(int fromIndex,
int toIndex) {
final ReentrantLock lock = l.lock;
lock.lock();
try {
checkForComodification();
if (fromIndex< 0 || toIndex >size)
throw new IndexOutOfBoundsException();
return new COWSubList< E >(l, fromIndex + offset,
toIndex + offset);
} finally {
lock.unlock();
}
}
|