1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2001-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: MessageQueue.java,v 1.32 2003/08/07 13:33:03 tanderson Exp $
44 *
45 * Date Author Changes
46 * 06/07/2001 jima Created
47 */
48 package org.exolab.jms.messagemgr;
49
50 import java.util.Comparator;
51 import java.util.Iterator;
52 import java.util.TreeSet;
53
54
55 /***
56 * The message queue stored messages based on a comparator. The implementation
57 * is based on a synchronized linked list.
58 * <p>
59 * We can easily improve on this implementation and this implementation is
60 * not synchronized.
61 *
62 * @version $Revision: 1.32 $ $Date: 2003/08/07 13:33:03 $
63 * @author <a href="mailto:jima@intalio.com">Jim Alateras</a>
64 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
65 */
66 public class MessageQueue {
67
68 /***
69 * The message queue is implemented as a linked list
70 */
71 private TreeSet _list = null;
72
73 /***
74 * Instantiate an instance of this class with the specified comparator
75 * the comparator is used to order the elements in the queue. Elements
76 * with the same order value are placed after each other.
77 *
78 * @param comparator used for ordering
79 */
80 public MessageQueue(Comparator comparator) {
81 _list = new TreeSet(comparator);
82 }
83
84 /***
85 * Add this element to the queue in the required order. It uses a
86 * binary search to locate the correct position
87 *
88 * @param object object to add
89 */
90 public synchronized void add(Object object) {
91 _list.add(object);
92 }
93
94 /***
95 * Check if the specified object exists
96 *
97 * @param object - object to check
98 * @return boolean - true if it is contained
99 */
100 public boolean contains(Object object) {
101 return _list.contains(object);
102 }
103
104 /***
105 * Check if the queue is empty
106 *
107 * @return boolean - true if the queue is empty
108 */
109 public boolean isEmpty() {
110 return _list.isEmpty();
111 }
112
113 /***
114 * Return all elements in the collection
115 *
116 * @return Object[] array of elements to return
117 */
118 public Object[] toArray() {
119 return _list.toArray();
120 }
121
122
123 /***
124 * Return an iterator to the list
125 *
126 * @return Iterator
127 */
128 public Iterator iterator() {
129 return _list.iterator();
130 }
131
132 /***
133 * Remove the object from the queue
134 *
135 * @param object object to remove
136 */
137 public synchronized boolean remove(Object object) {
138 return _list.remove(object);
139 }
140
141 /***
142 * Remove all the elements from the queue
143 */
144 public synchronized void clear() {
145 _list.clear();
146 }
147
148 /***
149 * Return the number elements in the queue
150 *
151 * @return int size of the queue
152 */
153 public int size() {
154 return _list.size();
155 }
156
157 /***
158 * Returns the first element in the queue.
159 *
160 * @return the first element in the queue, or <code>null</code>, if the
161 * queue is empty
162 */
163 public synchronized Object first() {
164 return (_list.size() > 0) ? _list.first() : null;
165 }
166
167 /***
168 * Returns the last element in the queue.
169 *
170 * @return the last element in the queue, or <code>null</code>, if the
171 * queue is empty
172 */
173 public synchronized Object last() {
174 return (_list.size() > 0) ? _list.last() : null;
175 }
176
177 /***
178 * Removes and returns the first element on the queue.
179 *
180 * @return the first element in the queue, or <code>null</code>, if the
181 * queue is empty
182 */
183 public synchronized Object removeFirst() {
184 Object object = first();
185 if (object != null) {
186 _list.remove(object);
187 }
188 return object;
189 }
190
191 /***
192 * Removes and returns the last element in the queue
193 *
194 * @return the last element in the queue, or <code>null</code>, if the
195 * queue is empty
196 */
197 public synchronized Object removeLast() {
198 Object object = last();
199 if (object != null) {
200 _list.remove(object);
201 }
202 return object;
203 }
204
205 } //-- MessageQueue
This page was automatically generated by Maven