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.25 by dl, Tue Jan 27 11:36:26 2004 UTC

# Line 1 | Line 1
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.
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/licenses/publicdomain
5   */
6  
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 > * <p>This class is a member of the
29 > * <a href="{@docRoot}/../guide/collections/index.html">
30 > * Java Collections Framework</a>.
31 > *  
32   * @since 1.5
33   * @author Doug Lea
34 + * @param <E> the type of elements held in this collection
35   */
36   public abstract class AbstractQueue<E>
37      extends AbstractCollection<E>
38      implements Queue<E> {
39  
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
40      /**
41 <     * @throws NullPointerException if the specified element is <tt>null</tt>
41 >     * Constructor for use by subclasses.
42       */
43 <    public abstract boolean offer(E o);
43 >    protected AbstractQueue() {
44 >    }
45 >
46  
47      /**
48 <     * Adds the specified element to this queue.
48 >     * Adds the specified element to this queue. This implementation
49 >     * returns <tt>true</tt> if <tt>offer</tt> succeeds, else
50 >     * throws an IllegalStateException.
51 >     *
52 >     * @param o the element
53       * @return <tt>true</tt> (as per the general contract of
54 <     * <tt>Collection.add</tt>).
54 >     *         <tt>Collection.add</tt>).
55       *
56       * @throws NullPointerException if the specified element is <tt>null</tt>
57 +     * @throws IllegalStateException if element cannot be added
58       */
59      public boolean add(E o) {
60          if (offer(o))
# Line 47 | Line 64 | public abstract class AbstractQueue<E>
64      }
65  
66      /**
67 <     * Adds all of the elements in the specified collection to this queue.
68 <     * The behavior of this operation is undefined if
69 <     * 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.
67 >     * Retrieves and removes the head of this queue.
68 >     * This implementation returns the result of <tt>poll</tt>
69 >     * unless the queue is empty.
70       *
71 <     * @param c collection whose elements are to be added to this queue
72 <     * @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 <     *
71 >     * @return the head of this queue.
72 >     * @throws NoSuchElementException if this queue is empty.
73       */
66    public boolean addAll(Collection<? extends E> c) {
67        return super.addAll(c);
68    }
69
70    /** @throws NoSuchElementException {@inheritDoc} */
74      public E remove() {
75          E x = poll();
76          if (x != null)
# Line 76 | Line 79 | public abstract class AbstractQueue<E>
79              throw new NoSuchElementException();
80      }
81  
82 <    /** @throws NoSuchElementException {@inheritDoc} */
82 >
83 >    /**
84 >     * Retrieves, but does not remove, the head of this queue.  
85 >     * This implementation returns the result of <tt>peek</tt>
86 >     * unless the queue is empty.
87 >     *
88 >     * @return the head of this queue.
89 >     * @throws NoSuchElementException if this queue is empty.
90 >     */
91      public E element() {
92          E x = peek();
93          if (x != null)
# Line 96 | Line 107 | public abstract class AbstractQueue<E>
107              ;
108      }
109  
110 <    // XXX Remove this redundant declaration, pending response from Neal Gafter.
111 <    public abstract Iterator<E> iterator();
112 < }
113 <
114 <
115 <
116 <
110 >    /**
111 >     * Adds all of the elements in the specified collection to this
112 >     * queue.  Attempts to addAll of a queue to itself result in
113 >     * <tt>IllegalArgumentException</tt>. Further, the behavior of
114 >     * this operation is undefined if the specified collection is
115 >     * modified while the operation is in progress.
116 >     *
117 >     * <p>This implementation iterates over the specified collection,
118 >     * and adds each element returned by the iterator to this
119 >     * collection, in turn.  A runtime exception encountered while
120 >     * trying to add an element (including, in particular, a
121 >     * <tt>null</tt> element) may result in only some of the elements
122 >     * having been successfully added when the associated exception is
123 >     * thrown.
124 >     *
125 >     * @param c collection whose elements are to be added to this collection.
126 >     * @return <tt>true</tt> if this collection changed as a result of the
127 >     *         call.
128 >     * @throws NullPointerException if the specified collection or
129 >     * any of its elements are null.
130 >     * @throws IllegalArgumentException if c is this queue.
131 >     *
132 >     * @see #add(Object)
133 >     */
134 >    public boolean addAll(Collection<? extends E> c) {
135 >        if (c == null)
136 >            throw new NullPointerException();
137 >        if (c == this)
138 >            throw new IllegalArgumentException();
139 >        boolean modified = false;
140 >        Iterator<? extends E> e = c.iterator();
141 >        while (e.hasNext()) {
142 >            if (add(e.next()))
143 >                modified = true;
144 >        }
145 >        return modified;
146 >    }
147  
148 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines