1 23 24 27 28 package com.sun.jdo.spi.persistence.support.sqlstore.sco; 29 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 import java.util.Iterator ; 33 import java.util.ResourceBundle ; 34 35 import com.sun.jdo.api.persistence.support.JDOUserException; 36 import com.sun.jdo.spi.persistence.support.sqlstore.PersistenceCapable; 37 import com.sun.jdo.spi.persistence.support.sqlstore.StateManager; 38 import com.sun.jdo.spi.persistence.support.sqlstore.SCOCollection; 39 import com.sun.jdo.spi.persistence.utility.I18NHelper; 40 import com.sun.jdo.spi.persistence.support.sqlstore.PersistenceManager; 41 42 43 49 public class HashSet 50 extends java.util.HashSet 51 implements SCOCollection 52 { 53 54 private transient PersistenceCapable owner; 55 56 private transient String fieldName; 57 58 private transient Class elementType; 59 60 private transient boolean allowNulls; 61 62 private transient java.util.HashSet added = new java.util.HashSet (); 63 64 private transient java.util.HashSet removed = new java.util.HashSet (); 65 66 private transient boolean isDeferred; 67 68 71 private final static ResourceBundle messages = I18NHelper.loadBundle( 72 "com.sun.jdo.spi.persistence.support.sqlstore.impl.Bundle", HashSet.class.getClassLoader()); 74 75 private final static ResourceBundle messages1 = I18NHelper.loadBundle( 76 "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", HashSet.class.getClassLoader()); 78 79 80 89 public HashSet(Object owner, String fieldName, Class elementType, boolean allowNulls) 90 { 91 super(); 92 if (owner instanceof PersistenceCapable) 93 { 94 this.owner = (PersistenceCapable)owner; 95 this.fieldName = fieldName; 96 } 97 this.elementType = elementType; 98 this.allowNulls = allowNulls; 99 } 100 101 114 public HashSet(Object owner, String fieldName, 115 Class elementType, boolean allowNulls, 116 int initialCapacity) 117 { 118 super(initialCapacity); 119 if (owner instanceof PersistenceCapable) 120 { 121 this.owner = (PersistenceCapable)owner; 122 this.fieldName = fieldName; 123 } 124 this.elementType = elementType; 125 this.allowNulls = allowNulls; 126 } 127 128 130 139 public boolean add(Object o) 140 { 141 if (allowNulls == false && o == null) 142 { 143 throw new JDOUserException(I18NHelper.getMessage(messages, 144 "sco.nulls_not_allowed")); } 146 147 if (elementType != null && !elementType.isAssignableFrom(o.getClass())) 148 { 149 throw new JDOUserException(I18NHelper.getMessage(messages, 150 "sco.classcastexception", elementType.getName()), new ClassCastException (), new Object [] {o}); 152 } 153 154 if (owner != null) 155 { 156 StateManager stateManager = owner.jdoGetStateManager(); 157 158 if (stateManager != null) 159 { 160 PersistenceManager pm = (PersistenceManager) stateManager.getPersistenceManagerInternal(); 161 162 pm.acquireShareLock(); 163 164 boolean modified = false; 165 166 try 167 { 168 pm.acquireFieldUpdateLock(); 169 try 170 { 171 stateManager.makeDirty(fieldName); 173 174 modified = super.add(o); 175 176 if (modified) 177 { 178 if (removed.remove(o) == false) 179 { 180 added.add(o); 181 } 182 stateManager.applyUpdates(fieldName, this); 183 } 184 return modified; 185 } 186 finally 187 { 188 pm.releaseFieldUpdateLock(); 189 } 190 191 } 192 catch (JDOUserException e) 193 { 194 Object [] failedObjects = e.getFailedObjectArray(); 195 196 if (modified && (failedObjects != null)) 197 { 198 for (int i = 0; i < failedObjects.length; i++) 204 { 205 Object failedObject = failedObjects[i]; 206 207 if (failedObject == o) 208 { 209 super.remove(failedObject); 210 break; 211 } 212 } 213 } 214 215 throw e; 216 } 217 finally 218 { 219 pm.releaseShareLock(); 220 } 221 } 222 } 223 224 return super.add(o); 225 } 226 227 239 public boolean addAll(Collection c) 240 { 241 if (allowNulls == false && c.contains(null)) 242 { 243 throw new JDOUserException(I18NHelper.getMessage(messages, 244 "sco.nulls_not_allowed")); } 246 247 ArrayList errc = new ArrayList(); 248 249 if (elementType != null) 250 { 251 Iterator i = c.iterator(); 253 while (i.hasNext()) 254 { 255 Object o = i.next(); 256 if (!elementType.isAssignableFrom(o.getClass())) 257 errc.add(o); 258 } 259 } 260 261 if (errc != null && errc.size() > 0) 262 { 263 throw new JDOUserException(I18NHelper.getMessage(messages, 264 "sco.classcastexception", elementType.getName()), new ClassCastException (), errc.toArray()); 266 } 267 268 boolean modified = false; 269 270 if (owner != null) 271 { 272 StateManager stateManager = owner.jdoGetStateManager(); 273 274 if (stateManager != null) 275 { 276 PersistenceManager pm = (PersistenceManager) stateManager.getPersistenceManagerInternal(); 277 278 pm.acquireShareLock(); 279 280 try 281 { 282 pm.acquireFieldUpdateLock(); 283 try 284 { 285 stateManager.makeDirty(fieldName); 287 288 for (Iterator iter = c.iterator(); iter.hasNext();) 289 { 290 Object o = iter.next(); 291 if (!super.contains(o)) 292 { 293 if (removed.remove(o) == false) 294 { 295 added.add(o); 296 } 297 298 super.add(o); 299 modified = true; 300 } 301 } 302 303 if (modified) 305 { 306 stateManager.applyUpdates(fieldName, this); 307 } 308 return modified; 309 } 310 finally 311 { 312 pm.releaseFieldUpdateLock(); 313 } 314 } 315 catch (JDOUserException e) 316 { 317 Object [] failedObjects = e.getFailedObjectArray(); 318 319 if (modified && (failedObjects != null)) 320 { 321 for (int i = 0; i < failedObjects.length; i++) 322 { 323 super.remove(failedObjects[i]); 324 } 325 } 326 327 throw e; 328 } 329 finally 330 { 331 pm.releaseShareLock(); 332 } 333 } 334 } 335 336 return super.addAll(c); 337 } 338 339 346 public boolean remove(Object o) 347 { 348 350 if (owner != null) 351 { 352 StateManager stateManager = owner.jdoGetStateManager(); 353 354 if (stateManager != null) 355 { 356 PersistenceManager pm = (PersistenceManager) stateManager.getPersistenceManagerInternal(); 357 358 pm.acquireShareLock(); 359 360 try 361 { 362 pm.acquireFieldUpdateLock(); 363 try 364 { 365 stateManager.makeDirty(fieldName); 366 367 boolean modified = super.remove(o); 368 369 if (modified) 370 { 371 if (added.remove(o) == false) 372 { 373 removed.add(o); 374 } 375 376 stateManager.applyUpdates(fieldName, this); 377 } 378 379 return modified; 380 } 381 finally 382 { 383 pm.releaseFieldUpdateLock(); 384 } 385 } 386 finally 387 { 388 pm.releaseShareLock(); 389 } 390 } 391 } 392 393 return super.remove(o); 394 } 395 396 397 412 public boolean removeAll(Collection c) 413 { 414 416 if (owner != null) 417 { 418 StateManager stateManager = owner.jdoGetStateManager(); 419 420 if (stateManager != null) 421 { 422 PersistenceManager pm = (PersistenceManager) stateManager.getPersistenceManagerInternal(); 423 424 pm.acquireShareLock(); 425 426 try 427 { 428 pm.acquireFieldUpdateLock(); 429 try 430 { 431 stateManager.makeDirty(fieldName); 432 433 for (Iterator iter = c.iterator(); iter.hasNext();) 434 { 435 Object o = iter.next(); 436 if (super.contains(o)) 437 { 438 if (added.remove(o) == false) 439 { 440 removed.add(o); 441 } 442 } 443 } 444 445 boolean modified = super.removeAll(c); 446 447 if (modified) 449 { 450 stateManager.applyUpdates(fieldName, this); 451 } 452 453 return modified; 454 } 455 finally 456 { 457 pm.releaseFieldUpdateLock(); 458 } 459 } 460 finally 461 { 462 pm.releaseShareLock(); 463 } 464 } 465 } 466 467 return super.removeAll(c); 468 } 469 470 483 public boolean retainAll(Collection c) 484 { 485 if (owner != null) 486 { 487 StateManager stateManager = owner.jdoGetStateManager(); 488 489 if (stateManager != null) 490 { 491 PersistenceManager pm = (PersistenceManager) stateManager.getPersistenceManagerInternal(); 492 493 pm.acquireShareLock(); 494 495 try 496 { 497 pm.acquireFieldUpdateLock(); 498 try 499 { 500 stateManager.makeDirty(fieldName); 502 503 for (Iterator iter = super.iterator(); iter.hasNext();) 504 { 505 Object o = iter.next(); 506 if (!c.contains(o)) 507 { 508 if (added.remove(o) == false) 509 { 510 removed.add(o); 511 } 512 } 513 } 514 515 boolean modified = super.retainAll(c); 516 517 519 if (modified) 520 { 521 stateManager.applyUpdates(fieldName, this); 522 } 523 524 return modified; 525 } 526 finally 527 { 528 pm.releaseFieldUpdateLock(); 529 } 530 } 531 finally 532 { 533 pm.releaseShareLock(); 534 } 535 } 536 } 537 538 return super.retainAll(c); 539 } 540 541 545 public void clear() 546 { 547 if (owner != null) 548 { 549 StateManager stateManager = owner.jdoGetStateManager(); 550 551 if (stateManager != null) 552 { 553 PersistenceManager pm = (PersistenceManager) stateManager.getPersistenceManagerInternal(); 554 555 pm.acquireShareLock(); 556 557 try 558 { 559 pm.acquireFieldUpdateLock(); 560 try 561 { 562 stateManager.makeDirty(fieldName); 564 565 removed.clear(); 566 added.clear(); 567 568 for (Iterator iter = super.iterator(); iter.hasNext();) 569 { 570 removed.add(iter.next()); 571 } 572 573 super.clear(); 574 575 stateManager.applyUpdates(fieldName, this); 577 return; 578 } 579 finally 580 { 581 pm.releaseFieldUpdateLock(); 582 } 583 } 584 finally 585 { 586 pm.releaseShareLock(); 587 } 588 } 589 } 590 591 super.clear(); 592 } 593 594 602 public Object clone() 603 { 604 HashSet obj = (HashSet) super.clone(); 605 606 obj.unsetOwner(); 610 611 return obj; 612 } 613 614 621 public Iterator iterator() { 622 return new SCOHashIterator(super.iterator(), this); 623 } 624 625 private class SCOHashIterator implements Iterator { 626 Iterator _iterator = null; 627 HashSet _caller = null; 628 Object lastReturned = null; 629 630 SCOHashIterator(Iterator it, HashSet cl) { 631 _iterator = it; 632 _caller = cl; 633 } 634 635 public boolean hasNext() { 636 return _iterator.hasNext(); 637 } 638 639 public Object next() { 640 lastReturned = _iterator.next(); 641 return lastReturned; 642 } 643 644 public void remove() { 645 if (lastReturned == null) 647 throw new IllegalStateException (); 648 649 if (_caller.owner != null) { 650 StateManager stateManager = _caller.owner.jdoGetStateManager(); 652 653 if (stateManager != null) { 654 PersistenceManager pm = (PersistenceManager) stateManager.getPersistenceManagerInternal(); 655 656 pm.acquireShareLock(); 657 658 try { 659 pm.acquireFieldUpdateLock(); 660 try 661 { 662 stateManager.makeDirty(_caller.fieldName); 663 664 _iterator.remove(); 665 666 if (added.remove(lastReturned) == false) { 667 removed.add(lastReturned); 668 } 669 670 stateManager.applyUpdates(_caller.fieldName, _caller); 671 672 } finally { 673 pm.releaseFieldUpdateLock(); 674 } 675 } 676 finally 677 { 678 pm.releaseShareLock(); 679 } 680 } 681 } else { 682 _iterator.remove(); 684 } 685 lastReturned = null; 686 } 687 } 688 689 694 698 public Object cloneInternal() 699 { 700 return super.clone(); 701 } 702 703 706 public void reset() 707 { 708 if (added != null) 710 added.clear(); 711 712 if (removed != null) 713 removed.clear(); 714 } 715 716 719 public void markDeferred() 720 { 721 isDeferred = true; 722 } 723 724 727 public boolean isDeferred() 728 { 729 return isDeferred; 730 } 731 732 737 public void applyDeferredUpdates(Collection c) 738 { 739 if (!isDeferred) 740 { 741 return; 743 } 744 745 isDeferred = false; 746 addAllInternal(c); 747 748 addAllInternal(added); 749 removeAllInternal(removed); 750 added.clear(); 751 removed.clear(); 752 } 753 754 758 public void addInternal(Object o) 759 { 760 if (isDeferred) 761 { 762 if (removed.remove(o) == false) 763 { 764 added.add(o); 765 } 766 } 767 else 768 { 769 super.add(o); 770 } 771 } 772 773 774 778 public void addAllInternal(Collection c) 779 { 780 if (c == null) 781 { 782 return; 783 } 784 785 Iterator iter = c.iterator(); 786 787 while (iter.hasNext()) 788 { 789 addInternal(iter.next()); 790 } 791 } 792 793 796 public void addToBaseCollection(Object o) 797 { 798 super.add(o); 799 } 800 801 805 public void removeAllInternal(Collection c) 806 { 807 if (c == null) 808 { 809 return; 810 } 811 812 Iterator iter = c.iterator(); 813 814 while (iter.hasNext()) 815 { 816 removeInternal(iter.next()); 817 } 818 } 819 820 825 public Collection getAdded() 826 { 827 return (Collection )added; 828 } 829 830 835 public Collection getRemoved() 836 { 837 return (Collection )removed; 838 } 839 840 841 844 public void clearInternal() 845 { 846 super.clear(); 847 this.reset(); 848 } 849 850 853 public void removeInternal(Object o) 854 { 855 if (isDeferred) 856 { 857 if (added.remove(o) == false) 858 { 859 removed.add(o); 860 } 861 862 } 863 else 864 { 865 super.remove(o); 866 } 867 } 868 869 872 public void unsetOwner() 873 { 874 this.owner = null; 875 this.fieldName = null; 876 this.elementType = null; 877 added.clear(); 878 removed.clear(); 879 } 880 881 886 public Object getOwner() 887 { 888 return this.owner; 889 } 890 891 896 public String getFieldName() 897 { 898 return this.fieldName; 899 } 900 901 904 public StateManager makeDirty() 905 { 906 StateManager stateManager = owner.jdoGetStateManager(); 907 908 if (stateManager != null) 909 { 910 stateManager.makeDirty(fieldName); 911 } 912 913 return stateManager; 914 } 915 916 917 920 public void applyUpdates(StateManager sm, boolean modified) 921 { 922 if (modified && sm != null) 923 { 924 sm.applyUpdates(fieldName, this); 925 } 926 } 927 928 936 public void setOwner(Object owner, String fieldName, Class elementType) { 937 938 if (this.owner != null) { 939 throw new JDOUserException(I18NHelper.getMessage( 940 messages1, "core.statemanager.anotherowner"), new Object []{this.owner, this.fieldName}); 942 } 943 if (owner instanceof PersistenceCapable) { 944 this.owner = (PersistenceCapable)owner; 945 this.fieldName = fieldName; 946 this.elementType = elementType; 947 } 948 } 949 } 950 951 952 953 954 | Popular Tags |