1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain. Use, modify, and |
4 |
* redistribute this code in any way without acknowledgement. |
5 |
*/ |
6 |
|
7 |
package java.util; |
8 |
|
9 |
/** |
10 |
* AbstractQueue provides default implementations of add, remove, and |
11 |
* element based on offer, poll, and peek, respectively but that throw |
12 |
* exceptions instead of indicating failure via false or null returns. |
13 |
* The provided implementations all assume that the base implementation |
14 |
* does <em>not</em> allow null elements. |
15 |
*/ |
16 |
|
17 |
public abstract class AbstractQueue<E> extends AbstractCollection<E> implements Queue<E> { |
18 |
|
19 |
public boolean add(E x) { |
20 |
if (offer(x)) |
21 |
return true; |
22 |
else |
23 |
throw new IllegalStateException("Queue full"); |
24 |
} |
25 |
|
26 |
public E remove() { |
27 |
E x = poll(); |
28 |
if (x != null) |
29 |
return x; |
30 |
else |
31 |
throw new NoSuchElementException(); |
32 |
} |
33 |
|
34 |
public E element() { |
35 |
E x = peek(); |
36 |
if (x != null) |
37 |
return x; |
38 |
else |
39 |
throw new NoSuchElementException(); |
40 |
} |
41 |
|
42 |
public void clear() { |
43 |
while (poll() != null) |
44 |
; |
45 |
} |
46 |
|
47 |
} |