com.sosnoski.util.queue
Class QueueBase

java.lang.Object
  |
  +--com.sosnoski.util.GrowableBase
        |
        +--com.sosnoski.util.queue.QueueBase
Direct Known Subclasses:
IntQueue, ObjectQueue, StringQueue

public abstract class QueueBase
extends GrowableBase

Base class for type-specific growable circular queue classes with any type of values (including primitive types). This class builds on the basic structure provided by GrowableBase, specializing it for usage as a queue. See the base class description for details of the implementation.

Queues based on this class are unsynchronized in order to provide the best possible performance for typical usage scenarios, so explicit synchronization must be implemented by the subclass or the application in cases where they are to be modified in a multithreaded environment.

Subclasses need to implement the abstract methods defined by the base class for working with the data array, as well as the actual data access methods (at least the basic add() and remove() methods).

Version:
1.0
Author:
Dennis M. Sosnoski

Field Summary
protected  int m_emptyOffset
          Offset for removing next item from queue.
protected  int m_fillOffset
          Offset for adding next item to queue.
 
Fields inherited from class com.sosnoski.util.GrowableBase
DEFAULT_SIZE, m_countLimit, m_maximumGrowth
 
Constructor Summary
QueueBase(int size, java.lang.Class type)
          Constructor with partial specification.
QueueBase(int size, int growth, java.lang.Class type)
          Constructor with full specification.
QueueBase(QueueBase base)
          Copy (clone) constructor.
 
Method Summary
protected  java.lang.Object buildArray(java.lang.Class type)
          Constructs and returns a simple array containing the same data as held in this queue.
 void clear()
          Set the queue to the empty state.
 void discard(int count)
          Discard items from queue.
protected  int getAddIndex()
          Make space for adding a value to those in the queue.
protected  int getRemoveIndex()
          Removes the next item from the queue.
 boolean isEmpty()
          Check if queue is empty.
protected  void resizeCopy(java.lang.Object base, java.lang.Object grown)
          Copy data after array resize.
 int size()
          Get the number of values currently present in the queue.
 
Methods inherited from class com.sosnoski.util.GrowableBase
buildArray, discardValues, ensureCapacity, getArray, growArray, setArray
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_fillOffset

protected int m_fillOffset
Offset for adding next item to queue.

m_emptyOffset

protected int m_emptyOffset
Offset for removing next item from queue.
Constructor Detail

QueueBase

public QueueBase(int size,
                 int growth,
                 java.lang.Class type)
Constructor with full specification.
Parameters:
size - number of elements initially allowed in queue
growth - maximum size increment for growing queue
type - queue element type

QueueBase

public QueueBase(int size,
                 java.lang.Class type)
Constructor with partial specification.
Parameters:
size - number of elements initially allowed in queue
type - queue element type

QueueBase

public QueueBase(QueueBase base)
Copy (clone) constructor.
Parameters:
from - instance being copied
Method Detail

resizeCopy

protected void resizeCopy(java.lang.Object base,
                          java.lang.Object grown)
Copy data after array resize. This override of the default implementation rearranges the data present in the queue when the data was wrapped in the original array.
Overrides:
resizeCopy in class GrowableBase
Parameters:
base - original array containing data
grown - resized array for data

getAddIndex

protected int getAddIndex()
Make space for adding a value to those in the queue. If the underlying array is full, it is grown by the appropriate size increment so that the index value returned is always valid for the array in use by the time of the return.
Returns:
index position to store added element

getRemoveIndex

protected int getRemoveIndex()
Removes the next item from the queue.
Returns:
index position of removed element
Throws:
IllegalStateException - on attempt to remove an item from an empty queue

size

public int size()
Get the number of values currently present in the queue.
Returns:
count of values present

isEmpty

public boolean isEmpty()
Check if queue is empty.
Returns:
true if queue empty, false if not

clear

public void clear()
Set the queue to the empty state.

discard

public void discard(int count)
Discard items from queue.
Parameters:
count - number of items to be discarded
Throws:
IllegalStateException - if attempt to discard more items than present on queue

buildArray

protected java.lang.Object buildArray(java.lang.Class type)
Constructs and returns a simple array containing the same data as held in this queue.
Parameters:
type - element type for constructed array
Returns:
array containing a copy of the data


Company Web Site

XML Benchmark Home