1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.util.queue;
22
23 /***
24 * Queue class represents a queue of objects in which elements are removed in
25 * the same order they were entered.This is often referred to as
26 * first-in-first-out (FIFO).
27 */
28 import java.util.LinkedList;
29 import java.util.Iterator;
30
31 /***
32 * TODO Add docs
33 *
34 *
35 * Contributing Author(s):
36 *
37 * Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
38 * Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
39 *
40 * @author michaellif
41 * @version $Revision: 1.6 $ ($Author: vlads $) $Date: 2004/12/07 09:04:52 $
42 */
43 public class Queue implements java.io.Serializable {
44
45 /***
46 * Stable <code>serialVersionUID</code>.
47 */
48 private static final long serialVersionUID = -5344516264926467012L;
49
50 /*** DOCUMENT ME! */
51 private LinkedList items;
52
53 /***
54 * Creats an empty queue
55 */
56 public Queue() {
57 items = new LinkedList();
58 }
59
60 /***
61 * Inserts a new element at the rear of the queue.
62 *
63 * @param element element to be inserted.
64 */
65 public Object enqueue(Object element) {
66 items.add(element);
67
68 return element;
69 }
70
71 /***
72 * Inserts a new element at the begining of the queue.
73 *
74 * @param element element to be inserted.
75 */
76 public Object enqueueFirst(Object element) {
77 items.addFirst(element);
78 return element;
79 }
80
81 /***
82 * Inserts a Collection of new element at the rear of the queue.
83 *
84 * @param elements Collection to be inserted.
85 */
86 public boolean enqueueAll(Queue elements) {
87 if (elements == null) {
88 return false;
89 }
90 return items.addAll(elements.items);
91 }
92
93 /***
94 * Removes the element at the top of the queue.
95 *
96 * @return the removed element.
97 *
98 * @throws EmptyQueueException if the queue is empty.
99 */
100 public Object dequeue() {
101 if (items.size() == 0) {
102 throw new EmptyQueueException();
103 }
104
105 return items.removeFirst();
106 }
107
108 /***
109 * Inspects the element at the top of the queue without removing it.
110 *
111 * @return the element at the top of the queue.
112 *
113 * @throws EmptyQueueException if the queue is empty.
114 */
115 public Object getFirst() {
116 if (items.size() == 0) {
117 throw new EmptyQueueException();
118 }
119 return items.getFirst();
120 }
121
122 public Object get(int i) {
123 if (items.size() <= i) {
124 return null;
125 }
126 return items.get(i);
127 }
128
129 /***
130 * Inspects the element at the end of the queue without removing it.
131 *
132 * @return the element at the top of the queue.
133 *
134 * @throws EmptyQueueException if the queue is empty.
135 */
136 public Object getLast() {
137 if (items.size() == 0) {
138 throw new EmptyQueueException();
139 }
140
141 return items.getLast();
142 }
143
144 /***
145 * @return the number of elements at the queue.
146 */
147 public int size() {
148 return items.size();
149 }
150
151 /***
152 * @return true if the queue is empty.
153 */
154 public boolean isEmpty() {
155 return (size() == 0);
156 }
157
158 /***
159 * Removes all elements at the queue.
160 */
161 public void clear() {
162 items.clear();
163 }
164
165 /***
166 * Removes all the elements from the queue and returns the values as array.
167 *
168 * @return
169 */
170 public Object[] dequeueAll() {
171 Object[] retVal = items.toArray();
172 items.clear();
173
174 return retVal;
175 }
176
177 /***
178 * Removes all the elements from the queue and returns the values as array.
179 *
180 * @return
181 */
182 public Object[] dequeueAll(Object[] a) {
183 Object[] retVal = items.toArray(a);
184 items.clear();
185
186 return retVal;
187 }
188
189 public Iterator iterator() {
190 return items.iterator();
191 }
192
193 /***
194 * Log4j event Queue dumper
195 *
196 * @return String
197 */
198 public String toString() {
199 StringBuffer buffer = new StringBuffer(120 * size());
200 for (Iterator i = iterator(); i.hasNext(); ) {
201 buffer.append(i.next());
202 buffer.append("\n");
203 }
204 (new Throwable()).printStackTrace();
205 return buffer.toString();
206 }
207 }