1 package org.jacorb.notification.queue; 2 3 23 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import org.jacorb.notification.interfaces.Message; 30 31 import EDU.oswego.cs.dl.util.concurrent.Heap; 32 33 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 52 public BoundedPriorityEventQueue(int maxSize, 53 EventQueueOverflowStrategy overflowStrategy) 54 { 55 super(maxSize, overflowStrategy, new Object ()); 56 57 maxCapacity_ = maxSize; 58 59 heap_ = newHeap(); 60 } 61 62 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 _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 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 _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 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 _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 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 _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 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 _events = new ArrayList (); 172 Object _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 getAllElementsInternal() 191 { 192 List _events = new ArrayList (); 193 Object _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 _all = getAllElementsInternal(); 209 210 Message[] _ret = new Message[_all.size()]; 211 212 Iterator 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 toString() 237 { 238 return heap_.toString(); 239 } 240 } 241 | Popular Tags |