ptolemy.actor.util
Class FIFOQueue

java.lang.Object
  extended by ptolemy.actor.util.FIFOQueue
All Implemented Interfaces:
java.lang.Cloneable

public class FIFOQueue
extends java.lang.Object
implements java.lang.Cloneable

A first-in, first-out (FIFO) queue with variable capacity and optional history. Objects are appended to the queue with the put() method, and removed from the queue with the take() method. The object removed is the oldest one in the queue. By default, the capacity is infinite, but it can be set to any nonnegative size. If the history capacity is greater than zero (or infinite, by setting the capacity to INFINITE_CAPACITY), then objects removed from the queue are transferred to a history queue rather than simply removed. By default, the history capacity is zero.

Since:
Ptolemy II 0.2
Version:
$Id: FIFOQueue.java 57040 2010-01-27 20:52:32Z cxh $
Author:
Edward A. Lee, Xiaojun Liu
Accepted Rating:
Green (liuj)
Proposed Rating:
Green (eal)

Field Summary
private  Nameable _container
           
private  int _historyCapacity
           
private  java.util.LinkedList _historyList
           
private  int _queueCapacity
           
private  java.util.LinkedList _queueList
           
static int INFINITE_CAPACITY
          Used to indicate that the size of the queue or the history queue is infinite.
 
Constructor Summary
FIFOQueue()
          Construct an empty queue with no container.
FIFOQueue(FIFOQueue model)
          Copy constructor.
FIFOQueue(Nameable container)
          Construct an empty queue with the specified container.
 
Method Summary
 void clear()
          Remove all items currently stored in the queue and clear the history queue.
 java.lang.Object clone()
          Clone this queue.
 java.util.List elementList()
          List the objects in the queue, beginning with the oldest.
 java.util.Enumeration elements()
          Deprecated. Used elementList() instead.
 java.lang.Object get(int offset)
          Return an object in the queue or history.
 int getCapacity()
          Return the queue capacity, or INFINITE_CAPACITY if it is unbounded.
 Nameable getContainer()
          Return the container of the queue, or null if there is none.
 int getHistoryCapacity()
          Return the capacity of the history queue.
 java.util.List historyElementList()
          List the objects in the history, which are the N most recent objects taken from the queue, beginning with the oldest, where N is less than or equal to the history capacity.
 java.util.Enumeration historyElements()
          Deprecated. Use historyElementList() instead.
 int historySize()
          Return the number of objects in the history.
 boolean isFull()
          Return true if the number of objects in the queue equals the queue capacity.
 boolean put(java.lang.Object element)
          Put an object in the queue and return true if this will not cause the capacity to be exceeded.
 void setCapacity(int capacity)
          Set queue capacity.
 void setContainer(Nameable container)
          Set the container of the queue.
 void setHistoryCapacity(int capacity)
          Set the capacity of the history queue.
 int size()
          Return the number of objects in the queue.
 java.lang.Object take()
          Remove the oldest object from the queue and return it.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INFINITE_CAPACITY

public static final int INFINITE_CAPACITY
Used to indicate that the size of the queue or the history queue is infinite.

See Also:
Constant Field Values

_container

private Nameable _container

_queueCapacity

private int _queueCapacity

_queueList

private java.util.LinkedList _queueList

_historyCapacity

private int _historyCapacity

_historyList

private java.util.LinkedList _historyList
Constructor Detail

FIFOQueue

public FIFOQueue()
Construct an empty queue with no container.


FIFOQueue

public FIFOQueue(Nameable container)
Construct an empty queue with the specified container. The container is only used for error reporting.

Parameters:
container - The container of the queue.

FIFOQueue

public FIFOQueue(FIFOQueue model)
Copy constructor. Create a copy of the specified queue, but with no container. This is useful to permit enumerations over a queue while the queue continues to be modified. The objects in the queue themselves are not cloned.

Parameters:
model - The queue to be copied.
Method Detail

clear

public void clear()
Remove all items currently stored in the queue and clear the history queue. The queue capacity, history capacity and container remain unchanged.


clone

public java.lang.Object clone()
Clone this queue. The cloned queue has no container. The objects in the queue themselves are not cloned.

Overrides:
clone in class java.lang.Object
Returns:
A clone of this queue.

elementList

public java.util.List elementList()
List the objects in the queue, beginning with the oldest.

Returns:
A list of objects.

elements

