Queue ADT Specification

Let’s document specifications to make it clear what each method is expected to do and under what conditions it might throw exceptions.

/**
 * A Queue is a collection that supports first-in-first-out (FIFO) access.
 * @param <T> the type of elements in this queue
 */
public interface Queue<T> {
  /**
   * Adds an item to the back of this queue.
   * @param item the item to be added to this queue
   * @throws IllegalArgumentException if the item is null
   */
  void enqueue(T item);

  /**
   * Removes the item at the front of this queue.
   * @throws NoSuchElementException if this queue is empty
   */
  void dequeue();

  /**
   * Returns the item at the front of this queue without removing it.
   * @return the item at the front of this queue
   * @throws NoSuchElementException if this queue is empty
   */
  T front();

  /**
   * Returns true if this queue contains no elements.
   * @return true if this queue is empty, false otherwise
   */
  boolean isEmpty();
}

These are the same rules we had for the stack, applied to the queue’s two ends.

You can find a suite of tests for this interface in the QueueTest class within the code accompanying this chapter that further clarifies the expected behavior of the queue ADT.