1   /*
2    * %W% %E%
3    *
4    * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
5    * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6    */
7   
8   package java.util;
9   
10  /**
11   * This class provides skeletal implementations of some {@link Queue}
12   * operations. The implementations in this class are appropriate when
13   * the base implementation does <em>not</em> allow <tt>null</tt>
14   * elements.  Methods {@link #add add}, {@link #remove remove}, and
15   * {@link #element element} are based on {@link #offer offer}, {@link
16   * #poll poll}, and {@link #peek peek}, respectively but throw
17   * exceptions instead of indicating failure via <tt>false</tt> or
18   * <tt>null</tt> returns.
19   *
20   * <p> A <tt>Queue</tt> implementation that extends this class must
21   * minimally define a method {@link Queue#offer} which does not permit
22   * insertion of <tt>null</tt> elements, along with methods {@link
23   * Queue#peek}, {@link Queue#poll}, {@link Collection#size}, and a
24   * {@link Collection#iterator} supporting {@link
25   * Iterator#remove}. Typically, additional methods will be overridden
26   * as well. If these requirements cannot be met, consider instead
27   * subclassing {@link AbstractCollection}.
28   *
29   * <p>This class is a member of the
30   * <a href="{@docRoot}/../technotes/guides/collections/index.html">
31   * Java Collections Framework</a>.
32   *
33   * @since 1.5
34   * @author Doug Lea
35   * @param <E> the type of elements held in this collection
36   */
37  public abstract class AbstractQueue<E>
38      extends AbstractCollection<E>
39      implements Queue<E> {
40  
41      /**
42       * Constructor for use by subclasses.
43       */
44      protected AbstractQueue() {
45      }
46  
47      /**
48       * Inserts the specified element into this queue if it is possible to do so
49       * immediately without violating capacity restrictions, returning
50       * <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
51       * if no space is currently available.
52       *
53       * <p>This implementation returns <tt>true</tt> if <tt>offer</tt> succeeds,
54       * else throws an <tt>IllegalStateException</tt>.
55       *
56       * @param e the element to add
57       * @return <tt>true</tt> (as specified by {@link Collection#add})
58       * @throws IllegalStateException if the element cannot be added at this
59       *         time due to capacity restrictions
60       * @throws ClassCastException if the class of the specified element
61       *         prevents it from being added to this queue
62       * @throws NullPointerException if the specified element is null and
63       *         this queue does not permit null elements
64       * @throws IllegalArgumentException if some property of this element
65       *         prevents it from being added to this queue
66       */
67      public boolean add(E e) {
68          if (offer(e))
69              return true;
70          else
71              throw new IllegalStateException("Queue full");
72      }
73  
74      /**
75       * Retrieves and removes the head of this queue.  This method differs
76       * from {@link #poll poll} only in that it throws an exception if this
77       * queue is empty.
78       *
79       * <p>This implementation returns the result of <tt>poll</tt>
80       * unless the queue is empty.
81       *
82       * @return the head of this queue
83       * @throws NoSuchElementException if this queue is empty
84       */
85      public E remove() {
86          E x = poll();
87          if (x != null)
88              return x;
89          else
90              throw new NoSuchElementException();
91      }
92  
93      /**
94       * Retrieves, but does not remove, the head of this queue.  This method
95       * differs from {@link #peek peek} only in that it throws an exception if
96       * this queue is empty.
97       *
98       * <p>This implementation returns the result of <tt>peek</tt>
99       * unless the queue is empty.
100      *
101      * @return the head of this queue
102      * @throws NoSuchElementException if this queue is empty
103      */
104     public E element() {
105         E x = peek();
106         if (x != null)
107             return x;
108         else
109             throw new NoSuchElementException();
110     }
111 
112     /**
113      * Removes all of the elements from this queue.
114      * The queue will be empty after this call returns.
115      *
116      * <p>This implementation repeatedly invokes {@link #poll poll} until it
117      * returns <tt>null</tt>.
118      */
119     public void clear() {
120         while (poll() != null)
121             ;
122     }
123 
124     /**
125      * Adds all of the elements in the specified collection to this
126      * queue.  Attempts to addAll of a queue to itself result in
127      * <tt>IllegalArgumentException</tt>. Further, the behavior of
128      * this operation is undefined if the specified collection is
129      * modified while the operation is in progress.
130      *
131      * <p>This implementation iterates over the specified collection,
132      * and adds each element returned by the iterator to this
133      * queue, in turn.  A runtime exception encountered while
134      * trying to add an element (including, in particular, a
135      * <tt>null</tt> element) may result in only some of the elements
136      * having been successfully added when the associated exception is
137      * thrown.
138      *
139      * @param c collection containing elements to be added to this queue
140      * @return <tt>true</tt> if this queue changed as a result of the call
141      * @throws ClassCastException if the class of an element of the specified
142      *         collection prevents it from being added to this queue
143      * @throws NullPointerException if the specified collection contains a
144      *         null element and this queue does not permit null elements,
145      *         or if the specified collection is null
146      * @throws IllegalArgumentException if some property of an element of the
147      *         specified collection prevents it from being added to this
148      *         queue, or if the specified collection is this queue
149      * @throws IllegalStateException if not all the elements can be added at
150      *         this time due to insertion restrictions
151      * @see #add(Object)
152      */
153     public boolean addAll(Collection<? extends E> c) {
154         if (c == null)
155             throw new NullPointerException();
156         if (c == this)
157             throw new IllegalArgumentException();
158         boolean modified = false;
159         Iterator<? extends E> e = c.iterator();
160         while (e.hasNext()) {
161             if (add(e.next()))
162                 modified = true;
163         }
164         return modified;
165     }
166 
167 }
168