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.11 by jsr166, Mon Nov 28 03:59:23 2005 UTC vs.
Revision 1.18 by jsr166, Sun Mar 19 17:40:40 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 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) {
133        int size = c.size();
134        // 10% for growth
135        int cap = ((size/10)+1)*11;
136        if (cap > 0) {
137            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);
148    }
149
150    private void initFromConcurrentlyMutating(Collection<? extends E> c) {
131          elementData = c.toArray();
132          size = elementData.length;
133          // c.toArray might (incorrectly) not return Object[] (see 6260652)
# Line 155 | Line 135 | public class ArrayList<E> extends Abstra
135              elementData = Arrays.copyOf(elementData, size, Object[].class);
136      }
137  
158    private final static Object UNALLOCATED = new Object();
159
138      /**
139       * Trims the capacity of this <tt>ArrayList</tt> instance to be the
140       * list's current size.  An application can use this operation to minimize
# Line 189 | Line 167 | public class ArrayList<E> extends Abstra
167       * @param minCapacity the desired minimum capacity
168       */
169      private void growArray(int minCapacity) {
170 <        if (minCapacity < 0)
171 <            throw new OutOfMemoryError(); // int overflow
170 >        if (minCapacity < 0) // overflow
171 >            throw new OutOfMemoryError();
172          int oldCapacity = elementData.length;
173 <        // Double size if small; else grow by 50%
174 <        int newCapacity = ((oldCapacity < 64)?
175 <                           ((oldCapacity + 1) * 2):
176 <                           ((oldCapacity * 3) / 2));
177 <        if (newCapacity < minCapacity)
178 <            newCapacity = minCapacity;
179 <        elementData = Arrays.copyOf(elementData, newCapacity);
173 >        // Double size if small; else grow by 50%
174 >        int newCapacity = ((oldCapacity < 64) ?
175 >                           ((oldCapacity + 1) * 2) :
176 >                           ((oldCapacity / 2) * 3));
177 >        if (newCapacity < 0) // overflow
178 >            newCapacity = Integer.MAX_VALUE;
179 >        if (newCapacity < minCapacity)
180 >            newCapacity = minCapacity;
181 >        elementData = Arrays.copyOf(elementData, newCapacity);
182      }
183  
184      /**
# Line 602 | Line 582 | public class ArrayList<E> extends Abstra
582          for (int i=0; i<size; i++)
583              s.writeObject(elementData[i]);
584  
585 <        if (modCount != expectedModCount) {
585 >        if (expectedModCount != modCount) {
586              throw new ConcurrentModificationException();
587          }
588  
# Line 685 | Line 665 | public class ArrayList<E> extends Abstra
665          }
666  
667          public boolean hasNext() {
668 <            return cursor < size;
668 >            return cursor != size;
669          }
670  
671          public boolean hasPrevious() {
672 <            return cursor > 0;
672 >            return cursor != 0;
673          }
674  
675          public int nextIndex() {
# Line 725 | Line 705 | public class ArrayList<E> extends Abstra
705              } catch (IndexOutOfBoundsException ex) {
706                  throw new NoSuchElementException();
707              } finally {
708 <                if (modCount != expectedModCount)
708 >                if (expectedModCount != modCount)
709                      throw new ConcurrentModificationException();
710              }
711          }
# Line 733 | Line 713 | public class ArrayList<E> extends Abstra
713          public void remove() {
714              if (lastRet < 0)
715                  throw new IllegalStateException();
716 <            if (modCount != expectedModCount)
716 >            if (expectedModCount != modCount)
717                  throw new ConcurrentModificationException();
718              ArrayList.this.remove(lastRet);
719              if (lastRet < cursor)
# Line 745 | Line 725 | public class ArrayList<E> extends Abstra
725          public void set(E e) {
726              if (lastRet < 0)
727                  throw new IllegalStateException();
728 <            if (modCount != expectedModCount)
728 >            if (expectedModCount != modCount)
729                  throw new ConcurrentModificationException();
730              ArrayList.this.set(lastRet, e);
731              expectedModCount = modCount;
732          }
733  
734          public void add(E e) {
735 <            if (modCount != expectedModCount)
735 >            if (expectedModCount != modCount)
736                  throw new ConcurrentModificationException();
737 <            ArrayList.this.add(cursor++, e);
738 <            lastRet = -1;
739 <            expectedModCount = modCount;
737 >            try {
738 >                ArrayList.this.add(cursor++, e);
739 >                lastRet = -1;
740 >                expectedModCount = modCount;
741 >            } catch (IndexOutOfBoundsException ex) {
742 >                throw new ConcurrentModificationException();
743 >            }
744          }
745      }
746   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines