< prev index next >

src/java.base/share/classes/java/util/AbstractList.java

Print this page




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;






  27 
  28 /**
  29  * This class provides a skeletal implementation of the {@link List}
  30  * interface to minimize the effort required to implement this interface
  31  * backed by a "random access" data store (such as an array).  For sequential
  32  * access data (such as a linked list), {@link AbstractSequentialList} should
  33  * be used in preference to this class.
  34  *
  35  * <p>To implement an unmodifiable list, the programmer needs only to extend
  36  * this class and provide implementations for the {@link #get(int)} and
  37  * {@link List#size() size()} methods.
  38  *
  39  * <p>To implement a modifiable list, the programmer must additionally
  40  * override the {@link #set(int, Object) set(int, E)} method (which otherwise
  41  * throws an {@code UnsupportedOperationException}).  If the list is
  42  * variable-size the programmer must additionally override the
  43  * {@link #add(int, Object) add(int, E)} and {@link #remove(int)} methods.
  44  *
  45  * <p>The programmer should generally provide a void (no argument) and collection
  46  * constructor, as per the recommendation in the {@link Collection} interface


  51  * list iterator are implemented by this class, on top of the "random access"
  52  * methods:
  53  * {@link #get(int)},
  54  * {@link #set(int, Object) set(int, E)},
  55  * {@link #add(int, Object) add(int, E)} and
  56  * {@link #remove(int)}.
  57  *
  58  * <p>The documentation for each non-abstract method in this class describes its
  59  * implementation in detail.  Each of these methods may be overridden if the
  60  * collection being implemented admits a more efficient implementation.
  61  *
  62  * <p>This class is a member of the
  63  * <a href="{@docRoot}/../technotes/guides/collections/index.html">
  64  * Java Collections Framework</a>.
  65  *
  66  * @author  Josh Bloch
  67  * @author  Neal Gafter
  68  * @since 1.2
  69  */
  70 
  71 public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
  72     /**
  73      * Sole constructor.  (For invocation by subclass constructors, typically
  74      * implicit.)
  75      */
  76     protected AbstractList() {
  77     }
  78 
  79     /**
  80      * Appends the specified element to the end of this list (optional
  81      * operation).
  82      *
  83      * <p>Lists that support this operation may place limitations on what
  84      * elements may be added to this list.  In particular, some
  85      * lists will refuse to add null elements, and others will impose
  86      * restrictions on the type of elements that may be added.  List
  87      * classes should clearly specify in their documentation any restrictions
  88      * on what elements may be added.
  89      *
  90      * @implSpec
  91      * This implementation calls {@code add(size(), e)}.


 163      */
 164     public E remove(int index) {
 165         throw new UnsupportedOperationException();
 166     }
 167 
 168 
 169     // Search Operations
 170 
 171     /**
 172      * {@inheritDoc}
 173      *
 174      * @implSpec
 175      * This implementation first gets a list iterator (with
 176      * {@code listIterator()}).  Then, it iterates over the list until the
 177      * specified element is found or the end of the list is reached.
 178      *
 179      * @throws ClassCastException   {@inheritDoc}
 180      * @throws NullPointerException {@inheritDoc}
 181      */
 182     public int indexOf(Object o) {












 183         ListIterator<E> it = listIterator();
 184         if (o==null) {
 185             while (it.hasNext())
 186                 if (it.next()==null)
 187                     return it.previousIndex();
 188         } else {
 189             while (it.hasNext())
 190                 if (o.equals(it.next()))
 191                     return it.previousIndex();
 192         }
 193         return -1;
 194     }

 195 
 196     /**
 197      * {@inheritDoc}
 198      *
 199      * @implSpec
 200      * This implementation first gets a list iterator that points to the end
 201      * of the list (with {@code listIterator(size())}).  Then, it iterates
 202      * backwards over the list until the specified element is found, or the
 203      * beginning of the list is reached.
 204      *
 205      * @throws ClassCastException   {@inheritDoc}
 206      * @throws NullPointerException {@inheritDoc}
 207      */
 208     public int lastIndexOf(Object o) {




 209         ListIterator<E> it = listIterator(size());
 210         if (o==null) {
 211             while (it.hasPrevious())
 212                 if (it.previous()==null)








 213                     return it.nextIndex();
 214         } else {
 215             while (it.hasPrevious())
 216                 if (o.equals(it.previous()))
 217                     return it.nextIndex();
 218         }
 219         return -1;
 220     }

 221 
 222 
 223     // Bulk Operations
 224 
 225     /**
 226      * Removes all of the elements from this list (optional operation).
 227      * The list will be empty after this call returns.
 228      *
 229      * @implSpec
 230      * This implementation calls {@code removeRange(0, size())}.
 231      *
 232      * <p>Note that this implementation throws an
 233      * {@code UnsupportedOperationException} unless {@code remove(int
 234      * index)} or {@code removeRange(int fromIndex, int toIndex)} is
 235      * overridden.
 236      *
 237      * @throws UnsupportedOperationException if the {@code clear} operation
 238      *         is not supported by this list
 239      */
 240     public void clear() {


 247      * @implSpec
 248      * This implementation gets an iterator over the specified collection
 249      * and iterates over it, inserting the elements obtained from the
 250      * iterator into this list at the appropriate position, one at a time,
 251      * using {@code add(int, E)}.
 252      * Many implementations will override this method for efficiency.
 253      *
 254      * <p>Note that this implementation throws an
 255      * {@code UnsupportedOperationException} unless
 256      * {@link #add(int, Object) add(int, E)} is overridden.
 257      *
 258      * @throws UnsupportedOperationException {@inheritDoc}
 259      * @throws ClassCastException            {@inheritDoc}
 260      * @throws NullPointerException          {@inheritDoc}
 261      * @throws IllegalArgumentException      {@inheritDoc}
 262      * @throws IndexOutOfBoundsException     {@inheritDoc}
 263      */
 264     public boolean addAll(int index, Collection<? extends E> c) {
 265         rangeCheckForAdd(index);
 266         boolean modified = false;
 267         for (E e : c) {


 268             add(index++, e);
 269             modified = true;
 270         }
 271         return modified;
 272     }
 273 
 274 
 275     // Iterators
 276 
 277     /**
 278      * Returns an iterator over the elements in this list in proper sequence.
 279      *
 280      * @implSpec
 281      * This implementation returns a straightforward implementation of the
 282      * iterator interface, relying on the backing list's {@code size()},
 283      * {@code get(int)}, and {@code remove(int)} methods.
 284      *
 285      * <p>Note that the iterator returned by this method will throw an
 286      * {@link UnsupportedOperationException} in response to its
 287      * {@code remove} method unless the list's {@code remove(int)} method is


 505      * {@code true} if and only if the specified object is also a list, both
 506      * lists have the same size, and all corresponding pairs of elements in
 507      * the two lists are <i>equal</i>.  (Two elements {@code e1} and
 508      * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null :
 509      * e1.equals(e2))}.)  In other words, two lists are defined to be
 510      * equal if they contain the same elements in the same order.
 511      *
 512      * @implSpec
 513      * This implementation first checks if the specified object is this
 514      * list. If so, it returns {@code true}; if not, it checks if the
 515      * specified object is a list. If not, it returns {@code false}; if so,
 516      * it iterates over both lists, comparing corresponding pairs of elements.
 517      * If any comparison returns {@code false}, this method returns
 518      * {@code false}.  If either iterator runs out of elements before the
 519      * other it returns {@code false} (as the lists are of unequal length);
 520      * otherwise it returns {@code true} when the iterations complete.
 521      *
 522      * @param o the object to be compared for equality with this list
 523      * @return {@code true} if the specified object is equal to this list
 524      */

 525     public boolean equals(Object o) {
 526         if (o == this)
 527             return true;
 528         if (!(o instanceof List))
 529             return false;
 530 
 531         ListIterator<E> e1 = listIterator();
 532         ListIterator<?> e2 = ((List<?>) o).listIterator();
 533         while (e1.hasNext() && e2.hasNext()) {
 534             E o1 = e1.next();
 535             Object o2 = e2.next();
 536             if (!(o1==null ? o2==null : o1.equals(o2)))
 537                 return false;
 538         }
 539         return !(e1.hasNext() || e2.hasNext());
 540     }
 541 
 542     /**
 543      * Returns the hash code value for this list.
 544      *
 545      * @implSpec
 546      * This implementation uses exactly the code that is used to define the
 547      * list hash function in the documentation for the {@link List#hashCode}
 548      * method.
 549      *
 550      * @return the hash code value for this list
 551      */
 552     public int hashCode() {
 553         int hashCode = 1;
 554         for (E e : this)
 555             hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());



 556         return hashCode;
 557     }
 558 
 559     /**
 560      * Removes from this list all of the elements whose index is between
 561      * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
 562      * Shifts any succeeding elements to the left (reduces their index).
 563      * This call shortens the list by {@code (toIndex - fromIndex)} elements.
 564      * (If {@code toIndex==fromIndex}, this operation has no effect.)
 565      *
 566      * <p>This method is called by the {@code clear} operation on this list
 567      * and its subLists.  Overriding this method to take advantage of
 568      * the internals of the list implementation can <i>substantially</i>
 569      * improve the performance of the {@code clear} operation on this list
 570      * and its subLists.
 571      *
 572      * @implSpec
 573      * This implementation gets a list iterator positioned before
 574      * {@code fromIndex}, and repeatedly calls {@code ListIterator.next}
 575      * followed by {@code ListIterator.remove} until the entire range has


 608      * {@code remove(int)} methods (and any other methods that it overrides
 609      * that result in structural modifications to the list).  A single call to
 610      * {@code add(int, E)} or {@code remove(int)} must add no more than
 611      * one to this field, or the iterators (and list iterators) will throw
 612      * bogus {@code ConcurrentModificationExceptions}.  If an implementation
 613      * does not wish to provide fail-fast iterators, this field may be
 614      * ignored.
 615      */
 616     protected transient int modCount = 0;
 617 
 618     private void rangeCheckForAdd(int index) {
 619         if (index < 0 || index > size())
 620             throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 621     }
 622 
 623     private String outOfBoundsMsg(int index) {
 624         return "Index: "+index+", Size: "+size();
 625     }
 626 }
 627 
 628 class SubList<E> extends AbstractList<E> {
 629     private final AbstractList<E> l;
 630     private final int offset;
 631     private int size;
 632 
 633     SubList(AbstractList<E> list, int fromIndex, int toIndex) {
 634         if (fromIndex < 0)
 635             throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
 636         if (toIndex > list.size())
 637             throw new IndexOutOfBoundsException("toIndex = " + toIndex);
 638         if (fromIndex > toIndex)
 639             throw new IllegalArgumentException("fromIndex(" + fromIndex +
 640                                                ") > toIndex(" + toIndex + ")");
 641         l = list;
 642         offset = fromIndex;
 643         size = toIndex - fromIndex;
 644         this.modCount = l.modCount;
 645     }
 646 
 647     public E set(int index, E element) {
 648         rangeCheck(index);


 768     private void rangeCheck(int index) {
 769         if (index < 0 || index >= size)
 770             throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 771     }
 772 
 773     private void rangeCheckForAdd(int index) {
 774         if (index < 0 || index > size)
 775             throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 776     }
 777 
 778     private String outOfBoundsMsg(int index) {
 779         return "Index: "+index+", Size: "+size;
 780     }
 781 
 782     private void checkForComodification() {
 783         if (this.modCount != l.modCount)
 784             throw new ConcurrentModificationException();
 785     }
 786 }
 787 
 788 class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
 789     RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {
 790         super(list, fromIndex, toIndex);
 791     }
 792 
 793     public List<E> subList(int fromIndex, int toIndex) {
 794         return new RandomAccessSubList<>(this, fromIndex, toIndex);
 795     }
 796 }


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javany.util;
  27 
  28 import java.util.ConcurrentModificationException;
  29 import java.util.NoSuchElementException;
  30 import java.util.RandomAccess;
  31 
  32 import javany.util.function.*;
  33 
  34 /**
  35  * This class provides a skeletal implementation of the {@link List}
  36  * interface to minimize the effort required to implement this interface
  37  * backed by a "random access" data store (such as an array).  For sequential
  38  * access data (such as a linked list), {@link AbstractSequentialList} should
  39  * be used in preference to this class.
  40  *
  41  * <p>To implement an unmodifiable list, the programmer needs only to extend
  42  * this class and provide implementations for the {@link #get(int)} and
  43  * {@link List#size() size()} methods.
  44  *
  45  * <p>To implement a modifiable list, the programmer must additionally
  46  * override the {@link #set(int, Object) set(int, E)} method (which otherwise
  47  * throws an {@code UnsupportedOperationException}).  If the list is
  48  * variable-size the programmer must additionally override the
  49  * {@link #add(int, Object) add(int, E)} and {@link #remove(int)} methods.
  50  *
  51  * <p>The programmer should generally provide a void (no argument) and collection
  52  * constructor, as per the recommendation in the {@link Collection} interface


  57  * list iterator are implemented by this class, on top of the "random access"
  58  * methods:
  59  * {@link #get(int)},
  60  * {@link #set(int, Object) set(int, E)},
  61  * {@link #add(int, Object) add(int, E)} and
  62  * {@link #remove(int)}.
  63  *
  64  * <p>The documentation for each non-abstract method in this class describes its
  65  * implementation in detail.  Each of these methods may be overridden if the
  66  * collection being implemented admits a more efficient implementation.
  67  *
  68  * <p>This class is a member of the
  69  * <a href="{@docRoot}/../technotes/guides/collections/index.html">
  70  * Java Collections Framework</a>.
  71  *
  72  * @author  Josh Bloch
  73  * @author  Neal Gafter
  74  * @since 1.2
  75  */
  76 
  77 public abstract class AbstractList<any E> extends AbstractCollection<E> implements List<E> {
  78     /**
  79      * Sole constructor.  (For invocation by subclass constructors, typically
  80      * implicit.)
  81      */
  82     protected AbstractList() {
  83     }
  84 
  85     /**
  86      * Appends the specified element to the end of this list (optional
  87      * operation).
  88      *
  89      * <p>Lists that support this operation may place limitations on what
  90      * elements may be added to this list.  In particular, some
  91      * lists will refuse to add null elements, and others will impose
  92      * restrictions on the type of elements that may be added.  List
  93      * classes should clearly specify in their documentation any restrictions
  94      * on what elements may be added.
  95      *
  96      * @implSpec
  97      * This implementation calls {@code add(size(), e)}.


 169      */
 170     public E remove(int index) {
 171         throw new UnsupportedOperationException();
 172     }
 173 
 174 
 175     // Search Operations
 176 
 177     /**
 178      * {@inheritDoc}
 179      *
 180      * @implSpec
 181      * This implementation first gets a list iterator (with
 182      * {@code listIterator()}).  Then, it iterates over the list until the
 183      * specified element is found or the end of the list is reached.
 184      *
 185      * @throws ClassCastException   {@inheritDoc}
 186      * @throws NullPointerException {@inheritDoc}
 187      */
 188     public int indexOf(Object o) {
 189         __WhereVal(E) {
 190             if (o == null) {
 191                 return -1;
 192             }
 193             ListIterator<E> it = listIterator();
 194             Function<E, Object> box = Any.converter();
 195             while (it.hasNext())
 196                 if (o.equals(box.apply(it.next()))) // boxing
 197                     return it.previousIndex();
 198             return -1;
 199         }
 200         __WhereRef(E) {
 201             ListIterator<E> it = listIterator();
 202             if (o == null) {
 203                 while (it.hasNext())
 204                     if (it.next() == null)
 205                         return it.previousIndex();
 206             } else {
 207                 while (it.hasNext())
 208                     if (o.equals(it.next()))
 209                         return it.previousIndex();
 210             }
 211             return -1;
 212         }
 213     }
 214 
 215     /**
 216      * {@inheritDoc}
 217      *
 218      * @implSpec
 219      * This implementation first gets a list iterator that points to the end
 220      * of the list (with {@code listIterator(size())}).  Then, it iterates
 221      * backwards over the list until the specified element is found, or the
 222      * beginning of the list is reached.
 223      *
 224      * @throws ClassCastException   {@inheritDoc}
 225      * @throws NullPointerException {@inheritDoc}
 226      */
 227     public int lastIndexOf(Object o) {
 228         __WhereVal(E) {
 229             if (o == null) {
 230                 return -1;
 231             }
 232             ListIterator<E> it = listIterator(size());
 233             Function<E, Object> box = Any.converter();
 234             while (it.hasPrevious())
 235                 if (o.equals(box.apply(it.previous()))) // boxing
 236                     return it.nextIndex();
 237             return -1;
 238         }
 239         __WhereRef(E) {
 240             ListIterator<E> it = listIterator(size());
 241             if (o == null) {
 242                 while (it.hasPrevious())
 243                     if (it.previous() == null)
 244                         return it.nextIndex();
 245             } else {
 246                 while (it.hasPrevious())
 247                     if (o.equals(it.previous()))
 248                         return it.nextIndex();
 249             }
 250             return -1;
 251         }
 252     }
 253 
 254 
 255     // Bulk Operations
 256 
 257     /**
 258      * Removes all of the elements from this list (optional operation).
 259      * The list will be empty after this call returns.
 260      *
 261      * @implSpec
 262      * This implementation calls {@code removeRange(0, size())}.
 263      *
 264      * <p>Note that this implementation throws an
 265      * {@code UnsupportedOperationException} unless {@code remove(int
 266      * index)} or {@code removeRange(int fromIndex, int toIndex)} is
 267      * overridden.
 268      *
 269      * @throws UnsupportedOperationException if the {@code clear} operation
 270      *         is not supported by this list
 271      */
 272     public void clear() {


 279      * @implSpec
 280      * This implementation gets an iterator over the specified collection
 281      * and iterates over it, inserting the elements obtained from the
 282      * iterator into this list at the appropriate position, one at a time,
 283      * using {@code add(int, E)}.
 284      * Many implementations will override this method for efficiency.
 285      *
 286      * <p>Note that this implementation throws an
 287      * {@code UnsupportedOperationException} unless
 288      * {@link #add(int, Object) add(int, E)} is overridden.
 289      *
 290      * @throws UnsupportedOperationException {@inheritDoc}
 291      * @throws ClassCastException            {@inheritDoc}
 292      * @throws NullPointerException          {@inheritDoc}
 293      * @throws IllegalArgumentException      {@inheritDoc}
 294      * @throws IndexOutOfBoundsException     {@inheritDoc}
 295      */
 296     public boolean addAll(int index, Collection<? extends E> c) {
 297         rangeCheckForAdd(index);
 298         boolean modified = false;
 299         Iterator<? extends E> it = c.iterator();
 300         while (it.hasNext()) {
 301             E e = it.next();
 302             add(index++, e);
 303             modified = true;
 304         }
 305         return modified;
 306     }
 307 
 308 
 309     // Iterators
 310 
 311     /**
 312      * Returns an iterator over the elements in this list in proper sequence.
 313      *
 314      * @implSpec
 315      * This implementation returns a straightforward implementation of the
 316      * iterator interface, relying on the backing list's {@code size()},
 317      * {@code get(int)}, and {@code remove(int)} methods.
 318      *
 319      * <p>Note that the iterator returned by this method will throw an
 320      * {@link UnsupportedOperationException} in response to its
 321      * {@code remove} method unless the list's {@code remove(int)} method is


 539      * {@code true} if and only if the specified object is also a list, both
 540      * lists have the same size, and all corresponding pairs of elements in
 541      * the two lists are <i>equal</i>.  (Two elements {@code e1} and
 542      * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null :
 543      * e1.equals(e2))}.)  In other words, two lists are defined to be
 544      * equal if they contain the same elements in the same order.
 545      *
 546      * @implSpec
 547      * This implementation first checks if the specified object is this
 548      * list. If so, it returns {@code true}; if not, it checks if the
 549      * specified object is a list. If not, it returns {@code false}; if so,
 550      * it iterates over both lists, comparing corresponding pairs of elements.
 551      * If any comparison returns {@code false}, this method returns
 552      * {@code false}.  If either iterator runs out of elements before the
 553      * other it returns {@code false} (as the lists are of unequal length);
 554      * otherwise it returns {@code true} when the iterations complete.
 555      *
 556      * @param o the object to be compared for equality with this list
 557      * @return {@code true} if the specified object is equal to this list
 558      */
 559     @SuppressWarnings("unchecked")
 560     public boolean equals(Object o) {
 561         if (o == this)
 562             return true;
 563         if (!(o instanceof List<E>))
 564             return false;
 565 
 566         ListIterator<E> e1 = listIterator();
 567         ListIterator<? extends E> e2 = ((List<? extends E>) o).listIterator();
 568         while (e1.hasNext() && e2.hasNext()) {
 569             if (!Any.equals(e1.next(), e2.next()))


 570                 return false;
 571         }
 572         return !(e1.hasNext() || e2.hasNext());
 573     }
 574 
 575     /**
 576      * Returns the hash code value for this list.
 577      *
 578      * @implSpec
 579      * This implementation uses exactly the code that is used to define the
 580      * list hash function in the documentation for the {@link List#hashCode}
 581      * method.
 582      *
 583      * @return the hash code value for this list
 584      */
 585     public int hashCode() {
 586         int hashCode = 1;
 587         Iterator<E> it = iterator();
 588         while (it.hasNext()) {
 589             E e = it.next();
 590             hashCode = 31 * hashCode + Any.hashCode(e);
 591         }
 592         return hashCode;
 593     }
 594 
 595     /**
 596      * Removes from this list all of the elements whose index is between
 597      * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
 598      * Shifts any succeeding elements to the left (reduces their index).
 599      * This call shortens the list by {@code (toIndex - fromIndex)} elements.
 600      * (If {@code toIndex==fromIndex}, this operation has no effect.)
 601      *
 602      * <p>This method is called by the {@code clear} operation on this list
 603      * and its subLists.  Overriding this method to take advantage of
 604      * the internals of the list implementation can <i>substantially</i>
 605      * improve the performance of the {@code clear} operation on this list
 606      * and its subLists.
 607      *
 608      * @implSpec
 609      * This implementation gets a list iterator positioned before
 610      * {@code fromIndex}, and repeatedly calls {@code ListIterator.next}
 611      * followed by {@code ListIterator.remove} until the entire range has


 644      * {@code remove(int)} methods (and any other methods that it overrides
 645      * that result in structural modifications to the list).  A single call to
 646      * {@code add(int, E)} or {@code remove(int)} must add no more than
 647      * one to this field, or the iterators (and list iterators) will throw
 648      * bogus {@code ConcurrentModificationExceptions}.  If an implementation
 649      * does not wish to provide fail-fast iterators, this field may be
 650      * ignored.
 651      */
 652     protected transient int modCount = 0;
 653 
 654     private void rangeCheckForAdd(int index) {
 655         if (index < 0 || index > size())
 656             throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 657     }
 658 
 659     private String outOfBoundsMsg(int index) {
 660         return "Index: "+index+", Size: "+size();
 661     }
 662 }
 663 
 664 class SubList<any E> extends AbstractList<E> {
 665     private final AbstractList<E> l;
 666     private final int offset;
 667     private int size;
 668 
 669     SubList(AbstractList<E> list, int fromIndex, int toIndex) {
 670         if (fromIndex < 0)
 671             throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
 672         if (toIndex > list.size())
 673             throw new IndexOutOfBoundsException("toIndex = " + toIndex);
 674         if (fromIndex > toIndex)
 675             throw new IllegalArgumentException("fromIndex(" + fromIndex +
 676                                                ") > toIndex(" + toIndex + ")");
 677         l = list;
 678         offset = fromIndex;
 679         size = toIndex - fromIndex;
 680         this.modCount = l.modCount;
 681     }
 682 
 683     public E set(int index, E element) {
 684         rangeCheck(index);


 804     private void rangeCheck(int index) {
 805         if (index < 0 || index >= size)
 806             throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 807     }
 808 
 809     private void rangeCheckForAdd(int index) {
 810         if (index < 0 || index > size)
 811             throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 812     }
 813 
 814     private String outOfBoundsMsg(int index) {
 815         return "Index: "+index+", Size: "+size;
 816     }
 817 
 818     private void checkForComodification() {
 819         if (this.modCount != l.modCount)
 820             throw new ConcurrentModificationException();
 821     }
 822 }
 823 
 824 class RandomAccessSubList<any E> extends SubList<E> implements RandomAccess {
 825     RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {
 826         super(list, fromIndex, toIndex);
 827     }
 828 
 829     public List<E> subList(int fromIndex, int toIndex) {
 830         return new RandomAccessSubList<>(this, fromIndex, toIndex);
 831     }
 832 }
< prev index next >