0N/A/*
3261N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.util;
0N/A
0N/A/**
0N/A * This class provides a skeletal implementation of the {@link List}
0N/A * interface to minimize the effort required to implement this interface
0N/A * backed by a "random access" data store (such as an array). For sequential
0N/A * access data (such as a linked list), {@link AbstractSequentialList} should
0N/A * be used in preference to this class.
0N/A *
0N/A * <p>To implement an unmodifiable list, the programmer needs only to extend
0N/A * this class and provide implementations for the {@link #get(int)} and
0N/A * {@link List#size() size()} methods.
0N/A *
0N/A * <p>To implement a modifiable list, the programmer must additionally
0N/A * override the {@link #set(int, Object) set(int, E)} method (which otherwise
0N/A * throws an {@code UnsupportedOperationException}). If the list is
0N/A * variable-size the programmer must additionally override the
0N/A * {@link #add(int, Object) add(int, E)} and {@link #remove(int)} methods.
0N/A *
0N/A * <p>The programmer should generally provide a void (no argument) and collection
0N/A * constructor, as per the recommendation in the {@link Collection} interface
0N/A * specification.
0N/A *
0N/A * <p>Unlike the other abstract collection implementations, the programmer does
0N/A * <i>not</i> have to provide an iterator implementation; the iterator and
0N/A * list iterator are implemented by this class, on top of the "random access"
0N/A * methods:
0N/A * {@link #get(int)},
0N/A * {@link #set(int, Object) set(int, E)},
0N/A * {@link #add(int, Object) add(int, E)} and
0N/A * {@link #remove(int)}.
0N/A *
0N/A * <p>The documentation for each non-abstract method in this class describes its
0N/A * implementation in detail. Each of these methods may be overridden if the
0N/A * collection being implemented admits a more efficient implementation.
0N/A *
0N/A * <p>This class is a member of the
0N/A * <a href="{@docRoot}/../technotes/guides/collections/index.html">
0N/A * Java Collections Framework</a>.
0N/A *
0N/A * @author Josh Bloch
0N/A * @author Neal Gafter
0N/A * @since 1.2
0N/A */
0N/A
0N/Apublic abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
0N/A /**
0N/A * Sole constructor. (For invocation by subclass constructors, typically
0N/A * implicit.)
0N/A */
0N/A protected AbstractList() {
0N/A }
0N/A
0N/A /**
0N/A * Appends the specified element to the end of this list (optional
0N/A * operation).
0N/A *
0N/A * <p>Lists that support this operation may place limitations on what
0N/A * elements may be added to this list. In particular, some
0N/A * lists will refuse to add null elements, and others will impose
0N/A * restrictions on the type of elements that may be added. List
0N/A * classes should clearly specify in their documentation any restrictions
0N/A * on what elements may be added.
0N/A *
0N/A * <p>This implementation calls {@code add(size(), e)}.
0N/A *
0N/A * <p>Note that this implementation throws an
0N/A * {@code UnsupportedOperationException} unless
0N/A * {@link #add(int, Object) add(int, E)} is overridden.
0N/A *
0N/A * @param e element to be appended to this list
0N/A * @return {@code true} (as specified by {@link Collection#add})
0N/A * @throws UnsupportedOperationException if the {@code add} operation
0N/A * is not supported by this list
0N/A * @throws ClassCastException if the class of the specified element
0N/A * prevents it from being added to this list
0N/A * @throws NullPointerException if the specified element is null and this
0N/A * list does not permit null elements
0N/A * @throws IllegalArgumentException if some property of this element
0N/A * prevents it from being added to this list
0N/A */
0N/A public boolean add(E e) {
0N/A add(size(), e);
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A *
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A abstract public E get(int index);
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A *
0N/A * <p>This implementation always throws an
0N/A * {@code UnsupportedOperationException}.
0N/A *
0N/A * @throws UnsupportedOperationException {@inheritDoc}
0N/A * @throws ClassCastException {@inheritDoc}
0N/A * @throws NullPointerException {@inheritDoc}
0N/A * @throws IllegalArgumentException {@inheritDoc}
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public E set(int index, E element) {
0N/A throw new UnsupportedOperationException();
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A *
0N/A * <p>This implementation always throws an
0N/A * {@code UnsupportedOperationException}.
0N/A *
0N/A * @throws UnsupportedOperationException {@inheritDoc}
0N/A * @throws ClassCastException {@inheritDoc}
0N/A * @throws NullPointerException {@inheritDoc}
0N/A * @throws IllegalArgumentException {@inheritDoc}
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public void add(int index, E element) {
0N/A throw new UnsupportedOperationException();
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A *
0N/A * <p>This implementation always throws an
0N/A * {@code UnsupportedOperationException}.
0N/A *
0N/A * @throws UnsupportedOperationException {@inheritDoc}
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public E remove(int index) {
0N/A throw new UnsupportedOperationException();
0N/A }
0N/A
0N/A
0N/A // Search Operations
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A *
0N/A * <p>This implementation first gets a list iterator (with
0N/A * {@code listIterator()}). Then, it iterates over the list until the
0N/A * specified element is found or the end of the list is reached.
0N/A *
0N/A * @throws ClassCastException {@inheritDoc}
0N/A * @throws NullPointerException {@inheritDoc}
0N/A */
0N/A public int indexOf(Object o) {
3203N/A ListIterator<E> it = listIterator();
0N/A if (o==null) {
3203N/A while (it.hasNext())
3203N/A if (it.next()==null)
3203N/A return it.previousIndex();
0N/A } else {
3203N/A while (it.hasNext())
3203N/A if (o.equals(it.next()))
3203N/A return it.previousIndex();
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A *
0N/A * <p>This implementation first gets a list iterator that points to the end
0N/A * of the list (with {@code listIterator(size())}). Then, it iterates
0N/A * backwards over the list until the specified element is found, or the
0N/A * beginning of the list is reached.
0N/A *
0N/A * @throws ClassCastException {@inheritDoc}
0N/A * @throws NullPointerException {@inheritDoc}
0N/A */
0N/A public int lastIndexOf(Object o) {
3203N/A ListIterator<E> it = listIterator(size());
0N/A if (o==null) {
3203N/A while (it.hasPrevious())
3203N/A if (it.previous()==null)
3203N/A return it.nextIndex();
0N/A } else {
3203N/A while (it.hasPrevious())
3203N/A if (o.equals(it.previous()))
3203N/A return it.nextIndex();
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A
0N/A // Bulk Operations
0N/A
0N/A /**
0N/A * Removes all of the elements from this list (optional operation).
0N/A * The list will be empty after this call returns.
0N/A *
0N/A * <p>This implementation calls {@code removeRange(0, size())}.
0N/A *
0N/A * <p>Note that this implementation throws an
0N/A * {@code UnsupportedOperationException} unless {@code remove(int
0N/A * index)} or {@code removeRange(int fromIndex, int toIndex)} is
0N/A * overridden.
0N/A *
0N/A * @throws UnsupportedOperationException if the {@code clear} operation
0N/A * is not supported by this list
0N/A */
0N/A public void clear() {
0N/A removeRange(0, size());
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A *
0N/A * <p>This implementation gets an iterator over the specified collection
0N/A * and iterates over it, inserting the elements obtained from the
0N/A * iterator into this list at the appropriate position, one at a time,
0N/A * using {@code add(int, E)}.
0N/A * Many implementations will override this method for efficiency.
0N/A *
0N/A * <p>Note that this implementation throws an
0N/A * {@code UnsupportedOperationException} unless
0N/A * {@link #add(int, Object) add(int, E)} is overridden.
0N/A *
0N/A * @throws UnsupportedOperationException {@inheritDoc}
0N/A * @throws ClassCastException {@inheritDoc}
0N/A * @throws NullPointerException {@inheritDoc}
0N/A * @throws IllegalArgumentException {@inheritDoc}
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public boolean addAll(int index, Collection<? extends E> c) {
0N/A rangeCheckForAdd(index);
0N/A boolean modified = false;
1771N/A for (E e : c) {
1771N/A add(index++, e);
0N/A modified = true;
0N/A }
0N/A return modified;
0N/A }
0N/A
0N/A
0N/A // Iterators
0N/A
0N/A /**
0N/A * Returns an iterator over the elements in this list in proper sequence.
0N/A *
0N/A * <p>This implementation returns a straightforward implementation of the
0N/A * iterator interface, relying on the backing list's {@code size()},
0N/A * {@code get(int)}, and {@code remove(int)} methods.
0N/A *
0N/A * <p>Note that the iterator returned by this method will throw an
0N/A * {@link UnsupportedOperationException} in response to its
0N/A * {@code remove} method unless the list's {@code remove(int)} method is
0N/A * overridden.
0N/A *
0N/A * <p>This implementation can be made to throw runtime exceptions in the
0N/A * face of concurrent modification, as described in the specification
0N/A * for the (protected) {@link #modCount} field.
0N/A *
0N/A * @return an iterator over the elements in this list in proper sequence
0N/A */
0N/A public Iterator<E> iterator() {
0N/A return new Itr();
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A *
0N/A * <p>This implementation returns {@code listIterator(0)}.
0N/A *
0N/A * @see #listIterator(int)
0N/A */
0N/A public ListIterator<E> listIterator() {
0N/A return listIterator(0);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A *
0N/A * <p>This implementation returns a straightforward implementation of the
0N/A * {@code ListIterator} interface that extends the implementation of the
0N/A * {@code Iterator} interface returned by the {@code iterator()} method.
0N/A * The {@code ListIterator} implementation relies on the backing list's
0N/A * {@code get(int)}, {@code set(int, E)}, {@code add(int, E)}
0N/A * and {@code remove(int)} methods.
0N/A *
0N/A * <p>Note that the list iterator returned by this implementation will
0N/A * throw an {@link UnsupportedOperationException} in response to its
0N/A * {@code remove}, {@code set} and {@code add} methods unless the
0N/A * list's {@code remove(int)}, {@code set(int, E)}, and
0N/A * {@code add(int, E)} methods are overridden.
0N/A *
0N/A * <p>This implementation can be made to throw runtime exceptions in the
0N/A * face of concurrent modification, as described in the specification for
0N/A * the (protected) {@link #modCount} field.
0N/A *
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public ListIterator<E> listIterator(final int index) {
0N/A rangeCheckForAdd(index);
0N/A
0N/A return new ListItr(index);
0N/A }
0N/A
0N/A private class Itr implements Iterator<E> {
0N/A /**
0N/A * Index of element to be returned by subsequent call to next.
0N/A */
0N/A int cursor = 0;
0N/A
0N/A /**
0N/A * Index of element returned by most recent call to next or
0N/A * previous. Reset to -1 if this element is deleted by a call
0N/A * to remove.
0N/A */
0N/A int lastRet = -1;
0N/A
0N/A /**
0N/A * The modCount value that the iterator believes that the backing
0N/A * List should have. If this expectation is violated, the iterator
0N/A * has detected concurrent modification.
0N/A */
0N/A int expectedModCount = modCount;
0N/A
0N/A public boolean hasNext() {
0N/A return cursor != size();
0N/A }
0N/A
0N/A public E next() {
0N/A checkForComodification();
0N/A try {
0N/A int i = cursor;
0N/A E next = get(i);
0N/A lastRet = i;
0N/A cursor = i + 1;
0N/A return next;
0N/A } catch (IndexOutOfBoundsException e) {
0N/A checkForComodification();
0N/A throw new NoSuchElementException();
0N/A }
0N/A }
0N/A
0N/A public void remove() {
0N/A if (lastRet < 0)
0N/A throw new IllegalStateException();
0N/A checkForComodification();
0N/A
0N/A try {
0N/A AbstractList.this.remove(lastRet);
0N/A if (lastRet < cursor)
0N/A cursor--;
0N/A lastRet = -1;
0N/A expectedModCount = modCount;
0N/A } catch (IndexOutOfBoundsException e) {
0N/A throw new ConcurrentModificationException();
0N/A }
0N/A }
0N/A
0N/A final void checkForComodification() {
0N/A if (modCount != expectedModCount)
0N/A throw new ConcurrentModificationException();
0N/A }
0N/A }
0N/A
0N/A private class ListItr extends Itr implements ListIterator<E> {
0N/A ListItr(int index) {
0N/A cursor = index;
0N/A }
0N/A
0N/A public boolean hasPrevious() {
0N/A return cursor != 0;
0N/A }
0N/A
0N/A public E previous() {
0N/A checkForComodification();
0N/A try {
0N/A int i = cursor - 1;
0N/A E previous = get(i);
0N/A lastRet = cursor = i;
0N/A return previous;
0N/A } catch (IndexOutOfBoundsException e) {
0N/A checkForComodification();
0N/A throw new NoSuchElementException();
0N/A }
0N/A }
0N/A
0N/A public int nextIndex() {
0N/A return cursor;
0N/A }
0N/A
0N/A public int previousIndex() {
0N/A return cursor-1;
0N/A }
0N/A
0N/A public void set(E e) {
0N/A if (lastRet < 0)
0N/A throw new IllegalStateException();
0N/A checkForComodification();
0N/A
0N/A try {
0N/A AbstractList.this.set(lastRet, e);
0N/A expectedModCount = modCount;
0N/A } catch (IndexOutOfBoundsException ex) {
0N/A throw new ConcurrentModificationException();
0N/A }
0N/A }
0N/A
0N/A public void add(E e) {
0N/A checkForComodification();
0N/A
0N/A try {
0N/A int i = cursor;
0N/A AbstractList.this.add(i, e);
0N/A lastRet = -1;
0N/A cursor = i + 1;
0N/A expectedModCount = modCount;
0N/A } catch (IndexOutOfBoundsException ex) {
0N/A throw new ConcurrentModificationException();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A *
0N/A * <p>This implementation returns a list that subclasses
0N/A * {@code AbstractList}. The subclass stores, in private fields, the
0N/A * offset of the subList within the backing list, the size of the subList
0N/A * (which can change over its lifetime), and the expected
0N/A * {@code modCount} value of the backing list. There are two variants
0N/A * of the subclass, one of which implements {@code RandomAccess}.
0N/A * If this list implements {@code RandomAccess} the returned list will
0N/A * be an instance of the subclass that implements {@code RandomAccess}.
0N/A *
0N/A * <p>The subclass's {@code set(int, E)}, {@code get(int)},
0N/A * {@code add(int, E)}, {@code remove(int)}, {@code addAll(int,
0N/A * Collection)} and {@code removeRange(int, int)} methods all
0N/A * delegate to the corresponding methods on the backing abstract list,
0N/A * after bounds-checking the index and adjusting for the offset. The
0N/A * {@code addAll(Collection c)} method merely returns {@code addAll(size,
0N/A * c)}.
0N/A *
0N/A * <p>The {@code listIterator(int)} method returns a "wrapper object"
0N/A * over a list iterator on the backing list, which is created with the
0N/A * corresponding method on the backing list. The {@code iterator} method
0N/A * merely returns {@code listIterator()}, and the {@code size} method
0N/A * merely returns the subclass's {@code size} field.
0N/A *
0N/A * <p>All methods first check to see if the actual {@code modCount} of
0N/A * the backing list is equal to its expected value, and throw a
0N/A * {@code ConcurrentModificationException} if it is not.
0N/A *
0N/A * @throws IndexOutOfBoundsException if an endpoint index value is out of range
0N/A * {@code (fromIndex < 0 || toIndex > size)}
0N/A * @throws IllegalArgumentException if the endpoint indices are out of order
0N/A * {@code (fromIndex > toIndex)}
0N/A */
0N/A public List<E> subList(int fromIndex, int toIndex) {
0N/A return (this instanceof RandomAccess ?
3323N/A new RandomAccessSubList<>(this, fromIndex, toIndex) :
3323N/A new SubList<>(this, fromIndex, toIndex));
0N/A }
0N/A
0N/A // Comparison and hashing
0N/A
0N/A /**
0N/A * Compares the specified object with this list for equality. Returns
0N/A * {@code true} if and only if the specified object is also a list, both
0N/A * lists have the same size, and all corresponding pairs of elements in
0N/A * the two lists are <i>equal</i>. (Two elements {@code e1} and
0N/A * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null :
0N/A * e1.equals(e2))}.) In other words, two lists are defined to be
0N/A * equal if they contain the same elements in the same order.<p>
0N/A *
0N/A * This implementation first checks if the specified object is this
0N/A * list. If so, it returns {@code true}; if not, it checks if the
0N/A * specified object is a list. If not, it returns {@code false}; if so,
0N/A * it iterates over both lists, comparing corresponding pairs of elements.
0N/A * If any comparison returns {@code false}, this method returns
0N/A * {@code false}. If either iterator runs out of elements before the
0N/A * other it returns {@code false} (as the lists are of unequal length);
0N/A * otherwise it returns {@code true} when the iterations complete.
0N/A *
0N/A * @param o the object to be compared for equality with this list
0N/A * @return {@code true} if the specified object is equal to this list
0N/A */
0N/A public boolean equals(Object o) {
0N/A if (o == this)
0N/A return true;
0N/A if (!(o instanceof List))
0N/A return false;
0N/A
0N/A ListIterator<E> e1 = listIterator();
0N/A ListIterator e2 = ((List) o).listIterator();
3203N/A while (e1.hasNext() && e2.hasNext()) {
0N/A E o1 = e1.next();
0N/A Object o2 = e2.next();
0N/A if (!(o1==null ? o2==null : o1.equals(o2)))
0N/A return false;
0N/A }
0N/A return !(e1.hasNext() || e2.hasNext());
0N/A }
0N/A
0N/A /**
0N/A * Returns the hash code value for this list.
0N/A *
0N/A * <p>This implementation uses exactly the code that is used to define the
0N/A * list hash function in the documentation for the {@link List#hashCode}
0N/A * method.
0N/A *
0N/A * @return the hash code value for this list
0N/A */
0N/A public int hashCode() {
0N/A int hashCode = 1;
0N/A for (E e : this)
0N/A hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
0N/A return hashCode;
0N/A }
0N/A
0N/A /**
0N/A * Removes from this list all of the elements whose index is between
0N/A * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
0N/A * Shifts any succeeding elements to the left (reduces their index).
0N/A * This call shortens the list by {@code (toIndex - fromIndex)} elements.
0N/A * (If {@code toIndex==fromIndex}, this operation has no effect.)
0N/A *
0N/A * <p>This method is called by the {@code clear} operation on this list
0N/A * and its subLists. Overriding this method to take advantage of
0N/A * the internals of the list implementation can <i>substantially</i>
0N/A * improve the performance of the {@code clear} operation on this list
0N/A * and its subLists.
0N/A *
0N/A * <p>This implementation gets a list iterator positioned before
0N/A * {@code fromIndex}, and repeatedly calls {@code ListIterator.next}
0N/A * followed by {@code ListIterator.remove} until the entire range has
0N/A * been removed. <b>Note: if {@code ListIterator.remove} requires linear
0N/A * time, this implementation requires quadratic time.</b>
0N/A *
0N/A * @param fromIndex index of first element to be removed
0N/A * @param toIndex index after last element to be removed
0N/A */
0N/A protected void removeRange(int fromIndex, int toIndex) {
0N/A ListIterator<E> it = listIterator(fromIndex);
0N/A for (int i=0, n=toIndex-fromIndex; i<n; i++) {
0N/A it.next();
0N/A it.remove();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The number of times this list has been <i>structurally modified</i>.
0N/A * Structural modifications are those that change the size of the
0N/A * list, or otherwise perturb it in such a fashion that iterations in
0N/A * progress may yield incorrect results.
0N/A *
0N/A * <p>This field is used by the iterator and list iterator implementation
0N/A * returned by the {@code iterator} and {@code listIterator} methods.
0N/A * If the value of this field changes unexpectedly, the iterator (or list
0N/A * iterator) will throw a {@code ConcurrentModificationException} in
0N/A * response to the {@code next}, {@code remove}, {@code previous},
0N/A * {@code set} or {@code add} operations. This provides
0N/A * <i>fail-fast</i> behavior, rather than non-deterministic behavior in
0N/A * the face of concurrent modification during iteration.
0N/A *
0N/A * <p><b>Use of this field by subclasses is optional.</b> If a subclass
0N/A * wishes to provide fail-fast iterators (and list iterators), then it
0N/A * merely has to increment this field in its {@code add(int, E)} and
0N/A * {@code remove(int)} methods (and any other methods that it overrides
0N/A * that result in structural modifications to the list). A single call to
0N/A * {@code add(int, E)} or {@code remove(int)} must add no more than
0N/A * one to this field, or the iterators (and list iterators) will throw
0N/A * bogus {@code ConcurrentModificationExceptions}. If an implementation
0N/A * does not wish to provide fail-fast iterators, this field may be
0N/A * ignored.
0N/A */
0N/A protected transient int modCount = 0;
0N/A
0N/A private void rangeCheckForAdd(int index) {
0N/A if (index < 0 || index > size())
0N/A throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
0N/A }
0N/A
0N/A private String outOfBoundsMsg(int index) {
0N/A return "Index: "+index+", Size: "+size();
0N/A }
0N/A}
0N/A
0N/Aclass SubList<E> extends AbstractList<E> {
0N/A private final AbstractList<E> l;
0N/A private final int offset;
0N/A private int size;
0N/A
0N/A SubList(AbstractList<E> list, int fromIndex, int toIndex) {
0N/A if (fromIndex < 0)
0N/A throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
0N/A if (toIndex > list.size())
0N/A throw new IndexOutOfBoundsException("toIndex = " + toIndex);
0N/A if (fromIndex > toIndex)
0N/A throw new IllegalArgumentException("fromIndex(" + fromIndex +
0N/A ") > toIndex(" + toIndex + ")");
0N/A l = list;
0N/A offset = fromIndex;
0N/A size = toIndex - fromIndex;
0N/A this.modCount = l.modCount;
0N/A }
0N/A
0N/A public E set(int index, E element) {
0N/A rangeCheck(index);
0N/A checkForComodification();
0N/A return l.set(index+offset, element);
0N/A }
0N/A
0N/A public E get(int index) {
0N/A rangeCheck(index);
0N/A checkForComodification();
0N/A return l.get(index+offset);
0N/A }
0N/A
0N/A public int size() {
0N/A checkForComodification();
0N/A return size;
0N/A }
0N/A
0N/A public void add(int index, E element) {
0N/A rangeCheckForAdd(index);
0N/A checkForComodification();
0N/A l.add(index+offset, element);
0N/A this.modCount = l.modCount;
0N/A size++;
0N/A }
0N/A
0N/A public E remove(int index) {
0N/A rangeCheck(index);
0N/A checkForComodification();
0N/A E result = l.remove(index+offset);
0N/A this.modCount = l.modCount;
0N/A size--;
0N/A return result;
0N/A }
0N/A
0N/A protected void removeRange(int fromIndex, int toIndex) {
0N/A checkForComodification();
0N/A l.removeRange(fromIndex+offset, toIndex+offset);
0N/A this.modCount = l.modCount;
0N/A size -= (toIndex-fromIndex);
0N/A }
0N/A
0N/A public boolean addAll(Collection<? extends E> c) {
0N/A return addAll(size, c);
0N/A }
0N/A
0N/A public boolean addAll(int index, Collection<? extends E> c) {
0N/A rangeCheckForAdd(index);
0N/A int cSize = c.size();
0N/A if (cSize==0)
0N/A return false;
0N/A
0N/A checkForComodification();
0N/A l.addAll(offset+index, c);
0N/A this.modCount = l.modCount;
0N/A size += cSize;
0N/A return true;
0N/A }
0N/A
0N/A public Iterator<E> iterator() {
0N/A return listIterator();
0N/A }
0N/A
0N/A public ListIterator<E> listIterator(final int index) {
0N/A checkForComodification();
0N/A rangeCheckForAdd(index);
0N/A
0N/A return new ListIterator<E>() {
0N/A private final ListIterator<E> i = l.listIterator(index+offset);
0N/A
0N/A public boolean hasNext() {
0N/A return nextIndex() < size;
0N/A }
0N/A
0N/A public E next() {
0N/A if (hasNext())
0N/A return i.next();
0N/A else
0N/A throw new NoSuchElementException();
0N/A }
0N/A
0N/A public boolean hasPrevious() {
0N/A return previousIndex() >= 0;
0N/A }
0N/A
0N/A public E previous() {
0N/A if (hasPrevious())
0N/A return i.previous();
0N/A else
0N/A throw new NoSuchElementException();
0N/A }
0N/A
0N/A public int nextIndex() {
0N/A return i.nextIndex() - offset;
0N/A }
0N/A
0N/A public int previousIndex() {
0N/A return i.previousIndex() - offset;
0N/A }
0N/A
0N/A public void remove() {
0N/A i.remove();
0N/A SubList.this.modCount = l.modCount;
0N/A size--;
0N/A }
0N/A
0N/A public void set(E e) {
0N/A i.set(e);
0N/A }
0N/A
0N/A public void add(E e) {
0N/A i.add(e);
0N/A SubList.this.modCount = l.modCount;
0N/A size++;
0N/A }
0N/A };
0N/A }
0N/A
0N/A public List<E> subList(int fromIndex, int toIndex) {
3323N/A return new SubList<>(this, fromIndex, toIndex);
0N/A }
0N/A
0N/A private void rangeCheck(int index) {
0N/A if (index < 0 || index >= size)
0N/A throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
0N/A }
0N/A
0N/A private void rangeCheckForAdd(int index) {
0N/A if (index < 0 || index > size)
0N/A throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
0N/A }
0N/A
0N/A private String outOfBoundsMsg(int index) {
0N/A return "Index: "+index+", Size: "+size;
0N/A }
0N/A
0N/A private void checkForComodification() {
0N/A if (this.modCount != l.modCount)
0N/A throw new ConcurrentModificationException();
0N/A }
0N/A}
0N/A
0N/Aclass RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
0N/A RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {
0N/A super(list, fromIndex, toIndex);
0N/A }
0N/A
0N/A public List<E> subList(int fromIndex, int toIndex) {
3323N/A return new RandomAccessSubList<>(this, fromIndex, toIndex);
0N/A }
0N/A}