ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/AbstractQueue.java
(Generate patch)

Comparing jsr166/src/main/java/util/AbstractQueue.java (file contents):
Revision 1.12 by dholmes, Wed Aug 6 01:57:53 2003 UTC vs.
Revision 1.21 by dl, Sun Oct 19 13:38:29 2003 UTC

# Line 7 | Line 7
7   package java.util;
8  
9   /**
10 < * <tt>AbstractQueue</tt> provides default implementations of
11 < * {@link #add add}, {@link #remove remove}, and {@link #element element}
12 < * based on
13 < * {@link #offer offer}, {@link #poll poll}, and {@link #peek peek},
14 < * respectively but that
15 < * throw exceptions instead of indicating failure via <tt>false</tt> or
10 > * This class provides skeletal implementations of some {@link Queue}
11 > * operations. The implementations in this class are appropriate when
12 > * the base implementation does <em>not</em> allow <tt>null</tt>
13 > * elements.  Methods {@link #add add}, {@link #remove remove}, and
14 > * {@link #element element} are based on {@link #offer offer}, {@link
15 > * #poll poll}, and {@link #peek peek}, respectively but throw
16 > * exceptions instead of indicating failure via <tt>false</tt> or
17   * <tt>null</tt> returns.
18 < * <p>The provided implementations all assume that the base implementation
19 < * does <em>not</em> allow <tt>null</tt> elements.
18 > *
19 > * <p> A <tt>Queue</tt> implementation that extends this class must
20 > * minimally define a method {@link Queue#offer} which does not permit
21 > * insertion of <tt>null</tt> elements, along with methods {@link
22 > * Queue#peek}, {@link Queue#poll}, {@link Collection#size}, and a
23 > * {@link Collection#iterator} supporting {@link
24 > * Iterator#remove}. Typically, additional methods will be overridden
25 > * as well. If these requirements cannot be met, consider instead
26 > * subclassing {@link AbstractCollection}.
27 > *
28   * @since 1.5
29   * @author Doug Lea
30 + * @param <E> the type of elements held in this collection
31   */
32   public abstract class AbstractQueue<E>
33      extends AbstractCollection<E>
34      implements Queue<E> {
35  
26    // note that optional methods are not optional here or in our subclasses,
27    // so we redefine each optional method to document that it is not optional
28    // We also inherit, or define, all necessary @throws comments
29
36      /**
37 <     * @throws NullPointerException if the specified element is <tt>null</tt>
37 >     * Constructor for use by subclasses.
38       */
39 <    public abstract boolean offer(E o);
39 >    protected AbstractQueue() {
40 >    }
41 >
42  
43      /**
44 <     * Adds the specified element to this queue.
44 >     * Adds the specified element to this queue. This implementation
45 >     * returns <tt>true</tt> if <tt>offer</tt> succeeds, else
46 >     * throws an IllegalStateException.
47 >     *
48 >     * @param o the element
49       * @return <tt>true</tt> (as per the general contract of
50 <     * <tt>Collection.add</tt>).
50 >     *         <tt>Collection.add</tt>).
51       *
52       * @throws NullPointerException if the specified element is <tt>null</tt>
53 +     * @throws IllegalStateException if element cannot be added
54       */
55      public boolean add(E o) {
56          if (offer(o))
# Line 47 | Line 60 | public abstract class AbstractQueue<E>
60      }
61  
62      /**
63 <     * Adds all of the elements in the specified collection to this queue.
64 <     * The behavior of this operation is undefined if
65 <     * the specified collection is modified while the operation is in
53 <     * progress.  (This implies that the behavior of this call is undefined if
54 <     * the specified collection is this queue, and this queue is nonempty.)
55 <     * <p>
56 <     * This implementation iterates over the specified collection, and adds
57 <     * each object returned by the iterator to this queue, in turn.
63 >     * Retrieves and removes the head of this queue.
64 >     * This implementation returns the result of <tt>poll</tt>
65 >     * unless the queue is empty.
66       *
67 <     * @param c collection whose elements are to be added to this queue
68 <     * @return <tt>true</tt> if this collection changed as a result of the
61 <     *         call.
62 <     * @throws NullPointerException if <tt>c</tt> or any element in <tt>c</tt>
63 <     * is <tt>null</tt>
64 <     *
67 >     * @return the head of this queue.
68 >     * @throws NoSuchElementException if this queue is empty.
69       */
66    public boolean addAll(Collection<? extends E> c) {
67        return super.addAll(c);
68    }
69
70    /** @throws NoSuchElementException {@inheritDoc} */
70      public E remove() {
71          E x = poll();
72          if (x != null)
# Line 76 | Line 75 | public abstract class AbstractQueue<E>
75              throw new NoSuchElementException();
76      }
77  
78 <    /** @throws NoSuchElementException {@inheritDoc} */
78 >
79 >    /**
80 >     * Retrieves, but does not remove, the head of this queue.  
81 >     * This implementation returns the result of <tt>peek</tt>
82 >     * unless the queue is empty.
83 >     *
84 >     * @return the head of this queue.
85 >     * @throws NoSuchElementException if this queue is empty.
86 >     */
87      public E element() {
88          E x = peek();
89          if (x != null)
# Line 96 | Line 103 | public abstract class AbstractQueue<E>
103              ;
104      }
105  
106 <    // XXX Remove this redundant declaration, pending response from Neal Gafter.
107 <    public abstract Iterator<E> iterator();
108 < }
109 <
110 <
111 <
112 <
106 >    /**
107 >     * Adds all of the elements in the specified collection to this
108 >     * queue.  Attempts to addAll of a queue to itself result in
109 >     * <tt>IllegalArgumentException</tt>. Further, the behavior of
110 >     * this operation is undefined if the specified collection is
111 >     * modified while the operation is in progress.
112 >     *
113 >     * <p>This implementation iterates over the specified collection,
114 >     * and adds each element returned by the iterator to this
115 >     * collection, in turn.  A runtime exception encountered while
116 >     * trying to add an element (including, in particular, a
117 >     * <tt>null</tt> element) may result in only some of the elements
118 >     * having been successfully added when the associated exception is
119 >     * thrown.
120 >     *
121 >     * @param c collection whose elements are to be added to this collection.
122 >     * @return <tt>true</tt> if this collection changed as a result of the
123 >     *         call.
124 >     * @throws NullPointerException if the specified collection, or
125 >     * any of its elements are null.
126 >     * @throws IllegalArgumentException if c is this queue.
127 >     *
128 >     * @see #add(Object)
129 >     */
130 >    public boolean addAll(Collection<? extends E> c) {
131 >        if (c == null)
132 >            throw new NullPointerException();
133 >        if (c == this)
134 >            throw new IllegalArgumentException();
135 >        boolean modified = false;
136 >        Iterator<? extends E> e = c.iterator();
137 >        while (e.hasNext()) {
138 >            if (add(e.next()))
139 >                modified = true;
140 >        }
141 >        return modified;
142 >    }
143  
144 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines