Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/java/util/AbstractCollection.java
+++ new/src/share/classes/java/util/AbstractCollection.java
1 1 /*
2 2 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25
26 26 package java.util;
27 27
28 28 /**
29 29 * This class provides a skeletal implementation of the <tt>Collection</tt>
30 30 * interface, to minimize the effort required to implement this interface. <p>
31 31 *
32 32 * To implement an unmodifiable collection, the programmer needs only to
33 33 * extend this class and provide implementations for the <tt>iterator</tt> and
34 34 * <tt>size</tt> methods. (The iterator returned by the <tt>iterator</tt>
35 35 * method must implement <tt>hasNext</tt> and <tt>next</tt>.)<p>
36 36 *
37 37 * To implement a modifiable collection, the programmer must additionally
38 38 * override this class's <tt>add</tt> method (which otherwise throws an
39 39 * <tt>UnsupportedOperationException</tt>), and the iterator returned by the
40 40 * <tt>iterator</tt> method must additionally implement its <tt>remove</tt>
41 41 * method.<p>
42 42 *
43 43 * The programmer should generally provide a void (no argument) and
44 44 * <tt>Collection</tt> constructor, as per the recommendation in the
45 45 * <tt>Collection</tt> interface specification.<p>
46 46 *
47 47 * The documentation for each non-abstract method in this class describes its
48 48 * implementation in detail. Each of these methods may be overridden if
49 49 * the collection being implemented admits a more efficient implementation.<p>
50 50 *
51 51 * This class is a member of the
52 52 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
53 53 * Java Collections Framework</a>.
54 54 *
55 55 * @author Josh Bloch
56 56 * @author Neal Gafter
57 57 * @see Collection
58 58 * @since 1.2
59 59 */
60 60
61 61 public abstract class AbstractCollection<E> implements Collection<E> {
62 62 /**
63 63 * Sole constructor. (For invocation by subclass constructors, typically
64 64 * implicit.)
65 65 */
66 66 protected AbstractCollection() {
67 67 }
68 68
69 69 // Query Operations
70 70
71 71 /**
72 72 * Returns an iterator over the elements contained in this collection.
73 73 *
74 74 * @return an iterator over the elements contained in this collection
75 75 */
76 76 public abstract Iterator<E> iterator();
77 77
78 78 public abstract int size();
79 79
80 80 /**
81 81 * {@inheritDoc}
82 82 *
83 83 * <p>This implementation returns <tt>size() == 0</tt>.
84 84 */
85 85 public boolean isEmpty() {
86 86 return size() == 0;
87 87 }
88 88
↓ open down ↓ |
88 lines elided |
↑ open up ↑ |
89 89 /**
90 90 * {@inheritDoc}
91 91 *
92 92 * <p>This implementation iterates over the elements in the collection,
93 93 * checking each element in turn for equality with the specified element.
94 94 *
95 95 * @throws ClassCastException {@inheritDoc}
96 96 * @throws NullPointerException {@inheritDoc}
97 97 */
98 98 public boolean contains(Object o) {
99 - Iterator<E> e = iterator();
99 + Iterator<E> it = iterator();
100 100 if (o==null) {
101 - while (e.hasNext())
102 - if (e.next()==null)
101 + while (it.hasNext())
102 + if (it.next()==null)
103 103 return true;
104 104 } else {
105 - while (e.hasNext())
106 - if (o.equals(e.next()))
105 + while (it.hasNext())
106 + if (o.equals(it.next()))
107 107 return true;
108 108 }
109 109 return false;
110 110 }
111 111
112 112 /**
113 113 * {@inheritDoc}
114 114 *
115 115 * <p>This implementation returns an array containing all the elements
116 116 * returned by this collection's iterator, in the same order, stored in
117 117 * consecutive elements of the array, starting with index {@code 0}.
118 118 * The length of the returned array is equal to the number of elements
119 119 * returned by the iterator, even if the size of this collection changes
120 120 * during iteration, as might happen if the collection permits
121 121 * concurrent modification during iteration. The {@code size} method is
122 122 * called only as an optimization hint; the correct result is returned
123 123 * even if the iterator returns a different number of elements.
124 124 *
125 125 * <p>This method is equivalent to:
126 126 *
127 127 * <pre> {@code
128 128 * List<E> list = new ArrayList<E>(size());
129 129 * for (E e : this)
130 130 * list.add(e);
131 131 * return list.toArray();
132 132 * }</pre>
133 133 */
134 134 public Object[] toArray() {
135 135 // Estimate size of array; be prepared to see more or fewer elements
136 136 Object[] r = new Object[size()];
137 137 Iterator<E> it = iterator();
138 138 for (int i = 0; i < r.length; i++) {
139 139 if (! it.hasNext()) // fewer elements than expected
140 140 return Arrays.copyOf(r, i);
141 141 r[i] = it.next();
142 142 }
143 143 return it.hasNext() ? finishToArray(r, it) : r;
144 144 }
145 145
146 146 /**
147 147 * {@inheritDoc}
148 148 *
149 149 * <p>This implementation returns an array containing all the elements
150 150 * returned by this collection's iterator in the same order, stored in
151 151 * consecutive elements of the array, starting with index {@code 0}.
152 152 * If the number of elements returned by the iterator is too large to
153 153 * fit into the specified array, then the elements are returned in a
154 154 * newly allocated array with length equal to the number of elements
155 155 * returned by the iterator, even if the size of this collection
156 156 * changes during iteration, as might happen if the collection permits
157 157 * concurrent modification during iteration. The {@code size} method is
158 158 * called only as an optimization hint; the correct result is returned
159 159 * even if the iterator returns a different number of elements.
160 160 *
161 161 * <p>This method is equivalent to:
162 162 *
163 163 * <pre> {@code
164 164 * List<E> list = new ArrayList<E>(size());
165 165 * for (E e : this)
166 166 * list.add(e);
167 167 * return list.toArray(a);
168 168 * }</pre>
169 169 *
170 170 * @throws ArrayStoreException {@inheritDoc}
171 171 * @throws NullPointerException {@inheritDoc}
172 172 */
173 173 public <T> T[] toArray(T[] a) {
174 174 // Estimate size of array; be prepared to see more or fewer elements
175 175 int size = size();
176 176 T[] r = a.length >= size ? a :
177 177 (T[])java.lang.reflect.Array
178 178 .newInstance(a.getClass().getComponentType(), size);
179 179 Iterator<E> it = iterator();
180 180
181 181 for (int i = 0; i < r.length; i++) {
182 182 if (! it.hasNext()) { // fewer elements than expected
183 183 if (a != r)
184 184 return Arrays.copyOf(r, i);
185 185 r[i] = null; // null-terminate
186 186 return r;
187 187 }
188 188 r[i] = (T)it.next();
189 189 }
190 190 return it.hasNext() ? finishToArray(r, it) : r;
191 191 }
192 192
193 193 /**
194 194 * The maximum size of array to allocate.
195 195 * Some VMs reserve some header words in an array.
196 196 * Attempts to allocate larger arrays may result in
197 197 * OutOfMemoryError: Requested array size exceeds VM limit
198 198 */
199 199 private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
200 200
201 201 /**
202 202 * Reallocates the array being used within toArray when the iterator
203 203 * returned more elements than expected, and finishes filling it from
204 204 * the iterator.
205 205 *
206 206 * @param r the array, replete with previously stored elements
207 207 * @param it the in-progress iterator over this collection
208 208 * @return array containing the elements in the given array, plus any
209 209 * further elements returned by the iterator, trimmed to size
210 210 */
211 211 private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
212 212 int i = r.length;
213 213 while (it.hasNext()) {
214 214 int cap = r.length;
215 215 if (i == cap) {
216 216 int newCap = cap + (cap >> 1) + 1;
217 217 // overflow-conscious code
218 218 if (newCap - MAX_ARRAY_SIZE > 0)
219 219 newCap = hugeCapacity(cap + 1);
220 220 r = Arrays.copyOf(r, newCap);
221 221 }
222 222 r[i++] = (T)it.next();
223 223 }
224 224 // trim if overallocated
225 225 return (i == r.length) ? r : Arrays.copyOf(r, i);
226 226 }
227 227
228 228 private static int hugeCapacity(int minCapacity) {
229 229 if (minCapacity < 0) // overflow
230 230 throw new OutOfMemoryError
231 231 ("Required array size too large");
232 232 return (minCapacity > MAX_ARRAY_SIZE) ?
233 233 Integer.MAX_VALUE :
234 234 MAX_ARRAY_SIZE;
235 235 }
236 236
237 237 // Modification Operations
238 238
239 239 /**
240 240 * {@inheritDoc}
241 241 *
242 242 * <p>This implementation always throws an
243 243 * <tt>UnsupportedOperationException</tt>.
244 244 *
245 245 * @throws UnsupportedOperationException {@inheritDoc}
246 246 * @throws ClassCastException {@inheritDoc}
247 247 * @throws NullPointerException {@inheritDoc}
248 248 * @throws IllegalArgumentException {@inheritDoc}
249 249 * @throws IllegalStateException {@inheritDoc}
250 250 */
251 251 public boolean add(E e) {
252 252 throw new UnsupportedOperationException();
253 253 }
254 254
255 255 /**
256 256 * {@inheritDoc}
257 257 *
258 258 * <p>This implementation iterates over the collection looking for the
259 259 * specified element. If it finds the element, it removes the element
260 260 * from the collection using the iterator's remove method.
261 261 *
↓ open down ↓ |
145 lines elided |
↑ open up ↑ |
262 262 * <p>Note that this implementation throws an
263 263 * <tt>UnsupportedOperationException</tt> if the iterator returned by this
264 264 * collection's iterator method does not implement the <tt>remove</tt>
265 265 * method and this collection contains the specified object.
266 266 *
267 267 * @throws UnsupportedOperationException {@inheritDoc}
268 268 * @throws ClassCastException {@inheritDoc}
269 269 * @throws NullPointerException {@inheritDoc}
270 270 */
271 271 public boolean remove(Object o) {
272 - Iterator<E> e = iterator();
272 + Iterator<E> it = iterator();
273 273 if (o==null) {
274 - while (e.hasNext()) {
275 - if (e.next()==null) {
276 - e.remove();
274 + while (it.hasNext()) {
275 + if (it.next()==null) {
276 + it.remove();
277 277 return true;
278 278 }
279 279 }
280 280 } else {
281 - while (e.hasNext()) {
282 - if (o.equals(e.next())) {
283 - e.remove();
281 + while (it.hasNext()) {
282 + if (o.equals(it.next())) {
283 + it.remove();
284 284 return true;
285 285 }
286 286 }
287 287 }
288 288 return false;
289 289 }
290 290
291 291
292 292 // Bulk Operations
293 293
294 294 /**
295 295 * {@inheritDoc}
296 296 *
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
297 297 * <p>This implementation iterates over the specified collection,
298 298 * checking each element returned by the iterator in turn to see
299 299 * if it's contained in this collection. If all elements are so
300 300 * contained <tt>true</tt> is returned, otherwise <tt>false</tt>.
301 301 *
302 302 * @throws ClassCastException {@inheritDoc}
303 303 * @throws NullPointerException {@inheritDoc}
304 304 * @see #contains(Object)
305 305 */
306 306 public boolean containsAll(Collection<?> c) {
307 - Iterator<?> e = c.iterator();
308 - while (e.hasNext())
309 - if (!contains(e.next()))
307 + for (Object e : c)
308 + if (!contains(e))
310 309 return false;
311 310 return true;
312 311 }
313 312
314 313 /**
315 314 * {@inheritDoc}
316 315 *
317 316 * <p>This implementation iterates over the specified collection, and adds
318 317 * each object returned by the iterator to this collection, in turn.
319 318 *
320 319 * <p>Note that this implementation will throw an
321 320 * <tt>UnsupportedOperationException</tt> unless <tt>add</tt> is
322 321 * overridden (assuming the specified collection is non-empty).
323 322 *
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
324 323 * @throws UnsupportedOperationException {@inheritDoc}
325 324 * @throws ClassCastException {@inheritDoc}
326 325 * @throws NullPointerException {@inheritDoc}
327 326 * @throws IllegalArgumentException {@inheritDoc}
328 327 * @throws IllegalStateException {@inheritDoc}
329 328 *
330 329 * @see #add(Object)
331 330 */
332 331 public boolean addAll(Collection<? extends E> c) {
333 332 boolean modified = false;
334 - Iterator<? extends E> e = c.iterator();
335 - while (e.hasNext()) {
336 - if (add(e.next()))
333 + for (E e : c)
334 + if (add(e))
337 335 modified = true;
338 - }
339 336 return modified;
340 337 }
341 338
342 339 /**
343 340 * {@inheritDoc}
344 341 *
345 342 * <p>This implementation iterates over this collection, checking each
346 343 * element returned by the iterator in turn to see if it's contained
347 344 * in the specified collection. If it's so contained, it's removed from
348 345 * this collection with the iterator's <tt>remove</tt> method.
349 346 *
350 347 * <p>Note that this implementation will throw an
351 348 * <tt>UnsupportedOperationException</tt> if the iterator returned by the
352 349 * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
353 350 * and this collection contains one or more elements in common with the
354 351 * specified collection.
↓ open down ↓ |
6 lines elided |
↑ open up ↑ |
355 352 *
356 353 * @throws UnsupportedOperationException {@inheritDoc}
357 354 * @throws ClassCastException {@inheritDoc}
358 355 * @throws NullPointerException {@inheritDoc}
359 356 *
360 357 * @see #remove(Object)
361 358 * @see #contains(Object)
362 359 */
363 360 public boolean removeAll(Collection<?> c) {
364 361 boolean modified = false;
365 - Iterator<?> e = iterator();
366 - while (e.hasNext()) {
367 - if (c.contains(e.next())) {
368 - e.remove();
362 + Iterator<?> it = iterator();
363 + while (it.hasNext()) {
364 + if (c.contains(it.next())) {
365 + it.remove();
369 366 modified = true;
370 367 }
371 368 }
372 369 return modified;
373 370 }
374 371
375 372 /**
376 373 * {@inheritDoc}
377 374 *
378 375 * <p>This implementation iterates over this collection, checking each
379 376 * element returned by the iterator in turn to see if it's contained
380 377 * in the specified collection. If it's not so contained, it's removed
381 378 * from this collection with the iterator's <tt>remove</tt> method.
382 379 *
383 380 * <p>Note that this implementation will throw an
384 381 * <tt>UnsupportedOperationException</tt> if the iterator returned by the
385 382 * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
386 383 * and this collection contains one or more elements not present in the
387 384 * specified collection.
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
388 385 *
389 386 * @throws UnsupportedOperationException {@inheritDoc}
390 387 * @throws ClassCastException {@inheritDoc}
391 388 * @throws NullPointerException {@inheritDoc}
392 389 *
393 390 * @see #remove(Object)
394 391 * @see #contains(Object)
395 392 */
396 393 public boolean retainAll(Collection<?> c) {
397 394 boolean modified = false;
398 - Iterator<E> e = iterator();
399 - while (e.hasNext()) {
400 - if (!c.contains(e.next())) {
401 - e.remove();
395 + Iterator<E> it = iterator();
396 + while (it.hasNext()) {
397 + if (!c.contains(it.next())) {
398 + it.remove();
402 399 modified = true;
403 400 }
404 401 }
405 402 return modified;
406 403 }
407 404
408 405 /**
409 406 * {@inheritDoc}
410 407 *
411 408 * <p>This implementation iterates over this collection, removing each
412 409 * element using the <tt>Iterator.remove</tt> operation. Most
413 410 * implementations will probably choose to override this method for
↓ open down ↓ |
2 lines elided |
↑ open up ↑ |
414 411 * efficiency.
415 412 *
416 413 * <p>Note that this implementation will throw an
417 414 * <tt>UnsupportedOperationException</tt> if the iterator returned by this
418 415 * collection's <tt>iterator</tt> method does not implement the
419 416 * <tt>remove</tt> method and this collection is non-empty.
420 417 *
421 418 * @throws UnsupportedOperationException {@inheritDoc}
422 419 */
423 420 public void clear() {
424 - Iterator<E> e = iterator();
425 - while (e.hasNext()) {
426 - e.next();
427 - e.remove();
421 + Iterator<E> it = iterator();
422 + while (it.hasNext()) {
423 + it.next();
424 + it.remove();
428 425 }
429 426 }
430 427
431 428
432 429 // String conversion
433 430
434 431 /**
435 432 * Returns a string representation of this collection. The string
436 433 * representation consists of a list of the collection's elements in the
437 434 * order they are returned by its iterator, enclosed in square brackets
438 435 * (<tt>"[]"</tt>). Adjacent elements are separated by the characters
439 436 * <tt>", "</tt> (comma and space). Elements are converted to strings as
440 437 * by {@link String#valueOf(Object)}.
441 438 *
442 439 * @return a string representation of this collection
443 440 */
444 441 public String toString() {
445 - Iterator<E> i = iterator();
446 - if (! i.hasNext())
442 + Iterator<E> it = iterator();
443 + if (! it.hasNext())
447 444 return "[]";
448 445
449 446 StringBuilder sb = new StringBuilder();
450 447 sb.append('[');
451 448 for (;;) {
452 - E e = i.next();
449 + E e = it.next();
453 450 sb.append(e == this ? "(this Collection)" : e);
454 - if (! i.hasNext())
451 + if (! it.hasNext())
455 452 return sb.append(']').toString();
456 - sb.append(", ");
453 + sb.append(',').append(' ');
457 454 }
458 455 }
459 456
460 457 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX