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.14 by dl, Fri Sep 12 15:38:26 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   * This class provides skeletal implementations of some {@link Queue}
# Line 19 | Line 20 | package java.util;
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}, {@link
24 < * Collection#remove}, and a {@link Collection#iterator} supporting
25 < * {@link Iterator#remove}. If these requirements cannot be met,
26 < * consider instead subclassing {@link AbstractCollection}.
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>
# Line 38 | Line 45 | public abstract class AbstractQueue<E>
45      }
46  
47      /**
48 <     * Inserts the specified element to this queue, if possible.
49 <     *
50 <     * @param o the element to add.
51 <     * @return <tt>true</tt> if it was possible to add the element to
52 <     *         this queue, else <tt>false</tt>
53 <     * @throws NullPointerException if the specified element is <tt>null</tt>
54 <     */
55 <    //    public boolean offer(E o) { return false; }
56 <    // FIXME: Replace above no-op with following abstract version
57 <    // when javac allows it.
58 <    public abstract boolean offer(E o);
59 <
60 <    /**
61 <     * Adds the specified element to this queue. This implementation
62 <     * returns <tt>true</tt> if <tt>offer</tt> succeeds, else
63 <     * throws an IllegalStateException.
64 <     * th
65 <     * @param o the element
59 <     * @return <tt>true</tt> (as per the general contract of
60 <     *         <tt>Collection.add</tt>).
61 <     *
62 <     * @throws NullPointerException if the specified element is <tt>null</tt>
63 <     * @throws IllegalStateException if element cannot be added
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 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 <     * Retrieves and removes the head of this queue.
76 <     * This implementation returns the result of <tt>poll</tt>
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.
82 >     * @return the head of this queue
83 >     * @throws NoSuchElementException if this queue is empty
84       */
85      public E remove() {
86          E x = poll();
# Line 85 | Line 90 | public abstract class AbstractQueue<E>
90              throw new NoSuchElementException();
91      }
92  
88
93      /**
94 <     * Retrieves, but does not remove, the head of this queue.  
95 <     * This implementation returns the result of <tt>peek</tt>
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.
101 >     * @return the head of this queue
102 >     * @throws NoSuchElementException if this queue is empty
103       */
104      public E element() {
105          E x = peek();
# Line 103 | 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 113 | Line 121 | public abstract class AbstractQueue<E>
121              ;
122      }
123  
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
124      /**
125       * Adds all of the elements in the specified collection to this
126 <     * queue.  The behavior of this operation is undefined if the
127 <     * specified collection is modified while the operation is in
128 <     * progress.  (This implies that the behavior of this call is
129 <     * undefined if the specified collection is this queue, and this
130 <     * queue is nonempty.)
131 <     *
132 <     * <p> This implementation iterates over the specified collection,
133 <     * and uses the <tt>add</tt> method to insert each object returned by
134 <     * the iterator to this queue, in turn.
135 <     *
136 <     * @param c collection whose elements are to be added to this queue
137 <     * @return <tt>true</tt> if this queue changed as a result of the
138 <     *         call.
139 <     * @throws NullPointerException if <tt>c</tt> or any element in <tt>c</tt>
140 <     * is <tt>null</tt>
141 <     * @throws IllegalStateException if any element cannot be added.
142 <     *
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 <        return super.addAll(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