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.8 by jsr166, Sat Nov 26 20:39:51 2005 UTC vs.
Revision 1.16 by jsr166, Tue Feb 7 20:54:24 2006 UTC

# Line 1 | Line 1
1   /*
2   * %W% %E%
3   *
4 < * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
4 > * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5   * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6   */
7  
8   package java.util;
9 import java.util.*; // for javadoc (till 6280605 is fixed)
9  
10   /**
11   * Resizable-array implementation of the <tt>List</tt> interface.  Implements
# Line 189 | Line 188 | public class ArrayList<E> extends Abstra
188       * @param minCapacity the desired minimum capacity
189       */
190      private void growArray(int minCapacity) {
191 <        if (minCapacity < 0) throw new OutOfMemoryError(); // int overflow
191 >        if (minCapacity < 0) // overflow
192 >            throw new OutOfMemoryError();
193          int oldCapacity = elementData.length;
194          // Double size if small; else grow by 50%
195          int newCapacity = ((oldCapacity < 64)?
196 <                           (oldCapacity * 2):
197 <                           ((oldCapacity * 3)/2));
196 >                           ((oldCapacity + 1) * 2):
197 >                           ((oldCapacity / 2) * 3));
198 >        if (newCapacity < 0) // overflow
199 >            newCapacity = Integer.MAX_VALUE;
200          if (newCapacity < minCapacity)
201              newCapacity = minCapacity;
202          elementData = Arrays.copyOf(elementData, newCapacity);
# Line 344 | Line 346 | public class ArrayList<E> extends Abstra
346      // Positional Access Operations
347  
348      /**
349 <     * Throws an appropriate exception for indexing errors.
349 >     * Returns error message string for IndexOutOfBoundsExceptions
350       */
351 <    private static void rangeException(int i, int s) {
352 <        throw new IndexOutOfBoundsException("Index: " + i + ", Size: " + s);
351 >    private String ioobe(int index) {
352 >        return "Index: " + index + ", Size: " + size;
353      }
354  
355      /**
# Line 359 | Line 361 | public class ArrayList<E> extends Abstra
361       */
362      public E get(int index) {
363          if (index >= size)
364 <            rangeException(index, size);
364 >            throw new IndexOutOfBoundsException(ioobe(index));
365          return (E)elementData[index];
366      }
367  
# Line 374 | Line 376 | public class ArrayList<E> extends Abstra
376       */
377      public E set(int index, E element) {
378          if (index >= size)
379 <            rangeException(index, size);
379 >            throw new IndexOutOfBoundsException(ioobe(index));
380  
381          E oldValue = (E) elementData[index];
382          elementData[index] = element;
# Line 409 | Line 411 | public class ArrayList<E> extends Abstra
411      public void add(int index, E element) {
412          int s = size;
413          if (index > s || index < 0)
414 <            rangeException(index, s);
414 >            throw new IndexOutOfBoundsException(ioobe(index));
415          modCount++;
416          if (s >= elementData.length)
417              growArray(s + 1);
418 <        System.arraycopy(elementData, index,
418 >        System.arraycopy(elementData, index,
419                           elementData, index + 1, s - index);
420          elementData[index] = element;
421          size = s + 1;
# Line 431 | Line 433 | public class ArrayList<E> extends Abstra
433      public E remove(int index) {
434          int s = size - 1;
435          if (index > s)
436 <            rangeException(index, size);
436 >            throw new IndexOutOfBoundsException(ioobe(index));
437          modCount++;
438          E oldValue = (E)elementData[index];
439          int numMoved = s - index;
440          if (numMoved > 0)
441 <            System.arraycopy(elementData, index + 1,
441 >            System.arraycopy(elementData, index + 1,
442                               elementData, index, numMoved);
443          elementData[s] = null;
444          size = s;
# Line 539 | Line 541 | public class ArrayList<E> extends Abstra
541       */
542      public boolean addAll(int index, Collection<? extends E> c) {
543          if (index > size || index < 0)
544 <            throw new IndexOutOfBoundsException(
543 <                "Index: " + index + ", Size: " + size);
544 >            throw new IndexOutOfBoundsException(ioobe(index));
545  
546          Object[] a = c.toArray();
547          int numNew = a.length;
# Line 602 | Line 603 | public class ArrayList<E> extends Abstra
603          for (int i=0; i<size; i++)
604              s.writeObject(elementData[i]);
605  
606 <        if (modCount != expectedModCount) {
606 >        if (expectedModCount != modCount) {
607              throw new ConcurrentModificationException();
608          }
609  
# Line 650 | Line 651 | public class ArrayList<E> extends Abstra
651       */
652      public ListIterator<E> listIterator(int index) {
653          if (index < 0 || index > size)
654 <            throw new IndexOutOfBoundsException("Index: "+index);
654 >            throw new IndexOutOfBoundsException(ioobe(index));
655          return new ArrayListIterator(index);
656      }
657  
658      /**
659 +     * {@inheritDoc}
660 +     */
661 +    public ListIterator<E> listIterator() {
662 +        return new ArrayListIterator(0);
663 +    }
664 +
665 +    /**
666       * Returns an iterator over the elements in this list in proper sequence.
667       *
668       * @return an iterator over the elements in this list in proper sequence
# Line 664 | Line 672 | public class ArrayList<E> extends Abstra
672      }
673  
674      /**
675 <     * A streamlined version of AbstractList.Itr
675 >     * A streamlined version of AbstractList.ListItr
676       */
677      final class ArrayListIterator implements ListIterator<E> {
678          int cursor;           // index of next element to return;
# Line 678 | Line 686 | public class ArrayList<E> extends Abstra
686          }
687  
688          public boolean hasNext() {
689 <            return cursor < size;
689 >            return cursor != size;
690          }
691  
692          public boolean hasPrevious() {
693 <            return cursor > 0;
693 >            return cursor != 0;
694          }
695  
696          public int nextIndex() {
# Line 694 | Line 702 | public class ArrayList<E> extends Abstra
702          }
703  
704          public E next() {
705 <            if (expectedModCount == modCount) {
705 >            try {
706                  int i = cursor;
707 <                if (i < size) {
708 <                    try {
709 <                        E e = (E)elementData[i];
710 <                        lastRet = i;
711 <                        cursor = i + 1;
704 <                        return e;
705 <                    } catch (IndexOutOfBoundsException fallthrough) {
706 <                    }
707 <                }
708 <            }
709 <            // Prefer reporting CME if applicable on failures
710 <            if (expectedModCount == modCount)
707 >                E next = get(i);
708 >                lastRet = i;
709 >                cursor = i + 1;
710 >                return next;
711 >            } catch (IndexOutOfBoundsException ex) {
712                  throw new NoSuchElementException();
713 <            throw new ConcurrentModificationException();
713 >            } finally {
714 >                if (expectedModCount != modCount)
715 >                    throw new ConcurrentModificationException();
716 >            }
717          }
718  
719          public E previous() {
720 <            if (expectedModCount == modCount) {
720 >            try {
721                  int i = cursor - 1;
722 <                if (i < size) {
723 <                    try {
724 <                        E e = (E)elementData[i];
725 <                        lastRet = i;
726 <                        cursor = i;
723 <                        return e;
724 <                    } catch (IndexOutOfBoundsException fallthrough) {
725 <                    }
726 <                }
727 <            }
728 <            if (expectedModCount == modCount)
722 >                E prev = get(i);
723 >                lastRet = i;
724 >                cursor = i;
725 >                return prev;
726 >            } catch (IndexOutOfBoundsException ex) {
727                  throw new NoSuchElementException();
728 <            throw new ConcurrentModificationException();
728 >            } finally {
729 >                if (expectedModCount != modCount)
730 >                    throw new ConcurrentModificationException();
731 >            }
732          }
733  
734          public void remove() {
735              if (lastRet < 0)
736                  throw new IllegalStateException();
737 <            if (modCount != expectedModCount)
737 >            if (expectedModCount != modCount)
738                  throw new ConcurrentModificationException();
739              ArrayList.this.remove(lastRet);
740              if (lastRet < cursor)
# Line 745 | Line 746 | public class ArrayList<E> extends Abstra
746          public void set(E e) {
747              if (lastRet < 0)
748                  throw new IllegalStateException();
749 <            if (modCount != expectedModCount)
749 >            if (expectedModCount != modCount)
750                  throw new ConcurrentModificationException();
751              ArrayList.this.set(lastRet, e);
752              expectedModCount = modCount;
753          }
754  
755          public void add(E e) {
756 <            if (modCount != expectedModCount)
756 >            if (expectedModCount != modCount)
757                  throw new ConcurrentModificationException();
758 <            ArrayList.this.add(cursor++, e);
759 <            lastRet = -1;
760 <            expectedModCount = modCount;
758 >            try {
759 >                ArrayList.this.add(cursor++, e);
760 >                lastRet = -1;
761 >                expectedModCount = modCount;
762 >            } catch (IndexOutOfBoundsException ex) {
763 >                throw new ConcurrentModificationException();
764 >            }
765          }
766      }
762
767   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines