[cvs] / jsr166 / src / main / java / util / Queue.java Repository:
ViewVC logotype

Diff of /jsr166/src/main/java/util/Queue.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1, Wed May 14 21:30:45 2003 UTC revision 1.7, Tue Jun 24 14:34:30 2003 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.
5     */
6    
7  package java.util;  package java.util;
8    
9  /**  /**
10   * Queues are Collections supporting additional basic insertion,   * A Collection designed for holding elements prior to processing.
11   * extraction, and inspection operations.   * Besides basic {@link Collection} operations, queues provide
12   *   * additional insertion, extraction, and inspection operations.
13   * <p> Queues typically, but do not necessarily order elements in a  0 *
14   * FIFO (first-in-first-out) manner. Among the exceptions are priority   * <p>Queues typically, but do not necessarily, order elements in a
15   * queues, that order elements in accord with supplied   * FIFO (first-in-first-out) manner.  Among the exceptions are
16   * Comparators. Every Queue implementation must specify its ordering   * priority queues, which order elements according to a supplied
17   * guarantees,   * comparator, or the elements' natural ordering.  Every Queue
18   *   * implementation must specify its ordering guarantees.
19   * <p> The <tt>offer</tt> method adds an element if possible,   *
20   * otherwise returning <tt>false</tt>. This differs from the   * <p>The {@link #offer(E)} method adds an element if possible, otherwise
21   * Collections.add method, that throws an unchecked exception upon   * returning <tt>false</tt>.  This differs from the {@link
22     * Collections#add(Object)} method, which throws an unchecked exception upon
23   * failure. It is designed for use in collections in which failure to   * failure. It is designed for use in collections in which failure to
24   * add is a normal, rather than exceptional occurrence, for example,   * add is a normal, rather than exceptional occurrence, for example,
25   * in fixed-capacity queues.   * in fixed-capacity (or &ldquo;bounded&rdquo;) queues.
26    
27   *   *
28   * <p> The <tt>remove</tt> and <tt>poll</tt> methods delete and return   * <p>The {@link #remove()} and {@link #poll()} methods remove and
29   * an element in accord with the implementation's ordering policies --   * return an element in accord with the implementation's ordering
30   * for example, in FIFO queues, it will return the oldest element.   * policy.  Exactly which element is removed from the queue is a
31   * The <tt>remove</tt> and <tt>poll</tt> differ only in their behavior   * function of the queue's ordering policy, which differs from
32   * when the queue is empty: <tt>poll</tt> returns <tt>null</tt> while   * implementation to implementation.  Possible orderings include (but
33   * <tt>remove</tt> throws an exception. These are designed for usage   * are not limited to) first-in-first-out (FIFO), last-in-first-out
34   * contexts in which emptiness is considered to be normal versus   * (LIFO), element priority, and arbitrary.  The <tt>remove()</tt> and
35   * exceptional.   * <tt>poll()</tt> methods differ only in their behavior when the
36     * queue is empty: the <tt>remove()</tt> method throws an exception,
37     * while the <tt>poll()</tt> method returns <tt>null</tt>.
38   *   *
39   * <p> The <tt>element</tt> and <tt>peek</tt> methods return but do   * <p>The {@link #element()} and {@link #peek()} methods return but do
40   * not delete the element that would be obtained by a call to   * not delete the element that would be obtained by a call to
41   * <tt>remove</tt> and <tt>poll</tt> respectively.   * the <tt>remove</tt> and <tt>poll</tt> methods respectively.
42   *   *
43   * <p> The Queue interface does not define blocking queue methods   * <p>The <tt>Queue</tt> interface does not define the <i>blocking queue
44   * (i.e., those that wait for elements to appear and/or for space to   * methods</i>, which are common in concurrent programming.  These methods,
45   * be available) that are common in concurrent programming. These are   * which wait for elements to appear or for space to become available, are
46   * defined in the extended java.util.concurrent.BlockingQueue   * defined in the {@link java.util.concurrent.BlockingQueue} interface, which
47   * interface.   * extends this interface.
48   *   *
49   * <p> Queue implementations generally do not allow insertion of   * <p><tt>Queue</tt> implementations generally do not allow insertion
50   * <tt>null</tt>. Even in those that allow it, it is a very bad idea   * of <tt>null</tt> elements, although some implementations, such as
51   * to do so, since <tt>null</tt> is also used as a sentinel by   * {@link LinkedList}, do not prohibit insertion of <tt>null</tt>.
52   * <tt>poll</tt> to indicate that no elements exist.   * Even in the implementations that permit it, <tt>null</tt> should
53   **/   * not be inserted into a <tt>Queue</tt>, as <tt>null</tt> is also
54     * used as a special return value by the <tt>poll</tt> method to
55     * indicate that the queue contains no elements.
56     *
57     * <p>This interface is a member of the
58     * <a href="{@docRoot}/../guide/collections/index.html">
59     * Java Collections Framework</a>.
60     *
61     * @see Collection
62     * @see LinkedList
63     * @see PriorityQueue
64     * @see java.util.concurrent.LinkedQueue
65     * @see java.util.concurrent.BlockingQueue
66     * @see java.util.concurrent.ArrayBlockingQueue
67     * @see java.util.concurrent.LinkedBlockingQueue
68     * @see java.util.concurrent.PriorityBlockingQueue
69     * @since 1.5
70     * @author Doug Lea
71     */
72  public interface Queue<E> extends Collection<E> {  public interface Queue<E> extends Collection<E> {
73        /**
74         * Add the specified element to this queue, if possible.
75         *
76         * @param element the element to add.
77         * @return true if it was possible to add the element to the queue.
78         */
79        boolean offer(E element);
80    
81        /**
82         * Remove and return an element from the queue if one is available.
83         *
84         * @return an element previously on the queue, or <tt>null</tt> if the
85         *         queue is empty.
86         */
87        E poll();
88    
89      /**      /**
90       * Add the given object to this queue if possible.       * Remove and return an element from the queue.  This method differs
91       * @param x the object to add       * from the <tt>poll</tt> method in that it throws an exception if the
92       * @return true if successful       * queue is empty.
93       **/       *
94      public boolean offer(E x);       * @return an element previously on the queue.
95         * @throws NoSuchElementException if the queue is empty.
96      /**       */
97       * Delete and return an object from the queue if one is available.      E remove() throws NoSuchElementException;
98       * @return the object, or null if the queue is empty.  
99       **/      /**
100      public E poll();       * Return, but do not remove, an element from the queue, or <tt>null</tt>
101         * if the queue is empty.  This method returns the same object reference
102      /**       * that would be returned by by the <tt>poll</tt> method.  The two methods
103       * Delete and return the element produced by poll, if the queue is       * differ in that this method does not remove the element from the queue.
104       * not empty.       *
105       * @return an element       * @return an element on the queue, or <tt>null</tt> if the queue is empty.
106       * @throws NoSuchElementException if empty       */
107       **/      E peek();
108      public E remove() throws NoSuchElementException;  
109        /**
110      /**       * Return, but do not remove, an element from the queue.  This method
111       * Return but do not delete the element that will be returned by       * differs from the <tt>peek</tt> method in that it throws an exception if
112       * the next call to poll.       * the queue is empty.
113       * @return an element, or null if empty       *
114       **/       * @return an element on the queue.
115      public E peek();       * @throws NoSuchElementException if the queue is empty.
116         */
117      /**      E element() throws NoSuchElementException;
      * Return but do not delete the element that will be returned by  
      * the next call to poll, if the queue is not empty.  
      * @return an element  
      * @throws NoSuchElementException if empty  
      **/  
     public E element() throws NoSuchElementException;  
118  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.7

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8