org.cdlib.xtf.util
Class CircularQueue

Object
  extended by CircularQueue

public class CircularQueue
extends Object

A simple queue of fixed size, that provides very fast insertion, deletion, and scanning.

Author:
Martin Haye

Field Summary
private  int bottom
          Points to the current bottom entry
private  int count
          Number of entries currently in the queue
private  Object[] entries
          The current queue entries
private  int maxSize
          Max # of entries in the queue
static Tester tester
          Basic regression test
 
Constructor Summary
CircularQueue(int maxSize)
          Construct a queue that can hold, at most, 'maxSize' entries.
 
Method Summary
 void addHead(Object obj)
          Add an object to the start of the queue.
 void addTail(Object obj)
          Add an object to the end of the queue.
 void clear()
          Removes all entries from the queue.
 int count()
          Counts how many items are currently in the queue
 boolean isEmpty()
          Checks whether the queue is currently empty
 boolean isFull()
          Checks whether the queue is currently full
 Object peek(int distance)
          Peek into the queue but do not remove the object.
 Object removeHead()
          Removes and returns the first object in the queue.
 Object removeTail()
          Removes and returns the last object in the queue.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

maxSize

private int maxSize
Max # of entries in the queue


entries

private Object[] entries
The current queue entries


bottom

private int bottom
Points to the current bottom entry


count

private int count
Number of entries currently in the queue


tester

public static final Tester tester
Basic regression test

Constructor Detail

CircularQueue

public CircularQueue(int maxSize)
Construct a queue that can hold, at most, 'maxSize' entries.

Parameters:
maxSize - Maximum number of entries in the queue
Method Detail

addTail

public final void addTail(Object obj)
Add an object to the end of the queue. If the queue is full, the first object is removed to make room.

Parameters:
obj - Object to add.

addHead

public final void addHead(Object obj)
Add an object to the start of the queue. If the queue is full, the last object is removed to make room.

Parameters:
obj - Object to add.

removeHead

public final Object removeHead()
Removes and returns the first object in the queue.

Returns:
The object that was at the head, or null if the queue is empty.

removeTail

public final Object removeTail()
Removes and returns the last object in the queue.

Returns:
The object that was at the tail, or null if the queue is empty.

peek

public final Object peek(int distance)
Peek into the queue but do not remove the object.

Parameters:
distance - How far to peek into the queue (zero means peek at the head, one to peek at head+1, etc.)
Returns:
The object at the specified position, or null if queue is empty

clear

public final void clear()
Removes all entries from the queue.


count

public final int count()
Counts how many items are currently in the queue

Returns:
Number of items in the queue

isEmpty

public final boolean isEmpty()
Checks whether the queue is currently empty

Returns:
true if empty, false if anything is queued

isFull

public final boolean isFull()
Checks whether the queue is currently full

Returns:
true if full, false if there is room for more entries