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 BoundedDeadlineEventQueue extends AbstractBoundedEventQueue 43 { 44 private Heap heap_; 45 46 private long counter_ = 0; 47 48 50 public BoundedDeadlineEventQueue( int maxSize, 51 EventQueueOverflowStrategy overflowStrategy ) 52 { 53 super(maxSize, overflowStrategy, new Object ()); 54 55 heap_ = new Heap( maxSize, QueueUtil.ASCENDING_TIMEOUT_COMPARATOR ); 56 } 57 58 60 protected Message getNextElement() 61 { 62 return getEarliestTimeout(); 63 } 64 65 66 protected Message getOldestElement() 67 { 68 List _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 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 _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 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 _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 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 _events = new ArrayList (); 145 Object _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 getAllElementsInternal() 164 { 165 List _events = new ArrayList (heap_.size()); 166 Object _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 _all = getAllElementsInternal(); 180 181 Message[] _ret = new Message[ _all.size() ]; 182 183 Iterator 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 |