1 16 package org.apache.commons.collections; 17 18 import java.util.Collection ; 19 import java.util.Comparator ; 20 import java.util.ConcurrentModificationException ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Set ; 24 import java.util.SortedMap ; 25 import java.util.TreeMap ; 26 27 67 public class FastTreeMap extends TreeMap { 68 69 72 protected TreeMap map = null; 73 74 77 protected boolean fast = false; 78 79 80 83 86 public FastTreeMap() { 87 super(); 88 this.map = new TreeMap (); 89 } 90 91 96 public FastTreeMap(Comparator comparator) { 97 super(); 98 this.map = new TreeMap (comparator); 99 } 100 101 107 public FastTreeMap(Map map) { 108 super(); 109 this.map = new TreeMap (map); 110 } 111 112 118 public FastTreeMap(SortedMap map) { 119 super(); 120 this.map = new TreeMap (map); 121 } 122 123 124 127 132 public boolean getFast() { 133 return (this.fast); 134 } 135 136 141 public void setFast(boolean fast) { 142 this.fast = fast; 143 } 144 145 146 151 160 public Object get(Object key) { 161 if (fast) { 162 return (map.get(key)); 163 } else { 164 synchronized (map) { 165 return (map.get(key)); 166 } 167 } 168 } 169 170 175 public int size() { 176 if (fast) { 177 return (map.size()); 178 } else { 179 synchronized (map) { 180 return (map.size()); 181 } 182 } 183 } 184 185 190 public boolean isEmpty() { 191 if (fast) { 192 return (map.isEmpty()); 193 } else { 194 synchronized (map) { 195 return (map.isEmpty()); 196 } 197 } 198 } 199 200 207 public boolean containsKey(Object key) { 208 if (fast) { 209 return (map.containsKey(key)); 210 } else { 211 synchronized (map) { 212 return (map.containsKey(key)); 213 } 214 } 215 } 216 217 224 public boolean containsValue(Object value) { 225 if (fast) { 226 return (map.containsValue(value)); 227 } else { 228 synchronized (map) { 229 return (map.containsValue(value)); 230 } 231 } 232 } 233 234 240 public Comparator comparator() { 241 if (fast) { 242 return (map.comparator()); 243 } else { 244 synchronized (map) { 245 return (map.comparator()); 246 } 247 } 248 } 249 250 255 public Object firstKey() { 256 if (fast) { 257 return (map.firstKey()); 258 } else { 259 synchronized (map) { 260 return (map.firstKey()); 261 } 262 } 263 } 264 265 270 public Object lastKey() { 271 if (fast) { 272 return (map.lastKey()); 273 } else { 274 synchronized (map) { 275 return (map.lastKey()); 276 } 277 } 278 } 279 280 281 287 296 public Object put(Object key, Object value) { 297 if (fast) { 298 synchronized (this) { 299 TreeMap temp = (TreeMap ) map.clone(); 300 Object result = temp.put(key, value); 301 map = temp; 302 return (result); 303 } 304 } else { 305 synchronized (map) { 306 return (map.put(key, value)); 307 } 308 } 309 } 310 311 317 public void putAll(Map in) { 318 if (fast) { 319 synchronized (this) { 320 TreeMap temp = (TreeMap ) map.clone(); 321 temp.putAll(in); 322 map = temp; 323 } 324 } else { 325 synchronized (map) { 326 map.putAll(in); 327 } 328 } 329 } 330 331 338 public Object remove(Object key) { 339 if (fast) { 340 synchronized (this) { 341 TreeMap temp = (TreeMap ) map.clone(); 342 Object result = temp.remove(key); 343 map = temp; 344 return (result); 345 } 346 } else { 347 synchronized (map) { 348 return (map.remove(key)); 349 } 350 } 351 } 352 353 356 public void clear() { 357 if (fast) { 358 synchronized (this) { 359 map = new TreeMap (); 360 } 361 } else { 362 synchronized (map) { 363 map.clear(); 364 } 365 } 366 } 367 368 369 372 381 public boolean equals(Object o) { 382 if (o == this) { 384 return (true); 385 } else if (!(o instanceof Map )) { 386 return (false); 387 } 388 Map mo = (Map ) o; 389 390 if (fast) { 392 if (mo.size() != map.size()) { 393 return (false); 394 } 395 Iterator i = map.entrySet().iterator(); 396 while (i.hasNext()) { 397 Map.Entry e = (Map.Entry ) i.next(); 398 Object key = e.getKey(); 399 Object value = e.getValue(); 400 if (value == null) { 401 if (!(mo.get(key) == null && mo.containsKey(key))) { 402 return (false); 403 } 404 } else { 405 if (!value.equals(mo.get(key))) { 406 return (false); 407 } 408 } 409 } 410 return (true); 411 } else { 412 synchronized (map) { 413 if (mo.size() != map.size()) { 414 return (false); 415 } 416 Iterator i = map.entrySet().iterator(); 417 while (i.hasNext()) { 418 Map.Entry e = (Map.Entry ) i.next(); 419 Object key = e.getKey(); 420 Object value = e.getValue(); 421 if (value == null) { 422 if (!(mo.get(key) == null && mo.containsKey(key))) { 423 return (false); 424 } 425 } else { 426 if (!value.equals(mo.get(key))) { 427 return (false); 428 } 429 } 430 } 431 return (true); 432 } 433 } 434 } 435 436 443 public int hashCode() { 444 if (fast) { 445 int h = 0; 446 Iterator i = map.entrySet().iterator(); 447 while (i.hasNext()) { 448 h += i.next().hashCode(); 449 } 450 return (h); 451 } else { 452 synchronized (map) { 453 int h = 0; 454 Iterator i = map.entrySet().iterator(); 455 while (i.hasNext()) { 456 h += i.next().hashCode(); 457 } 458 return (h); 459 } 460 } 461 } 462 463 469 public Object clone() { 470 FastTreeMap results = null; 471 if (fast) { 472 results = new FastTreeMap(map); 473 } else { 474 synchronized (map) { 475 results = new FastTreeMap(map); 476 } 477 } 478 results.setFast(getFast()); 479 return (results); 480 } 481 482 483 486 493 public SortedMap headMap(Object key) { 494 if (fast) { 495 return (map.headMap(key)); 496 } else { 497 synchronized (map) { 498 return (map.headMap(key)); 499 } 500 } 501 } 502 503 511 public SortedMap subMap(Object fromKey, Object toKey) { 512 if (fast) { 513 return (map.subMap(fromKey, toKey)); 514 } else { 515 synchronized (map) { 516 return (map.subMap(fromKey, toKey)); 517 } 518 } 519 } 520 521 528 public SortedMap tailMap(Object key) { 529 if (fast) { 530 return (map.tailMap(key)); 531 } else { 532 synchronized (map) { 533 return (map.tailMap(key)); 534 } 535 } 536 } 537 538 539 542 546 public Set entrySet() { 547 return new EntrySet(); 548 } 549 550 553 public Set keySet() { 554 return new KeySet(); 555 } 556 557 560 public Collection values() { 561 return new Values(); 562 } 563 564 567 570 private abstract class CollectionView implements Collection { 571 572 public CollectionView() { 573 } 574 575 protected abstract Collection get(Map map); 576 protected abstract Object iteratorNext(Map.Entry entry); 577 578 579 public void clear() { 580 if (fast) { 581 synchronized (FastTreeMap.this) { 582 map = new TreeMap (); 583 } 584 } else { 585 synchronized (map) { 586 get(map).clear(); 587 } 588 } 589 } 590 591 public boolean remove(Object o) { 592 if (fast) { 593 synchronized (FastTreeMap.this) { 594 TreeMap temp = (TreeMap ) map.clone(); 595 boolean r = get(temp).remove(o); 596 map = temp; 597 return r; 598 } 599 } else { 600 synchronized (map) { 601 return get(map).remove(o); 602 } 603 } 604 } 605 606 public boolean removeAll(Collection o) { 607 if (fast) { 608 synchronized (FastTreeMap.this) { 609 TreeMap temp = (TreeMap ) map.clone(); 610 boolean r = get(temp).removeAll(o); 611 map = temp; 612 return r; 613 } 614 } else { 615 synchronized (map) { 616 return get(map).removeAll(o); 617 } 618 } 619 } 620 621 public boolean retainAll(Collection o) { 622 if (fast) { 623 synchronized (FastTreeMap.this) { 624 TreeMap temp = (TreeMap ) map.clone(); 625 boolean r = get(temp).retainAll(o); 626 map = temp; 627 return r; 628 } 629 } else { 630 synchronized (map) { 631 return get(map).retainAll(o); 632 } 633 } 634 } 635 636 public int size() { 637 if (fast) { 638 return get(map).size(); 639 } else { 640 synchronized (map) { 641 return get(map).size(); 642 } 643 } 644 } 645 646 647 public boolean isEmpty() { 648 if (fast) { 649 return get(map).isEmpty(); 650 } else { 651 synchronized (map) { 652 return get(map).isEmpty(); 653 } 654 } 655 } 656 657 public boolean contains(Object o) { 658 if (fast) { 659 return get(map).contains(o); 660 } else { 661 synchronized (map) { 662 return get(map).contains(o); 663 } 664 } 665 } 666 667 public boolean containsAll(Collection o) { 668 if (fast) { 669 return get(map).containsAll(o); 670 } else { 671 synchronized (map) { 672 return get(map).containsAll(o); 673 } 674 } 675 } 676 677 public Object [] toArray(Object [] o) { 678 if (fast) { 679 return get(map).toArray(o); 680 } else { 681 synchronized (map) { 682 return get(map).toArray(o); 683 } 684 } 685 } 686 687 public Object [] toArray() { 688 if (fast) { 689 return get(map).toArray(); 690 } else { 691 synchronized (map) { 692 return get(map).toArray(); 693 } 694 } 695 } 696 697 698 public boolean equals(Object o) { 699 if (o == this) return true; 700 if (fast) { 701 return get(map).equals(o); 702 } else { 703 synchronized (map) { 704 return get(map).equals(o); 705 } 706 } 707 } 708 709 public int hashCode() { 710 if (fast) { 711 return get(map).hashCode(); 712 } else { 713 synchronized (map) { 714 return get(map).hashCode(); 715 } 716 } 717 } 718 719 public boolean add(Object o) { 720 throw new UnsupportedOperationException (); 721 } 722 723 public boolean addAll(Collection c) { 724 throw new UnsupportedOperationException (); 725 } 726 727 public Iterator iterator() { 728 return new CollectionViewIterator(); 729 } 730 731 private class CollectionViewIterator implements Iterator { 732 733 private Map expected; 734 private Map.Entry lastReturned = null; 735 private Iterator iterator; 736 737 public CollectionViewIterator() { 738 this.expected = map; 739 this.iterator = expected.entrySet().iterator(); 740 } 741 742 public boolean hasNext() { 743 if (expected != map) { 744 throw new ConcurrentModificationException (); 745 } 746 return iterator.hasNext(); 747 } 748 749 public Object next() { 750 if (expected != map) { 751 throw new ConcurrentModificationException (); 752 } 753 lastReturned = (Map.Entry )iterator.next(); 754 return iteratorNext(lastReturned); 755 } 756 757 public void remove() { 758 if (lastReturned == null) { 759 throw new IllegalStateException (); 760 } 761 if (fast) { 762 synchronized (FastTreeMap.this) { 763 if (expected != map) { 764 throw new ConcurrentModificationException (); 765 } 766 FastTreeMap.this.remove(lastReturned.getKey()); 767 lastReturned = null; 768 expected = map; 769 } 770 } else { 771 iterator.remove(); 772 lastReturned = null; 773 } 774 } 775 } 776 } 777 778 781 private class KeySet extends CollectionView implements Set { 782 783 protected Collection get(Map map) { 784 return map.keySet(); 785 } 786 787 protected Object iteratorNext(Map.Entry entry) { 788 return entry.getKey(); 789 } 790 791 } 792 793 796 private class Values extends CollectionView { 797 798 protected Collection get(Map map) { 799 return map.values(); 800 } 801 802 protected Object iteratorNext(Map.Entry entry) { 803 return entry.getValue(); 804 } 805 } 806 807 810 private class EntrySet extends CollectionView implements Set { 811 812 protected Collection get(Map map) { 813 return map.entrySet(); 814 } 815 816 817 protected Object iteratorNext(Map.Entry entry) { 818 return entry; 819 } 820 821 } 822 823 } 824 | Popular Tags |