1. /*
  2. * @(#)AbstractQueue.java 1.6 04/01/27
  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. * This class provides skeletal implementations of some {@link Queue}
  10. * operations. The implementations in this class are appropriate when
  11. * the base implementation does <em>not</em> allow <tt>null</tt>
  12. * elements. Methods {@link #add add}, {@link #remove remove}, and
  13. * {@link #element element} are based on {@link #offer offer}, {@link
  14. * #poll poll}, and {@link #peek peek}, respectively but throw
  15. * exceptions instead of indicating failure via <tt>false</tt> or
  16. * <tt>null</tt> returns.
  17. *
  18. * <p> A <tt>Queue</tt> implementation that extends this class must
  19. * minimally define a method {@link Queue#offer} which does not permit
  20. * insertion of <tt>null</tt> elements, along with methods {@link
  21. * Queue#peek}, {@link Queue#poll}, {@link Collection#size}, and a
  22. * {@link Collection#iterator} supporting {@link
  23. * Iterator#remove}. Typically, additional methods will be overridden
  24. * as well. If these requirements cannot be met, consider instead
  25. * subclassing {@link AbstractCollection}.
  26. *
  27. * <p>This class is a member of the
  28. * <a href="{@docRoot}/../guide/collections/index.html">
  29. * Java Collections Framework</a>.
  30. *
  31. * @since 1.5
  32. * @author Doug Lea
  33. * @param <E> the type of elements held in this collection
  34. */
  35. public abstract class AbstractQueue<E>
  36. extends AbstractCollection<E>
  37. implements Queue<E> {
  38. /**
  39. * Constructor for use by subclasses.
  40. */
  41. protected AbstractQueue() {
  42. }
  43. /**
  44. * Adds the specified element to this queue. This implementation
  45. * returns <tt>true</tt> if <tt>offer</tt> succeeds, else
  46. * throws an IllegalStateException.
  47. *
  48. * @param o the element
  49. * @return <tt>true</tt> (as per the general contract of
  50. * <tt>Collection.add</tt>).
  51. *
  52. * @throws NullPointerException if the specified element is <tt>null</tt>
  53. * @throws IllegalStateException if element cannot be added
  54. */
  55. public boolean add(E o) {
  56. if (offer(o))
  57. return true;
  58. else
  59. throw new IllegalStateException("Queue full");
  60. }
  61. /**
  62. * Retrieves and removes the head of this queue.
  63. * This implementation returns the result of <tt>poll</tt>
  64. * unless the queue is empty.
  65. *
  66. * @return the head of this queue.
  67. * @throws NoSuchElementException if this queue is empty.
  68. */
  69. public E remove() {
  70. E x = poll();
  71. if (x != null)
  72. return x;
  73. else
  74. throw new NoSuchElementException();
  75. }
  76. /**
  77. * Retrieves, but does not remove, the head of this queue.
  78. * This implementation returns the result of <tt>peek</tt>
  79. * unless the queue is empty.
  80. *
  81. * @return the head of this queue.
  82. * @throws NoSuchElementException if this queue is empty.
  83. */
  84. public E element() {
  85. E x = peek();
  86. if (x != null)
  87. return x;
  88. else
  89. throw new NoSuchElementException();
  90. }
  91. /**
  92. * Removes all of the elements from this collection.
  93. * The collection will be empty after this call returns.
  94. * <p>This implementation repeatedly invokes {@link #poll poll} until it
  95. * returns <tt>null</tt>.
  96. */
  97. public void clear() {
  98. while (poll() != null)
  99. ;
  100. }
  101. /**
  102. * Adds all of the elements in the specified collection to this
  103. * queue. Attempts to addAll of a queue to itself result in
  104. * <tt>IllegalArgumentException</tt>. Further, the behavior of
  105. * this operation is undefined if the specified collection is
  106. * modified while the operation is in progress.
  107. *
  108. * <p>This implementation iterates over the specified collection,
  109. * and adds each element returned by the iterator to this
  110. * collection, in turn. A runtime exception encountered while
  111. * trying to add an element (including, in particular, a
  112. * <tt>null</tt> element) may result in only some of the elements
  113. * having been successfully added when the associated exception is
  114. * thrown.
  115. *
  116. * @param c collection whose elements are to be added to this collection.
  117. * @return <tt>true</tt> if this collection changed as a result of the
  118. * call.
  119. * @throws NullPointerException if the specified collection or
  120. * any of its elements are null.
  121. * @throws IllegalArgumentException if c is this queue.
  122. *
  123. * @see #add(Object)
  124. */
  125. public boolean addAll(Collection<? extends E> c) {
  126. if (c == null)
  127. throw new NullPointerException();
  128. if (c == this)
  129. throw new IllegalArgumentException();
  130. boolean modified = false;
  131. Iterator<? extends E> e = c.iterator();
  132. while (e.hasNext()) {
  133. if (add(e.next()))
  134. modified = true;
  135. }
  136. return modified;
  137. }
  138. }