View Javadoc

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-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: MessageQueue.java,v 1.32.2.1 2004/05/01 12:01:15 tanderson Exp $
44   */
45  package org.exolab.jms.messagemgr;
46  
47  import java.util.Comparator;
48  import java.util.Iterator;
49  import java.util.TreeSet;
50  
51  
52  /***
53   * The message queue stored messages based on a comparator. The implementation
54   * is based on a synchronized linked list.
55   * <p>
56   * We can easily improve on this implementation and this implementation is
57   * not synchronized.
58   *
59   * @version     $Revision: 1.32.2.1 $ $Date: 2004/05/01 12:01:15 $
60   * @author      <a href="mailto:jima@intalio.com">Jim Alateras</a>
61   * @author      <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
62   */
63  public class MessageQueue {
64  
65      /***
66       * The message queue is implemented as a linked list
67       */
68      private TreeSet _list = null;
69  
70      /***
71       * Instantiate an instance of this class with the specified comparator
72       * the comparator is used to order the elements in the queue. Elements
73       * with the same order value are placed after each other.
74       *
75       * @param       comparator          used for ordering
76       */
77      public MessageQueue(Comparator comparator) {
78          _list = new TreeSet(comparator);
79      }
80  
81      /***
82       * Add this element to the queue in the required order. It uses a
83       * binary search to locate the correct position
84       *
85       * @param       object              object to add
86       */
87      public synchronized void add(Object object) {
88          _list.add(object);
89      }
90  
91      /***
92       * Check if the specified object exists
93       *
94       * @param object - object to check
95       * @return boolean - true if it is contained
96       */
97      public synchronized boolean contains(Object object) {
98          return _list.contains(object);
99      }
100 
101     /***
102      * Check if the queue is empty
103      *
104      * @return boolean - true if the queue is empty
105      */
106     public synchronized boolean isEmpty() {
107         return _list.isEmpty();
108     }
109 
110     /***
111      * Return all elements in the collection
112      *
113      * @return Object[] array of elements to return
114      */
115     public synchronized Object[] toArray() {
116         return _list.toArray();
117     }
118 
119 
120     /***
121      * Return an iterator to the list
122      *
123      * @return Iterator
124      */
125     public synchronized Iterator iterator() {
126         return _list.iterator();
127     }
128 
129     /***
130      * Remove the object from the queue
131      *
132      * @param       object          object to remove
133      */
134     public synchronized boolean remove(Object object) {
135         return _list.remove(object);
136     }
137 
138     /***
139      * Remove all the elements from the queue
140      */
141     public synchronized void clear() {
142         _list.clear();
143     }
144 
145     /***
146      * Return the number elements in the queue
147      *
148      * @return      int         size of the queue
149      */
150     public synchronized int size() {
151         return _list.size();
152     }
153 
154     /***
155      * Returns the first element in the queue.
156      *
157      * @return the first element in the queue, or <code>null</code>, if the
158      * queue is empty
159      */
160     public synchronized Object first() {
161         return (_list.size() > 0) ? _list.first() : null;
162     }
163 
164     /***
165      * Returns the last element in the queue.
166      *
167      * @return the last element in the queue, or <code>null</code>, if the
168      * queue is empty
169      */
170     public synchronized Object last() {
171         return (_list.size() > 0) ? _list.last() : null;
172     }
173 
174     /***
175      * Removes and returns the first element on the queue.
176      *
177      * @return the first element in the queue, or <code>null</code>, if the
178      * queue is empty
179      */
180     public synchronized Object removeFirst() {
181         Object object = first();
182         if (object != null) {
183             _list.remove(object);
184         }
185         return object;
186     }
187 
188     /***
189      * Removes and returns the last element in the queue
190      *
191      * @return the last element in the queue, or <code>null</code>, if the
192      * queue is empty
193      */
194     public synchronized Object removeLast() {
195         Object object = last();
196         if (object != null) {
197             _list.remove(object);
198         }
199         return object;
200     }
201 
202 } //-- MessageQueue