1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* Copyright 1997-2006 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; |
44 |
|
* <tt>Collection</tt> constructor, as per the recommendation in the |
45 |
|
* <tt>Collection</tt> interface specification.<p> |
46 |
|
* |
47 |
< |
* The documentation for each non-abstract methods in this class describes its |
47 |
> |
* The documentation for each non-abstract method in this class describes its |
48 |
|
* implementation in detail. Each of these methods may be overridden if |
49 |
|
* the collection being implemented admits a more efficient implementation.<p> |
50 |
|
* |
113 |
|
/** |
114 |
|
* {@inheritDoc} |
115 |
|
* |
116 |
< |
* <p>This implementation allocates the array to be returned, and iterates |
117 |
< |
* over the elements in the collection, storing each object reference in |
118 |
< |
* the next consecutive element of the array, starting with element 0. |
116 |
> |
* <p>This implementation returns an array containing all the elements |
117 |
> |
* returned by this collection's iterator, in the same order, stored in |
118 |
> |
* consecutive elements of the array, starting with index {@code 0}. |
119 |
> |
* The length of the returned array is equal to the number of elements |
120 |
> |
* returned by the iterator, even if the size of this collection changes |
121 |
> |
* during iteration, as might happen if the collection permits |
122 |
> |
* concurrent modification during iteration. The {@code size} method is |
123 |
> |
* called only as an optimization hint; the correct result is returned |
124 |
> |
* even if the iterator returns a different number of elements. |
125 |
> |
* |
126 |
> |
* <p>This method is equivalent to: |
127 |
> |
* |
128 |
> |
* <pre> {@code |
129 |
> |
* List<E> list = new ArrayList<E>(size()); |
130 |
> |
* for (E e : this) |
131 |
> |
* list.add(e); |
132 |
> |
* return list.toArray(); |
133 |
> |
* }</pre> |
134 |
|
*/ |
135 |
|
public Object[] toArray() { |
136 |
|
// Estimate size of array; be prepared to see more or fewer elements |
137 |
|
Object[] r = new Object[size()]; |
105 |
– |
int i = 0; |
138 |
|
Iterator<E> it = iterator(); |
139 |
< |
while (i < r.length && it.hasNext()) |
140 |
< |
r[i++] = it.next(); |
141 |
< |
// Trim if overallocated; expand if underallocated |
142 |
< |
if (i < r.length || it.hasNext()) |
143 |
< |
return resizeAndFinishToArray(r, i, it); |
144 |
< |
return r; |
139 |
> |
for (int i = 0; i < r.length; i++) { |
140 |
> |
if (! it.hasNext()) // fewer elements than expected |
141 |
> |
return Arrays.copyOf(r, i); |
142 |
> |
r[i] = it.next(); |
143 |
> |
} |
144 |
> |
return it.hasNext() ? finishToArray(r, it) : r; |
145 |
|
} |
146 |
|
|
147 |
|
/** |
148 |
|
* {@inheritDoc} |
149 |
|
* |
150 |
< |
* <p>This implementation checks if the array is large enough to contain the |
151 |
< |
* collection; if not, it allocates a new array of the correct size and |
152 |
< |
* type (using reflection). Then, it iterates over the collection, |
153 |
< |
* storing each object reference in the next consecutive element of the |
154 |
< |
* array, starting with element 0. If the array is larger than the |
155 |
< |
* collection, a <tt>null</tt> is stored in the first location after the |
156 |
< |
* end of the collection. |
150 |
> |
* <p>This implementation returns an array containing all the elements |
151 |
> |
* returned by this collection's iterator in the same order, stored in |
152 |
> |
* consecutive elements of the array, starting with index {@code 0}. |
153 |
> |
* If the number of elements returned by the iterator is too large to |
154 |
> |
* fit into the specified array, then the elements are returned in a |
155 |
> |
* newly allocated array with length equal to the number of elements |
156 |
> |
* returned by the iterator, even if the size of this collection |
157 |
> |
* changes during iteration, as might happen if the collection permits |
158 |
> |
* concurrent modification during iteration. The {@code size} method is |
159 |
> |
* called only as an optimization hint; the correct result is returned |
160 |
> |
* even if the iterator returns a different number of elements. |
161 |
> |
* |
162 |
> |
* <p>This method is equivalent to: |
163 |
> |
* |
164 |
> |
* <pre> {@code |
165 |
> |
* List<E> list = new ArrayList<E>(size()); |
166 |
> |
* for (E e : this) |
167 |
> |
* list.add(e); |
168 |
> |
* return list.toArray(a); |
169 |
> |
* }</pre> |
170 |
|
* |
171 |
|
* @throws ArrayStoreException {@inheritDoc} |
172 |
|
* @throws NullPointerException {@inheritDoc} |
177 |
|
T[] r = a.length >= size ? a : |
178 |
|
(T[])java.lang.reflect.Array |
179 |
|
.newInstance(a.getClass().getComponentType(), size); |
135 |
– |
int i = 0; |
180 |
|
Iterator<E> it = iterator(); |
181 |
< |
while (i < r.length && it.hasNext()) |
182 |
< |
r[i++] = (T)it.next(); |
183 |
< |
// Trim if overallocated; expand if underallocated |
184 |
< |
if (it.hasNext() || (r != a && i < r.length)) |
185 |
< |
return resizeAndFinishToArray(r, i, it); |
186 |
< |
if (i < r.length) |
187 |
< |
r[i] = null; // null-terminate if provided array is too big |
188 |
< |
return r; |
181 |
> |
|
182 |
> |
for (int i = 0; i < r.length; i++) { |
183 |
> |
if (! it.hasNext()) { // fewer elements than expected |
184 |
> |
if (a != r) |
185 |
> |
return Arrays.copyOf(r, i); |
186 |
> |
r[i] = null; // null-terminate |
187 |
> |
return r; |
188 |
> |
} |
189 |
> |
r[i] = (T)it.next(); |
190 |
> |
} |
191 |
> |
return it.hasNext() ? finishToArray(r, it) : r; |
192 |
|
} |
193 |
|
|
194 |
|
/** |
195 |
< |
* Reallocates the array being used within toArray that has a |
196 |
< |
* different number of elements than expected, and finishes |
197 |
< |
* filling it from the given iterator, if necessary. |
198 |
< |
* |
199 |
< |
* @param r the array |
200 |
< |
* @param i the next array index to fill |
154 |
< |
* @param it the in-progress iterator over the collection |
195 |
> |
* Reallocates the array being used within toArray when the iterator |
196 |
> |
* returned more elements than expected, and finishes filling it from |
197 |
> |
* the iterator. |
198 |
> |
* |
199 |
> |
* @param r the array, replete with previously stored elements |
200 |
> |
* @param it the in-progress iterator over this collection |
201 |
|
* @return array containing the elements in the given array, plus any |
202 |
|
* further elements returned by the iterator, trimmed to size |
203 |
|
*/ |
204 |
< |
private static <T> T[] resizeAndFinishToArray(T[] r, int i, Iterator<?> it) { |
204 |
> |
private static <T> T[] finishToArray(T[] r, Iterator<?> it) { |
205 |
> |
int i = r.length; |
206 |
|
while (it.hasNext()) { |
207 |
|
int cap = r.length; |
208 |
< |
if (i < cap) |
162 |
< |
r[i++] = (T)it.next(); |
163 |
< |
else { |
208 |
> |
if (i == cap) { |
209 |
|
int newCap = ((cap / 2) + 1) * 3; |
210 |
|
if (newCap <= cap) { // integer overflow |
211 |
|
if (cap == Integer.MAX_VALUE) |
212 |
|
throw new OutOfMemoryError |
213 |
|
("Required array size too large"); |
214 |
< |
newCap = Integer.MAX_VALUE; |
214 |
> |
newCap = Integer.MAX_VALUE; |
215 |
|
} |
216 |
< |
r = Arrays.copyOf(r, newCap); |
217 |
< |
} |
216 |
> |
r = Arrays.copyOf(r, newCap); |
217 |
> |
} |
218 |
> |
r[i++] = (T)it.next(); |
219 |
|
} |
220 |
|
// trim if overallocated |
221 |
< |
return i == r.length ? r : Arrays.copyOf(r, i); |
221 |
> |
return (i == r.length) ? r : Arrays.copyOf(r, i); |
222 |
|
} |
223 |
|
|
224 |
|
// Modification Operations |