KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > queue > BoundedPriorityEventQueue


1 package org.jacorb.notification.queue;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.jacorb.notification.interfaces.Message;
30
31 import EDU.oswego.cs.dl.util.concurrent.Heap;
32
33 /**
34  * Note that most of the methods are not thread-safe. this causes no problem as
35  * the methods are not intended to be directly called by clients. instead the superclass
36  * implements the interface EventQueue and invokes the methods thereby synchronizing access.
37
38  * @author Alphonse Bendt
39  * @version $Id: BoundedPriorityEventQueue.java,v 1.9 2005/05/01 21:52:41 alphonse.bendt Exp $
40  */

41
42 public class BoundedPriorityEventQueue extends AbstractBoundedEventQueue
43 {
44     private Heap heap_;
45
46     private long counter_ = 0;
47     
48     private final int maxCapacity_;
49
50     ////////////////////////////////////////
51

52     public BoundedPriorityEventQueue(int maxSize,
53                                      EventQueueOverflowStrategy overflowStrategy)
54     {
55         super(maxSize, overflowStrategy, new Object JavaDoc());
56
57         maxCapacity_ = maxSize;
58         
59         heap_ = newHeap();
60     }
61
62     ////////////////////////////////////////
63

64     private Heap newHeap()
65     {
66         return new Heap2(maxCapacity_, QueueUtil.DESCENDING_PRIORITY_COMPARATOR);
67     }
68
69     protected Message getNextElement()
70     {
71         return ((HeapEntry)heap_.extract()).event_;
72     }
73
74
75     protected Message getEarliestTimeout()
76     {
77         List JavaDoc _all = getAllElementsInternal();
78
79         Collections.sort(_all, QueueUtil.ASCENDING_TIMEOUT_COMPARATOR);
80
81         HeapEntry _earliest = (HeapEntry)_all.remove(0);
82
83         Heap _newHeap = newHeap();
84         
85         Iterator JavaDoc i = _all.iterator();
86
87         while (i.hasNext())
88         {
89             HeapEntry e = (HeapEntry)i.next();
90             _newHeap.insert(e);
91         }
92
93         heap_ = _newHeap;
94
95         return _earliest.event_;
96     }
97
98
99     protected Message getOldestElement()
100     {
101         List JavaDoc _all = getAllElementsInternal();
102
103         Collections.sort(_all, QueueUtil.ASCENDING_AGE_COMPARATOR);
104
105         HeapEntry _oldest = (HeapEntry)_all.remove(0);
106
107         Heap _newHeap = newHeap();
108         
109         Iterator JavaDoc i = _all.iterator();
110
111         while (i.hasNext())
112         {
113             HeapEntry e = (HeapEntry)i.next();
114             _newHeap.insert(e);
115         }
116
117         return _oldest.event_;
118     }
119
120
121     protected Message getYoungestElement()
122     {
123         List JavaDoc _all = getAllElementsInternal();
124
125         Collections.sort(_all, QueueUtil.DESCENDING_AGE_COMPARATOR);
126
127         HeapEntry _youngest = (HeapEntry)_all.remove(0);
128
129         Heap _newHeap = newHeap();
130         
131         Iterator JavaDoc i = _all.iterator();
132
133         while (i.hasNext())
134         {
135             HeapEntry e = (HeapEntry)i.next();
136             _newHeap.insert(e);
137         }
138
139         heap_ = _newHeap;
140
141         return _youngest.event_;
142     }
143
144
145     protected Message getLeastPriority()
146     {
147         List JavaDoc _all = getAllElementsInternal();
148
149         Collections.sort(_all, QueueUtil.ASCENDING_PRIORITY_COMPARATOR);
150
151         HeapEntry _leastPriority = (HeapEntry)_all.remove(0);
152
153         Heap _newHeap = newHeap();
154         
155         Iterator JavaDoc i = _all.iterator();
156
157         while (i.hasNext())
158         {
159             HeapEntry e = (HeapEntry)i.next();
160             _newHeap.insert(e);
161         }
162
163         heap_ = _newHeap;
164
165         return _leastPriority.event_;
166     }
167
168
169     protected Message[] getElements(int max)
170     {
171         List JavaDoc _events = new ArrayList JavaDoc();
172         Object JavaDoc _element;
173
174         while ((_element = heap_.extract()) != null && (_events.size() <= max))
175         {
176             _events.add(((HeapEntry)_element).event_);
177         }
178
179         return (Message[])
180                _events.toArray(QueueUtil.MESSAGE_ARRAY_TEMPLATE);
181     }
182
183
184     protected void addElement(Message event)
185     {
186         heap_.insert(new HeapEntry(event, counter_++));
187     }
188
189
190     private List JavaDoc getAllElementsInternal()
191     {
192         List JavaDoc _events = new ArrayList JavaDoc();
193         Object JavaDoc _element;
194
195         while ((_element = heap_.extract()) != null)
196         {
197             _events.add(_element);
198         }
199
200         heap_.clear();
201         
202         return _events;
203     }
204
205
206     protected Message[] getAllElements()
207     {
208         List JavaDoc _all = getAllElementsInternal();
209
210         Message[] _ret = new Message[_all.size()];
211
212         Iterator JavaDoc i = _all.iterator();
213
214         int x = 0;
215         while (i.hasNext())
216         {
217             HeapEntry e = (HeapEntry)i.next();
218             _ret[x++] = e.event_;
219         }
220
221         return _ret;
222     }
223
224
225     public boolean isEmpty()
226     {
227         return (getSize() == 0);
228     }
229
230
231     public int getSize()
232     {
233         return heap_.size();
234     }
235     
236     public String JavaDoc toString()
237     {
238         return heap_.toString();
239     }
240 }
241
Popular Tags