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

Comparing jsr166/src/main/java/util/ArrayList.java (file contents):
Revision 1.25 by jsr166, Tue Sep 11 15:38:02 2007 UTC vs.
Revision 1.26 by jsr166, Sun May 18 23:47:55 2008 UTC

# Line 93 | Line 93 | package java.util;
93   * @author  Josh Bloch
94   * @author  Neal Gafter
95   * @version %I%, %G%
96 < * @see     Collection
97 < * @see     List
98 < * @see     LinkedList
99 < * @see     Vector
96 > * @see     Collection
97 > * @see     List
98 > * @see     LinkedList
99 > * @see     Vector
100   * @since   1.2
101   */
102  
# Line 126 | Line 126 | public class ArrayList<E> extends Abstra
126       *            is negative
127       */
128      public ArrayList(int initialCapacity) {
129 <        super();
129 >        super();
130          if (initialCapacity < 0)
131              throw new IllegalArgumentException("Illegal Capacity: "+
132                                                 initialCapacity);
133 <        this.elementData = new Object[initialCapacity];
133 >        this.elementData = new Object[initialCapacity];
134      }
135  
136      /**
137       * Constructs an empty list with an initial capacity of ten.
138       */
139      public ArrayList() {
140 <        this(10);
140 >        this(10);
141      }
142  
143      /**
# Line 149 | Line 149 | public class ArrayList<E> extends Abstra
149       * @throws NullPointerException if the specified collection is null
150       */
151      public ArrayList(Collection<? extends E> c) {
152 <        elementData = c.toArray();
153 <        size = elementData.length;
154 <        // c.toArray might (incorrectly) not return Object[] (see 6260652)
155 <        if (elementData.getClass() != Object[].class)
156 <            elementData = Arrays.copyOf(elementData, size, Object[].class);
152 >        elementData = c.toArray();
153 >        size = elementData.length;
154 >        // c.toArray might (incorrectly) not return Object[] (see 6260652)
155 >        if (elementData.getClass() != Object[].class)
156 >            elementData = Arrays.copyOf(elementData, size, Object[].class);
157      }
158  
159      /**
# Line 162 | Line 162 | public class ArrayList<E> extends Abstra
162       * the storage of an <tt>ArrayList</tt> instance.
163       */
164      public void trimToSize() {
165 <        modCount++;
166 <        int oldCapacity = elementData.length;
167 <        if (size < oldCapacity) {
165 >        modCount++;
166 >        int oldCapacity = elementData.length;
167 >        if (size < oldCapacity) {
168              elementData = Arrays.copyOf(elementData, size);
169 <        }
169 >        }
170      }
171  
172      /**
# Line 177 | Line 177 | public class ArrayList<E> extends Abstra
177       * @param   minCapacity   the desired minimum capacity
178       */
179      public void ensureCapacity(int minCapacity) {
180 <        modCount++;
181 <        int oldCapacity = elementData.length;
182 <        if (minCapacity > oldCapacity) {
183 <            Object oldData[] = elementData;
184 <            int newCapacity = (oldCapacity * 3)/2 + 1;
185 <            if (newCapacity < minCapacity)
186 <                newCapacity = minCapacity;
180 >        modCount++;
181 >        int oldCapacity = elementData.length;
182 >        if (minCapacity > oldCapacity) {
183 >            Object oldData[] = elementData;
184 >            int newCapacity = (oldCapacity * 3)/2 + 1;
185 >            if (newCapacity < minCapacity)
186 >                newCapacity = minCapacity;
187              // minCapacity is usually close to size, so this is a win:
188              elementData = Arrays.copyOf(elementData, newCapacity);
189 <        }
189 >        }
190      }
191  
192      /**
# Line 195 | Line 195 | public class ArrayList<E> extends Abstra
195       * @return the number of elements in this list
196       */
197      public int size() {
198 <        return size;
198 >        return size;
199      }
200  
201      /**
# Line 204 | Line 204 | public class ArrayList<E> extends Abstra
204       * @return <tt>true</tt> if this list contains no elements
205       */
206      public boolean isEmpty() {
207 <        return size == 0;
207 >        return size == 0;
208      }
209  
210      /**
# Line 217 | Line 217 | public class ArrayList<E> extends Abstra
217       * @return <tt>true</tt> if this list contains the specified element
218       */
219      public boolean contains(Object o) {
220 <        return indexOf(o) >= 0;
220 >        return indexOf(o) >= 0;
221      }
222  
223      /**
# Line 228 | Line 228 | public class ArrayList<E> extends Abstra
228       * or -1 if there is no such index.
229       */
230      public int indexOf(Object o) {
231 <        if (o == null) {
232 <            for (int i = 0; i < size; i++)
233 <                if (elementData[i]==null)
234 <                    return i;
235 <        } else {
236 <            for (int i = 0; i < size; i++)
237 <                if (o.equals(elementData[i]))
238 <                    return i;
239 <        }
240 <        return -1;
231 >        if (o == null) {
232 >            for (int i = 0; i < size; i++)
233 >                if (elementData[i]==null)
234 >                    return i;
235 >        } else {
236 >            for (int i = 0; i < size; i++)
237 >                if (o.equals(elementData[i]))
238 >                    return i;
239 >        }
240 >        return -1;
241      }
242  
243      /**
# Line 248 | Line 248 | public class ArrayList<E> extends Abstra
248       * or -1 if there is no such index.
249       */
250      public int lastIndexOf(Object o) {
251 <        if (o == null) {
252 <            for (int i = size-1; i >= 0; i--)
253 <                if (elementData[i]==null)
254 <                    return i;
255 <        } else {
256 <            for (int i = size-1; i >= 0; i--)
257 <                if (o.equals(elementData[i]))
258 <                    return i;
259 <        }
260 <        return -1;
251 >        if (o == null) {
252 >            for (int i = size-1; i >= 0; i--)
253 >                if (elementData[i]==null)
254 >                    return i;
255 >        } else {
256 >            for (int i = size-1; i >= 0; i--)
257 >                if (o.equals(elementData[i]))
258 >                    return i;
259 >        }
260 >        return -1;
261      }
262  
263      /**
# Line 267 | Line 267 | public class ArrayList<E> extends Abstra
267       * @return a clone of this <tt>ArrayList</tt> instance
268       */
269      public Object clone() {
270 <        try {
271 <            @SuppressWarnings("unchecked")
272 <                ArrayList<E> v = (ArrayList<E>) super.clone();
273 <            v.elementData = Arrays.copyOf(elementData, size);
274 <            v.modCount = 0;
275 <            return v;
276 <        } catch (CloneNotSupportedException e) {
277 <            // this shouldn't happen, since we are Cloneable
278 <            throw new InternalError();
279 <        }
270 >        try {
271 >            @SuppressWarnings("unchecked")
272 >                ArrayList<E> v = (ArrayList<E>) super.clone();
273 >            v.elementData = Arrays.copyOf(elementData, size);
274 >            v.modCount = 0;
275 >            return v;
276 >        } catch (CloneNotSupportedException e) {
277 >            // this shouldn't happen, since we are Cloneable
278 >            throw new InternalError();
279 >        }
280      }
281  
282      /**
# Line 326 | Line 326 | public class ArrayList<E> extends Abstra
326          if (a.length < size)
327              // Make a new array of a's runtime type, but my contents:
328              return (T[]) Arrays.copyOf(elementData, size, a.getClass());
329 <        System.arraycopy(elementData, 0, a, 0, size);
329 >        System.arraycopy(elementData, 0, a, 0, size);
330          if (a.length > size)
331              a[size] = null;
332          return a;
# Line 336 | Line 336 | public class ArrayList<E> extends Abstra
336  
337      @SuppressWarnings("unchecked")
338      E elementData(int index) {
339 <        return (E) elementData[index];
339 >        return (E) elementData[index];
340      }
341  
342      /**
# Line 347 | Line 347 | public class ArrayList<E> extends Abstra
347       * @throws IndexOutOfBoundsException {@inheritDoc}
348       */
349      public E get(int index) {
350 <        rangeCheck(index);
350 >        rangeCheck(index);
351  
352 <        return elementData(index);
352 >        return elementData(index);
353      }
354  
355      /**
# Line 362 | Line 362 | public class ArrayList<E> extends Abstra
362       * @throws IndexOutOfBoundsException {@inheritDoc}
363       */
364      public E set(int index, E element) {
365 <        rangeCheck(index);
365 >        rangeCheck(index);
366  
367 <        E oldValue = elementData(index);
368 <        elementData[index] = element;
369 <        return oldValue;
367 >        E oldValue = elementData(index);
368 >        elementData[index] = element;
369 >        return oldValue;
370      }
371  
372      /**
# Line 376 | Line 376 | public class ArrayList<E> extends Abstra
376       * @return <tt>true</tt> (as specified by {@link Collection#add})
377       */
378      public boolean add(E e) {
379 <        ensureCapacity(size + 1);  // Increments modCount!!
380 <        elementData[size++] = e;
381 <        return true;
379 >        ensureCapacity(size + 1);  // Increments modCount!!
380 >        elementData[size++] = e;
381 >        return true;
382      }
383  
384      /**
# Line 391 | Line 391 | public class ArrayList<E> extends Abstra
391       * @throws IndexOutOfBoundsException {@inheritDoc}
392       */
393      public void add(int index, E element) {
394 <        rangeCheckForAdd(index);
394 >        rangeCheckForAdd(index);
395  
396 <        ensureCapacity(size+1);  // Increments modCount!!
397 <        System.arraycopy(elementData, index, elementData, index + 1,
398 <                         size - index);
399 <        elementData[index] = element;
400 <        size++;
396 >        ensureCapacity(size+1);  // Increments modCount!!
397 >        System.arraycopy(elementData, index, elementData, index + 1,
398 >                         size - index);
399 >        elementData[index] = element;
400 >        size++;
401      }
402  
403      /**
# Line 410 | Line 410 | public class ArrayList<E> extends Abstra
410       * @throws IndexOutOfBoundsException {@inheritDoc}
411       */
412      public E remove(int index) {
413 <        rangeCheck(index);
413 >        rangeCheck(index);
414  
415 <        modCount++;
416 <        E oldValue = elementData(index);
415 >        modCount++;
416 >        E oldValue = elementData(index);
417  
418 <        int numMoved = size - index - 1;
419 <        if (numMoved > 0)
420 <            System.arraycopy(elementData, index+1, elementData, index,
421 <                             numMoved);
422 <        elementData[--size] = null; // Let gc do its work
418 >        int numMoved = size - index - 1;
419 >        if (numMoved > 0)
420 >            System.arraycopy(elementData, index+1, elementData, index,
421 >                             numMoved);
422 >        elementData[--size] = null; // Let gc do its work
423  
424 <        return oldValue;
424 >        return oldValue;
425      }
426  
427      /**
# Line 438 | Line 438 | public class ArrayList<E> extends Abstra
438       * @return <tt>true</tt> if this list contained the specified element
439       */
440      public boolean remove(Object o) {
441 <        if (o == null) {
441 >        if (o == null) {
442 >            for (int index = 0; index < size; index++)
443 >                if (elementData[index] == null) {
444 >                    fastRemove(index);
445 >                    return true;
446 >                }
447 >        } else {
448              for (int index = 0; index < size; index++)
449 <                if (elementData[index] == null) {
450 <                    fastRemove(index);
451 <                    return true;
452 <                }
447 <        } else {
448 <            for (int index = 0; index < size; index++)
449 <                if (o.equals(elementData[index])) {
450 <                    fastRemove(index);
451 <                    return true;
452 <                }
449 >                if (o.equals(elementData[index])) {
450 >                    fastRemove(index);
451 >                    return true;
452 >                }
453          }
454 <        return false;
454 >        return false;
455      }
456  
457      /*
# Line 472 | Line 472 | public class ArrayList<E> extends Abstra
472       * be empty after this call returns.
473       */
474      public void clear() {
475 <        modCount++;
475 >        modCount++;
476  
477 <        // Let gc do its work
478 <        for (int i = 0; i < size; i++)
479 <            elementData[i] = null;
477 >        // Let gc do its work
478 >        for (int i = 0; i < size; i++)
479 >            elementData[i] = null;
480  
481 <        size = 0;
481 >        size = 0;
482      }
483  
484      /**
# Line 495 | Line 495 | public class ArrayList<E> extends Abstra
495       * @throws NullPointerException if the specified collection is null
496       */
497      public boolean addAll(Collection<? extends E> c) {
498 <        Object[] a = c.toArray();
498 >        Object[] a = c.toArray();
499          int numNew = a.length;
500 <        ensureCapacity(size + numNew);  // Increments modCount
500 >        ensureCapacity(size + numNew);  // Increments modCount
501          System.arraycopy(a, 0, elementData, size, numNew);
502          size += numNew;
503 <        return numNew != 0;
503 >        return numNew != 0;
504      }
505  
506      /**
# Line 519 | Line 519 | public class ArrayList<E> extends Abstra
519       * @throws NullPointerException if the specified collection is null
520       */
521      public boolean addAll(int index, Collection<? extends E> c) {
522 <        rangeCheckForAdd(index);
522 >        rangeCheckForAdd(index);
523  
524 <        Object[] a = c.toArray();
525 <        int numNew = a.length;
526 <        ensureCapacity(size + numNew);  // Increments modCount
527 <
528 <        int numMoved = size - index;
529 <        if (numMoved > 0)
530 <            System.arraycopy(elementData, index, elementData, index + numNew,
531 <                             numMoved);
524 >        Object[] a = c.toArray();
525 >        int numNew = a.length;
526 >        ensureCapacity(size + numNew);  // Increments modCount
527 >
528 >        int numMoved = size - index;
529 >        if (numMoved > 0)
530 >            System.arraycopy(elementData, index, elementData, index + numNew,
531 >                             numMoved);
532  
533          System.arraycopy(a, 0, elementData, index, numNew);
534 <        size += numNew;
535 <        return numNew != 0;
534 >        size += numNew;
535 >        return numNew != 0;
536      }
537  
538      /**
# Line 550 | Line 550 | public class ArrayList<E> extends Abstra
550       *          toIndex < fromIndex})
551       */
552      protected void removeRange(int fromIndex, int toIndex) {
553 <        modCount++;
554 <        int numMoved = size - toIndex;
553 >        modCount++;
554 >        int numMoved = size - toIndex;
555          System.arraycopy(elementData, toIndex, elementData, fromIndex,
556                           numMoved);
557  
558 <        // Let gc do its work
559 <        int newSize = size - (toIndex-fromIndex);
560 <        while (size != newSize)
561 <            elementData[--size] = null;
558 >        // Let gc do its work
559 >        int newSize = size - (toIndex-fromIndex);
560 >        while (size != newSize)
561 >            elementData[--size] = null;
562      }
563  
564      /**
# Line 576 | Line 576 | public class ArrayList<E> extends Abstra
576       * A version of rangeCheck used by add and addAll.
577       */
578      private void rangeCheckForAdd(int index) {
579 <        if (index > size || index < 0)
580 <            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
579 >        if (index > size || index < 0)
580 >            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
581      }
582  
583      /**
# Line 586 | Line 586 | public class ArrayList<E> extends Abstra
586       * this "outlining" performs best with both server and client VMs.
587       */
588      private String outOfBoundsMsg(int index) {
589 <        return "Index: "+index+", Size: "+size;
589 >        return "Index: "+index+", Size: "+size;
590      }
591  
592      /**
# Line 603 | Line 603 | public class ArrayList<E> extends Abstra
603       * @see Collection#contains(Object)
604       */
605      public boolean removeAll(Collection<?> c) {
606 <        return batchRemove(c, false);
606 >        return batchRemove(c, false);
607      }
608  
609      /**
# Line 621 | Line 621 | public class ArrayList<E> extends Abstra
621       * @see Collection#contains(Object)
622       */
623      public boolean retainAll(Collection<?> c) {
624 <        return batchRemove(c, true);
624 >        return batchRemove(c, true);
625      }
626  
627      private boolean batchRemove(Collection<?> c, boolean complement) {
628 <        final Object[] elementData = this.elementData;
629 <        int r = 0, w = 0;
630 <        boolean modified = false;
631 <        try {
632 <            for (; r < size; r++)
633 <                if (c.contains(elementData[r]) == complement)
634 <                    elementData[w++] = elementData[r];
635 <        } finally {
636 <            // Preserve behavioral compatibility with AbstractCollection,
637 <            // even if c.contains() throws.
638 <            if (r != size) {
639 <                System.arraycopy(elementData, r,
640 <                                 elementData, w,
641 <                                 size - r);
642 <                w += size - r;
643 <            }
644 <            if (w != size) {
645 <                for (int i = w; i < size; i++)
646 <                    elementData[i] = null;
647 <                modCount += size - w;
648 <                size = w;
649 <                modified = true;
650 <            }
651 <        }
652 <        return modified;
628 >        final Object[] elementData = this.elementData;
629 >        int r = 0, w = 0;
630 >        boolean modified = false;
631 >        try {
632 >            for (; r < size; r++)
633 >                if (c.contains(elementData[r]) == complement)
634 >                    elementData[w++] = elementData[r];
635 >        } finally {
636 >            // Preserve behavioral compatibility with AbstractCollection,
637 >            // even if c.contains() throws.
638 >            if (r != size) {
639 >                System.arraycopy(elementData, r,
640 >                                 elementData, w,
641 >                                 size - r);
642 >                w += size - r;
643 >            }
644 >            if (w != size) {
645 >                for (int i = w; i < size; i++)
646 >                    elementData[i] = null;
647 >                modCount += size - w;
648 >                size = w;
649 >                modified = true;
650 >            }
651 >        }
652 >        return modified;
653      }
654  
655      /**
# Line 662 | Line 662 | public class ArrayList<E> extends Abstra
662       */
663      private void writeObject(java.io.ObjectOutputStream s)
664          throws java.io.IOException{
665 <        // Write out element count, and any hidden stuff
666 <        int expectedModCount = modCount;
667 <        s.defaultWriteObject();
665 >        // Write out element count, and any hidden stuff
666 >        int expectedModCount = modCount;
667 >        s.defaultWriteObject();
668  
669          // Write out array length
670          s.writeInt(elementData.length);
671  
672 <        // Write out all elements in the proper order.
673 <        for (int i=0; i<size; i++)
672 >        // Write out all elements in the proper order.
673 >        for (int i=0; i<size; i++)
674              s.writeObject(elementData[i]);
675  
676 <        if (modCount != expectedModCount) {
676 >        if (modCount != expectedModCount) {
677              throw new ConcurrentModificationException();
678          }
679  
# Line 685 | Line 685 | public class ArrayList<E> extends Abstra
685       */
686      private void readObject(java.io.ObjectInputStream s)
687          throws java.io.IOException, ClassNotFoundException {
688 <        // Read in size, and any hidden stuff
689 <        s.defaultReadObject();
688 >        // Read in size, and any hidden stuff
689 >        s.defaultReadObject();
690  
691          // Read in array length and allocate array
692          int arrayLength = s.readInt();
693          Object[] a = elementData = new Object[arrayLength];
694  
695 <        // Read in all elements in the proper order.
696 <        for (int i=0; i<size; i++)
695 >        // Read in all elements in the proper order.
696 >        for (int i=0; i<size; i++)
697              a[i] = s.readObject();
698      }
699  
# Line 710 | Line 710 | public class ArrayList<E> extends Abstra
710       * @throws IndexOutOfBoundsException {@inheritDoc}
711       */
712      public ListIterator<E> listIterator(int index) {
713 <        if (index < 0 || index > size)
713 >        if (index < 0 || index > size)
714              throw new IndexOutOfBoundsException("Index: "+index);
715 <        return new ListItr(index);
715 >        return new ListItr(index);
716      }
717  
718      /**
# Line 724 | Line 724 | public class ArrayList<E> extends Abstra
724       * @see #listIterator(int)
725       */
726      public ListIterator<E> listIterator() {
727 <        return new ListItr(0);
727 >        return new ListItr(0);
728      }
729  
730      /**
# Line 735 | Line 735 | public class ArrayList<E> extends Abstra
735       * @return an iterator over the elements in this list in proper sequence
736       */
737      public Iterator<E> iterator() {
738 <        return new Itr();
738 >        return new Itr();
739      }
740  
741      /**
742       * An optimized version of AbstractList.Itr
743       */
744      private class Itr implements Iterator<E> {
745 <        int cursor;       // index of next element to return
746 <        int lastRet = -1; // index of last element returned; -1 if no such
747 <        int expectedModCount = modCount;
745 >        int cursor;       // index of next element to return
746 >        int lastRet = -1; // index of last element returned; -1 if no such
747 >        int expectedModCount = modCount;
748  
749 <        public boolean hasNext() {
749 >        public boolean hasNext() {
750              return cursor != size;
751 <        }
751 >        }
752 >
753 >        @SuppressWarnings("unchecked")
754 >        public E next() {
755 >            checkForComodification();
756 >            int i = cursor;
757 >            if (i >= size)
758 >                throw new NoSuchElementException();
759 >            Object[] elementData = ArrayList.this.elementData;
760 >            if (i >= elementData.length)
761 >                throw new ConcurrentModificationException();
762 >            cursor = i + 1;
763 >            return (E) elementData[lastRet = i];
764 >        }
765  
766 <        @SuppressWarnings("unchecked")
767 <        public E next() {
766 >        public void remove() {
767 >            if (lastRet < 0)
768 >                throw new IllegalStateException();
769              checkForComodification();
770 <            int i = cursor;
771 <            if (i >= size)
772 <                throw new NoSuchElementException();
773 <            Object[] elementData = ArrayList.this.elementData;
774 <            if (i >= elementData.length)
775 <                throw new ConcurrentModificationException();
776 <            cursor = i + 1;
777 <            return (E) elementData[lastRet = i];
778 <        }
779 <
780 <        public void remove() {
781 <            if (lastRet < 0)
782 <                throw new IllegalStateException();
783 <            checkForComodification();
784 <
771 <            try {
772 <                ArrayList.this.remove(lastRet);
773 <                cursor = lastRet;
774 <                lastRet = -1;
775 <                expectedModCount = modCount;
776 <            } catch (IndexOutOfBoundsException ex) {
777 <                throw new ConcurrentModificationException();
778 <            }
779 <        }
780 <
781 <        final void checkForComodification() {
782 <            if (modCount != expectedModCount)
783 <                throw new ConcurrentModificationException();
784 <        }
770 >
771 >            try {
772 >                ArrayList.this.remove(lastRet);
773 >                cursor = lastRet;
774 >                lastRet = -1;
775 >                expectedModCount = modCount;
776 >            } catch (IndexOutOfBoundsException ex) {
777 >                throw new ConcurrentModificationException();
778 >            }
779 >        }
780 >
781 >        final void checkForComodification() {
782 >            if (modCount != expectedModCount)
783 >                throw new ConcurrentModificationException();
784 >        }
785      }
786  
787      /**
788       * An optimized version of AbstractList.ListItr
789       */
790      private class ListItr extends Itr implements ListIterator<E> {
791 <        ListItr(int index) {
792 <            super();
793 <            cursor = index;
794 <        }
795 <
796 <        public boolean hasPrevious() {
797 <            return cursor != 0;
798 <        }
799 <
800 <        public int nextIndex() {
801 <            return cursor;
802 <        }
803 <
804 <        public int previousIndex() {
805 <            return cursor - 1;
806 <        }
791 >        ListItr(int index) {
792 >            super();
793 >            cursor = index;
794 >        }
795 >
796 >        public boolean hasPrevious() {
797 >            return cursor != 0;
798 >        }
799  
800 <        @SuppressWarnings("unchecked")
800 >        public int nextIndex() {
801 >            return cursor;
802 >        }
803 >
804 >        public int previousIndex() {
805 >            return cursor - 1;
806 >        }
807 >
808 >        @SuppressWarnings("unchecked")
809          public E previous() {
810 <            checkForComodification();
811 <            int i = cursor - 1;
812 <            if (i < 0)
813 <                throw new NoSuchElementException();
814 <            Object[] elementData = ArrayList.this.elementData;
815 <            if (i >= elementData.length)
816 <                throw new ConcurrentModificationException();
817 <            cursor = i;
818 <            return (E) elementData[lastRet = i];
819 <        }
820 <
821 <        public void set(E e) {
822 <            if (lastRet < 0)
823 <                throw new IllegalStateException();
824 <            checkForComodification();
825 <
826 <            try {
827 <                ArrayList.this.set(lastRet, e);
828 <            } catch (IndexOutOfBoundsException ex) {
829 <                throw new ConcurrentModificationException();
830 <            }
831 <        }
832 <
833 <        public void add(E e) {
834 <            checkForComodification();
835 <
836 <            try {
837 <                int i = cursor;
838 <                ArrayList.this.add(i, e);
839 <                cursor = i + 1;
840 <                lastRet = -1;
841 <                expectedModCount = modCount;
842 <            } catch (IndexOutOfBoundsException ex) {
843 <                throw new ConcurrentModificationException();
844 <            }
845 <        }
810 >            checkForComodification();
811 >            int i = cursor - 1;
812 >            if (i < 0)
813 >                throw new NoSuchElementException();
814 >            Object[] elementData = ArrayList.this.elementData;
815 >            if (i >= elementData.length)
816 >                throw new ConcurrentModificationException();
817 >            cursor = i;
818 >            return (E) elementData[lastRet = i];
819 >        }
820 >
821 >        public void set(E e) {
822 >            if (lastRet < 0)
823 >                throw new IllegalStateException();
824 >            checkForComodification();
825 >
826 >            try {
827 >                ArrayList.this.set(lastRet, e);
828 >            } catch (IndexOutOfBoundsException ex) {
829 >                throw new ConcurrentModificationException();
830 >            }
831 >        }
832 >
833 >        public void add(E e) {
834 >            checkForComodification();
835 >
836 >            try {
837 >                int i = cursor;
838 >                ArrayList.this.add(i, e);
839 >                cursor = i + 1;
840 >                lastRet = -1;
841 >                expectedModCount = modCount;
842 >            } catch (IndexOutOfBoundsException ex) {
843 >                throw new ConcurrentModificationException();
844 >            }
845 >        }
846      }
847  
848      /**
# Line 875 | Line 875 | public class ArrayList<E> extends Abstra
875       * @throws IllegalArgumentException {@inheritDoc}
876       */
877      public List<E> subList(int fromIndex, int toIndex) {
878 <        subListRangeCheck(fromIndex, toIndex, size);
878 >        subListRangeCheck(fromIndex, toIndex, size);
879          return new SubList(this, 0, fromIndex, toIndex);
880      }
881  
882      static void subListRangeCheck(int fromIndex, int toIndex, int size) {
883 <        if (fromIndex < 0)
884 <            throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
885 <        if (toIndex > size)
886 <            throw new IndexOutOfBoundsException("toIndex = " + toIndex);
887 <        if (fromIndex > toIndex)
888 <            throw new IllegalArgumentException("fromIndex(" + fromIndex +
889 <                                               ") > toIndex(" + toIndex + ")");
883 >        if (fromIndex < 0)
884 >            throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
885 >        if (toIndex > size)
886 >            throw new IndexOutOfBoundsException("toIndex = " + toIndex);
887 >        if (fromIndex > toIndex)
888 >            throw new IllegalArgumentException("fromIndex(" + fromIndex +
889 >                                               ") > toIndex(" + toIndex + ")");
890      }
891  
892      private class SubList extends AbstractList<E> implements RandomAccess {
893 <        private final AbstractList<E> parent;
894 <        private final int parentOffset;
895 <        private final int offset;
896 <        private int size;
897 <
898 <        SubList(AbstractList<E> parent,
899 <                int offset, int fromIndex, int toIndex) {
900 <            this.parent = parent;
901 <            this.parentOffset = fromIndex;
902 <            this.offset = offset + fromIndex;
903 <            this.size = toIndex - fromIndex;
904 <            this.modCount = ArrayList.this.modCount;
905 <        }
906 <
907 <        public E set(int index, E e) {
908 <            rangeCheck(index);
909 <            checkForComodification();
910 <            E oldValue = ArrayList.this.elementData(offset + index);
911 <            ArrayList.this.elementData[offset + index] = e;
912 <            return oldValue;
913 <        }
914 <
915 <        public E get(int index) {
916 <            rangeCheck(index);
917 <            checkForComodification();
918 <            return ArrayList.this.elementData(offset + index);
919 <        }
920 <
921 <        public int size() {
922 <            checkForComodification();
923 <            return this.size;
924 <        }
925 <
926 <        public void add(int index, E e) {
927 <            rangeCheckForAdd(index);
928 <            checkForComodification();
929 <            parent.add(parentOffset + index, e);
930 <            this.modCount = parent.modCount;
931 <            this.size++;
932 <        }
933 <
934 <        public E remove(int index) {
935 <            rangeCheck(index);
936 <            checkForComodification();
937 <            E result = parent.remove(parentOffset + index);
938 <            this.modCount = parent.modCount;
939 <            this.size--;
940 <            return result;
941 <        }
942 <
943 <        protected void removeRange(int fromIndex, int toIndex) {
944 <            checkForComodification();
945 <            parent.removeRange(parentOffset + fromIndex,
946 <                               parentOffset + toIndex);
947 <            this.modCount = parent.modCount;
948 <            this.size -= toIndex - fromIndex;
949 <        }
950 <
951 <        public boolean addAll(Collection<? extends E> c) {
952 <            return addAll(this.size, c);
953 <        }
954 <
955 <        public boolean addAll(int index, Collection<? extends E> c) {
956 <            rangeCheckForAdd(index);
957 <            int cSize = c.size();
958 <            if (cSize==0)
959 <                return false;
960 <
961 <            checkForComodification();
962 <            parent.addAll(parentOffset + index, c);
963 <            this.modCount = parent.modCount;
964 <            this.size += cSize;
965 <            return true;
966 <        }
967 <
968 <        public Iterator<E> iterator() {
969 <            return listIterator();
970 <        }
971 <
972 <        public ListIterator<E> listIterator(final int index) {
973 <            checkForComodification();
974 <            rangeCheckForAdd(index);
975 <
976 <            return new ListIterator<E>() {
977 <                int cursor = index;
978 <                int lastRet = -1;
979 <                int expectedModCount = ArrayList.this.modCount;
980 <
981 <                public boolean hasNext() {
982 <                    return cursor != SubList.this.size;
983 <                }
984 <
985 <                @SuppressWarnings("unchecked")
986 <                public E next() {
987 <                    checkForComodification();
988 <                    int i = cursor;
989 <                    if (i >= SubList.this.size)
990 <                        throw new NoSuchElementException();
991 <                    Object[] elementData = ArrayList.this.elementData;
992 <                    if (offset + i >= elementData.length)
993 <                        throw new ConcurrentModificationException();
994 <                    cursor = i + 1;
995 <                    return (E) elementData[offset + (lastRet = i)];
996 <                }
997 <
998 <                public boolean hasPrevious() {
999 <                    return cursor != 0;
1000 <                }
1001 <
1002 <                @SuppressWarnings("unchecked")
1003 <                public E previous() {
1004 <                    checkForComodification();
1005 <                    int i = cursor - 1;
1006 <                    if (i < 0)
1007 <                        throw new NoSuchElementException();
1008 <                    Object[] elementData = ArrayList.this.elementData;
1009 <                    if (offset + i >= elementData.length)
1010 <                        throw new ConcurrentModificationException();
1011 <                    cursor = i;
1012 <                    return (E) elementData[offset + (lastRet = i)];
1013 <                }
1014 <
1015 <                public int nextIndex() {
1016 <                    return cursor;
1017 <                }
1018 <
1019 <                public int previousIndex() {
1020 <                    return cursor - 1;
1021 <                }
1022 <
1023 <                public void remove() {
1024 <                    if (lastRet < 0)
1025 <                        throw new IllegalStateException();
1026 <                    checkForComodification();
1027 <
1028 <                    try {
1029 <                        SubList.this.remove(lastRet);
1030 <                        cursor = lastRet;
1031 <                        lastRet = -1;
1032 <                        expectedModCount = ArrayList.this.modCount;
1033 <                    } catch (IndexOutOfBoundsException ex) {
1034 <                        throw new ConcurrentModificationException();
1035 <                    }
1036 <                }
1037 <
1038 <                public void set(E e) {
1039 <                    if (lastRet < 0)
1040 <                        throw new IllegalStateException();
1041 <                    checkForComodification();
1042 <
1043 <                    try {
1044 <                        ArrayList.this.set(offset + lastRet, e);
1045 <                    } catch (IndexOutOfBoundsException ex) {
1046 <                        throw new ConcurrentModificationException();
1047 <                    }
1048 <                }
1049 <
1050 <                public void add(E e) {
1051 <                    checkForComodification();
1052 <
1053 <                    try {
1054 <                        int i = cursor;
1055 <                        SubList.this.add(i, e);
1056 <                        cursor = i + 1;
1057 <                        lastRet = -1;
1058 <                        expectedModCount = ArrayList.this.modCount;
1059 <                    } catch (IndexOutOfBoundsException ex) {
1060 <                        throw new ConcurrentModificationException();
1061 <                    }
1062 <                }
1063 <
1064 <                final void checkForComodification() {
1065 <                    if (expectedModCount != ArrayList.this.modCount)
1066 <                        throw new ConcurrentModificationException();
1067 <                }
1068 <            };
1069 <        }
1070 <
1071 <        public List<E> subList(int fromIndex, int toIndex) {
1072 <            subListRangeCheck(fromIndex, toIndex, size);
1073 <            return new SubList(this, offset, fromIndex, toIndex);
1074 <        }
1075 <
1076 <        private void rangeCheck(int index) {
1077 <            if (index < 0 || index >= this.size)
1078 <                throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
1079 <        }
1080 <
1081 <        private void rangeCheckForAdd(int index) {
1082 <            if (index < 0 || index > this.size)
1083 <                throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
1084 <        }
1085 <
1086 <        private String outOfBoundsMsg(int index) {
1087 <            return "Index: "+index+", Size: "+this.size;
1088 <        }
1089 <
1090 <        private void checkForComodification() {
1091 <            if (ArrayList.this.modCount != this.modCount)
1092 <                throw new ConcurrentModificationException();
1093 <        }
893 >        private final AbstractList<E> parent;
894 >        private final int parentOffset;
895 >        private final int offset;
896 >        private int size;
897 >
898 >        SubList(AbstractList<E> parent,
899 >                int offset, int fromIndex, int toIndex) {
900 >            this.parent = parent;
901 >            this.parentOffset = fromIndex;
902 >            this.offset = offset + fromIndex;
903 >            this.size = toIndex - fromIndex;
904 >            this.modCount = ArrayList.this.modCount;
905 >        }
906 >
907 >        public E set(int index, E e) {
908 >            rangeCheck(index);
909 >            checkForComodification();
910 >            E oldValue = ArrayList.this.elementData(offset + index);
911 >            ArrayList.this.elementData[offset + index] = e;
912 >            return oldValue;
913 >        }
914 >
915 >        public E get(int index) {
916 >            rangeCheck(index);
917 >            checkForComodification();
918 >            return ArrayList.this.elementData(offset + index);
919 >        }
920 >
921 >        public int size() {
922 >            checkForComodification();
923 >            return this.size;
924 >        }
925 >
926 >        public void add(int index, E e) {
927 >            rangeCheckForAdd(index);
928 >            checkForComodification();
929 >            parent.add(parentOffset + index, e);
930 >            this.modCount = parent.modCount;
931 >            this.size++;
932 >        }
933 >
934 >        public E remove(int index) {
935 >            rangeCheck(index);
936 >            checkForComodification();
937 >            E result = parent.remove(parentOffset + index);
938 >            this.modCount = parent.modCount;
939 >            this.size--;
940 >            return result;
941 >        }
942 >
943 >        protected void removeRange(int fromIndex, int toIndex) {
944 >            checkForComodification();
945 >            parent.removeRange(parentOffset + fromIndex,
946 >                               parentOffset + toIndex);
947 >            this.modCount = parent.modCount;
948 >            this.size -= toIndex - fromIndex;
949 >        }
950 >
951 >        public boolean addAll(Collection<? extends E> c) {
952 >            return addAll(this.size, c);
953 >        }
954 >
955 >        public boolean addAll(int index, Collection<? extends E> c) {
956 >            rangeCheckForAdd(index);
957 >            int cSize = c.size();
958 >            if (cSize==0)
959 >                return false;
960 >
961 >            checkForComodification();
962 >            parent.addAll(parentOffset + index, c);
963 >            this.modCount = parent.modCount;
964 >            this.size += cSize;
965 >            return true;
966 >        }
967 >
968 >        public Iterator<E> iterator() {
969 >            return listIterator();
970 >        }
971 >
972 >        public ListIterator<E> listIterator(final int index) {
973 >            checkForComodification();
974 >            rangeCheckForAdd(index);
975 >
976 >            return new ListIterator<E>() {
977 >                int cursor = index;
978 >                int lastRet = -1;
979 >                int expectedModCount = ArrayList.this.modCount;
980 >
981 >                public boolean hasNext() {
982 >                    return cursor != SubList.this.size;
983 >                }
984 >
985 >                @SuppressWarnings("unchecked")
986 >                public E next() {
987 >                    checkForComodification();
988 >                    int i = cursor;
989 >                    if (i >= SubList.this.size)
990 >                        throw new NoSuchElementException();
991 >                    Object[] elementData = ArrayList.this.elementData;
992 >                    if (offset + i >= elementData.length)
993 >                        throw new ConcurrentModificationException();
994 >                    cursor = i + 1;
995 >                    return (E) elementData[offset + (lastRet = i)];
996 >                }
997 >
998 >                public boolean hasPrevious() {
999 >                    return cursor != 0;
1000 >                }
1001 >
1002 >                @SuppressWarnings("unchecked")
1003 >                public E previous() {
1004 >                    checkForComodification();
1005 >                    int i = cursor - 1;
1006 >                    if (i < 0)
1007 >                        throw new NoSuchElementException();
1008 >                    Object[] elementData = ArrayList.this.elementData;
1009 >                    if (offset + i >= elementData.length)
1010 >                        throw new ConcurrentModificationException();
1011 >                    cursor = i;
1012 >                    return (E) elementData[offset + (lastRet = i)];
1013 >                }
1014 >
1015 >                public int nextIndex() {
1016 >                    return cursor;
1017 >                }
1018 >
1019 >                public int previousIndex() {
1020 >                    return cursor - 1;
1021 >                }
1022 >
1023 >                public void remove() {
1024 >                    if (lastRet < 0)
1025 >                        throw new IllegalStateException();
1026 >                    checkForComodification();
1027 >
1028 >                    try {
1029 >                        SubList.this.remove(lastRet);
1030 >                        cursor = lastRet;
1031 >                        lastRet = -1;
1032 >                        expectedModCount = ArrayList.this.modCount;
1033 >                    } catch (IndexOutOfBoundsException ex) {
1034 >                        throw new ConcurrentModificationException();
1035 >                    }
1036 >                }
1037 >
1038 >                public void set(E e) {
1039 >                    if (lastRet < 0)
1040 >                        throw new IllegalStateException();
1041 >                    checkForComodification();
1042 >
1043 >                    try {
1044 >                        ArrayList.this.set(offset + lastRet, e);
1045 >                    } catch (IndexOutOfBoundsException ex) {
1046 >                        throw new ConcurrentModificationException();
1047 >                    }
1048 >                }
1049 >
1050 >                public void add(E e) {
1051 >                    checkForComodification();
1052 >
1053 >                    try {
1054 >                        int i = cursor;
1055 >                        SubList.this.add(i, e);
1056 >                        cursor = i + 1;
1057 >                        lastRet = -1;
1058 >                        expectedModCount = ArrayList.this.modCount;
1059 >                    } catch (IndexOutOfBoundsException ex) {
1060 >                        throw new ConcurrentModificationException();
1061 >                    }
1062 >                }
1063 >
1064 >                final void checkForComodification() {
1065 >                    if (expectedModCount != ArrayList.this.modCount)
1066 >                        throw new ConcurrentModificationException();
1067 >                }
1068 >            };
1069 >        }
1070 >
1071 >        public List<E> subList(int fromIndex, int toIndex) {
1072 >            subListRangeCheck(fromIndex, toIndex, size);
1073 >            return new SubList(this, offset, fromIndex, toIndex);
1074 >        }
1075 >
1076 >        private void rangeCheck(int index) {
1077 >            if (index < 0 || index >= this.size)
1078 >                throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
1079 >        }
1080 >
1081 >        private void rangeCheckForAdd(int index) {
1082 >            if (index < 0 || index > this.size)
1083 >                throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
1084 >        }
1085 >
1086 >        private String outOfBoundsMsg(int index) {
1087 >            return "Index: "+index+", Size: "+this.size;
1088 >        }
1089 >
1090 >        private void checkForComodification() {
1091 >            if (ArrayList.this.modCount != this.modCount)
1092 >                throw new ConcurrentModificationException();
1093 >        }
1094      }
1095   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines