01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package java.util;
17:
18: /**
19: * Skeletal implementation of the Queue interface. <a
20: * href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/AbstractQueue.html">[Sun
21: * docs]</a>
22: *
23: * @param <E> element type.
24: */
25: public abstract class AbstractQueue<E> extends AbstractCollection<E>
26: implements Queue<E> {
27:
28: // Should not be instantiated directly.
29: protected AbstractQueue() {
30: }
31:
32: @Override
33: public boolean add(E o) {
34: if (offer(o)) {
35: return true;
36: }
37: throw new IllegalStateException(
38: "Unable to add element to queue");
39: }
40:
41: @Override
42: public boolean addAll(Collection<? extends E> c) {
43: if (c == this ) {
44: throw new IllegalArgumentException(
45: "Can't add a queue to itself");
46: }
47: boolean modified = false;
48: for (E val : c) {
49: modified |= add(val);
50: }
51: return modified;
52: }
53:
54: @Override
55: public void clear() {
56: while (poll() != null) {
57: // empty loop
58: }
59: }
60:
61: public E element() {
62: E e = peek();
63: if (e == null) {
64: throw new NoSuchElementException("Queue is empty");
65: }
66: return e;
67: }
68:
69: public abstract boolean offer(E o);
70:
71: public abstract E peek();
72:
73: public abstract E poll();
74:
75: public E remove() {
76: E e = poll();
77: if (e == null) {
78: throw new NoSuchElementException("Queue is empty");
79: }
80: return e;
81: }
82:
83: }
|