Produced by Araxis Merge on 12/5/2017 12:06:37 PM Central Standard Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
| # | Location | File | Last Modified |
|---|---|---|---|
| 1 | IV-eHMP_CIF.zip\IMAG_Source\VISA\Java\CoreRouter\main\src\java\gov\va\med\imaging\core\router\queue | AsynchronousCommandProcessorPriorityBlockingQueue.java | Mon Dec 4 21:35:16 2017 UTC |
| 2 | IV-eHMP_CIF.zip\IMAG_Source\VISA\Java\CoreRouter\main\src\java\gov\va\med\imaging\core\router\queue | AsynchronousCommandProcessorPriorityBlockingQueue.java | Mon Dec 4 21:58:32 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 4 | 1434 |
| Changed | 3 | 6 |
| Inserted | 0 | 0 |
| Removed | 0 | 0 |
| Whitespace | |
|---|---|
| Character case | Differences in character case are significant |
| Line endings | Differences in line endings (CR and LF characters) are ignored |
| CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
| 1 | package go v.va.med.i maging.cor e.router.q ueue; | |
| 2 | ||
| 3 | import gov .va.med.im aging.core .router.Ab stractComm andImpl; | |
| 4 | ||
| 5 | import jav a.util.Col lection; | |
| 6 | import jav a.util.Com parator; | |
| 7 | import jav a.util.Dat e; | |
| 8 | import jav a.util.Ite rator; | |
| 9 | import jav a.util.Pri orityQueue ; | |
| 10 | import jav a.util.Que ue; | |
| 11 | import jav a.util.con current.Bl ockingQueu e; | |
| 12 | import jav a.util.con current.Pr iorityBloc kingQueue; | |
| 13 | import jav a.util.con current.Ti meUnit; | |
| 14 | ||
| 15 | import org .apache.lo gging.log4 j.LogManag er; | |
| 16 | import org .apache.lo gging.log4 j.Logger; | |
| 17 | ||
| 18 | /** | |
| 19 | * This Qu eue implem entation c ombines a Priority Q ueue with scheduled availabili ty. | |
| 20 | * Each qu eue elemen t is marke d with opt ional prio rity and s cheduled a vailabilit y. | |
| 21 | * If an i tems sched uled avail ability ha s not occu rred yet t hen the it em is not available | |
| 22 | * and tak e() and | |
| 23 | * | |
| 24 | * An unbo unded bloc king queue that uses the same ordering r ules as cl ass Priori tyQueue on ce | |
| 25 | * a queue element's accessibl e time has passed. T his queue supplies b locking re trieval | |
| 26 | * operati ons. While this queu e is logic ally unbou nded, atte mpted | |
| 27 | * additio ns may fai l due to r esource ex haustion ( causing Ou tOfMemoryE rror). Thi s class do es | |
| 28 | * not per mit null e lements. A priority queue rely ing on nat ural order ing also d oes not pe rmit | |
| 29 | * inserti on of non- comparable objects ( doing so r esults in ClassCastE xception). | |
| 30 | * | |
| 31 | * This cl ass and it s iterator implement all of th e optional methods o f the Coll ection and Iterator | |
| 32 | * interfa ces. The I terator pr ovided in method ite rator() is not guara nteed to t raverse th e elements | |
| 33 | * of the PriorityBl ockingQueu e in any p articular order. If you need o rdered tra versal, co nsider usi ng | |
| 34 | * Arrays. sort(pq.to Array()). Also, meth od drainTo can be us ed to remo ve some or all eleme nts in pri ority | |
| 35 | * order a nd place t hem in ano ther colle ction. | |
| 36 | * | |
| 37 | * Operati ons on thi s class ma ke no guar antees abo ut the ord ering of e lements wi th equal p riority. I f you | |
| 38 | * need to enforce a n ordering , you can define cus tom classe s or compa rators tha t use a se condary ke y to | |
| 39 | * break t ies in pri mary prior ity values . For exam ple, here is a class that appl ies first- in-first-o ut | |
| 40 | * tie-bre aking to c omparable elements. To use it, you would insert a new FIFOEn try(anEntr y) instead of a | |
| 41 | * plain e ntry objec t. | |
| 42 | * | |
| 43 | * Whateve r the orde ring used, the head of the que ue is that element w hich would be remove d by a cal l to | |
| 44 | * remove( ) or poll( ). In a FI FO queue, all new el ements are inserted at the ta il of the queue. Oth er kinds | |
| 45 | * of queu es may use different placement rules. Ev ery Queue implementa tion must specify it s ordering propertie s. | |
| 46 | * | |
| 47 | * This qu eue is sor ted by a c ombination of elemen t accessib ility date , commence ment date and priori ty. | |
| 48 | * The hea d of this queue is t he least e lement wit h respect to the spe cified ord ering. | |
| 49 | * | |
| 50 | * @author
|
|
| 51 | * | |
| 52 | * @param <E> | |
| 53 | */ | |
| 54 | public cla ss Asynchr onousComma ndProcesso rPriorityB lockingQue ue | |
| 55 | implements BlockingQ ueue<Abstr actCommand Impl<?>> | |
| 56 | { | |
| 57 | // private fi nal Cluste rablePrior ityBlockin gQueue<Abs tractComma ndImpl<?>> priorityQ ueue; | |
| 58 | pr ivate fina l Priority BlockingQu eue<Abstra ctCommandI mpl<?>> pr iorityQueu e; | |
| 59 | pr ivate fina l Inaccess ibleQueueE lements sc heduledQue ue; | |
| 60 | pr ivate stat ic final l ong serial VersionUID = -639929 3227643514 590L; | |
| 61 | ||
| 62 | pr ivate Logg er logger = LogManag er.getLogg er(this.ge tClass()); | |
| 63 | ||
| 64 | /* * | |
| 65 | * Create an instance using the natural or dering of the elemen ts. | |
| 66 | * / | |
| 67 | pu blic Async hronousCom mandProces sorPriorit yBlockingQ ueue() | |
| 68 | { | |
| 69 | //pr iorityQueu e = new Cl usterableP riorityBlo ckingQueue <AbstractC ommandImpl <?>>( | |
| 70 | // 10, ne w Schedule dPriorityQ ueueElemen tPriorityC omparator( ) | |
| 71 | //); | |
| 72 | prio rityQueue = new Prio rityBlocki ngQueue<Ab stractComm andImpl<?> >( | |
| 73 | 10, ne w Schedule dPriorityQ ueueElemen tPriorityC omparator( ) | |
| 74 | ); | |
| 75 | ||
| 76 | sche duledQueue = new Ina ccessibleQ ueueElemen ts(priorit yQueue); | |
| 77 | } | |
| 78 | ||
| 79 | /* * | |
| 80 | * Create an instance using a Co mparator t o establis h the prio rity of th e elements . | |
| 81 | * | |
| 82 | * @param in itialCapac ity | |
| 83 | * @param co mparator | |
| 84 | * / | |
| 85 | pu blic Async hronousCom mandProces sorPriorit yBlockingQ ueue(int i nitialCapa city) | |
| 86 | { | |
| 87 | //pr iorityQueu e = new Cl usterableP riorityBlo ckingQueue <AbstractC ommandImpl <?>>( | |
| 88 | // initia lCapacity, new Sched uledPriori tyQueueEle mentPriori tyComparat or() | |
| 89 | //); | |
| 90 | prio rityQueue = new Prio rityBlocki ngQueue<Ab stractComm andImpl<?> >( | |
| 91 | 10, ne w Schedule dPriorityQ ueueElemen tPriorityC omparator( ) | |
| 92 | ); | |
| 93 | sche duledQueue = new Ina ccessibleQ ueueElemen ts(priorit yQueue); | |
| 94 | } | |
| 95 | ||
| 96 | // ========= ========== ========== ========== ========== ========== ========== ========= | |
| 97 | // Queue Add ition Meth ods | |
| 98 | // | |
| 99 | // The queue is unboun ded so the behavior of add(), put() and offer() is | |
| 100 | // identical . add() a nd its var iants are implemente d, put() a nd offer() | |
| 101 | // are pass- through ca lls to add (). | |
| 102 | // ========= ========== ========== ========== ========== ========== ========== ========= | |
| 103 | ||
| 104 | /* * | |
| 105 | * Add an el ement to t he priorit y queue, m aking it i mmediately accessibl e. | |
| 106 | * | |
| 107 | * @see java .util.Queu e#add(java .lang.Obje ct) | |
| 108 | * / | |
| 109 | @O verride | |
| 110 | public boolean a dd(Abstrac tCommandIm pl<?> e) | |
| 111 | { | |
| 112 | // if the el ement's ac cessible d ate has pa ssed then immediatel y add the element to the prior ity | |
| 113 | // queue | |
| 114 | if ( e.getAcc essibility Date().aft er(new Dat e()) ) | |
| 115 | retu rn schedul edQueue.ad d(e); | |
| 116 | el se | |
| 117 | return priorityQ ueue.add(e ); | |
| 118 | } | |
| 119 | ||
| 120 | @Overr ide | |
| 121 | public boolean a ddAll(Coll ection<? e xtends Abs tractComma ndImpl<?>> c) | |
| 122 | { | |
| 123 | bool ean result = true; | |
| 124 | for( Iterator<? extends A bstractCom mandImpl<? >> i = c.i terator(); i.hasNext (); ) | |
| 125 | result &= add(i. next()); | |
| 126 | ||
| 127 | retu rn result; | |
| 128 | } | |
| 129 | ||
| 130 | ||
| 131 | /** | |
| 132 | * Ins erts the s pecified e lement int o this pri ority queu e. | |
| 133 | * As the queue is unbound ed this me thod will never bloc k. | |
| 134 | * | |
| 135 | * @se e java.uti l.concurre nt.Blockin gQueue#off er(java.la ng.Object) | |
| 136 | */ | |
| 137 | @O verride | |
| 138 | public boolean o ffer(Abstr actCommand Impl<?> e) | |
| 139 | { | |
| 140 | retu rn add(e); | |
| 141 | } | |
| 142 | ||
| 143 | /* * | |
| 144 | * As the qu eue is unb ounded thi s method a lways retu rns immedi ately. | |
| 145 | * The timeo ut and tim e unit par ameters ar e ignored. | |
| 146 | * | |
| 147 | * @see java .util.conc urrent.Blo ckingQueue #offer(jav a.lang.Obj ect, long, java.util .concurren t.TimeUnit ) | |
| 148 | * / | |
| 149 | @O verride | |
| 150 | public boolean o ffer(Abstr actCommand Impl<?> e, long time out, TimeU nit unit) | |
| 151 | th rows Inter ruptedExce ption | |
| 152 | { | |
| 153 | retu rn add(e); | |
| 154 | } | |
| 155 | ||
| 156 | /* * | |
| 157 | * As the qu eue is unb ounded thi s method a lways retu rns immedi ately. | |
| 158 | * | |
| 159 | * @see java .util.conc urrent.Blo ckingQueue #put(java. lang.Objec t) | |
| 160 | * / | |
| 161 | @O verride | |
| 162 | public void put( AbstractCo mmandImpl< ?> e) | |
| 163 | th rows Inter ruptedExce ption | |
| 164 | { | |
| 165 | add( e); | |
| 166 | } | |
| 167 | ||
| 168 | // === ========== ========== ========== ========== ========== ========== ========== ========== ======= | |
| 169 | // Ele ment retri eval metho ds. | |
| 170 | // === ========== ========== ========== ========== ========== ========== ========== ========== ======= | |
| 171 | ||
| 172 | @O verride | |
| 173 | public AbstractC ommandImpl <?> elemen t() | |
| 174 | { | |
| 175 | return p riorityQue ue.element (); | |
| 176 | } | |
| 177 | ||
| 178 | /* * | |
| 179 | * Retrieves and remov es the hea d of this queue, | |
| 180 | * waiting i f necessar y until an element b ecomes ava ilable. | |
| 181 | * | |
| 182 | * @see java .util.conc urrent.Blo ckingQueue #take() | |
| 183 | * / | |
| 184 | @O verride | |
| 185 | public AbstractC ommandImpl <?> take() | |
| 186 | th rows Inter ruptedExce ption | |
| 187 | { | |
| 188 | return p riorityQue ue.take(); | |
| 189 | } | |
| 190 | ||
| 191 | /* * | |
| 192 | * Retrieves , but does not remov e, the hea d of this queue, or returns nu ll if this queue is empty. | |
| 193 | * | |
| 194 | * @see java .util.Queu e#peek() | |
| 195 | * / | |
| 196 | @O verride | |
| 197 | public AbstractC ommandImpl <?> peek() | |
| 198 | { | |
| 199 | return p riorityQue ue.peek(); | |
| 200 | } | |
| 201 | ||
| 202 | /* * | |
| 203 | * Retrieves and remov es the hea d of this queue, or returns nu ll if this queue is empty. | |
| 204 | * | |
| 205 | * @see java .util.Queu e#poll() | |
| 206 | * / | |
| 207 | @O verride | |
| 208 | public AbstractC ommandImpl <?> poll() | |
| 209 | { | |
| 210 | return p riorityQue ue.poll(); | |
| 211 | } | |
| 212 | ||
| 213 | /* * | |
| 214 | * Retrieves and remov es the hea d of this queue, wai ting up to the speci fied wait | |
| 215 | * time if n ecessary f or an elem ent to bec ome availa ble. | |
| 216 | * @see java .util.conc urrent.Blo ckingQueue #poll(long , java.uti l.concurre nt.TimeUni t) | |
| 217 | * / | |
| 218 | @O verride | |
| 219 | public AbstractC ommandImpl <?> poll(l ong timeou t, TimeUni t unit) | |
| 220 | throws Interrupt edExceptio n | |
| 221 | { | |
| 222 | return p riorityQue ue.poll(ti meout, uni t); | |
| 223 | } | |
| 224 | ||
| 225 | /* * | |
| 226 | * Retrieves and remov es the hea d of this queue. | |
| 227 | * This meth od differs from poll only in t hat it thr ows an exc eption if this queue is empty. | |
| 228 | * | |
| 229 | * @see java .util.Queu e#remove() | |
| 230 | * / | |
| 231 | @O verride | |
| 232 | public AbstractC ommandImpl <?> remove () | |
| 233 | { | |
| 234 | return p riorityQue ue.remove( ); | |
| 235 | } | |
| 236 | ||
| 237 | /* * | |
| 238 | * Drain all elements whose acce ssibility date has p assed to t he specifi ed collect ion. | |
| 239 | * | |
| 240 | * @see java .util.conc urrent.Blo ckingQueue #drainTo(j ava.util.C ollection) | |
| 241 | * / | |
| 242 | @O verride | |
| 243 | public int drain To(Collect ion<? supe r Abstract CommandImp l<?>> c) | |
| 244 | { | |
| 245 | retu rn priorit yQueue.dra inTo(c); | |
| 246 | } | |
| 247 | ||
| 248 | ||
| 249 | /* * | |
| 250 | * Drain up to maxElem ents eleme nts whose accessibil ity date h as passed to the spe cified col lection. | |
| 251 | * | |
| 252 | * @see java .util.conc urrent.Blo ckingQueue #drainTo(j ava.util.C ollection, int) | |
| 253 | * / | |
| 254 | ||
| 255 | @O verride | |
| 256 | public int drain To(Collect ion<? supe r Abstract CommandImp l<?>> c, i nt maxElem ents) | |
| 257 | { | |
| 258 | retu rn priorit yQueue.dra inTo(c, ma xElements) ; | |
| 259 | } | |
| 260 | ||
| 261 | /* * | |
| 262 | * Clear the entire qu eue, regar dless of t he element s accessib ility date . | |
| 263 | * @see java .util.Coll ection#cle ar() | |
| 264 | * / | |
| 265 | @O verride | |
| 266 | public void clea r() | |
| 267 | { | |
| 268 | sche duledQueue .clear(); | |
| 269 | prio rityQueue. clear(); | |
| 270 | } | |
| 271 | ||
| 272 | @O verride | |
| 273 | @S uppressWar nings("unc hecked") | |
| 274 | public boolean c ontains(Ob ject o) | |
| 275 | { | |
| 276 | if(o instanceo f Comparab le) | |
| 277 | { | |
| 278 | return scheduled Queue.cont ains(o) ? true : pri orityQueue .contains( o) ? true : false; | |
| 279 | } | |
| 280 | retu rn false; | |
| 281 | } | |
| 282 | ||
| 283 | @O verride | |
| 284 | public boolean c ontainsAll (Collectio n<?> c) | |
| 285 | { | |
| 286 | for( Iterator<? > i = c.it erator(); i.hasNext( ); ) | |
| 287 | if(! c ontains(i. next())) | |
| 288 | return f alse; | |
| 289 | retu rn true; | |
| 290 | } | |
| 291 | ||
| 292 | /* * | |
| 293 | * NOTE: eve n if this method ret urns false that the elements m ay not be | |
| 294 | * accessibl e (by sche duled time ) and ther efore peek (), poll() , and remo ve() | |
| 295 | * may still return nu ll or thro w exceptio ns | |
| 296 | * | |
| 297 | * @see java .util.Coll ection#isE mpty() | |
| 298 | * / | |
| 299 | @O verride | |
| 300 | public boolean i sEmpty() | |
| 301 | { | |
| 302 | return p riorityQue ue.isEmpty () && sche duledQueue .isEmpty() ; | |
| 303 | } | |
| 304 | ||
| 305 | /* * | |
| 306 | * Iterate o ver the co llection o f all elem ents regar dless of a ccessibili ty date. | |
| 307 | * The eleme nts will b e returned from the iterator i n: | |
| 308 | * 1.) order by priori ty (as det ermined by Comparato r or natur al orderin g) if the accessibil ity date h as passed | |
| 309 | * 2.) order by access ibility da te | |
| 310 | * This corr elates to the order that the e lements wo uld be acc essed usin g take() o r remove() , assuming that | |
| 311 | * no change s were mad e to the q ueue durin g iteratio n. | |
| 312 | * | |
| 313 | * @see java .util.Coll ection#ite rator() | |
| 314 | * / | |
| 315 | @O verride | |
| 316 | public Iterator< AbstractCo mmandImpl< ?>> iterat or() | |
| 317 | { | |
| 318 | retu rn new Ite rator<Abst ractComman dImpl<?>>( ) | |
| 319 | { | |
| 320 | privat e Iterator <AbstractC ommandImpl <?>> wrapp edIter = p riorityQue ue.iterato r(); | |
| 321 | boolea n complete dPriorityQ ueue = fal se; | |
| 322 | ||
| 323 | @Overr ide | |
| 324 | public b oolean has Next() | |
| 325 | { | |
| 326 | boolean result = w rappedIter .hasNext() ; | |
| 327 | if(!resu lt && !com pletedPrio rityQueue) | |
| 328 | { | |
| 329 | completedP riorityQue ue = true; | |
| 330 | wrappedIte r = schedu ledQueue.i terator(); | |
| 331 | result = w rappedIter .hasNext() ; | |
| 332 | } | |
| 333 | return res ult; | |
| 334 | } | |
| 335 | ||
| 336 | @Overr ide | |
| 337 | public A bstractCom mandImpl<? > next() | |
| 338 | { | |
| 339 | Abstract CommandImp l<?> resul t = wrappe dIter.next (); | |
| 340 | if(resul t == null && !comple tedPriorit yQueue) | |
| 341 | { | |
| 342 | completedP riorityQue ue = true; | |
| 343 | wrappedIte r = schedu ledQueue.i terator(); | |
| 344 | result = w rappedIter .next(); | |
| 345 | } | |
| 346 | return res ult; | |
| 347 | } | |
| 348 | ||
| 349 | @Overr ide | |
| 350 | public v oid remove () | |
| 351 | { | |
| 352 | wrappedI ter.remove (); | |
| 353 | } | |
| 354 | ||
| 355 | }; | |
| 356 | } | |
| 357 | ||
| 358 | @O verride | |
| 359 | public boolean r emove(Obje ct o) | |
| 360 | { | |
| 361 | retu rn priorit yQueue.rem ove(o) || scheduledQ ueue.remov e(o); | |
| 362 | } | |
| 363 | ||
| 364 | @O verride | |
| 365 | public boolean r emoveAll(C ollection< ?> c) | |
| 366 | { | |
| 367 | return f alse; | |
| 368 | } | |
| 369 | ||
| 370 | @O verride | |
| 371 | public boolean r etainAll(C ollection< ?> c) | |
| 372 | { | |
| 373 | return f alse; | |
| 374 | } | |
| 375 | ||
| 376 | @O verride | |
| 377 | public int size( ) | |
| 378 | { | |
| 379 | retu rn priorit yQueue.siz e() + sche duledQueue .size(); | |
| 380 | } | |
| 381 | ||
| 382 | public int acces sibleSize( ) | |
| 383 | { | |
| 384 | retu rn priorit yQueue.siz e(); | |
| 385 | } | |
| 386 | ||
| 387 | /** | |
| 388 | * Ret urn the nu mber of ac cessible q ueue eleme nts of at least the | |
| 389 | * spe cified pri ority | |
| 390 | * | |
| 391 | * @pa ram priori ty | |
| 392 | * @re turn | |
| 393 | */ | |
| 394 | public int acces sibleOfPri ority(Sche duledPrior ityQueueEl ement.Prio rity prior ity) | |
| 395 | { | |
| 396 | Ab stractComm andImpl<?> [] queueEl ements = | |
| 397 | prio rityQueue. toArray(ne w Abstract CommandImp l<?>[prior ityQueue.s ize()]); | |
| 398 | ||
| 399 | in t count = 0; | |
| 400 | fo r(Abstract CommandImp l<?> queue Element : queueEleme nts) | |
| 401 | if( queueEleme nt.getPrio rity().com pareTo(pri ority) >= 0) | |
| 402 | ++coun t; | |
| 403 | ||
| 404 | re turn count ; | |
| 405 | } | |
| 406 | ||
| 407 | /** | |
| 408 | * Ret urn an arr ay of the accessible elements. | |
| 409 | * | |
| 410 | * @se e java.uti l.Collecti on#toArray () | |
| 411 | */ | |
| 412 | @O verride | |
| 413 | public Object[] toArray() | |
| 414 | { | |
| 415 | return p riorityQue ue.toArray (new Abstr actCommand Impl[prior ityQueue.s ize()]); | |
| 416 | } | |
| 417 | ||
| 418 | /* * | |
| 419 | * Ret urn an arr ay of the accessible elements. | |
| 420 | * | |
| 421 | * @see java .util.Coll ection#toA rray(T[]) | |
| 422 | * / | |
| 423 | @O verride | |
| 424 | public <Asynchro nousComman dProcessor > Asynchro nousComman dProcessor [] toArray (Asynchron ousCommand Processor[ ] a) | |
| 425 | { | |
| 426 | return p riorityQue ue.toArray (a); | |
| 427 | } | |
| 428 | ||
| 429 | /* * | |
| 430 | * Ret urn the re maining ca pacity of the access ible eleme nts. | |
| 431 | * | |
| 432 | * @see java .util.conc urrent.Blo ckingQueue #remaining Capacity() | |
| 433 | * / | |
| 434 | @O verride | |
| 435 | public int remai ningCapaci ty() | |
| 436 | { | |
| 437 | return p riorityQue ue.remaini ngCapacity (); | |
| 438 | } | |
| 439 | ||
| 440 | /* * | |
| 441 | * A Compara tor that u ses the ta rget date and the pr iority to sort | |
| 442 | * the Sched uledPriori tyQueueEle ment insta nces in th e priority queue. | |
| 443 | * The order ing is don e first by the targe t date (lo wer dates compare hi gher), | |
| 444 | * then the priority ( higher pri ority comp ares highe r) | |
| 445 | * used when tasks are estimated to overla p in execu tion. | |
| 446 | * | |
| 447 | * "The head of this q ueue is th e least el ement with respect t o the spec ified orde ring." | |
| 448 | * "Whatever the order ing used, the head o f the queu e is that element wh ich would be | |
| 449 | * removed b y a call t o remove() or poll() ." | |
| 450 | * | |
| 451 | * @author
|
|
| 452 | * / | |
| 453 | cl ass Schedu ledPriorit yQueueElem entPriorit yComparato r | |
| 454 | im plements C omparator< AbstractCo mmandImpl< ?>> | |
| 455 | { | |
| 456 | /* | |
| 457 | * C ompares it s two argu ments for order. | |
| 458 | * R eturns: | |
| 459 | * n egative in teger - th e first ar gument is less than the second item | |
| 460 | * z ero - the first argu ment is eq ual to the second it em | |
| 461 | * p ositive in teger - th e first ar gument is greater th an the sec ond item | |
| 462 | */ | |
| 463 | @Ove rride | |
| 464 | pu blic int c ompare(Abs tractComma ndImpl<?> element1, AbstractCo mmandImpl< ?> element 2) | |
| 465 | { | |
| 466 | long p rocessingC ommencemen tDateDelta = element 1.getProce ssingComme ncementTar getDate(). getTime() - | |
| 467 | element2 .getProces singCommen cementTarg etDate().g etTime(); | |
| 468 | int re sult = 0; | |
| 469 | ||
| 470 | // if processing Commenceme ntDateDelt a < 0 then element1 target dat e is befor e element2 target da te | |
| 471 | // ele ment1 shou ld be sort ed as less than elem ent2 unles s element1 processin g | |
| 472 | // is estimated to overlap with elem ent2 proce ssing (ass uming sync hronous ex ecution) | |
| 473 | // and element2 has a high er priorit y | |
| 474 | if(pro cessingCom mencementD ateDelta < 0) | |
| 475 | { | |
| 476 | logger.d ebug(eleme nt1.toStri ng() + " c ommencemen t date is before " + element2. toString() + " comme ncement da te" ); | |
| 477 | ||
| 478 | // in th e default case the e stimated d uration is NOT provi ded, so ca nnot be us ed in the | |
| 479 | // prior ity calcul ation, sor t strictly by priori ty | |
| 480 | if(eleme nt1.getPro cessingDur ationEstim ate() < 0) | |
| 481 | { | |
| 482 | result = - 1 * ( elem ent1.getPr iority().c ompareTo(e lement2.ge tPriority( )) ); | |
| 483 | } | |
| 484 | else | |
| 485 | { | |
| 486 | Date eleme nt1Estimat edCompleti onDate = | |
| 487 | ne w Date(ele ment1.getP rocessingC ommencemen tTargetDat e().getTim e() + elem ent1.getPr ocessingDu rationEsti mate()); | |
| 488 | logger.deb ug(element 1.toString () + " est imated com pletion da te is " + element1Es timatedCom pletionDat e ); | |
| 489 | ||
| 490 | // if the estimated completion date of e lement1 is greater t han the ta rget date | |
| 491 | // of elem ent2 | |
| 492 | // and | |
| 493 | // element 2 is highe r priority | |
| 494 | // then el ement2 is sorted clo ser to the head (-1) | |
| 495 | if( elemen t1Estimate dCompletio nDate.comp areTo(elem ent2.getPr ocessingCo mmencement TargetDate ()) > 0 && | |
| 496 | elem ent2.getPr iority().c ompareTo(e lement1.ge tPriority( )) > 0) | |
| 497 | re sult = -1; // eleme nt2 is clo ser to the head than element1 | |
| 498 | else | |
| 499 | re sult = 1; | |
| 500 | } | |
| 501 | } | |
| 502 | // if targetDate Delta > 0 then eleme nt1 target date is a fter eleme nt2 target date | |
| 503 | // ele ment2 shou ld be sort ed as clos er to head than elem ent1 unles s element2 processin g | |
| 504 | // is estimated to overlap with elem ent1 proce ssing (ass uming sync hronous ex ecution) | |
| 505 | // and element1 has a high er priorit y | |
| 506 | else i f(processi ngCommence mentDateDe lta > 0) | |
| 507 | { | |
| 508 | logger.d ebug(eleme nt1.toStri ng() + " c ommencemen t date is before " + element2. toString() + " comme ncement da te" ); | |
| 509 | ||
| 510 | // in th e default case the e stimated d uration is NOT provi ded, so ca nnot be us ed in the | |
| 511 | // prior ity calcul ation, sor t strictly by priori ty | |
| 512 | if(eleme nt2.getPro cessingDur ationEstim ate() < 0) | |
| 513 | { | |
| 514 | result = - 1 * ( elem ent1.getPr iority().c ompareTo(e lement2.ge tPriority( )) ); | |
| 515 | } | |
| 516 | else | |
| 517 | { | |
| 518 | Date eleme nt2Estimat edCompleti onDate = | |
| 519 | ne w Date(ele ment2.getP rocessingC ommencemen tTargetDat e().getTim e() + elem ent2.getPr ocessingDu rationEsti mate()); | |
| 520 | logger.deb ug(element 2.toString () + " est imated com pletion da te is " + element2Es timatedCom pletionDat e ); | |
| 521 | ||
| 522 | // if the estimated completion date of e lement2 is greater t han the ta rget date | |
| 523 | // of elem ent1 | |
| 524 | // and | |
| 525 | // element 1 is highe r priority | |
| 526 | // then el ement1 is sorted clo ser to the head (-1) | |
| 527 | if( elemen t2Estimate dCompletio nDate.comp areTo(elem ent1.getPr ocessingCo mmencement TargetDate ()) > 0 && | |
| 528 | elem ent1.getPr iority().c ompareTo(e lement2.ge tPriority( )) > 0) | |
| 529 | re sult = -1; // eleme nt1 is gre ater than element2 | |
| 530 | else | |
| 531 | re sult = 1; | |
| 532 | } | |
| 533 | } | |
| 534 | else | |
| 535 | { | |
| 536 | logger.d ebug(eleme nt1.toStri ng() + " c ommencemen t date is equal to " + element 2.toString () + " com mencement date" ); | |
| 537 | ||
| 538 | // if th e processi ng commenc ement date s are the same then | |
| 539 | // sort by priorit y only | |
| 540 | // enum. compareTo( ) = "Retur ns a negat ive intege r, zero, o r a positi ve integer as | |
| 541 | // this object is less than, equal to, or greate r than the specified object. | |
| 542 | // Enum constants are only c omparable to other e num consta nts of the same enum type. | |
| 543 | // The n atural ord er impleme nted by th is method is the ord er in whic h the cons tants are declared." | |
| 544 | // i.e. if element 1 priority compared to element 2 priority is -1 mea ns that el ement1 | |
| 545 | // prior ity is low er (since priority i s in incre asing orde r) and ele ment1 shou ld come la ter | |
| 546 | // in th e processi ng (i.e. c loser to t he tail) t herefore r eturn 1 | |
| 547 | result = -1 * ( el ement1.get Priority() .compareTo (element2. getPriorit y()) ); | |
| 548 | } | |
| 549 | ||
| 550 | logger .debug( | |
| 551 | element1 .toString( ) + " is " + | |
| 552 | (result> 0 ? "lower priority" : result< 0 ? "highe r priority " : "equal priority" ) + | |
| 553 | " relati ve to " + element2.t oString()) ; | |
| 554 | ||
| 555 | return result; | |
| 556 | } | |
| 557 | } | |
| 558 | ||
| 559 | /* * | |
| 560 | * The Compa rator that orders th e schedule d queue by accessibl e date | |
| 561 | * @author
|
|
| 562 | * | |
| 563 | * / | |
| 564 | pr ivate clas s Schedule dPriorityQ ueueElemen tAccessibl eDateCompa rator | |
| 565 | im plements C omparator< ScheduledP riorityQue ueElement> | |
| 566 | { | |
| 567 | @Ove rride | |
| 568 | pu blic int c ompare(Sch eduledPrio rityQueueE lement o1, Scheduled PriorityQu eueElement o2) | |
| 569 | { | |
| 570 | int re sult = o1. getAccessi bilityDate ().compare To(o2.getA ccessibili tyDate()); | |
| 571 | return result; | |
| 572 | } | |
| 573 | } | |
| 574 | ||
| 575 | /* * | |
| 576 | * A queue t hat automa tically ad ds element s to the g iven desti nation | |
| 577 | * once the accessibil ity date o f its memb er element s is reach ed. | |
| 578 | * / | |
| 579 | pr ivate clas s Inaccess ibleQueueE lements | |
| 580 | { | |
| 581 | priv ate Priori tyQueue<Ab stractComm andImpl<?> > accessib leDatePrio rityQueue; | |
| 582 | priv ate static final lon g serialVe rsionUID = 1L; | |
| 583 | priv ate final ScheduleQu eueTransfe rThread tr ansferThre ad; | |
| 584 | priv ate final Queue<Abst ractComman dImpl<?>> destinatio n; | |
| 585 | ||
| 586 | Inac cessibleQu eueElement s(Queue<Ab stractComm andImpl<?> > destinat ion) | |
| 587 | { | |
| 588 | access ibleDatePr iorityQueu e = | |
| 589 | new Prio rityQueue< AbstractCo mmandImpl< ?>>(10, ne w Schedule dPriorityQ ueueElemen tAccessibl eDateCompa rator()); | |
| 590 | this.d estination = destina tion; | |
| 591 | transf erThread = new Sched uleQueueTr ansferThre ad(); | |
| 592 | transf erThread.s tart(); | |
| 593 | } | |
| 594 | ||
| 595 | pu blic int s ize() | |
| 596 | { | |
| 597 | retu rn accessi bleDatePri orityQueue .size(); | |
| 598 | } | |
| 599 | ||
| 600 | publ ic boolean remove(Ob ject o) | |
| 601 | { | |
| 602 | retu rn accessi bleDatePri orityQueue .remove(o) ; | |
| 603 | } | |
| 604 | ||
| 605 | publ ic Iterato r<Abstract CommandImp l<?>> iter ator() | |
| 606 | { | |
| 607 | retu rn accessi bleDatePri orityQueue .iterator( ); | |
| 608 | } | |
| 609 | ||
| 610 | publ ic boolean isEmpty() | |
| 611 | { | |
| 612 | retu rn accessi bleDatePri orityQueue .isEmpty() ; | |
| 613 | } | |
| 614 | ||
| 615 | publ ic boolean contains( Object o) | |
| 616 | { | |
| 617 | retu rn accessi bleDatePri orityQueue .contains( o); | |
| 618 | } | |
| 619 | ||
| 620 | publ ic void cl ear() | |
| 621 | { | |
| 622 | acce ssibleDate PriorityQu eue.clear( ); | |
| 623 | } | |
| 624 | ||
| 625 | publ ic Schedul edPriority QueueEleme nt peek() | |
| 626 | { | |
| 627 | return accessibl eDatePrior ityQueue.p eek(); | |
| 628 | } | |
| 629 | ||
| 630 | publ ic Schedul edPriority QueueEleme nt poll() | |
| 631 | { | |
| 632 | return accessibl eDatePrior ityQueue.p oll(); | |
| 633 | } | |
| 634 | ||
| 635 | publ ic boolean add(Abstr actCommand Impl<?> e) | |
| 636 | { | |
| 637 | logger .info("Add ing new sc heduled qu eue elemen t"); | |
| 638 | bool ean result = accessi bleDatePri orityQueue .add(e); | |
| 639 | newS cheduledEl ementNotif ication(); | |
| 640 | retu rn result; | |
| 641 | } | |
| 642 | ||
| 643 | // g et the dat e of the f irst eleme nt in the queue, the closest d ate in | |
| 644 | // t he future | |
| 645 | priv ate void n ewSchedule dElementNo tification () | |
| 646 | { | |
| 647 | Date n extAccessi bleElement Date = acc essibleDat ePriorityQ ueue.peek( ).getAcces sibilityDa te(); | |
| 648 | transf erThread.n extSchedul edElementN otificatio n(nextAcce ssibleElem entDate); | |
| 649 | } | |
| 650 | ||
| 651 | priv ate void t ransferEli gibleSched uledElemen ts() | |
| 652 | { | |
| 653 | // rem ove elemen t from the scheduled queue who se accessi bility dat e has pass ed | |
| 654 | // and add them to the des tination | |
| 655 | for(Sc heduledPri orityQueue Element el ement = ac cessibleDa tePriority Queue.peek (); | |
| 656 | element != null && element.g etAccessib ilityDate( ).before(n ew Date()) ; | |
| 657 | element = accessib leDatePrio rityQueue. peek()) | |
| 658 | { | |
| 659 | logger.i nfo("Trans fering sch eduled ele ment to pr iority que ue"); | |
| 660 | destinat ion.add(ac cessibleDa tePriority Queue.poll ()); | |
| 661 | } | |
| 662 | } | |
| 663 | ||
| 664 | priv ate class ScheduleQu eueTransfe rThread | |
| 665 | exte nds Thread | |
| 666 | { | |
| 667 | privat e Schedule QueueTrans ferThread( ) | |
| 668 | { | |
| 669 | super( ); | |
| 670 | this.s etDaemon(t rue); | |
| 671 | this.s etName("Sc heduleQueu eTransferT hread"); | |
| 672 | } | |
| 673 | ||
| 674 | void n extSchedul edElementN otificatio n(Date dat e) | |
| 675 | { | |
| 676 | synchron ized(this) | |
| 677 | { | |
| 678 | logger.inf o("Transfe r thread b eing notif ied of new scheduled queue ele ments"); | |
| 679 | notifyAll( ); | |
| 680 | } | |
| 681 | } | |
| 682 | ||
| 683 | @Overr ide | |
| 684 | publ ic void ru n() | |
| 685 | { | |
| 686 | while(tr ue) | |
| 687 | { | |
| 688 | try | |
| 689 | { | |
| 690 | sy nchronized (this) | |
| 691 | { | |
| 692 | // i f there is nothing l eft in the queue, sc hedule the next exec ution for infinity | |
| 693 | if(a ccessibleD atePriorit yQueue.isE mpty()) | |
| 694 | { | |
| 695 | logger .info("Tra nsfer thre ad waiting forever f or new sch eduled que ue element s"); | |
| 696 | wait() ; | |
| 697 | } | |
| 698 | else | |
| 699 | { | |
| 700 | // if this is th e first ti me through the loop, assume we have to r un | |
| 701 | long t imeout = a ccessibleD atePriorit yQueue.pee k().getAcc essibility Date().get Time() - ( new Date() ).getTime( ); | |
| 702 | if( ti meout <= 0 L ) timeou t = 1L; // a wai t of 0L me ans wait f orever and we do not want that ! | |
| 703 | logger .info("Tra nsfer thre ad waiting (" + time out + ") f or existin g schedule d queue el ements"); | |
| 704 | ||
| 705 | wait(t imeout); | |
| 706 | } | |
| 707 | ||
| 708 | tran sferEligib leSchedule dElements( ); | |
| 709 | } | |
| 710 | } | |
| 711 | catch (Int erruptedEx ception e) | |
| 712 | { | |
| 713 | e. printStack Trace(); | |
| 714 | } | |
| 715 | } | |
| 716 | } | |
| 717 | } | |
| 718 | } | |
| 719 | ||
| 720 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.