edu.cmu.cs.pattis.cs151xx.orderedCollections
Class LinkedQueue<E>

java.lang.Object
  extended by edu.cmu.cs.pattis.cs151xx.orderedCollections.AbstractOrderedCollection<E>
      extended by edu.cmu.cs.pattis.cs151xx.orderedCollections.AbstractQueue<E>
          extended by edu.cmu.cs.pattis.cs151xx.orderedCollections.LinkedQueue<E>
All Implemented Interfaces:
OrderedCollection<E>, java.lang.Iterable<E>

public class LinkedQueue<E>
extends AbstractQueue<E>

This class implements the OrderedCollection interface with FIFO behavior: first in/first out. It is backed by a linked list. It makes a guarantee that the iteration order is FIFO. This class permits the null element. This class offers constant time performance for the basic operations (add, remove, peekNext, and size). Iterating over this set requires time proportional to the size of the queue.

Note that this implementation is not synchronized. If multiple threads access a queue concurrently, and at least one of the threads modifies the queue, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the queue.

The iterators returned by this class's iterator method are fail-fast: if the queue is modified at any time after the iterator is created, in any way, the Iterator throws a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.


Constructor Summary
LinkedQueue()
          Constructs a new, empty queue.
LinkedQueue(java.util.Collection<E> c)
          Constructs a new queue containing the elements in the specified collection.
LinkedQueue(E[] o)
          Constructs a new queue containing the elements in the specified array.
LinkedQueue(OrderedCollection<E> o)
          Constructs a new queue containing the elements in the specified ordered collection.
 
Method Summary
 boolean add(E o)
          Ensures that this queue contains the specified element.
 void clear()
          Removes all of the elements from this queue.
 java.util.Iterator<E> iterator()
          Returns an iterator over the elements in this queue.
 E peek()
          Returns (without removing) the next elements in this queue, according to FIFO behavior.
 E remove()
          Returns/removes the next element in this queue, according to FIFO behavior.
 java.lang.String toString()
          Returns a string representation of this queue.
 
Methods inherited from class edu.cmu.cs.pattis.cs151xx.orderedCollections.AbstractQueue
equals
 
Methods inherited from class edu.cmu.cs.pattis.cs151xx.orderedCollections.AbstractOrderedCollection
addAll, hashCode, isEmpty, size, toArray, toArray, toCollection
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LinkedQueue

public LinkedQueue()
Constructs a new, empty queue.


LinkedQueue

public LinkedQueue(OrderedCollection<E> o)
Constructs a new queue containing the elements in the specified ordered collection.

Parameters:
o - - the ordered collection whose elements are to be placed into this queue.

LinkedQueue

public LinkedQueue(java.util.Collection<E> c)
Constructs a new queue containing the elements in the specified collection.

Parameters:
c - - the collection whose elements are to be placed into this queue.

LinkedQueue

public LinkedQueue(E[] o)
Constructs a new queue containing the elements in the specified array.

Parameters:
o - - the array whose elements are to be placed into this queue.
Method Detail

clear

public void clear()
Removes all of the elements from this queue. This queue will be empty after this method returns. This method overrides the one in AbstractOrderedCollection and is more efficient.

Specified by:
clear in interface OrderedCollection<E>
Overrides:
clear in class AbstractOrderedCollection<E>

add

public boolean add(E o)
Ensures that this queue contains the specified element. Returns true if this collection changed as a result of the call. This always happens because queues allow duplicates.

Specified by:
add in interface OrderedCollection<E>
Overrides:
add in class AbstractOrderedCollection<E>
Parameters:
o - - the element to store in the queue
Returns:
true if this queue collection changed as a result of the call.

remove

public E remove()
         throws java.util.NoSuchElementException
Returns/removes the next element in this queue, according to FIFO behavior.

Specified by:
remove in interface OrderedCollection<E>
Overrides:
remove in class AbstractOrderedCollection<E>
Returns:
the next element in this queue, according to FIFO behavior.
Throws:
java.util.NoSuchElementException - if the queue is empty

peek

public E peek()
       throws java.util.NoSuchElementException
Returns (without removing) the next elements in this queue, according to FIFO behavior.

Specified by:
peek in interface OrderedCollection<E>
Specified by:
peek in class AbstractOrderedCollection<E>
Returns:
the next element in this queue, according to FIFO behavior.
Throws:
java.util.NoSuchElementException - if the queue is empty

iterator

public java.util.Iterator<E> iterator()
Returns an iterator over the elements in this queue. The ordered is guaranteed to exhibit FIFO behavior.

Specified by:
iterator in interface OrderedCollection<E>
Specified by:
iterator in interface java.lang.Iterable<E>
Specified by:
iterator in class AbstractOrderedCollection<E>
Returns:
an iterator over the elements in this queue

toString

public java.lang.String toString()
Returns a string representation of this queue. The string representation consists of a count of the number of elements in the queue and a list of the queue's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

This implementation creates an empty string buffer, appends a left square bracket, and iterates over the collection appending the string representation of each element in turn. After appending each element except the last, the string ", " is appended. Finally a right bracket is appended. A string is obtained from the string buffer, and returned.

Overrides:
toString in class AbstractOrderedCollection<E>
Returns:
an array containing the elements of this queue