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 |
|
/** |
134 |
|
*/ |
135 |
|
public Object[] toArray() { |
136 |
|
// Estimate size of array; be prepared to see more or fewer elements |
137 |
< |
Object[] r = new Object[size()]; |
137 |
> |
Object[] r = new Object[size()]; |
138 |
|
Iterator<E> it = iterator(); |
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; |
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 |
|
/** |
179 |
|
.newInstance(a.getClass().getComponentType(), size); |
180 |
|
Iterator<E> it = iterator(); |
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; |
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 |
|
/** |
202 |
|
* further elements returned by the iterator, trimmed to size |
203 |
|
*/ |
204 |
|
private static <T> T[] finishToArray(T[] r, Iterator<?> it) { |
205 |
< |
int i = r.length; |
205 |
> |
int i = r.length; |
206 |
|
while (it.hasNext()) { |
207 |
|
int cap = r.length; |
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; |
215 |
< |
} |
216 |
< |
r = Arrays.copyOf(r, newCap); |
217 |
< |
} |
218 |
< |
r[i++] = (T)it.next(); |
211 |
> |
if (cap == Integer.MAX_VALUE) |
212 |
> |
throw new OutOfMemoryError |
213 |
> |
("Required array size too large"); |
214 |
> |
newCap = Integer.MAX_VALUE; |
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); |
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 |
|
} |