ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/AbstractList.java
(Generate patch)

Comparing jsr166/src/main/java/util/AbstractList.java (file contents):
Revision 1.15 by jsr166, Mon Jun 26 01:27:51 2006 UTC vs.
Revision 1.21 by jsr166, Tue Sep 1 22:28:19 2009 UTC

# Line 1 | Line 1
1   /*
2 < * %W% %E%
2 > * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
3 > * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4   *
5 < * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
6 < * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
5 > * This code is free software; you can redistribute it and/or modify it
6 > * under the terms of the GNU General Public License version 2 only, as
7 > * published by the Free Software Foundation.  Sun designates this
8 > * particular file as subject to the "Classpath" exception as provided
9 > * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 > * CA 95054 USA or visit www.sun.com if you need additional information or
23 > * have any questions.
24   */
25  
26   package java.util;
# Line 47 | Line 65 | package java.util;
65   *
66   * @author  Josh Bloch
67   * @author  Neal Gafter
50 * @version %I%, %G%
68   * @since 1.2
69   */
70  
# Line 88 | Line 105 | public abstract class AbstractList<E> ex
105       *         prevents it from being added to this list
106       */
107      public boolean add(E e) {
108 <        add(size(), e);
109 <        return true;
108 >        add(size(), e);
109 >        return true;
110      }
111  
112      /**
# Line 112 | Line 129 | public abstract class AbstractList<E> ex
129       * @throws IndexOutOfBoundsException     {@inheritDoc}
130       */
131      public E set(int index, E element) {
132 <        throw new UnsupportedOperationException();
132 >        throw new UnsupportedOperationException();
133      }
134  
135      /**
# Line 128 | Line 145 | public abstract class AbstractList<E> ex
145       * @throws IndexOutOfBoundsException     {@inheritDoc}
146       */
147      public void add(int index, E element) {
148 <        throw new UnsupportedOperationException();
148 >        throw new UnsupportedOperationException();
149      }
150  
151      /**
# Line 141 | Line 158 | public abstract class AbstractList<E> ex
158       * @throws IndexOutOfBoundsException     {@inheritDoc}
159       */
160      public E remove(int index) {
161 <        throw new UnsupportedOperationException();
161 >        throw new UnsupportedOperationException();
162      }
163  
164  
# Line 158 | Line 175 | public abstract class AbstractList<E> ex
175       * @throws NullPointerException {@inheritDoc}
176       */
177      public int indexOf(Object o) {
178 <        ListIterator<E> e = listIterator();
179 <        if (o==null) {
180 <            while (e.hasNext())
181 <                if (e.next()==null)
182 <                    return e.previousIndex();
183 <        } else {
184 <            while (e.hasNext())
185 <                if (o.equals(e.next()))
186 <                    return e.previousIndex();
187 <        }
188 <        return -1;
178 >        ListIterator<E> e = listIterator();
179 >        if (o==null) {
180 >            while (e.hasNext())
181 >                if (e.next()==null)
182 >                    return e.previousIndex();
183 >        } else {
184 >            while (e.hasNext())
185 >                if (o.equals(e.next()))
186 >                    return e.previousIndex();
187 >        }
188 >        return -1;
189      }
190  
191      /**
# Line 183 | Line 200 | public abstract class AbstractList<E> ex
200       * @throws NullPointerException {@inheritDoc}
201       */
202      public int lastIndexOf(Object o) {
203 <        ListIterator<E> e = listIterator(size());
204 <        if (o==null) {
205 <            while (e.hasPrevious())
206 <                if (e.previous()==null)
207 <                    return e.nextIndex();
208 <        } else {
209 <            while (e.hasPrevious())
210 <                if (o.equals(e.previous()))
211 <                    return e.nextIndex();
212 <        }
213 <        return -1;
203 >        ListIterator<E> e = listIterator(size());
204 >        if (o==null) {
205 >            while (e.hasPrevious())
206 >                if (e.previous()==null)
207 >                    return e.nextIndex();
208 >        } else {
209 >            while (e.hasPrevious())
210 >                if (o.equals(e.previous()))
211 >                    return e.nextIndex();
212 >        }
213 >        return -1;
214      }
215  
216  
# Line 237 | Line 254 | public abstract class AbstractList<E> ex
254       * @throws IndexOutOfBoundsException     {@inheritDoc}
255       */
256      public boolean addAll(int index, Collection<? extends E> c) {
257 <        boolean modified = false;
258 <        Iterator<? extends E> e = c.iterator();
259 <        while (e.hasNext()) {
260 <            add(index++, e.next());
261 <            modified = true;
262 <        }
263 <        return modified;
257 >        rangeCheckForAdd(index);
258 >        boolean modified = false;
259 >        for (E e : c) {
260 >            add(index++, e);
261 >            modified = true;
262 >        }
263 >        return modified;
264      }
265  
266  
# Line 257 | Line 274 | public abstract class AbstractList<E> ex
274       * {@code get(int)}, and {@code remove(int)} methods.
275       *
276       * <p>Note that the iterator returned by this method will throw an
277 <     * {@code UnsupportedOperationException} in response to its
277 >     * {@link UnsupportedOperationException} in response to its
278       * {@code remove} method unless the list's {@code remove(int)} method is
279       * overridden.
280       *
281       * <p>This implementation can be made to throw runtime exceptions in the
282       * face of concurrent modification, as described in the specification
283 <     * for the (protected) {@code modCount} field.
283 >     * for the (protected) {@link #modCount} field.
284       *
285       * @return an iterator over the elements in this list in proper sequence
269     *
270     * @see #modCount
286       */
287      public Iterator<E> iterator() {
288 <        return new Itr();
288 >        return new Itr();
289      }
290  
291      /**
# Line 281 | Line 296 | public abstract class AbstractList<E> ex
296       * @see #listIterator(int)
297       */
298      public ListIterator<E> listIterator() {
299 <        return listIterator(0);
299 >        return listIterator(0);
300      }
301  
302      /**
# Line 295 | Line 310 | public abstract class AbstractList<E> ex
310       * and {@code remove(int)} methods.
311       *
312       * <p>Note that the list iterator returned by this implementation will
313 <     * throw an {@code UnsupportedOperationException} in response to its
313 >     * throw an {@link UnsupportedOperationException} in response to its
314       * {@code remove}, {@code set} and {@code add} methods unless the
315       * list's {@code remove(int)}, {@code set(int, E)}, and
316       * {@code add(int, E)} methods are overridden.
317       *
318       * <p>This implementation can be made to throw runtime exceptions in the
319       * face of concurrent modification, as described in the specification for
320 <     * the (protected) {@code modCount} field.
320 >     * the (protected) {@link #modCount} field.
321       *
322       * @throws IndexOutOfBoundsException {@inheritDoc}
308     *
309     * @see #modCount
323       */
324      public ListIterator<E> listIterator(final int index) {
325 <        if (index<0 || index>size())
313 <          throw new IndexOutOfBoundsException("Index: "+index);
325 >        rangeCheckForAdd(index);
326  
327 <        return new ListItr(index);
327 >        return new ListItr(index);
328      }
329  
330      private class Itr implements Iterator<E> {
331 <        /**
332 <         * Index of element to be returned by subsequent call to next.
333 <         */
334 <        int cursor = 0;
335 <
336 <        /**
337 <         * Index of element returned by most recent call to next or
338 <         * previous.  Reset to -1 if this element is deleted by a call
339 <         * to remove.
340 <         */
341 <        int lastRet = -1;
342 <
343 <        /**
344 <         * The modCount value that the iterator believes that the backing
345 <         * List should have.  If this expectation is violated, the iterator
346 <         * has detected concurrent modification.
347 <         */
348 <        int expectedModCount = modCount;
331 >        /**
332 >         * Index of element to be returned by subsequent call to next.
333 >         */
334 >        int cursor = 0;
335 >
336 >        /**
337 >         * Index of element returned by most recent call to next or
338 >         * previous.  Reset to -1 if this element is deleted by a call
339 >         * to remove.
340 >         */
341 >        int lastRet = -1;
342 >
343 >        /**
344 >         * The modCount value that the iterator believes that the backing
345 >         * List should have.  If this expectation is violated, the iterator
346 >         * has detected concurrent modification.
347 >         */
348 >        int expectedModCount = modCount;
349  
350 <        public boolean hasNext() {
350 >        public boolean hasNext() {
351              return cursor != size();
352 <        }
352 >        }
353  
354 <        public E next() {
354 >        public E next() {
355              checkForComodification();
356 <            try {
357 <                E next = get(cursor);
358 <                lastRet = cursor++;
359 <                return next;
360 <            } catch (IndexOutOfBoundsException e) {
361 <                checkForComodification();
362 <                throw new NoSuchElementException();
363 <            }
364 <        }
365 <
366 <        public void remove() {
367 <            if (lastRet == -1)
368 <                throw new IllegalStateException();
356 >            try {
357 >                int i = cursor;
358 >                E next = get(i);
359 >                lastRet = i;
360 >                cursor = i + 1;
361 >                return next;
362 >            } catch (IndexOutOfBoundsException e) {
363 >                checkForComodification();
364 >                throw new NoSuchElementException();
365 >            }
366 >        }
367 >
368 >        public void remove() {
369 >            if (lastRet < 0)
370 >                throw new IllegalStateException();
371              checkForComodification();
372  
373 <            try {
374 <                AbstractList.this.remove(lastRet);
375 <                if (lastRet < cursor)
376 <                    cursor--;
377 <                lastRet = -1;
378 <                expectedModCount = modCount;
379 <            } catch (IndexOutOfBoundsException e) {
380 <                throw new ConcurrentModificationException();
381 <            }
382 <        }
383 <
384 <        final void checkForComodification() {
385 <            if (modCount != expectedModCount)
386 <                throw new ConcurrentModificationException();
387 <        }
373 >            try {
374 >                AbstractList.this.remove(lastRet);
375 >                if (lastRet < cursor)
376 >                    cursor--;
377 >                lastRet = -1;
378 >                expectedModCount = modCount;
379 >            } catch (IndexOutOfBoundsException e) {
380 >                throw new ConcurrentModificationException();
381 >            }
382 >        }
383 >
384 >        final void checkForComodification() {
385 >            if (modCount != expectedModCount)
386 >                throw new ConcurrentModificationException();
387 >        }
388      }
389  
390      private class ListItr extends Itr implements ListIterator<E> {
391 <        ListItr(int index) {
392 <            cursor = index;
393 <        }
394 <
395 <        public boolean hasPrevious() {
396 <            return cursor != 0;
397 <        }
391 >        ListItr(int index) {
392 >            cursor = index;
393 >        }
394 >
395 >        public boolean hasPrevious() {
396 >            return cursor != 0;
397 >        }
398  
399          public E previous() {
400              checkForComodification();
# Line 395 | Line 409 | public abstract class AbstractList<E> ex
409              }
410          }
411  
412 <        public int nextIndex() {
413 <            return cursor;
414 <        }
415 <
416 <        public int previousIndex() {
417 <            return cursor-1;
418 <        }
419 <
420 <        public void set(E e) {
421 <            if (lastRet == -1)
422 <                throw new IllegalStateException();
412 >        public int nextIndex() {
413 >            return cursor;
414 >        }
415 >
416 >        public int previousIndex() {
417 >            return cursor-1;
418 >        }
419 >
420 >        public void set(E e) {
421 >            if (lastRet < 0)
422 >                throw new IllegalStateException();
423              checkForComodification();
424  
425 <            try {
426 <                AbstractList.this.set(lastRet, e);
427 <                expectedModCount = modCount;
428 <            } catch (IndexOutOfBoundsException ex) {
429 <                throw new ConcurrentModificationException();
430 <            }
431 <        }
425 >            try {
426 >                AbstractList.this.set(lastRet, e);
427 >                expectedModCount = modCount;
428 >            } catch (IndexOutOfBoundsException ex) {
429 >                throw new ConcurrentModificationException();
430 >            }
431 >        }
432  
433 <        public void add(E e) {
433 >        public void add(E e) {
434              checkForComodification();
435  
436 <            try {
436 >            try {
437                  int i = cursor;
438 <                AbstractList.this.add(i, e);
438 >                AbstractList.this.add(i, e);
439 >                lastRet = -1;
440                  cursor = i + 1;
441 <                lastRet = -1;
442 <                expectedModCount = modCount;
443 <            } catch (IndexOutOfBoundsException ex) {
444 <                throw new ConcurrentModificationException();
445 <            }
431 <        }
441 >                expectedModCount = modCount;
442 >            } catch (IndexOutOfBoundsException ex) {
443 >                throw new ConcurrentModificationException();
444 >            }
445 >        }
446      }
447  
448      /**
# Line 461 | Line 475 | public abstract class AbstractList<E> ex
475       * the backing list is equal to its expected value, and throw a
476       * {@code ConcurrentModificationException} if it is not.
477       *
478 <     * @throws IndexOutOfBoundsException endpoint index value out of range
478 >     * @throws IndexOutOfBoundsException if an endpoint index value is out of range
479       *         {@code (fromIndex < 0 || toIndex > size)}
480       * @throws IllegalArgumentException if the endpoint indices are out of order
481       *         {@code (fromIndex > toIndex)}
482       */
483      public List<E> subList(int fromIndex, int toIndex) {
484          return (this instanceof RandomAccess ?
485 <                new RandomAccessSubList(this, this, fromIndex, fromIndex, toIndex) :
486 <                new SubList(this, this, fromIndex, fromIndex, toIndex));
485 >                new RandomAccessSubList<E>(this, fromIndex, toIndex) :
486 >                new SubList<E>(this, fromIndex, toIndex));
487      }
488  
489      // Comparison and hashing
# Line 481 | Line 495 | public abstract class AbstractList<E> ex
495       * the two lists are <i>equal</i>.  (Two elements {@code e1} and
496       * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null :
497       * e1.equals(e2))}.)  In other words, two lists are defined to be
498 <     * equal if they contain the same elements in the same order.
498 >     * equal if they contain the same elements in the same order.<p>
499       *
500 <     * <p>This implementation first checks if the specified object is this
500 >     * This implementation first checks if the specified object is this
501       * list. If so, it returns {@code true}; if not, it checks if the
502       * specified object is a list. If not, it returns {@code false}; if so,
503       * it iterates over both lists, comparing corresponding pairs of elements.
# Line 496 | Line 510 | public abstract class AbstractList<E> ex
510       * @return {@code true} if the specified object is equal to this list
511       */
512      public boolean equals(Object o) {
513 <        if (o == this)
514 <            return true;
515 <        if (!(o instanceof List))
516 <            return false;
517 <
518 <        ListIterator<E> e1 = listIterator();
519 <        ListIterator e2 = ((List) o).listIterator();
520 <        while(e1.hasNext() && e2.hasNext()) {
521 <            E o1 = e1.next();
522 <            Object o2 = e2.next();
523 <            if (!(o1==null ? o2==null : o1.equals(o2)))
524 <                return false;
525 <        }
526 <        return !(e1.hasNext() || e2.hasNext());
513 >        if (o == this)
514 >            return true;
515 >        if (!(o instanceof List))
516 >            return false;
517 >
518 >        ListIterator<E> e1 = listIterator();
519 >        ListIterator e2 = ((List) o).listIterator();
520 >        while(e1.hasNext() && e2.hasNext()) {
521 >            E o1 = e1.next();
522 >            Object o2 = e2.next();
523 >            if (!(o1==null ? o2==null : o1.equals(o2)))
524 >                return false;
525 >        }
526 >        return !(e1.hasNext() || e2.hasNext());
527      }
528  
529      /**
# Line 522 | Line 536 | public abstract class AbstractList<E> ex
536       * @return the hash code value for this list
537       */
538      public int hashCode() {
539 <        int hashCode = 1;
540 <        Iterator<E> i = iterator();
541 <        while (i.hasNext()) {
542 <            E obj = i.next();
529 <            hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
530 <        }
531 <        return hashCode;
539 >        int hashCode = 1;
540 >        for (E e : this)
541 >            hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
542 >        return hashCode;
543      }
544  
545      /**
546       * Removes from this list all of the elements whose index is between
547       * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
548       * Shifts any succeeding elements to the left (reduces their index).
549 <     * This call shortens the ArrayList by {@code (toIndex - fromIndex)}
550 <     * elements.  (If {@code toIndex==fromIndex}, this operation has no
540 <     * effect.)
549 >     * This call shortens the list by {@code (toIndex - fromIndex)} elements.
550 >     * (If {@code toIndex==fromIndex}, this operation has no effect.)
551       *
552       * <p>This method is called by the {@code clear} operation on this list
553       * and its subLists.  Overriding this method to take advantage of
# Line 589 | Line 599 | public abstract class AbstractList<E> ex
599       * ignored.
600       */
601      protected transient int modCount = 0;
602 +
603 +    private void rangeCheckForAdd(int index) {
604 +        if (index < 0 || index > size())
605 +            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
606 +    }
607 +
608 +    private String outOfBoundsMsg(int index) {
609 +        return "Index: "+index+", Size: "+size();
610 +    }
611   }
612  
594 /**
595 * Generic sublists. Non-nested to enable construction by other
596 * classes in this package.
597 */
613   class SubList<E> extends AbstractList<E> {
614 <    /*
615 <     * A SubList has both a "base", the ultimate backing list, as well
616 <     * as a "parent", which is the list or sublist creating this
617 <     * sublist. All methods that may cause structural modifications
618 <     * must propagate through the parent link, with O(k) performance
604 <     * where k is sublist depth. For example in the case of a
605 <     * sub-sub-list, invoking remove(x) will result in a chain of
606 <     * three remove calls. However, all other non-structurally
607 <     * modifying methods can bypass this chain, and relay directly to
608 <     * the base list. In particular, doing so signficantly speeds up
609 <     * the performance of iterators for deeply-nested sublists.
610 <     */
611 <    final AbstractList<E> base;   // Backing list
612 <    final AbstractList<E> parent; // Parent list
613 <    final int baseOffset;         // index wrt base
614 <    final int parentOffset;       // index wrt parent
615 <    int length;                   // Number of elements in this sublist
616 <
617 <    SubList(AbstractList<E> base,
618 <            AbstractList<E> parent,
619 <            int baseIndex,
620 <            int fromIndex,
621 <            int toIndex) {
614 >    private final AbstractList<E> l;
615 >    private final int offset;
616 >    private int size;
617 >
618 >    SubList(AbstractList<E> list, int fromIndex, int toIndex) {
619          if (fromIndex < 0)
620              throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
621 <        if (toIndex > parent.size())
621 >        if (toIndex > list.size())
622              throw new IndexOutOfBoundsException("toIndex = " + toIndex);
623          if (fromIndex > toIndex)
624              throw new IllegalArgumentException("fromIndex(" + fromIndex +
625                                                 ") > toIndex(" + toIndex + ")");
626 <        this.base = base;
627 <        this.parent = parent;
628 <        this.baseOffset = baseIndex;
629 <        this.parentOffset = fromIndex;
633 <        this.length = toIndex - fromIndex;
634 <        this.modCount = base.modCount;
635 <    }
636 <
637 <    /**
638 <     * Returns an IndexOutOfBoundsException with nicer message
639 <     */
640 <    private IndexOutOfBoundsException indexError(int index) {
641 <        return new IndexOutOfBoundsException("Index: " + index +
642 <                                             ", Size: " + length);
626 >        l = list;
627 >        offset = fromIndex;
628 >        size = toIndex - fromIndex;
629 >        this.modCount = l.modCount;
630      }
631  
632      public E set(int index, E element) {
633 <        if (index < 0 || index >= length)
634 <            throw indexError(index);
635 <        if (base.modCount != modCount)
649 <            throw new ConcurrentModificationException();
650 <        return base.set(index + baseOffset, element);
633 >        rangeCheck(index);
634 >        checkForComodification();
635 >        return l.set(index+offset, element);
636      }
637  
638      public E get(int index) {
639 <        if (index < 0 || index >= length)
640 <            throw indexError(index);
641 <        if (base.modCount != modCount)
657 <            throw new ConcurrentModificationException();
658 <        return base.get(index + baseOffset);
639 >        rangeCheck(index);
640 >        checkForComodification();
641 >        return l.get(index+offset);
642      }
643  
644      public int size() {
645 <        if (base.modCount != modCount)
646 <            throw new ConcurrentModificationException();
664 <        return length;
645 >        checkForComodification();
646 >        return size;
647      }
648  
649      public void add(int index, E element) {
650 <        if (index < 0 || index>length)
651 <            throw indexError(index);
652 <        if (base.modCount != modCount)
653 <            throw new ConcurrentModificationException();
654 <        parent.add(index + parentOffset, element);
673 <        length++;
674 <        modCount = base.modCount;
650 >        rangeCheckForAdd(index);
651 >        checkForComodification();
652 >        l.add(index+offset, element);
653 >        this.modCount = l.modCount;
654 >        size++;
655      }
656  
657      public E remove(int index) {
658 <        if (index < 0 || index >= length)
659 <            throw indexError(index);
660 <        if (base.modCount != modCount)
661 <            throw new ConcurrentModificationException();
662 <        E result = parent.remove(index + parentOffset);
683 <        length--;
684 <        modCount = base.modCount;
658 >        rangeCheck(index);
659 >        checkForComodification();
660 >        E result = l.remove(index+offset);
661 >        this.modCount = l.modCount;
662 >        size--;
663          return result;
664      }
665  
666      protected void removeRange(int fromIndex, int toIndex) {
667 <        if (base.modCount != modCount)
668 <            throw new ConcurrentModificationException();
669 <        parent.removeRange(fromIndex + parentOffset, toIndex + parentOffset);
670 <        length -= (toIndex-fromIndex);
693 <        modCount = base.modCount;
667 >        checkForComodification();
668 >        l.removeRange(fromIndex+offset, toIndex+offset);
669 >        this.modCount = l.modCount;
670 >        size -= (toIndex-fromIndex);
671      }
672  
673      public boolean addAll(Collection<? extends E> c) {
674 <        return addAll(length, c);
674 >        return addAll(size, c);
675      }
676  
677      public boolean addAll(int index, Collection<? extends E> c) {
678 <        if (index < 0 || index > length)
702 <            throw indexError(index);
678 >        rangeCheckForAdd(index);
679          int cSize = c.size();
680          if (cSize==0)
681              return false;
682  
683 <        if (base.modCount != modCount)
684 <            throw new ConcurrentModificationException();
685 <        parent.addAll(parentOffset + index, c);
686 <        length += cSize;
711 <        modCount = base.modCount;
683 >        checkForComodification();
684 >        l.addAll(offset+index, c);
685 >        this.modCount = l.modCount;
686 >        size += cSize;
687          return true;
688      }
689  
715    public List<E> subList(int fromIndex, int toIndex) {
716        return new SubList(base, this, fromIndex + baseOffset,
717                           fromIndex, toIndex);
718    }
719
690      public Iterator<E> iterator() {
691 <        return new SubListIterator(this, 0);
691 >        return listIterator();
692      }
693  
694 <    public ListIterator<E> listIterator() {
695 <        return new SubListIterator(this, 0);
696 <    }
694 >    public ListIterator<E> listIterator(final int index) {
695 >        checkForComodification();
696 >        rangeCheckForAdd(index);
697  
698 <    public ListIterator<E> listIterator(int index) {
699 <        if (index < 0 || index>length)
730 <            throw indexError(index);
731 <        return new SubListIterator(this, index);
732 <    }
698 >        return new ListIterator<E>() {
699 >            private final ListIterator<E> i = l.listIterator(index+offset);
700  
701 <    /**
702 <     * Generic sublist iterator obeying fastfail semantics via
703 <     * modCount.  The hasNext and next methods locally check for
737 <     * in-range indices before relaying to backing list to get
738 <     * element. If this either encounters an unexpected modCount or
739 <     * fails, the backing list must have been concurrently modified,
740 <     * and is so reported.  The add and remove methods performing
741 <     * structural modifications instead relay them through the
742 <     * sublist.
743 <     */
744 <    private static final class SubListIterator<E> implements ListIterator<E> {
745 <        final SubList<E> outer;       // Sublist creating this iteraor
746 <        final AbstractList<E> base;   // base list
747 <        final int offset;             // Cursor offset wrt base
748 <        int cursor;                   // Current index
749 <        int fence;                    // Upper bound on cursor
750 <        int lastRet;                  // Index of returned element, or -1
751 <        int expectedModCount;         // Expected modCount of base
752 <
753 <        SubListIterator(SubList<E> list, int index) {
754 <            this.lastRet = -1;
755 <            this.cursor = index;
756 <            this.outer = list;
757 <            this.offset = list.baseOffset;
758 <            this.fence = list.length;
759 <            this.base = list.base;
760 <            this.expectedModCount = base.modCount;
761 <        }
762 <
763 <        public boolean hasNext() {
764 <            return cursor < fence;
765 <        }
701 >            public boolean hasNext() {
702 >                return nextIndex() < size;
703 >            }
704  
705 <        public boolean hasPrevious() {
706 <            return cursor > 0;
707 <        }
705 >            public E next() {
706 >                if (hasNext())
707 >                    return i.next();
708 >                else
709 >                    throw new NoSuchElementException();
710 >            }
711  
712 <        public int nextIndex() {
713 <            return cursor;
714 <        }
712 >            public boolean hasPrevious() {
713 >                return previousIndex() >= 0;
714 >            }
715  
716 <        public int previousIndex() {
717 <            return cursor - 1;
718 <        }
716 >            public E previous() {
717 >                if (hasPrevious())
718 >                    return i.previous();
719 >                else
720 >                    throw new NoSuchElementException();
721 >            }
722  
723 <        public E next() {
724 <            int i = cursor;
781 <            if (cursor >= fence)
782 <                throw new NoSuchElementException();
783 <            if (expectedModCount == base.modCount) {
784 <                try {
785 <                    Object next = base.get(i + offset);
786 <                    lastRet = i;
787 <                    cursor = i + 1;
788 <                    return (E)next;
789 <                } catch (IndexOutOfBoundsException fallThrough) {
790 <                }
723 >            public int nextIndex() {
724 >                return i.nextIndex() - offset;
725              }
792            throw new ConcurrentModificationException();
793        }
726  
727 <        public E previous() {
728 <            int i = cursor - 1;
797 <            if (i < 0)
798 <                throw new NoSuchElementException();
799 <            if (expectedModCount == base.modCount) {
800 <                try {
801 <                    Object prev = base.get(i + offset);
802 <                    lastRet = i;
803 <                    cursor = i;
804 <                    return (E)prev;
805 <                } catch (IndexOutOfBoundsException fallThrough) {
806 <                }
727 >            public int previousIndex() {
728 >                return i.previousIndex() - offset;
729              }
808            throw new ConcurrentModificationException();
809        }
730  
731 <        public void set(E e) {
732 <            if (lastRet < 0)
733 <                throw new IllegalStateException();
734 <            if (expectedModCount != base.modCount)
815 <                throw new ConcurrentModificationException();
816 <            try {
817 <                outer.set(lastRet, e);
818 <                expectedModCount = base.modCount;
819 <            } catch (IndexOutOfBoundsException ex) {
820 <                throw new ConcurrentModificationException();
731 >            public void remove() {
732 >                i.remove();
733 >                SubList.this.modCount = l.modCount;
734 >                size--;
735              }
822        }
736  
737 <        public void remove() {
738 <            int i = lastRet;
826 <            if (i < 0)
827 <                throw new IllegalStateException();
828 <            if (expectedModCount != base.modCount)
829 <                throw new ConcurrentModificationException();
830 <            try {
831 <                outer.remove(i);
832 <                if (i < cursor)
833 <                    cursor--;
834 <                lastRet = -1;
835 <                fence = outer.length;
836 <                expectedModCount = base.modCount;
837 <            } catch (IndexOutOfBoundsException ex) {
838 <                throw new ConcurrentModificationException();
737 >            public void set(E e) {
738 >                i.set(e);
739              }
840        }
740  
741 <        public void add(E e) {
742 <            if (expectedModCount != base.modCount)
743 <                throw new ConcurrentModificationException();
744 <            try {
846 <                int i = cursor;
847 <                outer.add(i, e);
848 <                cursor = i + 1;
849 <                lastRet = -1;
850 <                fence = outer.length;
851 <                expectedModCount = base.modCount;
852 <            } catch (IndexOutOfBoundsException ex) {
853 <                throw new ConcurrentModificationException();
741 >            public void add(E e) {
742 >                i.add(e);
743 >                SubList.this.modCount = l.modCount;
744 >                size++;
745              }
746 <        }
746 >        };
747 >    }
748 >
749 >    public List<E> subList(int fromIndex, int toIndex) {
750 >        return new SubList<E>(this, fromIndex, toIndex);
751 >    }
752 >
753 >    private void rangeCheck(int index) {
754 >        if (index < 0 || index >= size)
755 >            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
756 >    }
757 >
758 >    private void rangeCheckForAdd(int index) {
759 >        if (index < 0 || index > size)
760 >            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
761      }
762  
763 +    private String outOfBoundsMsg(int index) {
764 +        return "Index: "+index+", Size: "+size;
765 +    }
766 +
767 +    private void checkForComodification() {
768 +        if (this.modCount != l.modCount)
769 +            throw new ConcurrentModificationException();
770 +    }
771   }
772  
773   class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
774 <    RandomAccessSubList(AbstractList<E> base,
775 <                        AbstractList<E> parent, int baseIndex,
863 <                        int fromIndex, int toIndex) {
864 <        super(base, parent, baseIndex, fromIndex, toIndex);
774 >    RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {
775 >        super(list, fromIndex, toIndex);
776      }
777  
778      public List<E> subList(int fromIndex, int toIndex) {
779 <        return new RandomAccessSubList(base, this, fromIndex + baseOffset,
869 <                                       fromIndex, toIndex);
779 >        return new RandomAccessSubList<E>(this, fromIndex, toIndex);
780      }
781   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines