1 24 25 package org.objectweb.dream.queue; 26 27 import java.util.Enumeration ; 28 29 import org.objectweb.dream.AbstractComponent; 30 import org.objectweb.dream.message.manager.MessageManager; 31 import org.objectweb.dream.pool.ObjectPool; 32 import org.objectweb.dream.pool.Recyclable; 33 import org.objectweb.fractal.api.NoSuchInterfaceException; 34 import org.objectweb.fractal.api.control.IllegalBindingException; 35 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 36 37 46 public class ListAddRemoveFirstLastFastImpl extends AbstractComponent 47 implements 48 List, 49 ListAddFirstLast, 50 ListRemoveFirstLast 51 { 52 53 private int numInList; 54 private Element first; 55 private Element last; 56 57 58 protected ObjectPool objectPoolItf; 59 60 64 67 public ListAddRemoveFirstLastFastImpl() 68 { 69 numInList = 0; 70 first = null; 71 last = null; 72 } 73 74 78 81 public void add(Object o) 82 { 83 addLast(o); 84 85 } 86 87 90 public Object remove() 91 { 92 return removeFirst(); 93 } 94 95 98 public boolean isEmpty() 99 { 100 return numInList == 0; 101 } 102 103 107 110 public void addLast(Object o) 111 { 112 Element lle = (Element) objectPoolItf.newInstance(); 113 lle.obj = o; 114 115 if (first == null) 116 { 117 first = lle; 118 last = lle; 119 } 120 else 121 { 122 last.next = lle; 123 lle.prev = last; 124 last = lle; 125 } 126 numInList++; 127 } 128 129 132 public void addFirst(Object o) 133 { 134 Element lle = (Element) objectPoolItf.newInstance(); 135 lle.obj = o; 136 137 if (first == null) 138 { 139 first = lle; 140 last = lle; 141 } 142 else 143 { 144 first.prev = lle; 145 lle.next = first; 146 first = lle; 147 } 148 numInList++; 149 } 150 151 155 158 public Object getLast() 159 { 160 if (last == null) 161 { 162 return null; 163 } 164 return last.obj; 165 } 166 167 170 public Object removeLast() 171 { 172 Element retme = null; 173 Object retobj = null; 174 175 if (last == null) 176 { 177 return null; 178 } 179 retme = last; 180 181 if (first == last) 182 { 183 first = null; 184 last = null; 185 } 186 else 187 { 188 last = last.prev; 189 last.next = null; 190 } 191 192 numInList--; 193 retobj = retme.obj; 194 objectPoolItf.recycleInstance(retme); 195 return retobj; 196 } 197 198 201 public Object getFirst() 202 { 203 if (first == null) 204 { 205 return null; 206 } 207 return first.obj; 208 } 209 210 213 public Object removeFirst() 214 { 215 Element retme = null; 216 Object retobj = null; 217 218 if (first == null) 219 return null; 220 retme = first; 221 222 if (first == last) 223 { 224 first = null; 225 last = null; 226 } 227 else 228 { 229 first = first.next; 230 first.prev = null; 231 } 232 233 numInList--; 234 retobj = retme.obj; 235 objectPoolItf.recycleInstance(retme); 236 return retobj; 237 } 238 239 243 250 public Object getItem(Object known) 251 { 252 Element tmp; 253 254 tmp = first; 255 while (tmp != null) 256 { 257 if (known.equals(tmp.obj)) 258 { 259 return tmp.obj; 260 } 261 tmp = tmp.next; 262 } 263 return null; 264 } 265 266 273 public Object removeItem(Object known) 274 { 275 Element tmp; 276 277 tmp = first; 278 while (tmp != null) 279 { 280 if (known.equals(tmp.obj)) 281 { 282 283 if (tmp == first) 285 { 286 return this.removeFirst(); 287 } 288 else if (tmp == last) 289 { 290 return this.removeLast(); 291 } 292 else 293 { 294 Object retobj; 296 297 (tmp.prev).next = tmp.next; 298 (tmp.next).prev = tmp.prev; 299 numInList--; 300 retobj = tmp.obj; 301 objectPoolItf.recycleInstance(tmp); 302 return retobj; 303 } 304 } 305 tmp = tmp.next; 306 } 307 return null; 308 } 309 310 318 public Enumeration elements() 319 { 320 return new FastLinkedListEnumeration(); 321 } 322 323 327 330 public String toString() 331 { 332 return toString(""); 333 } 334 335 private String toString(String prefix) 336 { 337 String pre = prefix; 338 if (pre == null) 339 pre = ""; 340 341 String ret = pre + "ListAddRemoveFirstLastFastImpl: length=" + numInList 342 + "\n"; 343 344 Element current = first; 345 while (current != null) 346 { 347 if (current.obj == null) 348 { 349 ret += pre + " (null)\n"; 350 } 351 352 else 353 { 354 if (current.obj instanceof ListAddRemoveFirstLastFastImpl) 355 { 356 ret += pre 357 + "LINKED LIST\n" 358 + ((ListAddRemoveFirstLastFastImpl) current.obj).toString(pre 359 + " "); 360 } 361 else 362 { 363 ret += pre + " " + current.obj.toString() + "\n"; 364 } 365 } 366 367 current = current.next; 368 } 369 370 return ret; 371 } 372 373 377 382 public static class Element implements Recyclable 383 { 384 Object obj; 385 Element prev; 386 Element next; 387 388 391 public void recycle() 392 { 393 } 395 396 } 397 398 402 408 public class FastLinkedListEnumeration implements Enumeration 409 { 410 private Element curElement; 411 412 415 public FastLinkedListEnumeration() 416 { 417 curElement = ListAddRemoveFirstLastFastImpl.this.first; 418 } 419 420 423 public boolean hasMoreElements() 424 { 425 if (curElement == null) 426 return false; 427 return true; 428 } 429 430 433 public Object nextElement() throws java.util.NoSuchElementException 434 { 435 Object retme; 436 437 if (curElement == null) 438 throw new java.util.NoSuchElementException (); 439 retme = curElement.obj; 440 curElement = curElement.next; 441 return retme; 442 } 443 } 444 445 449 453 public synchronized void bindFc(String clientItfName, Object serverItf) 454 throws NoSuchInterfaceException, IllegalBindingException, 455 IllegalLifeCycleException 456 { 457 super.bindFc(clientItfName, serverItf); 458 if (clientItfName.equals(ObjectPool.ITF_NAME)) 459 { 460 objectPoolItf = (ObjectPool) serverItf; 461 } 462 } 463 464 467 public String [] listFc() 468 { 469 return new String []{MessageManager.ITF_NAME, ObjectPool.ITF_NAME}; 470 } 471 472 private static void printTime(long long1, long long3, int int5) 474 { 475 long long6 = long3 - long1; 476 double double8 = (double) int5 / (double) long6; 477 double double10 = double8 * 1000.0; 478 479 System.out.println(int5 + " iterations in " + long6 + " milliseconds = " 480 + double10 + " iterations per second"); 481 } 482 483 } 484 485 | Popular Tags |