1 package fr.ove.utils;
2
3 import java.io.Serializable;
4 import java.util.*;
5 import fr.ove.utils.Queueable;
6 import fr.ove.utils.LinkedElement;
7
8 /***
9 * A queue.
10 *
11 * @author © 2000 DIRAT Laurent
12 * @version 1.0 28/06/2000
13 */
14 public class Queue implements Queueable, Serializable {
15 /***
16 * The head of the queue.
17 */
18 private LinkedElement head;
19
20 /***
21 * The tail of the queue.
22 */
23 private LinkedElement tail;
24
25 /***
26 * The number of elements.
27 */
28 private int nbElements;
29
30 /***
31 * The Constructor
32 */
33 public Queue() {
34 head = tail = null;
35 nbElements = 0;
36 }
37
38 /***
39 * Adds the specified element to the queue
40 */
41 public void enqueue(Object element) {
42 LinkedElement newElement = new LinkedElement(element);
43 if (nbElements == 0)
44 head = tail = newElement;
45 else {
46 newElement.setNext(tail);
47 tail.setPrevious(newElement);
48 tail = newElement;
49 }
50
51 nbElements++;
52 }
53
54 /***
55 * Removes the first element added into the queue.
56 * Returns the element removed. <CODE>null</CODE> if the queue is empty.
57 */
58 public Object dequeue() {
59 Object element = null;
60
61 if (nbElements != 0) {
62 LinkedElement dequeued = head;
63 element = dequeued.getElement();
64 head = dequeued.getPrevious();
65 dequeued.remove();
66
67 if (nbElements == 1)
68 head = tail = null;
69
70 nbElements--;
71 }
72
73 return element;
74 }
75
76 /***
77 * Returns the first element added into the queue.
78 * <CODE>null</CODE> if the queue is empty
79 */
80 public Object peek() {
81 if (nbElements != 0)
82 return head.getElement();
83 else
84 return null;
85 }
86
87 /***
88 * Checks if the queue is empty.
89 * Returns <CODE>true</CODE> if the queue is empty. <CODE>false</CODE> otherwise.
90 */
91 public boolean isEmpty() {
92 return (nbElements == 0);
93 }
94
95 /***
96 * Clears the queue
97 */
98 public void clear() {
99 head = tail = null;
100 nbElements = 0;
101 }
102
103 /***
104 * Returns an @see Enumeration of the elements in the queue.
105 */
106 public Enumeration elements() {
107 synchronized (this) {
108 return (Enumeration) new QueueEnumerator(this);
109 }
110 }
111
112
113 /***
114 * Returns an array of the elements contained in the queue.<BR>
115 * If the queue is empty, returns <CODE>null</CODE>.
116 */
117 public Object[] getElements() {
118 Object elements[] = null;
119
120 if (nbElements != 0) {
121 elements = new Object[nbElements];
122
123 LinkedElement ptr = tail;
124 int index = 0;
125 while (ptr != null) {
126 elements[index++] = ptr.getElement();
127 ptr = ptr.getNext();
128 }
129 }
130
131 return elements;
132 }
133
134 /***
135 * Returns a string representation of the queue
136 */
137 public String toString() {
138 String string = "";
139 LinkedElement ptr = tail;
140
141 while (ptr != null) {
142 string += " " + ptr.toString();
143 ptr = ptr.getNext();
144 }
145
146 return string + " ";
147 }
148
149
150
151
152
153
154 private class QueueEnumerator implements Enumeration, Serializable {
155 /***
156 * The queue to enumerate
157 */
158 Queue queue;
159
160 /***
161 * The next element in the queue;
162 */
163 LinkedElement next;
164
165 /***
166 * The constructor
167 * @param queue the queue to enumerate
168 */
169 public QueueEnumerator(Queue queue) {
170 this.queue = queue;
171 next = queue.head;
172 }
173
174 /***
175 * Tests if this enumeration contains more elements.
176 *
177 * @return <code>true</code> if this enumeration contains more elements;
178 * <code>false</code> otherwise.
179 */
180 public boolean hasMoreElements() {
181 return (next != null);
182 }
183
184 /***
185 * Returns the next element of this enumeration.
186 *
187 * @return the next element of this enumeration.
188 * @exception NoSuchElementException if no more elements exist.
189 */
190 public Object nextElement() {
191 synchronized (queue) {
192 if (next != null) {
193 Object element = next.getElement();
194 next = next.getPrevious();
195 return element;
196 }
197 }
198
199 throw new NoSuchElementException("QueueEnumerator");
200 }
201 }
202
203 }