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.11 by dholmes, Mon Aug 4 01:50:33 2003 UTC vs.
Revision 1.31 by jsr166, Mon Jul 18 19:14:17 2005 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 + import java.util.*; // for javadoc (till 6280605 is fixed)
9  
10   /**
11 < * <tt>AbstractQueue</tt> provides default implementations of
12 < * {@link #add add}, {@link #remove remove}, and {@link #element element}
13 < * based on
14 < * {@link #offer offer}, {@link #poll poll}, and {@link #peek peek},
15 < * respectively but that
16 < * throw exceptions instead of indicating failure via <tt>false</tt> or
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 < * <p>The provided implementations all assume that the base implementation
20 < * does <em>not</em> allow <tt>null</tt> elements.
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}/../guide/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 <    // note that optional methods are not optional here or in our subclasses,
42 <    // so we redefine each optional method to document that it is not optional
43 <    // We also inherit, or define, all necessary @throws comments
41 >    /**
42 >     * Constructor for use by subclasses.
43 >     */
44 >    protected AbstractQueue() {
45 >    }
46  
47      /**
48 <     * Adds the specified element to this queue.
49 <     * @return <tt>true</tt> (as per the general contract of
50 <     * <tt>Collection.add</tt>).
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 <     * @throws NullPointerException if the specified element is <tt>null</tt>
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 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 o) {
68 <        if (offer(o))
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 <     * Adds all of the elements in the specified collection to this queue.
76 <     * The behavior of this operation is undefined if
77 <     * the specified collection is modified while the operation is in
78 <     * progress.  (This implies that the behavior of this call is undefined if
79 <     * the specified collection is this queue, and this queue is nonempty.)
80 <     * <p>
81 <     * This implementation iterates over the specified collection, and adds
82 <     * each object returned by the iterator to this collection, in turn.
83 <     *
54 <     * @param c collection whose elements are to be added to this queue
55 <     * @return <tt>true</tt> if this collection changed as a result of the
56 <     *         call.
57 <     * @throws NullPointerException if <tt>c</tt> or any element in <tt>c</tt>
58 <     * is <tt>null</tt>
59 <     *
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       */
61    public boolean addAll(Collection<? extends E> c) {
62        return super.addAll(c);
63    }
64
65    /** @throws NoSuchElementException {@inheritDoc} */
85      public E remove() {
86          E x = poll();
87          if (x != null)
# Line 71 | Line 90 | public abstract class AbstractQueue<E>
90              throw new NoSuchElementException();
91      }
92  
93 <    /** @throws NoSuchElementException {@inheritDoc} */
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)
# Line 81 | Line 110 | public abstract class AbstractQueue<E>
110      }
111  
112      /**
113 <     * Removes all of the elements from this collection.
114 <     * The collection will be empty after this call returns.
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       */
# Line 91 | Line 121 | public abstract class AbstractQueue<E>
121              ;
122      }
123  
124 <    // XXX Remove this redundant declaration, pending response from Neal Gafter.
125 <    public abstract Iterator<E> iterator();
126 < }
127 <
128 <
129 <
130 <
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 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines