1 2 package net.sf.bddbddb.order; 3 4 5 /*** 6 * A collection designed for holding elements prior to processing. 7 * Besides basic {@link java.util.Collection Collection} operations, queues provide 8 * additional insertion, extraction, and inspection operations. 9 * 10 * <p>Queues typically, but do not necessarily, order elements in a 11 * FIFO (first-in-first-out) manner. Among the exceptions are 12 * priority queues, which order elements according to a supplied 13 * comparator, or the elements' natural ordering, and LIFO queues (or 14 * stacks) which order the elements LIFO (last-in-first-out). 15 * Whatever the ordering used, the <em>head</em> of the queue is that 16 * element which would be removed by a call to {@link #remove() } or 17 * {@link #poll()}. In a FIFO queue, all new elements are inserted at 18 * the <em> tail</em> of the queue. Other kinds of queues may use 19 * different placement rules. Every <tt>Queue</tt> implementation 20 * must specify its ordering properties. 21 * 22 * <p>The {@link #offer offer} method inserts an element if possible, 23 * otherwise returning <tt>false</tt>. This differs from the {@link 24 * java.util.Collection#add Collection.add} method, which can fail to 25 * add an element only by throwing an unchecked exception. The 26 * <tt>offer</tt> method is designed for use when failure is a normal, 27 * rather than exceptional occurrence, for example, in fixed-capacity 28 * (or "bounded") queues. 29 * 30 * <p>The {@link #remove()} and {@link #poll()} methods remove and 31 * return the head of the queue. 32 * Exactly which element is removed from the queue is a 33 * function of the queue's ordering policy, which differs from 34 * implementation to implementation. The <tt>remove()</tt> and 35 * <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 {@link #element()} and {@link #peek()} methods return, but do 40 * not remove, the head of the queue. 41 * 42 * <p>The <tt>Queue</tt> interface does not define the <i>blocking queue 43 * methods</i>, which are common in concurrent programming. These methods, 44 * which wait for elements to appear or for space to become available, are 45 * defined in the {@link java.util.concurrent.BlockingQueue} interface, which 46 * extends this interface. 47 * 48 * <p><tt>Queue</tt> implementations generally do not allow insertion 49 * of <tt>null</tt> elements, although some implementations, such as 50 * {@link LinkedList}, do not prohibit insertion of <tt>null</tt>. 51 * Even in the implementations that permit it, <tt>null</tt> should 52 * not be inserted into a <tt>Queue</tt>, as <tt>null</tt> is also 53 * used as a special return value by the <tt>poll</tt> method to 54 * indicate that the queue contains no elements. 55 * 56 * <p><tt>Queue</tt> implementations generally do not define 57 * element-based versions of methods <tt>equals</tt> and 58 * <tt>hashCode</tt> but instead inherit the identity based versions 59 * from class <tt>Object</tt>, because element-based equality is not 60 * always well-defined for queues with the same elements but different 61 * ordering properties. 62 * 63 * 64 * <p>This interface is a member of the 65 * <a href="{@docRoot}/../guide/collections/index.html"> 66 * Java Collections Framework</a>. 67 * 68 * @see java.util.Collection 69 * @see LinkedList 70 * @see PriorityQueue 71 * @see java.util.concurrent.LinkedBlockingQueue 72 * @see java.util.concurrent.BlockingQueue 73 * @see java.util.concurrent.ArrayBlockingQueue 74 * @see java.util.concurrent.LinkedBlockingQueue 75 * @see java.util.concurrent.PriorityBlockingQueue 76 * @since 1.5 77 * @author Doug Lea 78 * @param <E> the type of elements held in this collection 79 */ 80 81 import java.util.Collection; 82 import java.util.NoSuchElementException; 83 84 85 public interface Queue extends Collection { 86 87 /*** 88 * Inserts the specified element into this queue, if possible. When 89 * using queues that may impose insertion restrictions (for 90 * example capacity bounds), method <tt>offer</tt> is generally 91 * preferable to method {@link Collection#add}, which can fail to 92 * insert an element only by throwing an exception. 93 * 94 * @param o the element to insert. 95 * @return <tt>true</tt> if it was possible to add the element to 96 * this queue, else <tt>false</tt> 97 */ 98 boolean offer(Object o); 99 100 /*** 101 * Retrieves and removes the head of this queue, or <tt>null</tt> 102 * if this queue is empty. 103 * 104 * @return the head of this queue, or <tt>null</tt> if this 105 * queue is empty. 106 */ 107 Object poll(); 108 109 /*** 110 * Retrieves and removes the head of this queue. This method 111 * differs from the <tt>poll</tt> method in that it throws an 112 * exception if this queue is empty. 113 * 114 * @return the head of this queue. 115 * @throws NoSuchElementException if this queue is empty. 116 */ 117 Object remove(); 118 119 /*** 120 * Retrieves, but does not remove, the head of this queue, 121 * returning <tt>null</tt> if this queue is empty. 122 * 123 * @return the head of this queue, or <tt>null</tt> if this queue 124 * is empty. 125 */ 126 Object peek(); 127 128 /*** 129 * Retrieves, but does not remove, the head of this queue. This method 130 * differs from the <tt>peek</tt> method only in that it throws an 131 * exception if this queue is empty. 132 * 133 * @return the head of this queue. 134 * @throws NoSuchElementException if this queue is empty. 135 */ 136 Object element(); 137 }