1 21 package org.jacorb.collection; 22 23 24 25 import org.omg.CosCollection.*; 26 27 import org.jacorb.collection.util.*; 28 29 import java.util.*; 30 import org.omg.PortableServer.POA ; 31 32 import org.omg.PortableServer.Servant ; 33 34 import org.omg.CORBA.Any ; 35 36 import org.omg.CORBA.AnyHolder ; 37 38 39 40 class KeySortedCollectionImpl extends OrderedCollectionImpl implements KeySortedCollectionOperations { 41 42 protected SortedVector keys; 43 44 protected KeyNode test_key = new KeyNode(); 45 46 47 48 KeySortedCollectionImpl( OperationsOperations ops, POA poa, IteratorFactory iterator_factory ){ 49 50 super( ops, poa, iterator_factory ); 51 52 keys = new SortedVector( new KeyComparator( ops ) ); 53 54 }; 55 56 57 58 public org.omg.CORBA.TypeCode key_type(){ 59 60 return ops.key_type(); 61 62 }; 63 64 65 66 public synchronized boolean contains_element_with_key(Any key) throws KeyInvalid{ 67 68 check_key( key ); 69 70 test_key.key = key; 71 72 try { 73 74 if( keys.indexOf( test_key ) >= 0 ){ 75 76 return true; 77 78 } else { 79 80 return false; 81 82 } 83 84 } catch ( ObjectInvalid e ){ 85 86 throw new KeyInvalid(); 87 88 } 89 90 }; 91 92 93 94 public synchronized boolean contains_all_keys_from(KeyCollection collector) throws KeyInvalid{ 95 96 throw new org.omg.CORBA.NO_IMPLEMENT (); 97 98 }; 99 100 101 102 public synchronized boolean locate_or_add_element_with_key(Any element) throws ElementInvalid{ 103 104 check_element( element ); 105 106 test_key.key = ops.key( element ); 107 108 try { 109 110 if( keys.indexOf( test_key ) < 0 ){ 111 112 return add_element( element ); 113 114 }; 115 116 return false; 117 118 } catch ( ObjectInvalid e ){ 119 120 throw new ElementInvalid( ElementInvalidReason.element_type_invalid ); 121 122 } 123 124 }; 125 126 127 128 public synchronized boolean locate_or_add_element_with_key_set_iterator(Any element,org.omg.CosCollection. Iterator where) throws ElementInvalid,IteratorInvalid{ 129 130 check_element( element ); 131 132 test_key.key = ops.key( element ); 133 134 try { 135 136 if( keys.indexOf( test_key ) < 0 ){ 137 138 return add_element_set_iterator( element, where ); 139 140 }; 141 142 return false; 143 144 } catch ( ObjectInvalid e ){ 145 146 throw new ElementInvalid( ElementInvalidReason.element_type_invalid ); 147 148 } 149 150 }; 151 152 153 154 public synchronized boolean add_or_replace_element_with_key(Any element) throws ElementInvalid{ 155 156 check_element( element ); 157 158 test_key.key = ops.key( element ); 159 160 try { 161 162 int pos = keys.indexOf( test_key ); 163 164 pos = ((KeyNode)keys.elementAt( pos )).start_position; 165 166 if( pos < 0 ){ 167 168 return add_element( element ); 169 170 } else { 171 172 element_replace( pos, element ); 173 174 return false; 175 176 } 177 178 } catch ( ObjectInvalid e ) { 179 180 throw new ElementInvalid( ElementInvalidReason.element_type_invalid ); 181 182 } catch ( PositionInvalid e ) { 183 184 throw new ElementInvalid( ElementInvalidReason.positioning_property_invalid ); 185 186 } 187 188 }; 189 190 191 192 public synchronized boolean add_or_replace_element_with_key_set_iterator(Any element, org.omg.CosCollection.Iterator where) throws ElementInvalid,IteratorInvalid{ 193 194 check_element( element ); 195 196 test_key.key = ops.key( element ); 197 198 try { 199 200 int pos = keys.indexOf( test_key ); 201 202 pos = ((KeyNode)keys.elementAt( pos )).start_position; 203 204 if( pos < 0 ){ 205 206 return add_element_set_iterator( element, where ); 207 208 } else { 209 210 PositionalIteratorImpl i = check_iterator( where ); 211 212 element_replace( pos, element ); 213 214 i.set_pos(pos); 215 216 i.set_in_between( false ); 217 218 return false; 219 220 } 221 222 } catch ( ObjectInvalid e ){ 223 224 throw new ElementInvalid( ElementInvalidReason.element_type_invalid ); 225 226 } catch ( PositionInvalid e ) { 227 228 throw new ElementInvalid( ElementInvalidReason.positioning_property_invalid ); 229 230 } 231 232 }; 233 234 235 236 public synchronized boolean remove_element_with_key(Any key) throws KeyInvalid{ 237 238 check_key( key ); 239 240 test_key.key = key; 241 242 try { 243 244 int pos = keys.indexOf( test_key ); 245 246 if( pos < 0 ){ 247 248 return false; 249 250 } 251 252 pos = ((KeyNode)keys.elementAt( pos )).start_position; 253 254 element_remove( pos ); 255 256 return true; 257 258 } catch ( ObjectInvalid e ){ 259 260 throw new KeyInvalid(); 261 262 } catch ( EmptyCollection e ){ 263 264 throw new KeyInvalid(); 265 266 } catch ( PositionInvalid e ){ 267 268 throw new KeyInvalid(); 269 270 } 271 272 }; 273 274 275 276 public synchronized int remove_all_elements_with_key(Any key) throws KeyInvalid{ 277 278 check_key( key ); 279 280 test_key.key = key; 281 282 try { 283 284 int pos = keys.indexOf( test_key ); 285 286 if( pos < 0 ){ 287 288 return 0; 289 290 } 291 292 KeyNode node = (KeyNode)keys.elementAt( pos ); 293 294 pos = node.start_position; 295 296 for( int i=node.count; i>0; i-- ){ 297 298 element_remove( pos+i-1 ); 299 300 }; 301 302 return node.count; 303 304 } catch ( ObjectInvalid e ){ 305 306 throw new KeyInvalid(); 307 308 } catch ( EmptyCollection e ){ 309 310 throw new KeyInvalid(); 311 312 } catch ( PositionInvalid e ){ 313 314 throw new KeyInvalid(); 315 316 } 317 318 }; 319 320 321 322 public synchronized boolean replace_element_with_key(Any element) throws ElementInvalid{ 323 324 check_element( element ); 325 326 test_key.key = ops.key( element ); 327 328 try { 329 330 int pos = keys.indexOf( test_key ); 331 332 if( pos < 0 ){ 333 334 return false; 335 336 } 337 338 KeyNode node = (KeyNode)keys.elementAt( pos ); 339 340 pos = node.start_position; 341 342 element_replace( pos, element ); 343 344 return true; 345 346 } catch ( ObjectInvalid e ){ 347 348 throw new ElementInvalid( ElementInvalidReason.element_type_invalid ); 349 350 } catch ( PositionInvalid e ){ 351 352 throw new ElementInvalid( ElementInvalidReason.positioning_property_invalid ); 353 354 } 355 356 }; 357 358 359 360 public synchronized boolean replace_element_with_key_set_iterator(Any element, org.omg.CosCollection.Iterator where) throws ElementInvalid,IteratorInvalid{ 361 362 check_element( element ); 363 364 PositionalIteratorImpl i = check_iterator( where ); 365 366 test_key.key = ops.key( element ); 367 368 try { 369 370 int pos = keys.indexOf( test_key ); 371 372 if( pos < 0 ){ 373 374 return false; 375 376 } 377 378 KeyNode node = (KeyNode)keys.elementAt( pos ); 379 380 pos = node.start_position; 381 382 element_replace( pos, element ); 383 384 i.set_pos( pos ); 385 386 i.set_in_between( false ); 387 388 return true; 389 390 } catch ( ObjectInvalid e ){ 391 392 throw new ElementInvalid( ElementInvalidReason.element_type_invalid ); 393 394 } catch ( PositionInvalid e ){ 395 396 throw new ElementInvalid( ElementInvalidReason.positioning_property_invalid ); 397 398 } 399 400 }; 401 402 403 404 public synchronized boolean retrieve_element_with_key(Any key, AnyHolder element) throws KeyInvalid{ 405 406 check_key( key ); 407 408 test_key.key = key; 409 410 try { 411 412 int pos = keys.indexOf( test_key ); 413 414 if( pos < 0 ){ 415 416 return false; 417 418 } 419 420 KeyNode node = (KeyNode)keys.elementAt( pos ); 421 422 pos = node.start_position; 423 424 element.value = element_retrieve( pos ); 425 426 return true; 427 428 } catch ( ObjectInvalid e ){ 429 430 throw new KeyInvalid(); 431 432 } catch ( PositionInvalid e ){ 433 434 throw new KeyInvalid(); 435 436 } 437 438 }; 439 440 441 442 public synchronized void key(Any element, AnyHolder a_key) throws ElementInvalid{ 443 444 check_element( element ); 445 446 a_key.value = ops.key( element ); 447 448 }; 449 450 451 452 public synchronized void keys(Any [] elements, AnySequenceHolder a_keys) throws ElementInvalid{ 453 454 for( int i=0; i<elements.length; i++ ){ 455 456 check_element( elements[i] ); 457 458 } 459 460 a_keys.value = new Any [ elements.length ]; 461 462 for( int i=0; i<elements.length; i++ ){ 463 464 a_keys.value[i] = ops.key( elements[i] ); 465 466 } 467 468 }; 469 470 471 472 public synchronized boolean locate_element_with_key(Any key, org.omg.CosCollection.Iterator where) throws KeyInvalid,IteratorInvalid{ 473 474 check_key( key ); 475 476 PositionalIteratorImpl i = check_iterator( where ); 477 478 test_key.key = key; 479 480 try { 481 482 int pos = keys.indexOf( test_key ); 483 484 if( pos < 0 ){ 485 486 i.invalidate(); 487 488 return false; 489 490 } 491 492 KeyNode node = (KeyNode)keys.elementAt( pos ); 493 494 i.set_pos( node.start_position ); 495 496 i.set_in_between( false ); 497 498 return true; 499 500 } catch ( ObjectInvalid e ){ 501 502 throw new KeyInvalid(); 503 504 } 505 506 }; 507 508 509 510 public synchronized boolean locate_next_element_with_key(Any key, org.omg.CosCollection.Iterator where) throws KeyInvalid,IteratorInvalid{ 511 512 check_key( key ); 513 514 PositionalIteratorImpl i = check_iterator( where ); 515 516 i.check_invalid(); 517 518 int pos = i.get_pos(); 519 520 int new_pos = i.is_in_between()?pos:pos+1; 521 522 if( data.size() <= new_pos ){ 523 524 i.invalidate(); 525 526 } 527 528 test_key.key = key; 529 530 Any this_key = ops.key((Any )data.elementAt( new_pos )); 531 532 if( ops.key_equal( key, this_key ) ){ 533 534 i.set_pos( new_pos ); 535 536 i.set_in_between( false ); 537 538 return true; 539 540 } else { 541 542 i.invalidate(); 543 544 return false; 545 546 } 547 548 }; 549 550 551 552 public synchronized boolean locate_next_element_with_different_key(org.omg.CosCollection.Iterator where) throws IteratorInBetween,IteratorInvalid{ 553 554 PositionalIteratorImpl i = check_iterator( where ); 555 556 i.check_iterator(); 557 558 int pos = i.get_pos(); 559 560 Any key = ops.key((Any )data.elementAt( pos ) ); 561 562 test_key.key = key; 563 564 try { 565 566 int key_pos = keys.indexOf( test_key ); 567 568 if( key_pos == keys.size()-1 ){ 569 570 i.invalidate(); 571 572 return false; 573 574 } else { 575 576 KeyNode node = (KeyNode)keys.elementAt( key_pos+1 ); 577 578 i.set_pos( node.start_position ); 579 580 return true; 581 582 } 583 584 } catch ( ObjectInvalid e ){ 585 586 e.printStackTrace( System.out ); 587 588 throw new org.omg.CORBA.INTERNAL (); 589 590 } 591 592 }; 593 594 595 596 public synchronized int number_of_different_keys(){ 597 598 return keys.size(); 599 600 }; 601 602 603 604 public synchronized int number_of_elements_with_key(Any key) throws KeyInvalid{ 605 606 check_key( key ); 607 608 test_key.key = key; 609 610 try { 611 612 int pos = keys.indexOf( test_key ); 613 614 if( pos < 0 ){ 615 616 return 0; 617 618 }; 619 620 KeyNode node = (KeyNode)keys.elementAt( pos ); 621 622 return node.count; 623 624 } catch ( ObjectInvalid e ){ 625 626 throw new KeyInvalid(); 627 628 } 629 630 }; 631 632 633 634 636 637 638 public synchronized boolean locate_first_element_with_key(Any key, org.omg.CosCollection.Iterator where) throws KeyInvalid,IteratorInvalid{ 639 640 check_key( key ); 641 642 PositionalIteratorImpl i = check_iterator( where ); 643 644 try { 645 646 test_key.key = key; 647 648 int pos = keys.indexOf( test_key ); 649 650 if( pos >= 0 ){ 651 652 KeyNode node = (KeyNode)keys.elementAt( pos ); 653 654 i.set_pos( node.start_position ); 655 656 i.set_in_between( false ); 657 658 return true; 659 660 } else { 661 662 i.invalidate(); 663 664 return false; 665 666 } 667 668 } catch ( ObjectInvalid e ){ 669 670 throw new KeyInvalid(); 671 672 } 673 674 }; 675 676 677 678 public synchronized boolean locate_last_element_with_key(Any key, org.omg.CosCollection.Iterator where) throws KeyInvalid,IteratorInvalid{ 679 680 check_key( key ); 681 682 PositionalIteratorImpl i = check_iterator( where ); 683 684 try { 685 686 test_key.key = key; 687 688 int pos = keys.indexOf( test_key ); 689 690 if( pos >= 0 ){ 691 692 KeyNode node = (KeyNode)keys.elementAt( pos ); 693 694 i.set_pos( node.start_position + node.count - 1 ); 695 696 i.set_in_between( false ); 697 698 return true; 699 700 } else { 701 702 i.invalidate(); 703 704 return false; 705 706 } 707 708 } catch ( ObjectInvalid e ){ 709 710 throw new KeyInvalid(); 711 712 } 713 714 }; 715 716 717 718 public synchronized boolean locate_previous_element_with_key(Any key, org.omg.CosCollection.Iterator where) throws KeyInvalid,IteratorInvalid{ 719 720 check_key( key ); 721 722 PositionalIteratorImpl i = check_iterator( where ); 723 724 i.check_invalid(); 725 726 int pos = i.get_pos(); 727 728 if( pos == 0 ){ 729 730 i.invalidate(); 731 732 return false; 733 734 } 735 736 Any element = (Any )data.elementAt( pos-1 ); 737 738 if( ops.key_equal( key, ops.key( element ) ) ){ 739 740 i.set_pos( pos-1 ); 741 742 i.set_in_between( false ); 743 744 return true; 745 746 } 747 748 i.invalidate(); 749 750 return false; 751 752 }; 753 754 755 756 public synchronized boolean locate_previous_element_with_different_key(org.omg.CosCollection.Iterator where) throws IteratorInBetween,IteratorInvalid{ 757 758 PositionalIteratorImpl i = check_iterator( where ); 759 760 i.check_iterator(); 761 762 int pos = i.get_pos(); 763 764 Any key = ops.key( (Any )data.elementAt( pos ) ); 765 766 if( pos-- == 0 ){ 767 768 i.invalidate(); 769 770 return false; 771 772 } 773 774 while( pos >= 0 ) { 775 776 Any element = (Any )data.elementAt( pos ); 777 778 if( !ops.key_equal( key, ops.key( element ) ) ){ 779 780 i.set_pos( pos ); 781 782 i.set_in_between( false ); 783 784 return true; 785 786 } 787 788 pos--; 789 790 } 791 792 i.invalidate(); 793 794 return false; 795 796 }; 797 798 799 800 801 802 protected void element_inserted( int pos ) { 803 804 super.element_inserted( pos ); 805 806 Any key = ops.key( (Any )data.elementAt(pos) ); 807 808 key_inserted( key, pos ); 809 810 }; 811 812 813 814 protected void element_removed( int pos, Any old ){ 815 816 super.element_removed( pos, old ); 817 818 Any key = ops.key( old ); 819 820 key_removed( key ); 821 822 }; 823 824 825 826 protected void element_replaced( int pos, Any old ){ 827 828 super.element_replaced( pos, old ); 829 830 Any old_key = ops.key( old ); 831 832 Any new_key = ops.key( (Any )data.elementAt(pos) ); 833 834 if( !ops.equal( old_key, new_key ) ){ 835 836 key_removed( old_key ); 837 838 key_inserted( new_key, pos ); 839 840 } 841 842 }; 843 844 845 846 protected void check_key( Any key ) throws KeyInvalid { 847 848 if( !ops.check_key_type( key ) ){ 849 850 throw new KeyInvalid(); 851 852 } 853 854 }; 855 856 857 858 protected void key_inserted( Any key, int pos ){ 859 860 try { 861 862 test_key.key = key; 863 864 int key_pos = keys.indexOf( test_key ); 865 866 KeyNode node; 867 868 if( key_pos == -1 ){ 869 870 node = new KeyNode(); 871 872 node.key = key; 873 874 node.count = 0; 875 876 key_pos = keys.addElement( node ); 877 878 node.start_position = pos; 879 880 } else { 881 882 node = (KeyNode)keys.elementAt( key_pos ); 883 884 } 885 886 node.count++; 887 888 for( int i=key_pos+1; i<keys.size(); i++ ){ 889 890 node = (KeyNode)keys.elementAt( i ); 891 892 node.start_position++; 893 894 } 895 896 } catch ( ObjectInvalid e ){ 897 898 e.printStackTrace( System.out ); 899 900 throw new org.omg.CORBA.INTERNAL (); 901 902 } 903 904 }; 905 906 907 908 protected void key_removed( Any key ){ 909 910 try { 911 912 test_key.key = key; 913 914 int key_pos = keys.indexOf( test_key ); 915 916 KeyNode node = (KeyNode)keys.elementAt( key_pos ); 917 918 node.count--; 919 920 for( int i=key_pos+1; i<keys.size(); i++ ){ 921 922 node = (KeyNode)keys.elementAt( i ); 923 924 node.start_position--; 925 926 } 927 928 if( node.count == 0 ){ 929 930 keys.removeElementAt( key_pos ); 931 932 } 933 934 } catch ( ObjectInvalid e ){ 935 936 e.printStackTrace( System.out ); 937 938 throw new org.omg.CORBA.INTERNAL (); 939 940 } 941 942 }; 943 944 945 946 }; 947 948 949 950 951 952 953 954 955 956 957 958 959 960 | Popular Tags |