public java.util.Enumeration elements()
Deprecated. Used elementList() instead.

Enumerate the objects in the queue, beginning with the oldest. This method is deprecated and calls elementList()

Returns:
An enumeration of objects.

get

public java.lang.Object get(int offset)
                     throws java.util.NoSuchElementException
Return an object in the queue or history. The object is not removed from the queue or history. If the offset argument is zero, return the oldest object in the queue. If the offset is 1, return the second oldest object, etc. If there is no such object in the queue (the offset is greater than or equal to the current queue size), throw an exception. If the argument is -1, return the most recent object that was put in the history. If the argument is -2, return the second most recent object in the history, etc. If there is no such object in the history (the history capacity is zero or the absolute value of the offset is greater than the current size of the history queue), throw an exception.

Parameters:
offset - The position of the desired object.
Returns:
The desired object in the queue or history.
Throws:
java.util.NoSuchElementException - If the offset is out of range.

getCapacity

public int getCapacity()
Return the queue capacity, or INFINITE_CAPACITY if it is unbounded.

Returns:
The capacity of the queue.
See Also:
setCapacity(int)

getContainer

public Nameable getContainer()
Return the container of the queue, or null if there is none.

Returns:
The container of the queue.
See Also:
setContainer(ptolemy.kernel.util.Nameable)

getHistoryCapacity

public int getHistoryCapacity()
Return the capacity of the history queue. This will be zero if the history mechanism is disabled and INFINITE_CAPACITY if the history capacity is infinite.

Returns:
The capacity of the history queue.
See Also:
setHistoryCapacity(int)

historyElementList

public java.util.List historyElementList()
List the objects in the history, which are the N most recent objects taken from the queue, beginning with the oldest, where N is less than or equal to the history capacity. If the history capacity is infinite, then the list includes all objects previously taken from the queue. If the history capacity is zero, then return an empty list.

Returns:
A list of objects in the history.

historyElements

public java.util.Enumeration historyElements()
Deprecated. Use historyElementList() instead.

Enumerate the objects in the history, which are the N most recent objects taken from the queue, beginning with the oldest, where N is less than or equal to the history capacity. This method is deprecated and calls historyElementList().

Returns:
An enumeration of objects in the history.

historySize

public int historySize()
Return the number of objects in the history.

Returns:
The current number of objects in the history.

isFull

public boolean isFull()
Return true if the number of objects in the queue equals the queue capacity.

Returns:
A boolean indicating whether the queue is full.

put

public boolean put(java.lang.Object element)
Put an object in the queue and return true if this will not cause the capacity to be exceeded. Otherwise, do not put the object in the queue and return false.

Parameters:
element - An object to be put in the queue.
Returns:
A boolean indicating success.

setCapacity

public void setCapacity(int capacity)
                 throws IllegalActionException
Set queue capacity. Use INFINITE_CAPACITY to indicate unbounded capacity (which is the default). If the current size of the queue exceeds the desired capacity, throw an exception.

Parameters:
capacity - The desired capacity.
Throws:
IllegalActionException - If the queue contains more objects than the proposed capacity or the proposed capacity is illegal.
See Also:
getCapacity()

setContainer

public void setContainer(Nameable container)
Set the container of the queue. The container is only used for error reporting.

Parameters:
container - The container of this queue.
See Also:
getContainer()

setHistoryCapacity

public void setHistoryCapacity(int capacity)
                        throws IllegalActionException
Set the capacity of the history queue. Use 0 to disable the history mechanism and INFINITE_CAPACITY to make the history capacity unbounded. If the size of the history queue exceeds the desired capacity, remove the oldest objects from the history queue until its size equals the proposed capacity. Note that this can be used to clear the history queue by supplying 0 as the argument.

Parameters:
capacity - The desired capacity of the history queue.
Throws:
IllegalActionException - If the desired capacity is illegal.
See Also:
getHistoryCapacity()

size

public int size()
Return the number of objects in the queue.

Returns:
The number of objects in the queue.

take

public java.lang.Object take()
                      throws java.util.NoSuchElementException
Remove the oldest object from the queue and return it. If there is no such object in the queue (the queue is empty), throw an exception. If the history mechanism is enabled, then put the taken object in the history queue. If the capacity of the history queue would be exceeded by this, then first remove the oldest object in the history queue.

Returns:
An object from the queue.
Throws:
java.util.NoSuchElementException - If the queue is empty.