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.18 by jsr166, Sun Mar 19 17:40:40 2006 UTC vs.
Revision 1.24 by jsr166, Sun May 20 07:54:01 2007 UTC

# Line 1 | Line 1
1   /*
2 < * %W% %E%
2 > * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
3 > * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4   *
5 < * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
6 < * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
5 > * This code is free software; you can redistribute it and/or modify it
6 > * under the terms of the GNU General Public License version 2 only, as
7 > * published by the Free Software Foundation.  Sun designates this
8 > * particular file as subject to the "Classpath" exception as provided
9 > * by Sun in the LICENSE file that accompanied this code.
10 > *
11 > * This code is distributed in the hope that it will be useful, but WITHOUT
12 > * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 > * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 > * version 2 for more details (a copy is included in the LICENSE file that
15 > * accompanied this code).
16 > *
17 > * You should have received a copy of the GNU General Public License version
18 > * 2 along with this work; if not, write to the Free Software Foundation,
19 > * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 > *
21 > * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 > * CA 95054 USA or visit www.sun.com if you need additional information or
23 > * have any questions.
24   */
25  
26   package java.util;
# Line 66 | Line 84 | package java.util;
84   * should be used only to detect bugs.</i><p>
85   *
86   * This class is a member of the
87 < * <a href="{@docRoot}/../guide/collections/index.html">
87 > * <a href="{@docRoot}/../technotes/guides/collections/index.html">
88   * Java Collections Framework</a>.
89   *
90   * @author  Josh Bloch
# Line 170 | Line 188 | public class ArrayList<E> extends Abstra
188          if (minCapacity < 0) // overflow
189              throw new OutOfMemoryError();
190          int oldCapacity = elementData.length;
191 <        // Double size if small; else grow by 50%
192 <        int newCapacity = ((oldCapacity < 64) ?
193 <                           ((oldCapacity + 1) * 2) :
194 <                           ((oldCapacity / 2) * 3));
195 <        if (newCapacity < 0) // overflow
196 <            newCapacity = Integer.MAX_VALUE;
197 <        if (newCapacity < minCapacity)
198 <            newCapacity = minCapacity;
199 <        elementData = Arrays.copyOf(elementData, newCapacity);
191 >        // Double size if small; else grow by 50%
192 >        int newCapacity = ((oldCapacity < 64) ?
193 >                           ((oldCapacity + 1) * 2) :
194 >                           ((oldCapacity / 2) * 3));
195 >        if (newCapacity < 0) // overflow
196 >            newCapacity = Integer.MAX_VALUE;
197 >        if (newCapacity < minCapacity)
198 >            newCapacity = minCapacity;
199 >        elementData = Arrays.copyOf(elementData, newCapacity);
200      }
201  
202      /**
# Line 325 | Line 343 | public class ArrayList<E> extends Abstra
343      // Positional Access Operations
344  
345      /**
346 <     * Returns error message string for IndexOutOfBoundsExceptions
346 >     * Throws an appropriate exception for indexing errors.
347       */
348 <    private String ioobe(int index) {
349 <        return "Index: " + index + ", Size: " + size;
348 >    private static void indexOutOfBounds(int i, int s) {
349 >        throw new IndexOutOfBoundsException("Index: " + i + ", Size: " + s);
350      }
351  
352      /**
# Line 340 | Line 358 | public class ArrayList<E> extends Abstra
358       */
359      public E get(int index) {
360          if (index >= size)
361 <            throw new IndexOutOfBoundsException(ioobe(index));
362 <        return (E)elementData[index];
361 >            indexOutOfBounds(index, size);
362 >        return (E) elementData[index];
363      }
364  
365      /**
# Line 355 | Line 373 | public class ArrayList<E> extends Abstra
373       */
374      public E set(int index, E element) {
375          if (index >= size)
376 <            throw new IndexOutOfBoundsException(ioobe(index));
359 <
376 >            indexOutOfBounds(index, size);
377          E oldValue = (E) elementData[index];
378          elementData[index] = element;
379          return oldValue;
# Line 374 | Line 391 | public class ArrayList<E> extends Abstra
391          if (s >= elementData.length)
392              growArray(s + 1);
393          elementData[s] = e;
394 <        size = s + 1;
395 <        return true;
394 >        size = s + 1;
395 >        return true;
396      }
397  
398      /**
# Line 390 | Line 407 | public class ArrayList<E> extends Abstra
407      public void add(int index, E element) {
408          int s = size;
409          if (index > s || index < 0)
410 <            throw new IndexOutOfBoundsException(ioobe(index));
410 >            indexOutOfBounds(index, s);
411          modCount++;
412          if (s >= elementData.length)
413              growArray(s + 1);
414          System.arraycopy(elementData, index,
415 <                         elementData, index + 1, s - index);
415 >                         elementData, index + 1, s - index);
416          elementData[index] = element;
417          size = s + 1;
418      }
# Line 412 | Line 429 | public class ArrayList<E> extends Abstra
429      public E remove(int index) {
430          int s = size - 1;
431          if (index > s)
432 <            throw new IndexOutOfBoundsException(ioobe(index));
432 >            indexOutOfBounds(index, size);
433          modCount++;
434 <        E oldValue = (E)elementData[index];
434 >        E oldValue = (E) elementData[index];
435          int numMoved = s - index;
436          if (numMoved > 0)
437              System.arraycopy(elementData, index + 1,
438 <                             elementData, index, numMoved);
438 >                             elementData, index, numMoved);
439          elementData[s] = null;
440 <        size = s;
440 >        size = s;
441          return oldValue;
442      }
443  
# Line 520 | Line 537 | public class ArrayList<E> extends Abstra
537       */
538      public boolean addAll(int index, Collection<? extends E> c) {
539          if (index > size || index < 0)
540 <            throw new IndexOutOfBoundsException(ioobe(index));
540 >            indexOutOfBounds(index, size);
541  
542          Object[] a = c.toArray();
543          int numNew = a.length;
# Line 605 | Line 622 | public class ArrayList<E> extends Abstra
622          for (int i=0; i<size; i++)
623              a[i] = s.readObject();
624      }
608
609
610    /**
611     * Returns a list-iterator of the elements in this list (in proper
612     * sequence), starting at the specified position in the list.
613     * Obeys the general contract of <tt>List.listIterator(int)</tt>.<p>
614     *
615     * The list-iterator is <i>fail-fast</i>: if the list is structurally
616     * modified at any time after the Iterator is created, in any way except
617     * through the list-iterator's own <tt>remove</tt> or <tt>add</tt>
618     * methods, the list-iterator will throw a
619     * <tt>ConcurrentModificationException</tt>.  Thus, in the face of
620     * concurrent modification, the iterator fails quickly and cleanly, rather
621     * than risking arbitrary, non-deterministic behavior at an undetermined
622     * time in the future.
623     *
624     * @param index index of the first element to be returned from the
625     *              list-iterator (by a call to <tt>next</tt>)
626     * @return a ListIterator of the elements in this list (in proper
627     *         sequence), starting at the specified position in the list
628     * @throws IndexOutOfBoundsException {@inheritDoc}
629     * @see List#listIterator(int)
630     */
631    public ListIterator<E> listIterator(int index) {
632        if (index < 0 || index > size)
633            throw new IndexOutOfBoundsException(ioobe(index));
634        return new ArrayListIterator(index);
635    }
636
637    /**
638     * {@inheritDoc}
639     */
640    public ListIterator<E> listIterator() {
641        return new ArrayListIterator(0);
642    }
643
644    /**
645     * Returns an iterator over the elements in this list in proper sequence.
646     *
647     * @return an iterator over the elements in this list in proper sequence
648     */
649    public Iterator<E> iterator() {
650        return new ArrayListIterator(0);
651    }
652
653    /**
654     * A streamlined version of AbstractList.ListItr
655     */
656    final class ArrayListIterator implements ListIterator<E> {
657        int cursor;           // index of next element to return;
658        int lastRet;          // index of last element, or -1 if no such
659        int expectedModCount; // to check for CME
660
661        ArrayListIterator(int index) {
662            cursor = index;
663            lastRet = -1;
664            expectedModCount = modCount;
665        }
666
667        public boolean hasNext() {
668            return cursor != size;
669        }
670
671        public boolean hasPrevious() {
672            return cursor != 0;
673        }
674
675        public int nextIndex() {
676            return cursor;
677        }
678
679        public int previousIndex() {
680            return cursor - 1;
681        }
682
683        public E next() {
684            try {
685                int i = cursor;
686                E next = get(i);
687                lastRet = i;
688                cursor = i + 1;
689                return next;
690            } catch (IndexOutOfBoundsException ex) {
691                throw new NoSuchElementException();
692            } finally {
693                if (expectedModCount != modCount)
694                    throw new ConcurrentModificationException();
695            }
696        }
697
698        public E previous() {
699            try {
700                int i = cursor - 1;
701                E prev = get(i);
702                lastRet = i;
703                cursor = i;
704                return prev;
705            } catch (IndexOutOfBoundsException ex) {
706                throw new NoSuchElementException();
707            } finally {
708                if (expectedModCount != modCount)
709                    throw new ConcurrentModificationException();
710            }
711        }
712
713        public void remove() {
714            if (lastRet < 0)
715                throw new IllegalStateException();
716            if (expectedModCount != modCount)
717                throw new ConcurrentModificationException();
718            ArrayList.this.remove(lastRet);
719            if (lastRet < cursor)
720                cursor--;
721            lastRet = -1;
722            expectedModCount = modCount;
723        }
724
725        public void set(E e) {
726            if (lastRet < 0)
727                throw new IllegalStateException();
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 (expectedModCount != modCount)
736                throw new ConcurrentModificationException();
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    }
625   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines