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