1 |
|
/* |
2 |
< |
* @(#)AbstractCollection.java 1.33 05/09/09 |
2 |
> |
* %W% %E% |
3 |
|
* |
4 |
|
* Copyright 2005 Sun Microsystems, Inc. All rights reserved. |
5 |
|
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
103 |
|
// Estimate size of array; be prepared to see more or fewer elements |
104 |
|
Object[] r = new Object[size()]; |
105 |
|
int i = 0; |
106 |
< |
Iterator it = iterator(); |
106 |
> |
Iterator<E> it = iterator(); |
107 |
|
while (i < r.length && it.hasNext()) |
108 |
|
r[i++] = it.next(); |
109 |
|
// Trim if overallocated; expand if underallocated |
129 |
|
public <T> T[] toArray(T[] a) { |
130 |
|
// Estimate size of array; be prepared to see more or fewer elements |
131 |
|
int size = size(); |
132 |
< |
T[] r = a.length >= size? a : |
132 |
> |
T[] r = a.length >= size ? a : |
133 |
|
(T[])java.lang.reflect.Array |
134 |
|
.newInstance(a.getClass().getComponentType(), size); |
135 |
|
int i = 0; |
136 |
< |
Iterator it = iterator(); |
136 |
> |
Iterator<E> it = iterator(); |
137 |
|
while (i < r.length && it.hasNext()) |
138 |
|
r[i++] = (T)it.next(); |
139 |
|
// Trim if overallocated; expand if underallocated |
145 |
|
} |
146 |
|
|
147 |
|
/** |
148 |
< |
* Reallocate the array being used within toArray that has a |
149 |
< |
* different number of elements than expected, and finish filling |
150 |
< |
* it if necessary |
148 |
> |
* Reallocates the array being used within toArray that has a |
149 |
> |
* different number of elements than expected, and finishes |
150 |
> |
* filling it from the given iterator, if necessary. |
151 |
> |
* |
152 |
|
* @param r the array |
153 |
|
* @param i the next array index to fill |
154 |
|
* @param it the in-progress iterator over the collection |
155 |
< |
* @return larger array containing same elements |
155 |
> |
* @return array containing the elements in the given array, plus any |
156 |
> |
* further elements returned by the iterator, trimmed to size |
157 |
|
*/ |
158 |
< |
private static <T> T[] resizeAndFinishToArray(T[] r, int i, Iterator it) { |
158 |
> |
private static <T> T[] resizeAndFinishToArray(T[] r, int i, Iterator<?> it) { |
159 |
|
while (it.hasNext()) { |
160 |
|
int cap = r.length; |
161 |
|
if (i < cap) |
162 |
|
r[i++] = (T)it.next(); |
163 |
< |
else if (cap < Integer.MAX_VALUE) { // expand |
164 |
< |
int newCap = (cap * 3) / 2 + 1; |
165 |
< |
if (newCap <= cap) // integer overflow |
163 |
> |
else { |
164 |
> |
int newCap = ((cap / 2) + 1) * 3; |
165 |
> |
if (newCap <= cap) { // integer overflow |
166 |
> |
if (cap == Integer.MAX_VALUE) |
167 |
> |
throw new OutOfMemoryError |
168 |
> |
("Required array size too large"); |
169 |
|
newCap = Integer.MAX_VALUE; |
170 |
+ |
} |
171 |
|
r = Arrays.copyOf(r, newCap); |
172 |
< |
} else // can't expand |
173 |
< |
throw new OutOfMemoryError("Required array size too large"); |
168 |
< |
} |
172 |
> |
} |
173 |
> |
} |
174 |
|
// trim if overallocated |
175 |
< |
return i == r.length? r : Arrays.copyOf(r, i); |
175 |
> |
return i == r.length ? r : Arrays.copyOf(r, i); |
176 |
|
} |
177 |
|
|
178 |
|
// Modification Operations |