|
|
/*
* Enhydra Java Application Server Project
*
* The contents of this file are subject to the Enhydra Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License on
* the Enhydra web site ( http://www.enhydra.org/ ).
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific terms governing rights and limitations
* under the License.
*
* The Initial Developer of the Enhydra Application Server is Lutris
* Technologies, Inc. The Enhydra Application Server and portions created
* by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
* All Rights Reserved.
*
* Contributor(s):
*
* $Id: CircularQueue.java,v 1.15 2005/06/13 09:26:06 draganr Exp $
*/
package com.lutris.util;
/*
* Circular queue data structure implemented with an array
* of references to objects.
* It is the responsibility of the user to synchronize access to the queue.
*
* @version $Revision: 1.15 $
* @author Mark Sanguinetti
*/
public class CircularQueue {
/** Array of references to the objects being queued. */
protected Object queue[] = null;
/** The array index for the next object to be stored in the queue. */
protected int sIndex;
/** The array index for the next object to be removed from the queue. */
protected int rIndex;
/** Number of objects currently stored in the queue. */
protected int count;
/** The number of objects in the array (Queue size + 1). */
protected int qSize;
/**
* Creates a circular queue of size s (s objects).
*
* @param s The maximum number of elements to be queued.
*/
public CircularQueue(int s) {
qSize = s + 1;
sIndex = 0;
rIndex = qSize;
count = 0;
queue = new Object[qSize];
}
/**
* Stores an object in the queue.
*
* @param x The object to be stored in the queue.
* @return true if successful, false otherwise.
* @exception ArrayIndexOutOfBoundsException
*/
public boolean put(Object x) throws ArrayIndexOutOfBoundsException {
if ((sIndex + 1 == rIndex) ||
((sIndex + 1 == qSize ) && (rIndex == 0))) {
// queue is full
return false;
} else {
// insert object into queue.
queue[sIndex++] = x;
count++;
if (sIndex == qSize) {
// loop back
sIndex = 0;
}
}
return true;
}
/**
* Removes an object from the queue.
*
* @return a reference to the object being retrieved.
* @exception ArrayIndexOutOfBoundsException
*/
public Object get() throws ArrayIndexOutOfBoundsException {
if (rIndex == qSize) {
// loop back
rIndex = 0;
}
if (rIndex == sIndex) {
// queue is empty
return null;
} else {
// return object
count--;
Object obj = queue[rIndex];
queue[rIndex] = null;
rIndex++;
return obj;
}
}
/**
* Returns the total number of objects stored in the queue.
*
* @return The total number of objects in the queue.
*/
public int getCount() {
return count;
}
/**
* Checks to see if the queue is empty.
*
* @return true if queue is empty, false otherwise.
*/
public boolean isEmpty() {
return (count == 0 ? true : false);
}
}
|