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 |
|
* |
84 |
|
* <p>This implementation returns <tt>size() == 0</tt>. |
85 |
|
*/ |
86 |
|
public boolean isEmpty() { |
87 |
< |
return size() == 0; |
87 |
> |
return size() == 0; |
88 |
|
} |
89 |
|
|
90 |
|
/** |
97 |
|
* @throws NullPointerException {@inheritDoc} |
98 |
|
*/ |
99 |
|
public boolean contains(Object o) { |
100 |
< |
Iterator<E> e = iterator(); |
101 |
< |
if (o==null) { |
102 |
< |
while (e.hasNext()) |
103 |
< |
if (e.next()==null) |
104 |
< |
return true; |
105 |
< |
} else { |
106 |
< |
while (e.hasNext()) |
107 |
< |
if (o.equals(e.next())) |
108 |
< |
return true; |
109 |
< |
} |
110 |
< |
return false; |
100 |
> |
Iterator<E> e = iterator(); |
101 |
> |
if (o==null) { |
102 |
> |
while (e.hasNext()) |
103 |
> |
if (e.next()==null) |
104 |
> |
return true; |
105 |
> |
} else { |
106 |
> |
while (e.hasNext()) |
107 |
> |
if (o.equals(e.next())) |
108 |
> |
return true; |
109 |
> |
} |
110 |
> |
return false; |
111 |
|
} |
112 |
|
|
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; |
137 |
> |
Object[] r = new Object[size()]; |
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"); |
211 |
> |
if (cap == Integer.MAX_VALUE) |
212 |
> |
throw new OutOfMemoryError |
213 |
> |
("Required array size too large"); |
214 |
|
newCap = Integer.MAX_VALUE; |
215 |
< |
} |
215 |
> |
} |
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 |
236 |
|
* @throws IllegalStateException {@inheritDoc} |
237 |
|
*/ |
238 |
|
public boolean add(E e) { |
239 |
< |
throw new UnsupportedOperationException(); |
239 |
> |
throw new UnsupportedOperationException(); |
240 |
|
} |
241 |
|
|
242 |
|
/** |
256 |
|
* @throws NullPointerException {@inheritDoc} |
257 |
|
*/ |
258 |
|
public boolean remove(Object o) { |
259 |
< |
Iterator<E> e = iterator(); |
260 |
< |
if (o==null) { |
261 |
< |
while (e.hasNext()) { |
262 |
< |
if (e.next()==null) { |
263 |
< |
e.remove(); |
264 |
< |
return true; |
265 |
< |
} |
266 |
< |
} |
267 |
< |
} else { |
268 |
< |
while (e.hasNext()) { |
269 |
< |
if (o.equals(e.next())) { |
270 |
< |
e.remove(); |
271 |
< |
return true; |
272 |
< |
} |
273 |
< |
} |
274 |
< |
} |
275 |
< |
return false; |
259 |
> |
Iterator<E> e = iterator(); |
260 |
> |
if (o==null) { |
261 |
> |
while (e.hasNext()) { |
262 |
> |
if (e.next()==null) { |
263 |
> |
e.remove(); |
264 |
> |
return true; |
265 |
> |
} |
266 |
> |
} |
267 |
> |
} else { |
268 |
> |
while (e.hasNext()) { |
269 |
> |
if (o.equals(e.next())) { |
270 |
> |
e.remove(); |
271 |
> |
return true; |
272 |
> |
} |
273 |
> |
} |
274 |
> |
} |
275 |
> |
return false; |
276 |
|
} |
277 |
|
|
278 |
|
|
291 |
|
* @see #contains(Object) |
292 |
|
*/ |
293 |
|
public boolean containsAll(Collection<?> c) { |
294 |
< |
Iterator<?> e = c.iterator(); |
295 |
< |
while (e.hasNext()) |
296 |
< |
if (!contains(e.next())) |
297 |
< |
return false; |
298 |
< |
return true; |
294 |
> |
Iterator<?> e = c.iterator(); |
295 |
> |
while (e.hasNext()) |
296 |
> |
if (!contains(e.next())) |
297 |
> |
return false; |
298 |
> |
return true; |
299 |
|
} |
300 |
|
|
301 |
|
/** |
317 |
|
* @see #add(Object) |
318 |
|
*/ |
319 |
|
public boolean addAll(Collection<? extends E> c) { |
320 |
< |
boolean modified = false; |
321 |
< |
Iterator<? extends E> e = c.iterator(); |
322 |
< |
while (e.hasNext()) { |
323 |
< |
if (add(e.next())) |
324 |
< |
modified = true; |
325 |
< |
} |
326 |
< |
return modified; |
320 |
> |
boolean modified = false; |
321 |
> |
Iterator<? extends E> e = c.iterator(); |
322 |
> |
while (e.hasNext()) { |
323 |
> |
if (add(e.next())) |
324 |
> |
modified = true; |
325 |
> |
} |
326 |
> |
return modified; |
327 |
|
} |
328 |
|
|
329 |
|
/** |
348 |
|
* @see #contains(Object) |
349 |
|
*/ |
350 |
|
public boolean removeAll(Collection<?> c) { |
351 |
< |
boolean modified = false; |
352 |
< |
Iterator<?> e = iterator(); |
353 |
< |
while (e.hasNext()) { |
354 |
< |
if (c.contains(e.next())) { |
355 |
< |
e.remove(); |
356 |
< |
modified = true; |
357 |
< |
} |
358 |
< |
} |
359 |
< |
return modified; |
351 |
> |
boolean modified = false; |
352 |
> |
Iterator<?> e = iterator(); |
353 |
> |
while (e.hasNext()) { |
354 |
> |
if (c.contains(e.next())) { |
355 |
> |
e.remove(); |
356 |
> |
modified = true; |
357 |
> |
} |
358 |
> |
} |
359 |
> |
return modified; |
360 |
|
} |
361 |
|
|
362 |
|
/** |
381 |
|
* @see #contains(Object) |
382 |
|
*/ |
383 |
|
public boolean retainAll(Collection<?> c) { |
384 |
< |
boolean modified = false; |
385 |
< |
Iterator<E> e = iterator(); |
386 |
< |
while (e.hasNext()) { |
387 |
< |
if (!c.contains(e.next())) { |
388 |
< |
e.remove(); |
389 |
< |
modified = true; |
390 |
< |
} |
391 |
< |
} |
392 |
< |
return modified; |
384 |
> |
boolean modified = false; |
385 |
> |
Iterator<E> e = iterator(); |
386 |
> |
while (e.hasNext()) { |
387 |
> |
if (!c.contains(e.next())) { |
388 |
> |
e.remove(); |
389 |
> |
modified = true; |
390 |
> |
} |
391 |
> |
} |
392 |
> |
return modified; |
393 |
|
} |
394 |
|
|
395 |
|
/** |
408 |
|
* @throws UnsupportedOperationException {@inheritDoc} |
409 |
|
*/ |
410 |
|
public void clear() { |
411 |
< |
Iterator<E> e = iterator(); |
412 |
< |
while (e.hasNext()) { |
413 |
< |
e.next(); |
414 |
< |
e.remove(); |
415 |
< |
} |
411 |
> |
Iterator<E> e = iterator(); |
412 |
> |
while (e.hasNext()) { |
413 |
> |
e.next(); |
414 |
> |
e.remove(); |
415 |
> |
} |
416 |
|
} |
417 |
|
|
418 |
|
|
430 |
|
*/ |
431 |
|
public String toString() { |
432 |
|
Iterator<E> i = iterator(); |
433 |
< |
if (! i.hasNext()) |
434 |
< |
return "[]"; |
433 |
> |
if (! i.hasNext()) |
434 |
> |
return "[]"; |
435 |
|
|
436 |
< |
StringBuilder sb = new StringBuilder(); |
437 |
< |
sb.append('['); |
438 |
< |
for (;;) { |
439 |
< |
E e = i.next(); |
440 |
< |
sb.append(e == this ? "(this Collection)" : e); |
441 |
< |
if (! i.hasNext()) |
442 |
< |
return sb.append(']').toString(); |
443 |
< |
sb.append(", "); |
444 |
< |
} |
436 |
> |
StringBuilder sb = new StringBuilder(); |
437 |
> |
sb.append('['); |
438 |
> |
for (;;) { |
439 |
> |
E e = i.next(); |
440 |
> |
sb.append(e == this ? "(this Collection)" : e); |
441 |
> |
if (! i.hasNext()) |
442 |
> |
return sb.append(']').toString(); |
443 |
> |
sb.append(", "); |
444 |
> |
} |
445 |
|
} |
446 |
|
|
447 |
|
} |