The returned list iterator is fail-fast.
*
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public synchronized ListIterator listIterator(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: "+index);
return new ListItr(index);
}
/**
* Returns a list iterator over the elements in this list (in proper
* sequence).
*
* The returned list iterator is fail-fast.
*
* @see #listIterator(int)
*/
public ListIterator listIterator() {
return new ListItr(0);
}
/**
* Returns an iterator over the elements in this list in proper sequence.
*
* The returned iterator is fail-fast.
*
* @return an iterator over the elements in this list in proper sequence
*/
public Iterator iterator() {
return concurrentlyIterateList(this);
}
/**
* An optimized version of AbstractList.Itr
*/
private class Itr implements Iterator {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size;
}
@SuppressWarnings("unchecked")
public A next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = SynchronizedTypedArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (A) elementData[lastRet = i];
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
SynchronizedTypedArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
/**
* An optimized version of AbstractList.ListItr - TODO: replace with concurrent iterator or synchronize
*/
private class ListItr extends Itr implements ListIterator {
ListItr(int index) {
super();
cursor = index;
}
public boolean hasPrevious() {
return cursor != 0;
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
@SuppressWarnings("unchecked")
public A previous() {
checkForComodification();
int i = cursor - 1;
if (i < 0)
throw new NoSuchElementException();
Object[] elementData = SynchronizedTypedArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i;
return (A) elementData[lastRet = i];
}
public void set(A e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
SynchronizedTypedArrayList.this.set(lastRet, e);
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(A e) {
checkForComodification();
try {
int i = cursor;
SynchronizedTypedArrayList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}
public List subList(int fromIndex, int toIndex) {
_subListRangeCheck(fromIndex, toIndex, size);
return new SubList(this, 0, fromIndex, toIndex);
}
private class SubList extends SynchronizedArrayList_Base implements RandomAccess {
private final SynchronizedArrayList_Base parent;
private final int parentOffset;
private final int offset;
int size;
SubList(SynchronizedArrayList_Base parent,
int offset, int fromIndex, int toIndex) {
this.parent = parent;
this.parentOffset = fromIndex;
this.offset = offset + fromIndex;
this.size = toIndex - fromIndex;
this.modCount = SynchronizedTypedArrayList.this.modCount;
}
public A set(int index, A e) { synchronized(SynchronizedTypedArrayList.this) {
rangeCheck(index);
checkForComodification();
A oldValue = SynchronizedTypedArrayList.this.elementData(offset + index);
SynchronizedTypedArrayList.this.elementData[offset + index] = e;
return oldValue;
}}
public A get(int index) { synchronized(SynchronizedTypedArrayList.this) {
rangeCheck(index);
checkForComodification();
return SynchronizedTypedArrayList.this.elementData(offset + index);
}}
public int size() { synchronized(SynchronizedTypedArrayList.this) {
checkForComodification();
return this.size;
}}
public void add(int index, A e) { synchronized(SynchronizedTypedArrayList.this) {
rangeCheckForAdd(index);
checkForComodification();
parent.add(parentOffset + index, e);
this.modCount = parent.modCount();
this.size++;
}}
public A remove(int index) { synchronized(SynchronizedTypedArrayList.this) {
rangeCheck(index);
checkForComodification();
A result = parent.remove(parentOffset + index);
this.modCount = parent.modCount();
this.size--;
return result;
}}
public void removeRange(int fromIndex, int toIndex) { synchronized(SynchronizedTypedArrayList.this) {
checkForComodification();
parent.removeRange(parentOffset + fromIndex,
parentOffset + toIndex);
this.modCount = parent.modCount();
this.size -= toIndex - fromIndex;
}}
public boolean addAll(Collection extends A> c) {
return addAll(this.size, c);
}
public boolean addAll(int index, Collection extends A> c) { synchronized(SynchronizedTypedArrayList.this) {
rangeCheckForAdd(index);
int cSize = c.size();
if (cSize==0)
return false;
checkForComodification();
parent.addAll(parentOffset + index, c);
this.modCount = parent.modCount();
this.size += cSize;
return true;
}}
public Iterator iterator() {
return listIterator();
}
public ListIterator listIterator(final int index) { synchronized(SynchronizedTypedArrayList.this) {
checkForComodification();
rangeCheckForAdd(index);
final int offset = this.offset;
return new ListIterator() {
int cursor = index;
int lastRet = -1;
int expectedModCount = SynchronizedTypedArrayList.this.modCount;
public boolean hasNext() {
return cursor != SubList.this.size;
}
@SuppressWarnings("unchecked")
public A next() {
checkForComodification();
int i = cursor;
if (i >= SubList.this.size)
throw new NoSuchElementException();
Object[] elementData = SynchronizedTypedArrayList.this.elementData;
if (offset + i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (A) elementData[offset + (lastRet = i)];
}
public boolean hasPrevious() {
return cursor != 0;
}
@SuppressWarnings("unchecked")
public A previous() {
checkForComodification();
int i = cursor - 1;
if (i < 0)
throw new NoSuchElementException();
Object[] elementData = SynchronizedTypedArrayList.this.elementData;
if (offset + i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i;
return (A) elementData[offset + (lastRet = i)];
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
SubList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = SynchronizedTypedArrayList.this.modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void set(A e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
SynchronizedTypedArrayList.this.set(offset + lastRet, e);
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(A e) {
checkForComodification();
try {
int i = cursor;
SubList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = SynchronizedTypedArrayList.this.modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (expectedModCount != SynchronizedTypedArrayList.this.modCount)
throw new ConcurrentModificationException();
}
};
}}
public List subList(int fromIndex, int toIndex) { synchronized(SynchronizedTypedArrayList.this) {
_subListRangeCheck(fromIndex, toIndex, size);
return new SubList(parent, offset, fromIndex, toIndex);
}}
private void rangeCheck(int index) {
if (index < 0 || index >= this.size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private void rangeCheckForAdd(int index) {
if (index < 0 || index > this.size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+this.size;
}
private void checkForComodification() {
if (SynchronizedTypedArrayList.this.modCount != this.modCount)
throw new ConcurrentModificationException();
}
}
@Override
@SuppressWarnings("unchecked")
public synchronized void sort(Comparator super A> c) {
final int expectedModCount = modCount;
Arrays.sort((A[]) elementData, 0, size, c);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}
Class extends A> elementType() {
ret arrayElementType(elementData);
}
}