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.6 by jsr166, Sat Nov 26 04:35:16 2005 UTC vs.
Revision 1.14 by jsr166, Mon Dec 5 02:56:59 2005 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  
# Line 189 | Line 189 | public class ArrayList<E> extends Abstra
189       * @param minCapacity the desired minimum capacity
190       */
191      private void growArray(int minCapacity) {
192 +        if (minCapacity < 0) // overflow
193 +            throw new OutOfMemoryError();
194          int oldCapacity = elementData.length;
195          // Double size if small; else grow by 50%
196          int newCapacity = ((oldCapacity < 64)?
197 <                           (oldCapacity * 2):
198 <                           ((oldCapacity * 3)/2 + 1));
197 >                           ((oldCapacity + 1) * 2):
198 >                           ((oldCapacity / 2) * 3));
199 >        if (newCapacity < 0) // overflow
200 >            newCapacity = Integer.MAX_VALUE;
201          if (newCapacity < minCapacity)
202              newCapacity = minCapacity;
203          elementData = Arrays.copyOf(elementData, newCapacity);
# Line 343 | Line 347 | public class ArrayList<E> extends Abstra
347      // Positional Access Operations
348  
349      /**
350 <     * Creates and returns an appropriate exception for indexing errors.
350 >     * Returns error message string for IndexOutOfBoundsExceptions
351       */
352 <    private static IndexOutOfBoundsException rangeException(int i, int s) {
353 <        return new IndexOutOfBoundsException("Index: " + i + ", Size: " + s);
352 >    private String ioobe(int index) {
353 >        return "Index: " + index + ", Size: " + size;
354      }
355  
356      /**
# Line 358 | Line 362 | public class ArrayList<E> extends Abstra
362       */
363      public E get(int index) {
364          if (index >= size)
365 <            throw rangeException(index, size);
365 >            throw new IndexOutOfBoundsException(ioobe(index));
366          return (E)elementData[index];
367      }
368  
# Line 373 | Line 377 | public class ArrayList<E> extends Abstra
377       */
378      public E set(int index, E element) {
379          if (index >= size)
380 <             throw rangeException(index, size);
380 >            throw new IndexOutOfBoundsException(ioobe(index));
381  
382          E oldValue = (E) elementData[index];
383          elementData[index] = element;
# Line 387 | Line 391 | public class ArrayList<E> extends Abstra
391       * @return <tt>true</tt> (as specified by {@link Collection#add})
392       */
393      public boolean add(E e) {
394 <        ++modCount;
395 <        int s = size++;
394 >        modCount++;
395 >        int s = size;
396          if (s >= elementData.length)
397              growArray(s + 1);
398          elementData[s] = e;
399 +        size = s + 1;
400          return true;
401      }
402  
# Line 407 | Line 412 | public class ArrayList<E> extends Abstra
412      public void add(int index, E element) {
413          int s = size;
414          if (index > s || index < 0)
415 <            throw rangeException(index, s);
416 <        ++modCount;
412 <        size = s + 1;
415 >            throw new IndexOutOfBoundsException(ioobe(index));
416 >        modCount++;
417          if (s >= elementData.length)
418              growArray(s + 1);
419 <        System.arraycopy(elementData, index, elementData, index + 1,
420 <                         s - index);
419 >        System.arraycopy(elementData, index,
420 >                         elementData, index + 1, s - index);
421          elementData[index] = element;
422 +        size = s + 1;
423      }
424  
425      /**
# Line 429 | Line 434 | public class ArrayList<E> extends Abstra
434      public E remove(int index) {
435          int s = size - 1;
436          if (index > s)
437 <            throw rangeException(index, size);
433 <        size = s;
437 >            throw new IndexOutOfBoundsException(ioobe(index));
438          modCount++;
439 <        Object oldValue = elementData[index];
439 >        E oldValue = (E)elementData[index];
440          int numMoved = s - index;
441          if (numMoved > 0)
442 <            System.arraycopy(elementData, index+1, elementData, index,
443 <                             numMoved);
444 <        elementData[s] = null; // forget removed element
445 <        return (E)oldValue;
442 >            System.arraycopy(elementData, index + 1,
443 >                             elementData, index, numMoved);
444 >        elementData[s] = null;
445 >        size = s;
446 >        return oldValue;
447      }
448  
449      /**
# Line 537 | Line 542 | public class ArrayList<E> extends Abstra
542       */
543      public boolean addAll(int index, Collection<? extends E> c) {
544          if (index > size || index < 0)
545 <            throw new IndexOutOfBoundsException(
541 <                "Index: " + index + ", Size: " + size);
545 >            throw new IndexOutOfBoundsException(ioobe(index));
546  
547          Object[] a = c.toArray();
548          int numNew = a.length;
# Line 600 | Line 604 | public class ArrayList<E> extends Abstra
604          for (int i=0; i<size; i++)
605              s.writeObject(elementData[i]);
606  
607 <        if (modCount != expectedModCount) {
607 >        if (expectedModCount != modCount) {
608              throw new ConcurrentModificationException();
609          }
610  
# Line 648 | Line 652 | public class ArrayList<E> extends Abstra
652       */
653      public ListIterator<E> listIterator(int index) {
654          if (index < 0 || index > size)
655 <            throw new IndexOutOfBoundsException("Index: "+index);
655 >            throw new IndexOutOfBoundsException(ioobe(index));
656          return new ArrayListIterator(index);
657      }
658  
659      /**
660 +     * {@inheritDoc}
661 +     */
662 +    public ListIterator<E> listIterator() {
663 +        return new ArrayListIterator(0);
664 +    }
665 +
666 +    /**
667       * Returns an iterator over the elements in this list in proper sequence.
668       *
669       * @return an iterator over the elements in this list in proper sequence
# Line 662 | Line 673 | public class ArrayList<E> extends Abstra
673      }
674  
675      /**
676 <     * A streamlined version of AbstractList.Itr
676 >     * A streamlined version of AbstractList.ListItr
677       */
678      final class ArrayListIterator implements ListIterator<E> {
679          int cursor;           // index of next element to return;
# Line 676 | Line 687 | public class ArrayList<E> extends Abstra
687          }
688  
689          public boolean hasNext() {
690 <            return cursor < size;
690 >            return cursor != size;
691          }
692  
693          public boolean hasPrevious() {
694 <            return cursor > 0;
694 >            return cursor != 0;
695          }
696  
697          public int nextIndex() {
# Line 692 | Line 703 | public class ArrayList<E> extends Abstra
703          }
704  
705          public E next() {
706 <            if (expectedModCount == modCount) {
706 >            try {
707                  int i = cursor;
708 <                if (i < size) {
709 <                    try {
710 <                        E e = (E)elementData[i];
711 <                        lastRet = i;
712 <                        cursor = i + 1;
702 <                        return e;
703 <                    } catch (IndexOutOfBoundsException fallthrough) {
704 <                    }
705 <                }
706 <            }
707 <            // Prefer reporting CME if applicable on failures
708 <            if (expectedModCount == modCount)
708 >                E next = get(i);
709 >                lastRet = i;
710 >                cursor = i + 1;
711 >                return next;
712 >            } catch (IndexOutOfBoundsException ex) {
713                  throw new NoSuchElementException();
714 <            throw new ConcurrentModificationException();
714 >            } finally {
715 >                if (expectedModCount != modCount)
716 >                    throw new ConcurrentModificationException();
717 >            }
718          }
719  
720          public E previous() {
721 <            if (expectedModCount == modCount) {
721 >            try {
722                  int i = cursor - 1;
723 <                if (i < size) {
724 <                    try {
725 <                        E e = (E)elementData[i];
726 <                        lastRet = i;
727 <                        cursor = i;
721 <                        return e;
722 <                    } catch (IndexOutOfBoundsException fallthrough) {
723 <                    }
724 <                }
725 <            }
726 <            if (expectedModCount == modCount)
723 >                E prev = get(i);
724 >                lastRet = i;
725 >                cursor = i;
726 >                return prev;
727 >            } catch (IndexOutOfBoundsException ex) {
728                  throw new NoSuchElementException();
729 <            throw new ConcurrentModificationException();
729 >            } finally {
730 >                if (expectedModCount != modCount)
731 >                    throw new ConcurrentModificationException();
732 >            }
733          }
734  
735          public void remove() {
736              if (lastRet < 0)
737                  throw new IllegalStateException();
738 <            if (modCount != expectedModCount)
738 >            if (expectedModCount != modCount)
739                  throw new ConcurrentModificationException();
740              ArrayList.this.remove(lastRet);
741              if (lastRet < cursor)
# Line 743 | Line 747 | public class ArrayList<E> extends Abstra
747          public void set(E e) {
748              if (lastRet < 0)
749                  throw new IllegalStateException();
750 <            if (modCount != expectedModCount)
750 >            if (expectedModCount != modCount)
751                  throw new ConcurrentModificationException();
752              ArrayList.this.set(lastRet, e);
753              expectedModCount = modCount;
754          }
755  
756          public void add(E e) {
757 <            if (modCount != expectedModCount)
757 >            if (expectedModCount != modCount)
758                  throw new ConcurrentModificationException();
759 <            ArrayList.this.add(cursor++, e);
760 <            lastRet = -1;
761 <            expectedModCount = modCount;
759 >            try {
760 >                ArrayList.this.add(cursor++, e);
761 >                lastRet = -1;
762 >                expectedModCount = modCount;
763 >            } catch (IndexOutOfBoundsException ex) {
764 >                throw new ConcurrentModificationException();
765 >            }
766          }
767      }
760
768   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines