private static class COWSubListIterator<E> implements ListIterator<E> {
private final ListIterator<E> i;
private final int index;
private final int offset;
private final int size;
private COWSubListIterator(List<E> l, int index, int offset,
int size) {
this.index = index;
this.offset = offset;
this.size = size;
i = l.listIterator(index+offset);
}
public boolean hasNext() {
return nextIndex() < size;
}
public E next() {
if (hasNext())
return i.next();
else
throw new NoSuchElementException();
}
public boolean hasPrevious() {
return previousIndex() >= 0;
}
public E previous() {
if (hasPrevious())
return i.previous();
else
throw new NoSuchElementException();
}
public int nextIndex() {
return i.nextIndex() - offset;
}
public int previousIndex() {
return i.previousIndex() - offset;
}
public void remove() {
throw new UnsupportedOperationException();
}
public void set(E e) {
throw new UnsupportedOperationException();
}
public void add(E e) {
throw new UnsupportedOperationException();
}
}
// Support for resetting lock while deserializing
private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final long lockOffset;
static {
try {
lockOffset = unsafe.objectFieldOffset
(CopyOnWriteArrayList.class.getDeclaredField("lock"));
} catch (Exception ex) { throw new Error(ex); }
}
private void resetLock() {
unsafe.putObjectVolatile(this, lockOffset, new ReentrantLock());
}
}