1 package org.jacorb.collection; 2 3 4 5 42 43 44 45 import org.omg.CosCollection.*; 46 47 import org.omg.PortableServer.Servant ; 48 49 import org.omg.CORBA.Any ; 50 51 import org.omg.CORBA.AnyHolder ; 52 53 import org.jacorb.collection.util.*; 54 55 import java.util.*; 56 57 58 class PositionalIteratorImpl 59 60 implements org.omg.CosCollection.IteratorOperations 61 62 { 63 64 private Servant srvnt = null; 65 66 protected int pos = -1; 67 68 protected boolean in_between = false; 69 70 protected boolean read_only = true; 71 72 protected CollectionImpl collection; 73 74 75 76 PositionalIteratorImpl( CollectionImpl collection ){ 77 78 this.collection = collection; 79 80 }; 81 82 83 84 PositionalIteratorImpl( CollectionImpl collection, boolean read_only ){ 85 86 this.collection = collection; 87 88 this.read_only = read_only; 89 90 }; 91 92 93 94 public boolean set_to_first_element() { 95 96 synchronized( collection ){ 97 98 check_servant(); 99 100 if( collection.is_empty() ) { 101 102 invalidate(); 103 104 } else { 105 106 set_pos( 0 ); 107 108 }; 109 110 }; 111 112 return get_pos() == 0; 113 114 }; 115 116 117 118 public boolean set_to_next_element() throws IteratorInvalid { 119 120 return set_to_next_nth_element( 1 ); 121 122 }; 123 124 125 126 public boolean set_to_next_nth_element(int n) throws IteratorInvalid { 127 128 synchronized( collection ) { 129 130 check_invalid(); 131 132 int new_pos = get_pos()+n; 133 134 if( is_in_between() ){ 135 136 new_pos = get_pos()+n-1; 137 138 } 139 140 if( collection.number_of_elements() > new_pos && new_pos >= 0 ){ 141 142 set_pos( new_pos ); 143 144 } else { 145 146 invalidate(); 147 148 } 149 150 in_between = false; 151 152 return exist_next(); 153 154 } 155 156 }; 157 158 159 160 public boolean retrieve_element(AnyHolder element) throws IteratorInvalid,IteratorInBetween{ 161 162 synchronized( collection ){ 163 164 check_iterator(); 165 166 try { 167 168 element.value = collection.element_retrieve( get_pos() ); 169 170 return true; 171 172 } catch ( PositionInvalid e ){ 173 174 set_pos( -1 ); 175 176 throw new IteratorInvalid( IteratorInvalidReason.is_invalid ); 177 178 } 179 180 } 181 182 }; 183 184 185 186 public boolean retrieve_element_set_to_next(AnyHolder element, org.omg.CORBA.BooleanHolder more) throws IteratorInvalid,IteratorInBetween{ 187 188 synchronized( collection ){ 189 190 boolean rc = retrieve_element( element ); 191 192 set_to_next_element(); 193 194 more.value = exist_next(); 195 196 return rc; 197 198 } 199 200 }; 201 202 203 204 public boolean retrieve_next_n_elements(int n, AnySequenceHolder result, org.omg.CORBA.BooleanHolder more) throws IteratorInvalid,IteratorInBetween{ 205 206 Vector v = new Vector( n ); 207 208 int i = 0; 209 210 synchronized( collection ){ 211 212 check_iterator(); 213 214 boolean b = true; 215 216 AnyHolder a = new AnyHolder (); 217 218 more.value = true; 219 220 for ( i=0; (i<n || n==0) && get_pos() != -1; i++ ){ 221 222 try { 223 224 retrieve_element_set_to_next( a, more ); 225 226 v.addElement(a.value); 227 228 } catch ( IteratorInvalid e ){ 229 230 more.value = false; 231 232 break; 233 234 } 235 236 } 237 238 } 239 240 Any [] anies = new Any [v.size()]; 241 242 v.copyInto( (java.lang.Object [])anies ); 243 244 result.value = anies; 245 246 return i>0; 247 248 }; 249 250 251 252 public boolean not_equal_retrieve_element_set_to_next(org.omg.CosCollection.Iterator test, AnyHolder element) throws IteratorInvalid,IteratorInBetween{ 253 254 synchronized( collection ){ 255 256 check_iterator(); 257 258 if( is_equal( test ) ){ 259 260 retrieve_element( element ); 261 262 return false; 263 264 } else { 265 266 retrieve_element( element ); 267 268 set_to_next_element(); 269 270 return true; 271 272 } 273 274 } 275 276 } 277 278 279 280 public void remove_element() throws IteratorInvalid,IteratorInBetween{ 281 282 synchronized( collection ){ 283 284 check_iterator(); 285 286 check_read_only(); 287 288 try { 289 290 collection.element_remove( get_pos() ); 291 292 } catch ( PositionInvalid e ){ 293 294 invalidate(); 295 296 throw new IteratorInvalid( IteratorInvalidReason.is_invalid ); 297 298 } catch ( EmptyCollection e ){ 299 300 invalidate(); 301 302 throw new IteratorInvalid( IteratorInvalidReason.is_invalid ); 303 304 }; 305 306 }; 307 308 }; 309 310 311 312 public boolean remove_element_set_to_next() throws IteratorInvalid,IteratorInBetween{ 313 314 synchronized( collection ) { 315 316 remove_element(); 317 318 return set_to_next_element(); 319 320 } 321 322 }; 323 324 325 326 public boolean remove_next_n_elements(int n, org.omg.CORBA.IntHolder actual_number) throws IteratorInvalid,IteratorInBetween{ 327 328 synchronized( collection ){ 329 330 int count = 0; 331 332 for( int i=0; ( i<n || n==0 ) && get_pos() != -1; i++, count++ ){ 333 334 remove_element_set_to_next(); 335 336 } 337 338 actual_number.value = count; 339 340 return exist_next(); 341 342 } 343 344 } 345 346 347 348 public boolean not_equal_remove_element_set_to_next(org.omg.CosCollection.Iterator test) 349 350 throws IteratorInvalid,IteratorInBetween{ 351 352 synchronized( collection ){ 353 354 check_iterator(); 355 356 if( is_equal( test ) ){ 357 358 remove_element(); 359 360 return false; 361 362 } else { 363 364 remove_element_set_to_next(); 365 366 return true; 367 368 } 369 370 } 371 372 }; 373 374 375 376 public void replace_element(Any element) throws IteratorInvalid,IteratorInBetween,ElementInvalid{ 377 378 synchronized( collection ){ 379 380 check_iterator(); 381 382 check_read_only(); 383 384 try { 385 386 collection.element_replace( get_pos(), element ); 387 388 } catch ( PositionInvalid e ){ 389 390 invalidate(); 391 392 throw new IteratorInvalid( IteratorInvalidReason.is_invalid ); 393 394 } 395 396 } 397 398 }; 399 400 401 402 public boolean replace_element_set_to_next(Any element) throws IteratorInvalid,IteratorInBetween,ElementInvalid{ 403 404 synchronized( collection ){ 405 406 replace_element( element ); 407 408 return set_to_next_element(); 409 410 } 411 412 }; 413 414 415 416 public boolean replace_next_n_elements(Any [] elements, org.omg.CORBA.IntHolder actual_number) throws IteratorInvalid,IteratorInBetween,ElementInvalid{ 417 418 synchronized( collection ){ 419 420 actual_number.value = 0; 421 422 for( int i=0; i<elements.length && is_valid() ; i++, actual_number.value++){ 423 424 replace_element_set_to_next( elements[i] ); 425 426 } 427 428 return exist_next(); 429 430 } 431 432 }; 433 434 435 436 public boolean not_equal_replace_element_set_to_next(org.omg.CosCollection.Iterator test, Any element) throws IteratorInvalid,IteratorInBetween,ElementInvalid{ 437 438 synchronized( collection ){ 439 440 check_iterator(); 441 442 if( is_equal( test ) ){ 443 444 replace_element( element ); 445 446 return false; 447 448 } else { 449 450 replace_element_set_to_next( element ); 451 452 return true; 453 454 } 455 456 } 457 458 } 459 460 461 462 public boolean add_element_set_iterator(Any element) throws ElementInvalid{ 463 464 synchronized( collection ){ 465 466 pos = collection.element_add( element ); 467 468 set_pos( pos ); 469 470 in_between = false; 471 472 return true; 473 474 } 475 476 }; 477 478 479 480 public boolean add_n_elements_set_iterator(Any [] elements, org.omg.CORBA.IntHolder actual_number) throws ElementInvalid{ 481 482 synchronized( collection ){ 483 484 actual_number.value = 0; 485 486 int pos[] = new int[ elements.length ]; 487 488 for( int i=0; i<elements.length; i++ ){ 489 490 collection.check_element( elements[i] ); 491 492 } 493 494 for( int i=0; i<elements.length; i++, actual_number.value++ ){ 495 496 add_element_set_iterator( elements[i] ); 497 498 } 499 500 return true; 501 502 } 503 504 }; 505 506 507 508 public synchronized void invalidate(){ 509 510 pos = -1; 511 512 }; 513 514 515 516 public synchronized boolean is_valid(){ 517 518 return pos != -1; 519 520 }; 521 522 523 524 public synchronized boolean is_in_between(){ 525 526 return in_between; 527 528 }; 529 530 531 532 public synchronized boolean is_for(org.omg.CosCollection.Collection collector){ 533 534 return collection.is_this_you( collector ); 535 536 }; 537 538 539 540 public synchronized boolean is_const(){ 541 542 return read_only; 543 544 }; 545 546 547 548 public boolean is_equal(org.omg.CosCollection.Iterator test) throws IteratorInvalid{ 549 550 synchronized( collection ){ 551 552 PositionalIteratorImpl iter = collection.check_iterator( test ); 553 554 if( !is_valid() ){ 555 556 throw new IteratorInvalid( IteratorInvalidReason.is_invalid ); 557 558 } 559 560 return get_pos() == iter.get_pos(); 561 562 } 563 564 } 565 566 567 568 public org.omg.CosCollection.Iterator _clone(){ 569 570 synchronized( collection ) { 571 572 org.omg.CosCollection.Iterator iter = collection.create_iterator( read_only ); 573 574 try { 575 576 PositionalIteratorImpl i = collection.check_iterator( iter ); 577 578 i.set_pos( get_pos() ); 579 580 return iter; 581 582 } catch ( IteratorInvalid e ){ 583 584 e.printStackTrace( System.out ); 585 586 throw new org.omg.CORBA.INTERNAL (); 587 588 } 589 590 } 591 592 } 593 594 595 596 public void assign(org.omg.CosCollection.Iterator from_where) throws IteratorInvalid{ 597 598 synchronized( collection ) { 599 600 PositionalIteratorImpl i = collection.check_iterator( from_where ); 601 602 i.set_pos( get_pos() ); 603 604 i.set_in_between( is_in_between() ); 605 606 }; 607 608 }; 609 610 611 612 public synchronized void destroy(){ 613 614 synchronized( collection ) { 615 616 check_servant(); 617 618 collection.destroy_me( this ); 619 620 } 621 622 }; 623 624 625 626 public synchronized int get_pos(){ 627 628 check_servant(); 629 630 return pos; 631 632 }; 633 634 635 636 public synchronized void set_pos( int pos ){ 637 638 check_servant(); 639 640 this.pos = pos; 641 642 }; 643 644 645 646 public synchronized void set_in_between( boolean in_between ){ 647 648 check_servant(); 649 650 this.in_between = in_between; 651 652 }; 653 654 655 656 public synchronized void set_servant( Servant srvnt ){ 657 658 if( srvnt != null ){ 659 660 System.out.println("Error: Servant setted before!"); 661 662 throw new org.omg.CORBA.INTERNAL (); 663 664 } 665 666 }; 667 668 669 670 public synchronized Servant get_servant(){ 671 672 if( srvnt != null ){ 673 674 System.out.println("Error: Servant must be setted before!"); 675 676 throw new org.omg.CORBA.INTERNAL (); 677 678 } 679 680 return srvnt; 681 682 }; 683 684 685 686 protected void check_servant() { 687 688 if( srvnt == null ){ 689 690 System.out.println("Error: Servant must be setted before!"); 691 692 throw new org.omg.CORBA.INTERNAL (); 693 694 } 695 696 } 697 698 699 700 protected void check_invalid() throws IteratorInvalid { 701 702 check_servant(); 703 704 if( pos == -1 ){ 705 706 throw new IteratorInvalid( IteratorInvalidReason.is_invalid ); 707 708 } 709 710 } 711 712 713 714 protected void check_in_between() throws IteratorInBetween { 715 716 if( in_between ){ 717 718 throw new IteratorInBetween(); 719 720 } 721 722 } 723 724 725 726 protected void check_iterator() throws IteratorInBetween, IteratorInvalid { 727 728 check_invalid(); 729 730 check_in_between(); 731 732 }; 733 734 735 736 protected void check_read_only() throws IteratorInvalid { 737 738 if( read_only ){ 739 740 throw new IteratorInvalid( IteratorInvalidReason.is_const ); 741 742 } 743 744 } 745 746 747 748 protected synchronized boolean exist_next(){ 749 750 return is_valid() && collection.number_of_elements()-1 > get_pos(); 751 752 }; 753 754 }; 755 756 757 758 759 760 761 762 763 764 765 766 767 768 | Popular Tags |