1 9 package javolution.util; 10 11 import j2me.util.Collection; 12 import j2me.util.Iterator; 13 import j2me.util.List; 14 import j2me.util.ListIterator; 15 import j2me.util.Set; 16 import j2me.io.Serializable; 17 import j2me.lang.UnsupportedOperationException; 18 import j2mex.realtime.MemoryArea; 19 20 import javolution.Javolution; 21 import javolution.context.Realtime; 22 import javolution.context.RealtimeObject; 23 import javolution.lang.Reusable; 24 import javolution.text.Text; 25 import javolution.xml.XMLBinding; 26 import javolution.xml.XMLFormat; 27 import javolution.xml.stream.XMLStreamException; 28 29 62 public abstract class FastCollectionextends RealtimeObject implements 63 Collection, Reusable, Serializable { 64 65 69 protected static final XMLFormat XML = new XMLFormat( 70 Javolution.j2meGetClass("javolution.util.FastCollection")) { 71 72 public void read(InputElement xml, Object obj) throws XMLStreamException { 73 FastCollection fc = (FastCollection) obj; 74 while (xml.hasNext()) { 75 fc.add(xml.getNext()); 76 } 77 } 78 79 public void write(Object obj, OutputElement xml) throws XMLStreamException { 80 FastCollection fc = (FastCollection) obj; 81 for (Record r=fc.head(), end=fc.tail(); (r=r.getNext())!=end;) { 82 xml.add(fc.valueOf(r)); 83 } 84 } 85 }; 86 87 91 private Unmodifiable _unmodifiable; 92 93 96 protected FastCollection() { 97 } 98 99 104 public abstract int size(); 105 106 112 public abstract Record head(); 113 114 120 public abstract Record tail(); 121 122 128 public abstract Object valueOf(Record record); 129 130 142 public abstract void delete(Record record); 143 144 154 public Collectionunmodifiable() { 155 if (_unmodifiable == null) { 156 MemoryArea.getMemoryArea(this).executeInArea(new Runnable () { 157 public void run() { 158 _unmodifiable = new Unmodifiable(); 159 } 160 }); 161 } 162 return _unmodifiable; 163 } 164 165 172 public Iteratoriterator() { 173 return FastIterator.valueOf(this); 174 } 175 176 183 public FastComparator getValueComparator() { 184 return FastComparator.DEFAULT; 185 } 186 187 199 public boolean add(Object value) { 200 throw new UnsupportedOperationException (); 201 } 202 203 212 public boolean remove(Object value) { 213 final FastComparator valueComp = this.getValueComparator(); 214 for (Record r = head(), end = tail(); (r = r.getNext()) != end;) { 215 if (valueComp.areEqual(value, valueOf(r))) { 216 delete(r); 217 return true; 218 } 219 } 220 return false; 221 } 222 223 228 public void clear() { 229 for (Record head = head(), r = tail().getPrevious(); r != head; r = r 231 .getPrevious()) { 232 delete(r); 233 } 234 } 235 236 242 public final boolean isEmpty() { 243 return size() == 0; 244 } 245 246 254 public boolean contains(Object value) { 255 final FastComparator valueComp = this.getValueComparator(); 256 for (Record r = head(), end = tail(); (r = r.getNext()) != end;) { 257 if (valueComp.areEqual(value, valueOf(r))) 258 return true; 259 } 260 return false; 261 } 262 263 273 public boolean addAll(Collectionc) { 274 if (c instanceof FastCollection) 275 return addAll((FastCollection) c); 276 boolean modified = false; 277 Iteratoritr = c.iterator(); 278 int pos = c.size(); 279 while (--pos >= 0) { 280 if (add(itr.next())) { 281 modified = true; 282 } 283 } 284 return modified; 285 } 286 287 private boolean addAll(FastCollectionc) { 288 if (c instanceof FastTable) 289 return addAll((FastTable) c); 290 boolean modified = false; 291 for (Record r = c.head(), end = c.tail(); (r = r.getNext()) != end;) { 292 if (this.add(c.valueOf(r))) { 293 modified = true; 294 } 295 } 296 return modified; 297 } 298 299 private boolean addAll(FastTablec) { 300 boolean modified = false; 301 for (int i=0, n = c.size(); i < n;) { if (this.add(c.get(i++))) { 303 modified = true; 304 } 305 } 306 return modified; 307 } 308 309 317 public boolean containsAll(Collectionc) { 318 if (c instanceof FastCollection) 319 return containsAll((FastCollection) c); 320 Iteratoritr = c.iterator(); 321 int pos = c.size(); 322 while (--pos >= 0) { 323 if (!contains(itr.next())) { 324 return false; 325 } 326 } 327 return true; 328 } 329 330 private boolean containsAll(FastCollectionc) { 331 for (Record r = c.head(), end = c.tail(); (r = r.getNext()) != end;) { 332 if (!contains(c.valueOf(r))) { 333 return false; 334 } 335 } 336 return true; 337 } 338 339 348 public boolean removeAll(Collectionc) { 349 boolean modified = false; 350 for (Record head = head(), r = tail().getPrevious(), previous; r != head; r = previous) { 352 previous = r.getPrevious(); if (c.contains(valueOf(r))) { 354 delete(r); 355 modified = true; 356 } 357 } 358 return modified; 359 } 360 361 369 public boolean retainAll(Collectionc) { 370 boolean modified = false; 371 for (Record head = head(), r = tail().getPrevious(), previous; r != head; r = previous) { 373 previous = r.getPrevious(); if (!c.contains(valueOf(r))) { 375 delete(r); 376 modified = true; 377 } 378 } 379 return modified; 380 } 381 382 389 public Object [] toArray() { 390 return toArray(new Object [size()]); 391 } 392 393 407 public Object [] toArray(Object [] array) { 408 int size = size(); 409 if (array.length < size) 410 throw new UnsupportedOperationException ( 411 "Destination array too small"); 412 if (array.length > size) { 413 array[size] = null; } 415 int i = 0; 416 Object [] arrayView = array; 417 for (Record r = head(), end = tail(); (r = r.getNext()) != end;) { 418 arrayView[i++] = valueOf(r); 419 } 420 return array; 421 } 422 423 428 public Text toText() { 429 final Text sep = Text.valueOf(", "); 430 Text text = Text.valueOf('['); 431 for (Record r = head(), end = tail(); (r = r.getNext()) != end;) { 432 text = text.concat(Text.valueOf(valueOf(r))); 433 if (r.getNext() != end) { 434 text = text.concat(sep); 435 } 436 } 437 return text.concat(Text.valueOf(']')); 438 } 439 440 450 public boolean equals(Object obj) { 451 if (this instanceof List) 452 return equalsList(obj); 453 return obj == this 454 || (obj instanceof Collection 455 && ((Collection) obj).size() == size() && containsAll((Collection) obj)); 456 } 457 458 private boolean equalsList(Object obj) { 459 if (obj == this) 460 return true; 461 if (!(obj instanceof List)) 462 return false; 463 if (obj instanceof FastCollection) 464 return equalsList((FastCollection)obj); 465 List that = (List) obj; 466 if (this.size() != that.size()) 467 return false; 468 Iterator thatIterator = that.iterator(); 469 final FastComparator comp = this.getValueComparator(); 470 for (Record r = head(), end = tail(); (r = r.getNext()) != end;) { 471 Object o1 = valueOf(r); 472 Object o2 = thatIterator.next(); 473 if (!comp.areEqual(o1, o2)) 474 return false; 475 } 476 return true; 477 } 478 479 private boolean equalsList(FastCollection that) { 480 if (this.size() != that.size()) 481 return false; 482 Record t = that.head(); 483 final FastComparator comp = this.getValueComparator(); 484 for (Record r = head(), end = tail(); (r = r.getNext()) != end;) { 485 Object o1 = valueOf(r); 486 Object o2 = that.valueOf(t = t.getNext()); 487 if (!comp.areEqual(o1, o2)) 488 return false; 489 } 490 return true; 491 } 492 493 499 public int hashCode() { 500 if (this instanceof List) 501 return hashCodeList(); 502 final FastComparator valueComp = this.getValueComparator(); 503 int hash = 0; 504 for (Record r = head(), end = tail(); (r = r.getNext()) != end;) { 505 hash += valueComp.hashCodeOf(valueOf(r)); 506 } 507 return hash; 508 } 509 510 private int hashCodeList() { 511 final FastComparator comp = this.getValueComparator(); 512 int h = 1; 513 for (Record r = head(), end = tail(); (r = r.getNext()) != end;) { 514 h = 31 * h + comp.hashCodeOf(valueOf(r)); 515 } 516 return h; 517 518 } 519 520 public void reset() { 522 clear(); 523 } 524 525 public boolean move(ObjectSpace os) { 527 if (super.move(os)) { 528 for (Record r = head(), end = tail(); (r = r.getNext()) != end;) { 529 Object obj = valueOf(r); 530 if (obj instanceof Realtime) { 531 ((Realtime)obj).move(os); 532 } 533 } 534 return true; 535 } 536 return false; 537 } 538 539 543 public interface Record { 544 545 550 public Record getPrevious(); 551 552 557 public Record getNext(); 558 559 } 560 561 564 private final class Unmodifiable extends FastCollection implements 565 Set, List { 567 public int size() { 569 return FastCollection.this.size(); 570 } 571 572 public Record head() { 574 return FastCollection.this.head(); 575 } 576 577 public Record tail() { 579 return FastCollection.this.tail(); 580 } 581 582 public Object valueOf(Record record) { 584 return FastCollection.this.valueOf(record); 585 } 586 587 public boolean contains(Object value) { 589 return (FastCollection.this).contains(value); 590 } 591 592 public boolean containsAll(Collection c) { 594 return (FastCollection.this).containsAll(c); 595 } 596 597 public FastComparator getValueComparator() { 599 return FastCollection.this.getValueComparator(); 600 } 601 602 public boolean add(Object obj) { 604 throw new UnsupportedOperationException ("Unmodifiable"); 605 } 606 607 public void delete(Record node) { 609 throw new UnsupportedOperationException ("Unmodifiable"); 610 } 611 612 616 public boolean addAll(int index, Collection c) { 617 throw new UnsupportedOperationException ("Unmodifiable"); 618 } 619 620 public Object get(int index) { 621 return ((List)FastCollection.this).get(index); 622 } 623 624 public Object set(int index, Object element) { 625 throw new UnsupportedOperationException ("Unmodifiable"); 626 } 627 628 public void add(int index, Object element) { 629 throw new UnsupportedOperationException ("Unmodifiable"); 630 } 631 632 public Object remove(int index) { 633 throw new UnsupportedOperationException ("Unmodifiable"); 634 } 635 636 public int indexOf(Object o) { 637 return ((List)FastCollection.this).indexOf(o); 638 } 639 640 public int lastIndexOf(Object o) { 641 return ((List)FastCollection.this).lastIndexOf(o); 642 } 643 644 public ListIterator listIterator() { 645 throw new UnsupportedOperationException ( 646 "List iterator not supported for unmodifiable collection"); 647 } 648 649 public ListIterator listIterator(int index) { 650 throw new UnsupportedOperationException ( 651 "List iterator not supported for unmodifiable collection"); 652 } 653 654 public List subList(int fromIndex, int toIndex) { 655 throw new UnsupportedOperationException ( 656 "Sub-List not supported for unmodifiable collection"); 657 } 658 } 659 } | Popular Tags |