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.28 by jsr166, Mon May 19 00:32:45 2008 UTC

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