1 |
|
/* |
2 |
|
* %W% %E% |
3 |
|
* |
4 |
< |
* Copyright 2005 Sun Microsystems, Inc. All rights reserved. |
4 |
> |
* Copyright 2007 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 |
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 |
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) |
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 |
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)); |
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); |
325 |
|
// Positional Access Operations |
326 |
|
|
327 |
|
/** |
328 |
< |
* Returns error message string for IndexOutOfBoundsExceptions |
328 |
> |
* Throws an appropriate exception for indexing errors. |
329 |
|
*/ |
330 |
< |
private String ioobe(int index) { |
331 |
< |
return "Index: " + index + ", Size: " + size; |
330 |
> |
private static void indexOutOfBounds(int i, int s) { |
331 |
> |
throw new IndexOutOfBoundsException("Index: " + i + ", Size: " + s); |
332 |
|
} |
333 |
|
|
334 |
|
/** |
340 |
|
*/ |
341 |
|
public E get(int index) { |
342 |
|
if (index >= size) |
343 |
< |
throw new IndexOutOfBoundsException(ioobe(index)); |
344 |
< |
return (E)elementData[index]; |
343 |
> |
indexOutOfBounds(index, size); |
344 |
> |
return (E) elementData[index]; |
345 |
|
} |
346 |
|
|
347 |
|
/** |
355 |
|
*/ |
356 |
|
public E set(int index, E element) { |
357 |
|
if (index >= size) |
358 |
< |
throw new IndexOutOfBoundsException(ioobe(index)); |
379 |
< |
|
358 |
> |
indexOutOfBounds(index, size); |
359 |
|
E oldValue = (E) elementData[index]; |
360 |
|
elementData[index] = element; |
361 |
|
return oldValue; |
373 |
|
if (s >= elementData.length) |
374 |
|
growArray(s + 1); |
375 |
|
elementData[s] = e; |
376 |
< |
size = s + 1; |
377 |
< |
return true; |
376 |
> |
size = s + 1; |
377 |
> |
return true; |
378 |
|
} |
379 |
|
|
380 |
|
/** |
389 |
|
public void add(int index, E element) { |
390 |
|
int s = size; |
391 |
|
if (index > s || index < 0) |
392 |
< |
throw new IndexOutOfBoundsException(ioobe(index)); |
392 |
> |
indexOutOfBounds(index, s); |
393 |
|
modCount++; |
394 |
|
if (s >= elementData.length) |
395 |
|
growArray(s + 1); |
396 |
< |
System.arraycopy(elementData, index, |
397 |
< |
elementData, index + 1, s - index); |
396 |
> |
System.arraycopy(elementData, index, |
397 |
> |
elementData, index + 1, s - index); |
398 |
|
elementData[index] = element; |
399 |
|
size = s + 1; |
400 |
|
} |
411 |
|
public E remove(int index) { |
412 |
|
int s = size - 1; |
413 |
|
if (index > s) |
414 |
< |
throw new IndexOutOfBoundsException(ioobe(index)); |
414 |
> |
indexOutOfBounds(index, size); |
415 |
|
modCount++; |
416 |
< |
E oldValue = (E)elementData[index]; |
416 |
> |
E oldValue = (E) elementData[index]; |
417 |
|
int numMoved = s - index; |
418 |
|
if (numMoved > 0) |
419 |
< |
System.arraycopy(elementData, index + 1, |
420 |
< |
elementData, index, numMoved); |
419 |
> |
System.arraycopy(elementData, index + 1, |
420 |
> |
elementData, index, numMoved); |
421 |
|
elementData[s] = null; |
422 |
< |
size = s; |
422 |
> |
size = s; |
423 |
|
return oldValue; |
424 |
|
} |
425 |
|
|
519 |
|
*/ |
520 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
521 |
|
if (index > size || index < 0) |
522 |
< |
throw new IndexOutOfBoundsException(ioobe(index)); |
522 |
> |
indexOutOfBounds(index, size); |
523 |
|
|
524 |
|
Object[] a = c.toArray(); |
525 |
|
int numNew = a.length; |
581 |
|
for (int i=0; i<size; i++) |
582 |
|
s.writeObject(elementData[i]); |
583 |
|
|
584 |
< |
if (modCount != expectedModCount) { |
584 |
> |
if (expectedModCount != modCount) { |
585 |
|
throw new ConcurrentModificationException(); |
586 |
|
} |
587 |
|
|
604 |
|
for (int i=0; i<size; i++) |
605 |
|
a[i] = s.readObject(); |
606 |
|
} |
628 |
– |
|
629 |
– |
|
630 |
– |
/** |
631 |
– |
* Returns a list-iterator of the elements in this list (in proper |
632 |
– |
* sequence), starting at the specified position in the list. |
633 |
– |
* Obeys the general contract of <tt>List.listIterator(int)</tt>.<p> |
634 |
– |
* |
635 |
– |
* The list-iterator is <i>fail-fast</i>: if the list is structurally |
636 |
– |
* modified at any time after the Iterator is created, in any way except |
637 |
– |
* through the list-iterator's own <tt>remove</tt> or <tt>add</tt> |
638 |
– |
* methods, the list-iterator will throw a |
639 |
– |
* <tt>ConcurrentModificationException</tt>. Thus, in the face of |
640 |
– |
* concurrent modification, the iterator fails quickly and cleanly, rather |
641 |
– |
* than risking arbitrary, non-deterministic behavior at an undetermined |
642 |
– |
* time in the future. |
643 |
– |
* |
644 |
– |
* @param index index of the first element to be returned from the |
645 |
– |
* list-iterator (by a call to <tt>next</tt>) |
646 |
– |
* @return a ListIterator of the elements in this list (in proper |
647 |
– |
* sequence), starting at the specified position in the list |
648 |
– |
* @throws IndexOutOfBoundsException {@inheritDoc} |
649 |
– |
* @see List#listIterator(int) |
650 |
– |
*/ |
651 |
– |
public ListIterator<E> listIterator(int index) { |
652 |
– |
if (index < 0 || index > size) |
653 |
– |
throw new IndexOutOfBoundsException(ioobe(index)); |
654 |
– |
return new ArrayListIterator(index); |
655 |
– |
} |
656 |
– |
|
657 |
– |
/** |
658 |
– |
* {@inheritDoc} |
659 |
– |
*/ |
660 |
– |
public ListIterator<E> listIterator() { |
661 |
– |
return new ArrayListIterator(0); |
662 |
– |
} |
663 |
– |
|
664 |
– |
/** |
665 |
– |
* Returns an iterator over the elements in this list in proper sequence. |
666 |
– |
* |
667 |
– |
* @return an iterator over the elements in this list in proper sequence |
668 |
– |
*/ |
669 |
– |
public Iterator<E> iterator() { |
670 |
– |
return new ArrayListIterator(0); |
671 |
– |
} |
672 |
– |
|
673 |
– |
/** |
674 |
– |
* A streamlined version of AbstractList.ListItr |
675 |
– |
*/ |
676 |
– |
final class ArrayListIterator implements ListIterator<E> { |
677 |
– |
int cursor; // index of next element to return; |
678 |
– |
int lastRet; // index of last element, or -1 if no such |
679 |
– |
int expectedModCount; // to check for CME |
680 |
– |
|
681 |
– |
ArrayListIterator(int index) { |
682 |
– |
cursor = index; |
683 |
– |
lastRet = -1; |
684 |
– |
expectedModCount = modCount; |
685 |
– |
} |
686 |
– |
|
687 |
– |
public boolean hasNext() { |
688 |
– |
return cursor < size; |
689 |
– |
} |
690 |
– |
|
691 |
– |
public boolean hasPrevious() { |
692 |
– |
return cursor > 0; |
693 |
– |
} |
694 |
– |
|
695 |
– |
public int nextIndex() { |
696 |
– |
return cursor; |
697 |
– |
} |
698 |
– |
|
699 |
– |
public int previousIndex() { |
700 |
– |
return cursor - 1; |
701 |
– |
} |
702 |
– |
|
703 |
– |
public E next() { |
704 |
– |
try { |
705 |
– |
int i = cursor; |
706 |
– |
E next = get(i); |
707 |
– |
lastRet = i; |
708 |
– |
cursor = i + 1; |
709 |
– |
return next; |
710 |
– |
} catch (IndexOutOfBoundsException ex) { |
711 |
– |
throw new NoSuchElementException(); |
712 |
– |
} finally { |
713 |
– |
if (expectedModCount != modCount) |
714 |
– |
throw new ConcurrentModificationException(); |
715 |
– |
} |
716 |
– |
} |
717 |
– |
public E previous() { |
718 |
– |
try { |
719 |
– |
int i = cursor - 1; |
720 |
– |
E next = get(i); |
721 |
– |
lastRet = i; |
722 |
– |
cursor = i; |
723 |
– |
return next; |
724 |
– |
} catch (IndexOutOfBoundsException ex) { |
725 |
– |
throw new NoSuchElementException(); |
726 |
– |
} finally { |
727 |
– |
if (modCount != expectedModCount) |
728 |
– |
throw new ConcurrentModificationException(); |
729 |
– |
} |
730 |
– |
} |
731 |
– |
|
732 |
– |
public void remove() { |
733 |
– |
if (lastRet < 0) |
734 |
– |
throw new IllegalStateException(); |
735 |
– |
if (modCount != expectedModCount) |
736 |
– |
throw new ConcurrentModificationException(); |
737 |
– |
ArrayList.this.remove(lastRet); |
738 |
– |
if (lastRet < cursor) |
739 |
– |
cursor--; |
740 |
– |
lastRet = -1; |
741 |
– |
expectedModCount = modCount; |
742 |
– |
} |
743 |
– |
|
744 |
– |
public void set(E e) { |
745 |
– |
if (lastRet < 0) |
746 |
– |
throw new IllegalStateException(); |
747 |
– |
if (modCount != expectedModCount) |
748 |
– |
throw new ConcurrentModificationException(); |
749 |
– |
ArrayList.this.set(lastRet, e); |
750 |
– |
expectedModCount = modCount; |
751 |
– |
} |
752 |
– |
|
753 |
– |
public void add(E e) { |
754 |
– |
if (modCount != expectedModCount) |
755 |
– |
throw new ConcurrentModificationException(); |
756 |
– |
ArrayList.this.add(cursor++, e); |
757 |
– |
lastRet = -1; |
758 |
– |
expectedModCount = modCount; |
759 |
– |
} |
760 |
– |
} |
761 |
– |
|
607 |
|
} |