1 21 package oracle.toplink.essentials.internal.helper; 23 24 import java.util.*; 25 import oracle.toplink.essentials.exceptions.ValidationException; 26 27 39 public class ThreadCursoredList extends Vector { 40 41 42 protected boolean isComplete; 43 44 45 protected RuntimeException exception; 46 47 51 public ThreadCursoredList() { 52 this(10); 53 } 54 55 59 public ThreadCursoredList(int initialCapacity) { 60 this(initialCapacity, 0); 61 } 62 63 67 public ThreadCursoredList(int initialCapacity, int capacityIncrement) { 68 super(0); 69 this.isComplete = false; 70 } 71 72 75 public synchronized void add(int index, Object element) { 76 super.add(index, element); 77 this.notifyAll(); 78 } 79 80 83 public synchronized boolean add(Object element) { 84 boolean result = super.add(element); 85 notifyAll(); 86 return result; 87 } 88 89 92 public synchronized boolean addAll(int index, Collection collection) { 93 boolean result = super.addAll(index, collection); 94 notifyAll(); 95 return result; 96 } 97 98 101 public synchronized boolean addAll(Collection collection) { 102 boolean result = super.addAll(collection); 103 notifyAll(); 104 return result; 105 } 106 107 110 public synchronized void addElement(Object object) { 111 super.addElement(object); 112 notifyAll(); 113 } 114 115 118 public synchronized void clear() { 119 waitUntilComplete(); 120 super.clear(); 121 } 122 123 126 public synchronized Object clone() { 127 waitUntilComplete(); 128 return super.clone(); 129 } 130 131 134 public boolean hasException() { 135 return getException() != null; 136 } 137 138 141 public RuntimeException getException() { 142 return exception; 143 } 144 145 150 public synchronized void throwException(RuntimeException exception) { 151 this.exception = exception; 152 setIsComplete(true); 153 } 154 155 159 public synchronized boolean isComplete() { 160 if (hasException()) { 161 RuntimeException thrownException = this.exception; 163 this.exception = null; 164 throw thrownException; 165 } 166 return isComplete; 167 } 168 169 172 public synchronized void setIsComplete(boolean isComplete) { 173 this.isComplete = isComplete; 174 notifyAll(); 175 } 176 177 180 public synchronized void waitUntilComplete() { 181 while (!isComplete()) { 182 try { 183 wait(); 184 } catch (InterruptedException ignore) { 185 } 186 } 187 } 188 189 192 public synchronized void waitUntilAdd() { 193 try { 194 wait(); 195 } catch (InterruptedException ignore) { 196 } 197 } 198 199 202 public synchronized boolean contains(Object element) { 203 boolean result = super.contains(element); 204 if ((result != true) && (!isComplete())) { 205 waitUntilComplete(); 206 result = super.contains(element); 207 } 208 return result; 209 } 210 211 214 public synchronized boolean containsAll(Collection collection) { 215 boolean result = super.containsAll(collection); 216 if ((result != true) && (!isComplete())) { 217 waitUntilComplete(); 218 result = super.containsAll(collection); 219 } 220 return result; 221 } 222 223 226 public synchronized void copyInto(Object [] array) { 227 waitUntilComplete(); 228 super.copyInto(array); 229 } 230 231 234 public synchronized Object elementAt(int index) { 235 Object result = super.elementAt(index); 236 if ((result == null) && (!isComplete())) { 237 waitUntilComplete(); 238 result = super.elementAt(index); 239 } 240 return result; 241 } 242 243 protected int getSize() { 244 return super.size(); 245 } 246 247 250 public Enumeration elements() { 251 return new Enumeration() { 252 int count = 0; 253 254 public boolean hasMoreElements() { 255 synchronized (ThreadCursoredList.this) { 256 boolean result = count < ThreadCursoredList.this.getSize(); 257 while ((!result) && (!isComplete())) { 258 waitUntilAdd(); 259 result = count < ThreadCursoredList.this.getSize(); 260 } 261 return result; 262 } 263 } 264 265 public Object nextElement() { 266 synchronized (ThreadCursoredList.this) { 267 boolean result = count < ThreadCursoredList.this.getSize(); 268 while ((!result) && (!isComplete())) { 269 waitUntilAdd(); 270 result = count < ThreadCursoredList.this.getSize(); 271 } 272 if (result) { 273 return get(count++); 274 } 275 } 276 throw new NoSuchElementException("Vector Enumeration"); 277 } 278 }; 279 } 280 281 284 public synchronized boolean equals(Object object) { 285 waitUntilComplete(); 286 return super.equals(object); 287 } 288 289 292 public synchronized Object firstElement() { 293 while ((!isComplete()) && (super.size() < 1)) { 294 waitUntilAdd(); 295 } 296 return super.firstElement(); 297 } 298 299 302 public synchronized Object get(int index) { 303 while ((!isComplete()) && (super.size() < index)) { 304 waitUntilAdd(); 305 } 306 return super.get(index); 307 } 308 309 312 public synchronized int hashCode() { 313 waitUntilComplete(); 314 return super.hashCode(); 315 } 316 317 320 public int indexOf(Object element) { 321 int result = super.indexOf(element); 322 if ((result == -1) && (!isComplete())) { 323 waitUntilComplete(); 324 result = super.indexOf(element); 325 } 326 return result; 327 } 328 329 332 public synchronized int indexOf(Object element, int index) { 333 int result = super.indexOf(element, index); 334 if ((result == -1) && (!isComplete())) { 335 waitUntilComplete(); 336 result = super.indexOf(element, index); 337 } 338 return result; 339 } 340 341 344 public synchronized void insertElementAt(Object element, int index) { 345 super.insertElementAt(element, index); 346 notify(); 347 } 348 349 352 public boolean isEmpty() { 353 boolean result = super.isEmpty(); 354 if (result && (!isComplete())) { 355 waitUntilAdd(); 356 result = super.isEmpty(); 357 } 358 return result; 359 } 360 361 364 public Iterator iterator() { 365 throw ValidationException.operationNotSupported("iterator"); 366 } 367 368 371 public synchronized Object lastElement() { 372 waitUntilComplete(); 373 return super.lastElement(); 374 } 375 376 379 public int lastIndexOf(Object element) { 380 waitUntilComplete(); 381 return super.lastIndexOf(element); 382 } 383 384 387 public synchronized int lastIndexOf(Object element, int index) { 388 waitUntilComplete(); 389 return super.lastIndexOf(element, index); 390 } 391 392 395 public ListIterator listIterator() { 396 throw ValidationException.operationNotSupported("iterator"); 397 } 398 399 402 public ListIterator listIterator(final int index) { 403 throw ValidationException.operationNotSupported("iterator"); 404 } 405 406 409 public synchronized Object remove(int index) { 410 while ((!isComplete()) && (super.size() < index)) { 411 waitUntilAdd(); 412 } 413 return super.remove(index); 414 } 415 416 419 public boolean remove(Object element) { 420 boolean result = super.remove(element); 421 if ((!result) && (!isComplete())) { 422 waitUntilAdd(); 423 result = super.remove(element); 424 } 425 return result; 426 } 427 428 431 public synchronized boolean removeAll(Collection collection) { 432 waitUntilComplete(); 433 return super.removeAll(collection); 434 } 435 436 439 public synchronized void removeAllElements() { 440 waitUntilComplete(); 441 super.removeAllElements(); 442 } 443 444 447 public synchronized boolean removeElement(Object element) { 448 boolean result = super.removeElement(element); 449 if ((!result) && (!isComplete())) { 450 waitUntilAdd(); 451 result = super.removeElement(element); 452 } 453 return result; 454 } 455 456 459 public synchronized void removeElementAt(int index) { 460 while ((!isComplete()) && (super.size() < index)) { 461 waitUntilAdd(); 462 } 463 super.removeElementAt(index); 464 } 465 466 469 public synchronized boolean retainAll(Collection collection) { 470 waitUntilComplete(); 471 return super.retainAll(collection); 472 } 473 474 477 public synchronized Object set(int index, Object element) { 478 while ((!isComplete()) && (super.size() < index)) { 479 waitUntilAdd(); 480 } 481 return super.set(index, element); 482 } 483 484 487 public synchronized void setElementAt(Object element, int index) { 488 while ((!isComplete()) && (super.size() < index)) { 489 waitUntilAdd(); 490 } 491 super.setElementAt(element, index); 492 } 493 494 497 public int size() { 498 waitUntilComplete(); 499 return super.size(); 500 } 501 502 505 public List subList(int fromIndex, int toIndex) { 506 while ((!isComplete()) && (super.size() < toIndex)) { 507 waitUntilAdd(); 508 } 509 return super.subList(fromIndex, toIndex); 510 } 511 512 515 public synchronized Object [] toArray() { 516 waitUntilComplete(); 517 return super.toArray(); 518 } 519 520 523 public synchronized Object [] toArray(Object [] array) { 524 waitUntilComplete(); 525 return super.toArray(array); 526 } 527 528 531 public synchronized String toString() { 532 waitUntilComplete(); 533 return super.toString(); 534 } 535 536 539 public synchronized void trimToSize() { 540 waitUntilComplete(); 541 super.trimToSize(); 542 } 543 } 544 | Popular Tags |