KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > util > CircularQueue


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: CircularQueue.java,v 1.1 2004/04/21 19:31:58 slobodan Exp $
23  */

24
25 package com.lutris.util;
26
27 /*
28  * Circular queue data structure implemented with an array
29  * of references to objects.
30  * It is the responsibility of the user to synchronize access to the queue.
31  *
32  * @version $Revision: 1.1 $
33  * @author Mark Sanguinetti
34  */

35
36 public class CircularQueue {
37
38     /** Array of references to the objects being queued. */
39     protected Object JavaDoc queue[] = null;
40
41     /** The array index for the next object to be stored in the queue. */
42     protected int sIndex;
43
44     /** The array index for the next object to be removed from the queue. */
45     protected int rIndex;
46
47     /** Number of objects currently stored in the queue. */
48     protected int count;
49
50     /** The number of objects in the array (Queue size + 1). */
51     protected int qSize;
52
53     /**
54      * Creates a circular queue of size s (s objects).
55      *
56      * @param s The maximum number of elements to be queued.
57      */

58     public CircularQueue(int s) {
59         qSize = s + 1;
60         sIndex = 0;
61         rIndex = qSize;
62         count = 0;
63         queue = new Object JavaDoc[qSize];
64     }
65
66     /**
67      * Stores an object in the queue.
68      *
69      * @param x The object to be stored in the queue.
70      * @return true if successful, false otherwise.
71      * @exception ArrayIndexOutOfBoundsException
72      */

73     public boolean put(Object JavaDoc x) throws ArrayIndexOutOfBoundsException JavaDoc {
74
75         if ((sIndex + 1 == rIndex) ||
76             ((sIndex + 1 == qSize ) && (rIndex == 0))) {
77             // queue is full
78
return false;
79         } else {
80             // insert object into queue.
81
queue[sIndex++] = x;
82             count++;
83             if (sIndex == qSize) {
84                 // loop back
85
sIndex = 0;
86             }
87         }
88         return true;
89     }
90
91     /**
92      * Removes an object from the queue.
93      *
94      * @return a reference to the object being retrieved.
95      * @exception ArrayIndexOutOfBoundsException
96      */

97     public Object JavaDoc get() throws ArrayIndexOutOfBoundsException JavaDoc {
98
99         if (rIndex == qSize) {
100             // loop back
101
rIndex = 0;
102         }
103         if (rIndex == sIndex) {
104             // queue is empty
105
return null;
106         } else {
107             // return object
108
count--;
109             Object JavaDoc obj = queue[rIndex];
110             queue[rIndex] = null;
111             rIndex++;
112             return obj;
113         }
114     }
115
116     /**
117      * Returns the total number of objects stored in the queue.
118      *
119      * @return The total number of objects in the queue.
120      */

121     public int getCount() {
122         return count;
123     }
124
125     /**
126      * Checks to see if the queue is empty.
127      *
128      * @return true if queue is empty, false otherwise.
129      */

130     public boolean isEmpty() {
131         return (count == 0 ? true : false);
132     }
133 }
134
135
Popular Tags