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.29 by jsr166, Tue Jul 21 23:56:46 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines