1 |
tim |
1.1 |
package java.util; |
2 |
|
|
|
3 |
|
|
/** |
4 |
|
|
* AbstractQueue provides default implementations of add, remove, and |
5 |
|
|
* element based on offer, poll, and peek, respectively but that throw |
6 |
|
|
* exceptions instead of indicating failure via false or null returns. |
7 |
|
|
* The provided implementations all assume that the base implementation |
8 |
|
|
* does <em>not</em> allow null elements. |
9 |
|
|
*/ |
10 |
|
|
public abstract class AbstractQueue<E> extends AbstractCollection<E> implements Queue<E> { |
11 |
|
|
|
12 |
|
|
public boolean add(E x) { |
13 |
|
|
if (offer(x)) |
14 |
|
|
return true; |
15 |
|
|
else |
16 |
|
|
throw new IllegalStateException("Queue full"); |
17 |
|
|
} |
18 |
|
|
|
19 |
|
|
public E remove() { |
20 |
|
|
E x = poll(); |
21 |
|
|
if (x != null) |
22 |
|
|
return x; |
23 |
|
|
else |
24 |
|
|
throw new NoSuchElementException(); |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
public E element() { |
28 |
|
|
E x = peek(); |
29 |
|
|
if (x != null) |
30 |
|
|
return x; |
31 |
|
|
else |
32 |
|
|
throw new NoSuchElementException(); |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
public void clear() { |
36 |
|
|
while (poll() != null) |
37 |
|
|
; |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
} |