KickJava   Java API By Example, From Geeks To Geeks.

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


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: BoundedDeadlineEventQueue.java,v 1.6 2005/02/14 00:11:10 alphonse.bendt Exp $
40  */

41
42 public class BoundedDeadlineEventQueue extends AbstractBoundedEventQueue
43 {
44     private Heap heap_;
45
46     private long counter_ = 0;
47
48     ////////////////////////////////////////
49

50     public BoundedDeadlineEventQueue( int maxSize,
51                                       EventQueueOverflowStrategy overflowStrategy )
52     {
53         super(maxSize, overflowStrategy, new Object JavaDoc());
54         
55         heap_ = new Heap( maxSize, QueueUtil.ASCENDING_TIMEOUT_COMPARATOR );
56     }
57
58     ////////////////////////////////////////
59

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