001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: CircularQueue.java,v 1.2 2006-06-15 13:47:00 sinisa Exp $
022: */
023:
024: package com.lutris.util;
025:
026: /*
027: * Circular queue data structure implemented with an array
028: * of references to objects.
029: * It is the responsibility of the user to synchronize access to the queue.
030: *
031: * @version $Revision: 1.2 $
032: * @author Mark Sanguinetti
033: */
034:
035: public class CircularQueue {
036:
037: /** Array of references to the objects being queued. */
038: protected Object queue[] = null;
039:
040: /** The array index for the next object to be stored in the queue. */
041: protected int sIndex;
042:
043: /** The array index for the next object to be removed from the queue. */
044: protected int rIndex;
045:
046: /** Number of objects currently stored in the queue. */
047: protected int count;
048:
049: /** The number of objects in the array (Queue size + 1). */
050: protected int qSize;
051:
052: /**
053: * Creates a circular queue of size s (s objects).
054: *
055: * @param s The maximum number of elements to be queued.
056: */
057: public CircularQueue(int s) {
058: qSize = s + 1;
059: sIndex = 0;
060: rIndex = qSize;
061: count = 0;
062: queue = new Object[qSize];
063: }
064:
065: /**
066: * Stores an object in the queue.
067: *
068: * @param x The object to be stored in the queue.
069: * @return true if successful, false otherwise.
070: * @exception ArrayIndexOutOfBoundsException
071: */
072: public boolean put(Object x) throws ArrayIndexOutOfBoundsException {
073:
074: if ((sIndex + 1 == rIndex)
075: || ((sIndex + 1 == qSize) && (rIndex == 0))) {
076: // queue is full
077: return false;
078: } else {
079: // insert object into queue.
080: queue[sIndex++] = x;
081: count++;
082: if (sIndex == qSize) {
083: // loop back
084: sIndex = 0;
085: }
086: }
087: return true;
088: }
089:
090: /**
091: * Removes an object from the queue.
092: *
093: * @return a reference to the object being retrieved.
094: * @exception ArrayIndexOutOfBoundsException
095: */
096: public Object get() throws ArrayIndexOutOfBoundsException {
097:
098: if (rIndex == qSize) {
099: // loop back
100: rIndex = 0;
101: }
102: if (rIndex == sIndex) {
103: // queue is empty
104: return null;
105: } else {
106: // return object
107: count--;
108: Object obj = queue[rIndex];
109: queue[rIndex] = null;
110: rIndex++;
111: return obj;
112: }
113: }
114:
115: /**
116: * Returns the total number of objects stored in the queue.
117: *
118: * @return The total number of objects in the queue.
119: */
120: public int getCount() {
121: return count;
122: }
123:
124: /**
125: * Checks to see if the queue is empty.
126: *
127: * @return true if queue is empty, false otherwise.
128: */
129: public boolean isEmpty() {
130: return (count == 0 ? true : false);
131: }
132: }
|