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.15 by dl, Fri Sep 12 17:13:11 2003 UTC vs.
Revision 1.27 by jsr166, Mon May 2 08:35:49 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;
# Line 19 | Line 19 | package java.util;
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}, {@link
23 < * Collection#remove}, and a {@link Collection#iterator} supporting
24 < * {@link Iterator#remove}. If these requirements cannot be met,
25 < * consider instead subclassing {@link AbstractCollection}.
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>
# Line 37 | Line 43 | public abstract class AbstractQueue<E>
43      protected AbstractQueue() {
44      }
45  
40    /**
41     * Inserts the specified element to this queue, if possible.
42     *
43     * @param o the element to add.
44     * @return <tt>true</tt> if it was possible to add the element to
45     *         this queue, else <tt>false</tt>
46     * @throws NullPointerException if the specified element is <tt>null</tt>
47     */
48    public boolean offer(E o) { return false; }
49    // FIXME: Replace above no-op with following abstract version
50    // when javac allows it.
51    //    public abstract boolean offer(E o);
46  
47      /**
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 <     * th
52 <     * @param o the element
50 >     * throws an IllegalStateException.
51 >     *
52 >     * @param e the element
53       * @return <tt>true</tt> (as per the general contract of
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
56 >     * @throws NullPointerException if the specified element is <tt>null</tt>.
57 >     * @throws IllegalStateException if the element cannot be added.
58       */
59 <    public boolean add(E o) {
60 <        if (offer(o))
59 >    public boolean add(E e) {
60 >        if (offer(e))
61              return true;
62          else
63              throw new IllegalStateException("Queue full");
# Line 85 | Line 79 | public abstract class AbstractQueue<E>
79              throw new NoSuchElementException();
80      }
81  
88
82      /**
83 <     * Retrieves, but does not remove, the head of this queue.  
83 >     * Retrieves, but does not remove, the head of this queue.
84       * This implementation returns the result of <tt>peek</tt>
85       * unless the queue is empty.
86       *
# Line 103 | Line 96 | public abstract class AbstractQueue<E>
96      }
97  
98      /**
99 <     * Removes all of the elements from this collection.
100 <     * The collection will be empty after this call returns.
99 >     * Removes all of the elements from this queue.
100 >     * The queue will be empty after this call returns.
101       * <p>This implementation repeatedly invokes {@link #poll poll} until it
102       * returns <tt>null</tt>.
103       */
# Line 113 | Line 106 | public abstract class AbstractQueue<E>
106              ;
107      }
108  
116    // Declarations that mostly just remove optionality clauses.
117
118    /**
119     * Removes a single instance of the specified element from this
120     * queue, if one is present.  More formally, removes an element
121     * <tt>e</tt> such that <tt>(o==null ? e==null :
122     * o.equals(e))</tt>, if the queue contains such an element.
123     * Returns <tt>true</tt> if the queue contained the specified
124     * element (or equivalently, if the queue changed as a result of
125     * the call).
126     *
127     * @param o the element to remove
128     * @return true if the element was removed
129     */
130    public abstract boolean remove(Object o);
131
132    /**
133     * Removes from this queue all of its elements that are contained in
134     * the specified collection. <p>
135     *
136     * This implementation iterates over this queue, checking each
137     * element returned by the iterator in turn to see if it's contained
138     * in the specified collection.  If it's so contained, it's removed from
139     * this queue with the iterator's <tt>remove</tt> method.<p>
140     *
141     * @param c elements to be removed from this collection.
142     * @return <tt>true</tt> if this queue changed as a result of the
143     *         call.
144     * @throws NullPointerException if the specified collection is null.
145     */
146    public boolean removeAll(Collection<?> c) {
147        return super.removeAll(c);
148    }
149
150    /**
151     * Retains only the elements in this queue that are contained in the
152     * specified collection.  In other words, removes
153     * from this queue all of its elements that are not contained in the
154     * specified collection. <p>
155     *
156     * This implementation iterates over this queue, checking each
157     * element returned by the iterator in turn to see if it's contained
158     * in the specified collection.  If it's not so contained, it's removed
159     * from this queue with the iterator's <tt>remove</tt> method.<p>
160     *
161     * @param c elements to be retained in this collection.
162     * @return <tt>true</tt> if this queue changed as a result of the
163     *         call.
164     * @throws NullPointerException if the specified collection is null.
165     *
166     */
167    public boolean retainAll(Collection<?> c) {
168        return super.retainAll(c);
169    }
170
109      /**
110       * Adds all of the elements in the specified collection to this
111 <     * queue.  The behavior of this operation is undefined if the
112 <     * specified collection is modified while the operation is in
113 <     * progress.  (This implies that the behavior of this call is
114 <     * undefined if the specified collection is this queue, and this
115 <     * queue is nonempty.)
116 <     *
117 <     * <p> This implementation iterates over the specified collection,
118 <     * and uses the <tt>add</tt> method to insert each object returned by
119 <     * the iterator to this queue, in turn.
111 >     * queue.  Attempts to addAll of a queue to itself result in
112 >     * <tt>IllegalArgumentException</tt>. Further, the behavior of
113 >     * this operation is undefined if the specified collection is
114 >     * modified while the operation is in progress.
115 >     *
116 >     * <p>This implementation iterates over the specified collection,
117 >     * and adds each element returned by the iterator to this
118 >     * queue, in turn.  A runtime exception encountered while
119 >     * trying to add an element (including, in particular, a
120 >     * <tt>null</tt> element) may result in only some of the elements
121 >     * having been successfully added when the associated exception is
122 >     * thrown.
123       *
124 <     * @param c collection whose elements are to be added to this queue
124 >     * @param c collection whose elements are to be added to this queue.
125       * @return <tt>true</tt> if this queue changed as a result of the
126       *         call.
127 <     * @throws NullPointerException if <tt>c</tt> or any element in <tt>c</tt>
128 <     * is <tt>null</tt>
129 <     * @throws IllegalStateException if any element cannot be added.
130 <     *
127 >     * @throws NullPointerException if the specified collection or
128 >     *         any of its elements are null.
129 >     * @throws IllegalArgumentException if c is this queue.
130 >     *
131 >     * @see #add(Object)
132       */
133      public boolean addAll(Collection<? extends E> c) {
134 <        return super.addAll(c);
134 >        if (c == null)
135 >            throw new NullPointerException();
136 >        if (c == this)
137 >            throw new IllegalArgumentException();
138 >        boolean modified = false;
139 >        Iterator<? extends E> e = c.iterator();
140 >        while (e.hasNext()) {
141 >            if (add(e.next()))
142 >                modified = true;
143 >        }
144 >        return modified;
145      }
146  
147   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines