530 setArray(newElements);
531 return true;
532 }
533 }
534 return false;
535 } finally {
536 lock.unlock();
537 }
538 }
539
540 /**
541 * Removes from this list all of the elements whose index is between
542 * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive.
543 * Shifts any succeeding elements to the left (reduces their index).
544 * This call shortens the list by <tt>(toIndex - fromIndex)</tt> elements.
545 * (If <tt>toIndex==fromIndex</tt>, this operation has no effect.)
546 *
547 * @param fromIndex index of first element to be removed
548 * @param toIndex index after last element to be removed
549 * @throws IndexOutOfBoundsException if fromIndex or toIndex out of range
550 * (@code{fromIndex < 0 || toIndex > size() || toIndex < fromIndex})
551 */
552 private void removeRange(int fromIndex, int toIndex) {
553 final ReentrantLock lock = this.lock;
554 lock.lock();
555 try {
556 Object[] elements = getArray();
557 int len = elements.length;
558
559 if (fromIndex < 0 || toIndex > len || toIndex < fromIndex)
560 throw new IndexOutOfBoundsException();
561 int newlen = len - (toIndex - fromIndex);
562 int numMoved = len - toIndex;
563 if (numMoved == 0)
564 setArray(Arrays.copyOf(elements, newlen));
565 else {
566 Object[] newElements = new Object[newlen];
567 System.arraycopy(elements, 0, newElements, 0, fromIndex);
568 System.arraycopy(elements, toIndex, newElements,
569 fromIndex, numMoved);
570 setArray(newElements);
972 /**
973 * {@inheritDoc}
974 *
975 * <p>The returned iterator provides a snapshot of the state of the list
976 * when the iterator was constructed. No synchronization is needed while
977 * traversing the iterator. The iterator does <em>NOT</em> support the
978 * <tt>remove</tt>, <tt>set</tt> or <tt>add</tt> methods.
979 *
980 * @throws IndexOutOfBoundsException {@inheritDoc}
981 */
982 public ListIterator<E> listIterator(final int index) {
983 Object[] elements = getArray();
984 int len = elements.length;
985 if (index<0 || index>len)
986 throw new IndexOutOfBoundsException("Index: "+index);
987
988 return new COWIterator<E>(elements, index);
989 }
990
991 private static class COWIterator<E> implements ListIterator<E> {
992 /** Snapshot of the array **/
993 private final Object[] snapshot;
994 /** Index of element to be returned by subsequent call to next. */
995 private int cursor;
996
997 private COWIterator(Object[] elements, int initialCursor) {
998 cursor = initialCursor;
999 snapshot = elements;
1000 }
1001
1002 public boolean hasNext() {
1003 return cursor < snapshot.length;
1004 }
1005
1006 public boolean hasPrevious() {
1007 return cursor > 0;
1008 }
1009
1010 @SuppressWarnings("unchecked")
1011 public E next() {
1012 if (! hasNext())
|
530 setArray(newElements);
531 return true;
532 }
533 }
534 return false;
535 } finally {
536 lock.unlock();
537 }
538 }
539
540 /**
541 * Removes from this list all of the elements whose index is between
542 * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive.
543 * Shifts any succeeding elements to the left (reduces their index).
544 * This call shortens the list by <tt>(toIndex - fromIndex)</tt> elements.
545 * (If <tt>toIndex==fromIndex</tt>, this operation has no effect.)
546 *
547 * @param fromIndex index of first element to be removed
548 * @param toIndex index after last element to be removed
549 * @throws IndexOutOfBoundsException if fromIndex or toIndex out of range
550 * ({@code{fromIndex < 0 || toIndex > size() || toIndex < fromIndex})
551 */
552 private void removeRange(int fromIndex, int toIndex) {
553 final ReentrantLock lock = this.lock;
554 lock.lock();
555 try {
556 Object[] elements = getArray();
557 int len = elements.length;
558
559 if (fromIndex < 0 || toIndex > len || toIndex < fromIndex)
560 throw new IndexOutOfBoundsException();
561 int newlen = len - (toIndex - fromIndex);
562 int numMoved = len - toIndex;
563 if (numMoved == 0)
564 setArray(Arrays.copyOf(elements, newlen));
565 else {
566 Object[] newElements = new Object[newlen];
567 System.arraycopy(elements, 0, newElements, 0, fromIndex);
568 System.arraycopy(elements, toIndex, newElements,
569 fromIndex, numMoved);
570 setArray(newElements);
972 /**
973 * {@inheritDoc}
974 *
975 * <p>The returned iterator provides a snapshot of the state of the list
976 * when the iterator was constructed. No synchronization is needed while
977 * traversing the iterator. The iterator does <em>NOT</em> support the
978 * <tt>remove</tt>, <tt>set</tt> or <tt>add</tt> methods.
979 *
980 * @throws IndexOutOfBoundsException {@inheritDoc}
981 */
982 public ListIterator<E> listIterator(final int index) {
983 Object[] elements = getArray();
984 int len = elements.length;
985 if (index<0 || index>len)
986 throw new IndexOutOfBoundsException("Index: "+index);
987
988 return new COWIterator<E>(elements, index);
989 }
990
991 private static class COWIterator<E> implements ListIterator<E> {
992 /** Snapshot of the array */
993 private final Object[] snapshot;
994 /** Index of element to be returned by subsequent call to next. */
995 private int cursor;
996
997 private COWIterator(Object[] elements, int initialCursor) {
998 cursor = initialCursor;
999 snapshot = elements;
1000 }
1001
1002 public boolean hasNext() {
1003 return cursor < snapshot.length;
1004 }
1005
1006 public boolean hasPrevious() {
1007 return cursor > 0;
1008 }
1009
1010 @SuppressWarnings("unchecked")
1011 public E next() {
1012 if (! hasNext())
|