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.21 by jsr166, Sun May 28 23:36:29 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 67 | Line 66 | import java.util.*; // for javadoc (till
66   * should be used only to detect bugs.</i><p>
67   *
68   * This class is a member of the
69 < * <a href="{@docRoot}/../guide/collections/index.html">
69 > * <a href="{@docRoot}/../technotes/guides/collections/index.html">
70   * Java Collections Framework</a>.
71   *
72   * @author  Josh Bloch
# Line 123 | Line 122 | public class ArrayList<E> extends Abstra
122      /**
123       * Constructs a list containing the elements of the specified
124       * collection, in the order they are returned by the collection's
125 <     * iterator.  The <tt>ArrayList</tt> instance has an initial capacity of
127 <     * 110% the size of the specified collection.
125 >     * iterator.
126       *
127       * @param c the collection whose elements are to be placed into this list
128       * @throws NullPointerException if the specified collection is null
129       */
130      public ArrayList(Collection<? extends E> c) {
131 <        int size = c.size();
132 <        // 10% for growth
133 <        int cap = ((size/10)+1)*11;
134 <        if (cap > 0) {
135 <            Object[] a = new Object[cap];
138 <            a[size] = a[size+1] = UNALLOCATED;
139 <            Object[] b = c.toArray(a);
140 <            if (b[size] == null && b[size+1] == UNALLOCATED) {
141 <                b[size+1] = null;
142 <                elementData = b;
143 <                this.size = size;
144 <                return;
145 <            }
146 <        }
147 <        initFromConcurrentlyMutating(c);
131 >        elementData = c.toArray();
132 >        size = elementData.length;
133 >        // c.toArray might (incorrectly) not return Object[] (see 6260652)
134 >        if (elementData.getClass() != Object[].class)
135 >            elementData = Arrays.copyOf(elementData, size, Object[].class);
136      }
137  
138      private void initFromConcurrentlyMutating(Collection<? extends E> c) {
# Line 189 | Line 177 | public class ArrayList<E> extends Abstra
177       * @param minCapacity the desired minimum capacity
178       */
179      private void growArray(int minCapacity) {
180 +        if (minCapacity < 0) // overflow
181 +            throw new OutOfMemoryError();
182          int oldCapacity = elementData.length;
183          // Double size if small; else grow by 50%
184 <        int newCapacity = ((oldCapacity < 64)?
185 <                           (oldCapacity * 2):
186 <                           ((oldCapacity * 3)/2 + 1));
184 >        int newCapacity = ((oldCapacity < 64) ?
185 >                           ((oldCapacity + 1) * 2) :
186 >                           ((oldCapacity / 2) * 3));
187 >        if (newCapacity < 0) // overflow
188 >            newCapacity = Integer.MAX_VALUE;
189          if (newCapacity < minCapacity)
190              newCapacity = minCapacity;
191          elementData = Arrays.copyOf(elementData, newCapacity);
# Line 343 | Line 335 | public class ArrayList<E> extends Abstra
335      // Positional Access Operations
336  
337      /**
338 <     * Creates and returns an appropriate exception for indexing errors.
338 >     * Throws an appropriate exception for indexing errors.
339       */
340 <    private static IndexOutOfBoundsException rangeException(int i, int s) {
341 <        return new IndexOutOfBoundsException("Index: " + i + ", Size: " + s);
340 >    private static void indexOutOfBounds(int i, int s) {
341 >        throw new IndexOutOfBoundsException("Index: " + i + ", Size: " + s);
342      }
343  
344      /**
# Line 358 | Line 350 | public class ArrayList<E> extends Abstra
350       */
351      public E get(int index) {
352          if (index >= size)
353 <            throw rangeException(index, size);
354 <        return (E)elementData[index];
353 >            indexOutOfBounds(index, size);
354 >        return (E) elementData[index];
355      }
356  
357      /**
# Line 373 | Line 365 | public class ArrayList<E> extends Abstra
365       */
366      public E set(int index, E element) {
367          if (index >= size)
368 <             throw rangeException(index, size);
377 <
368 >            indexOutOfBounds(index, size);
369          E oldValue = (E) elementData[index];
370          elementData[index] = element;
371          return oldValue;
# Line 387 | Line 378 | public class ArrayList<E> extends Abstra
378       * @return <tt>true</tt> (as specified by {@link Collection#add})
379       */
380      public boolean add(E e) {
381 <        ++modCount;
382 <        int s = size++;
381 >        modCount++;
382 >        int s = size;
383          if (s >= elementData.length)
384              growArray(s + 1);
385          elementData[s] = e;
386 <        return true;
386 >        size = s + 1;
387 >        return true;
388      }
389  
390      /**
# Line 407 | Line 399 | public class ArrayList<E> extends Abstra
399      public void add(int index, E element) {
400          int s = size;
401          if (index > s || index < 0)
402 <            throw rangeException(index, s);
403 <        ++modCount;
412 <        size = s + 1;
402 >            indexOutOfBounds(index, s);
403 >        modCount++;
404          if (s >= elementData.length)
405              growArray(s + 1);
406 <        System.arraycopy(elementData, index, elementData, index + 1,
407 <                         s - index);
406 >        System.arraycopy(elementData, index,
407 >                         elementData, index + 1, s - index);
408          elementData[index] = element;
409 +        size = s + 1;
410      }
411  
412      /**
# Line 429 | Line 421 | public class ArrayList<E> extends Abstra
421      public E remove(int index) {
422          int s = size - 1;
423          if (index > s)
424 <            throw rangeException(index, size);
433 <        size = s;
424 >            indexOutOfBounds(index, size);
425          modCount++;
426 <        Object oldValue = elementData[index];
426 >        E oldValue = (E) elementData[index];
427          int numMoved = s - index;
428          if (numMoved > 0)
429 <            System.arraycopy(elementData, index+1, elementData, index,
430 <                             numMoved);
431 <        elementData[s] = null; // forget removed element
432 <        return (E)oldValue;
429 >            System.arraycopy(elementData, index + 1,
430 >                             elementData, index, numMoved);
431 >        elementData[s] = null;
432 >        size = s;
433 >        return oldValue;
434      }
435  
436      /**
# Line 537 | Line 529 | public class ArrayList<E> extends Abstra
529       */
530      public boolean addAll(int index, Collection<? extends E> c) {
531          if (index > size || index < 0)
532 <            throw new IndexOutOfBoundsException(
541 <                "Index: " + index + ", Size: " + size);
532 >            indexOutOfBounds(index, size);
533  
534          Object[] a = c.toArray();
535          int numNew = a.length;
# Line 600 | Line 591 | public class ArrayList<E> extends Abstra
591          for (int i=0; i<size; i++)
592              s.writeObject(elementData[i]);
593  
594 <        if (modCount != expectedModCount) {
594 >        if (expectedModCount != modCount) {
595              throw new ConcurrentModificationException();
596          }
597  
# Line 623 | Line 614 | public class ArrayList<E> extends Abstra
614          for (int i=0; i<size; i++)
615              a[i] = s.readObject();
616      }
626
627
628    /**
629     * Returns a list-iterator of the elements in this list (in proper
630     * sequence), starting at the specified position in the list.
631     * Obeys the general contract of <tt>List.listIterator(int)</tt>.<p>
632     *
633     * The list-iterator is <i>fail-fast</i>: if the list is structurally
634     * modified at any time after the Iterator is created, in any way except
635     * through the list-iterator's own <tt>remove</tt> or <tt>add</tt>
636     * methods, the list-iterator will throw a
637     * <tt>ConcurrentModificationException</tt>.  Thus, in the face of
638     * concurrent modification, the iterator fails quickly and cleanly, rather
639     * than risking arbitrary, non-deterministic behavior at an undetermined
640     * time in the future.
641     *
642     * @param index index of the first element to be returned from the
643     *              list-iterator (by a call to <tt>next</tt>)
644     * @return a ListIterator of the elements in this list (in proper
645     *         sequence), starting at the specified position in the list
646     * @throws IndexOutOfBoundsException {@inheritDoc}
647     * @see List#listIterator(int)
648     */
649    public ListIterator<E> listIterator(int index) {
650        if (index < 0 || index > size)
651            throw new IndexOutOfBoundsException("Index: "+index);
652        return new ArrayListIterator(index);
653    }
654
655    /**
656     * Returns an iterator over the elements in this list in proper sequence.
657     *
658     * @return an iterator over the elements in this list in proper sequence
659     */
660    public Iterator<E> iterator() {
661        return new ArrayListIterator(0);
662    }
663
664    /**
665     * A streamlined version of AbstractList.Itr
666     */
667    final class ArrayListIterator implements ListIterator<E> {
668        int cursor;           // index of next element to return;
669        int lastRet;          // index of last element, or -1 if no such
670        int expectedModCount; // to check for CME
671
672        ArrayListIterator(int index) {
673            cursor = index;
674            lastRet = -1;
675            expectedModCount = modCount;
676        }
677
678        public boolean hasNext() {
679            return cursor < size;
680        }
681
682        public boolean hasPrevious() {
683            return cursor > 0;
684        }
685
686        public int nextIndex() {
687            return cursor;
688        }
689
690        public int previousIndex() {
691            return cursor - 1;
692        }
693
694        public E next() {
695            if (expectedModCount == modCount) {
696                int i = cursor;
697                if (i < size) {
698                    try {
699                        E e = (E)elementData[i];
700                        lastRet = i;
701                        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)
709                throw new NoSuchElementException();
710            throw new ConcurrentModificationException();
711        }
712
713        public E previous() {
714            if (expectedModCount == modCount) {
715                int i = cursor - 1;
716                if (i < size) {
717                    try {
718                        E e = (E)elementData[i];
719                        lastRet = i;
720                        cursor = i;
721                        return e;
722                    } catch (IndexOutOfBoundsException fallthrough) {
723                    }
724                }
725            }
726            if (expectedModCount == modCount)
727                throw new NoSuchElementException();
728            throw new ConcurrentModificationException();
729        }
730
731        public void remove() {
732            if (lastRet < 0)
733                throw new IllegalStateException();
734            if (modCount != expectedModCount)
735                throw new ConcurrentModificationException();
736            ArrayList.this.remove(lastRet);
737            if (lastRet < cursor)
738                cursor--;
739            lastRet = -1;
740            expectedModCount = modCount;
741        }
742
743        public void set(E e) {
744            if (lastRet < 0)
745                throw new IllegalStateException();
746            if (modCount != expectedModCount)
747                throw new ConcurrentModificationException();
748            ArrayList.this.set(lastRet, e);
749            expectedModCount = modCount;
750        }
751
752        public void add(E e) {
753            if (modCount != expectedModCount)
754                throw new ConcurrentModificationException();
755            ArrayList.this.add(cursor++, e);
756            lastRet = -1;
757            expectedModCount = modCount;
758        }
759    }
760
617   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines