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-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: MessageQueue.java,v 1.3 2005/12/20 20:31:59 tanderson Exp $
44   */
45  package org.exolab.jms.messagemgr;
46  
47  import java.util.HashMap;
48  import java.util.Map;
49  import java.util.SortedMap;
50  import java.util.TreeMap;
51  
52  
53  /***
54   * <code>MessageQueue</code> implements a synchronized queue of
55   * {@link MessageHandle} instances.
56   * .<p/>
57   * Message handles are ordered using {@link MessageHandleComparator}
58   *
59   * @version     $Revision: 1.3 $ $Date: 2005/12/20 20:31:59 $
60   * @author      <a href="mailto:jima@comware.com.au">Jim Alateras</a>
61   * @author      <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
62   */
63  class MessageQueue {
64  
65      /***
66       * The message handle queue.
67       */
68      private final SortedMap _queue = new TreeMap(new MessageHandleComparator());
69  
70      /***
71       * The set of handles, keyed on message identifier.
72       */
73      private final Map _handles = new HashMap();
74  
75  
76      /***
77       * Add a message handle.
78       *
79       * @param handle the message handle
80       * @return <code>true</code> if queue set did not already contain the handle
81       */
82      public synchronized boolean add(MessageHandle handle) {
83          boolean added = false;
84          if (_queue.put(handle, handle) == null) {
85              _handles.put(handle.getMessageId(), handle);
86              added = true;
87          }
88          return added;
89      }
90  
91      /***
92       * Determines if a message handle exists.
93       *
94       * @param handle the message handle
95       * @return <code>true</code> if it exists
96       */
97      public synchronized boolean contains(MessageHandle handle) {
98          return _queue.containsKey(handle);
99      }
100 
101     /***
102      * Return all elements in the queue.
103      *
104      * @return a list of message handles
105      */
106     public synchronized MessageHandle[] toArray() {
107         return (MessageHandle[]) _queue.keySet().toArray(new MessageHandle[0]);
108     }
109 
110     /***
111      * Removes a message handle from the queue.
112      *
113      * @param handle the message handle to remove
114      * @return the removed handle, or <code>null</code> if it wasn't present
115      */
116     public synchronized MessageHandle remove(MessageHandle handle) {
117         MessageHandle result = (MessageHandle) _queue.remove(handle);
118         if (result != null) {
119             _handles.remove(handle.getMessageId());
120         }
121         return result;
122     }
123 
124     /***
125      * Removes a message handle from the queue.
126      *
127      * @param messageId the message identifier of the handle to remove
128      * @return the removed handle, or <code>null</code> if it wasn't present
129      */
130     public synchronized MessageHandle remove(String messageId) {
131         MessageHandle result = (MessageHandle) _handles.remove(messageId);
132         if (result != null) {
133             _queue.remove(result);
134         }
135         return result;
136     }
137 
138     /***
139      * Removes all the elements from the queue.
140      */
141     public synchronized void clear() {
142         _queue.clear();
143         _handles.clear();
144     }
145 
146     /***
147      * Returns the number of message handles in the queue.
148      *
149      * @return the number message handles in the queue
150      */
151     public synchronized int size() {
152         return _queue.size();
153     }
154 
155     /***
156      * Removes and returns the first message handle in the queue.
157      *
158      * @return the first message handle in the queue, or <code>null</code>,
159      * if the queue is empty
160      */
161     public synchronized MessageHandle removeFirst() {
162         MessageHandle first = null;
163         if (_queue.size() > 0) {
164             first = (MessageHandle) _queue.firstKey();
165             _queue.remove(first);
166             _handles.remove(first.getMessageId());
167         }
168         return first;
169     }
170 
171 }