1 23 24 27 28 package com.sun.jdo.spi.persistence.support.sqlstore.sco; 29 30 import java.util.Collection ; 31 import java.util.Iterator ; 32 import java.util.ResourceBundle ; 33 34 import com.sun.jdo.api.persistence.support.JDOUserException; 35 import com.sun.jdo.api.persistence.support.JDOUnsupportedOptionException; 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 41 42 48 public class Vector 49 extends java.util.Vector 50 implements SCOCollection 51 { 52 53 private transient PersistenceCapable owner; 54 55 private transient String fieldName; 56 57 private transient Class elementType; 58 59 private transient boolean allowNulls; 60 61 private transient java.util.Vector added = new java.util.Vector (); 62 63 private transient java.util.Vector removed = new java.util.Vector (); 64 65 68 private final static ResourceBundle messages = I18NHelper.loadBundle( 69 "com.sun.jdo.spi.persistence.support.sqlstore.impl.Bundle", Vector.class.getClassLoader()); 71 72 private final static ResourceBundle messages1 = I18NHelper.loadBundle( 73 "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", Vector.class.getClassLoader()); 75 76 85 public Vector(Object owner, String fieldName, Class elementType, boolean allowNulls) 86 { 87 super(); 88 if (owner instanceof PersistenceCapable) 89 { 90 this.owner = (PersistenceCapable)owner; 91 this.fieldName = fieldName; 92 } 93 this.elementType = elementType; 94 this.allowNulls = allowNulls; 95 } 96 97 109 public Vector(Object owner, String fieldName, 110 Class elementType, boolean allowNulls, int initialCapacity) 111 { 112 super(initialCapacity); 113 if (owner instanceof PersistenceCapable) 114 { 115 this.owner = (PersistenceCapable)owner; 116 this.fieldName = fieldName; 117 } 118 this.elementType = elementType; 119 this.allowNulls = allowNulls; 120 } 121 122 123 124 134 public synchronized void setElementAt(Object obj, int index) { 135 136 throwUnsupportedOption(); 137 138 if (obj == null) 139 { 140 if (allowNulls == false) 141 { 142 throw new JDOUserException(I18NHelper.getMessage(messages, 143 "sco.nulls_not_allowed")); } 145 this.removeElementAt(index); 147 } 148 149 if (elementType == null || elementType.isAssignableFrom(obj.getClass())) 150 { 151 StateManager stateManager = this.makeDirty(); 153 154 Object o = super.elementAt(index); 155 super.setElementAt(obj, index); 156 157 if (added.remove(o) == false) 158 removed.add(o); 159 160 if (removed.remove(obj) == false) 161 added.add(obj); 162 163 this.applyUpdates(stateManager, true); 165 166 } else { 167 throw new JDOUserException(I18NHelper.getMessage(messages, 168 "sco.classcastexception", elementType.getName()), new ClassCastException (), new Object [] {obj}); 170 } 171 172 } 173 174 175 182 public synchronized void removeElementAt(int index) { 183 184 throwUnsupportedOption(); 185 186 StateManager stateManager = this.makeDirty(); 188 189 Object obj = super.elementAt(index); 190 super.removeElementAt(index); 191 if (added.remove(obj) == false) 192 removed.add(obj); 193 194 this.applyUpdates(stateManager, true); 196 197 } 198 199 208 public synchronized void insertElementAt(Object obj, int index) { 209 if (allowNulls == false && obj == null) 210 { 211 throw new JDOUserException(I18NHelper.getMessage(messages, 212 "sco.nulls_not_allowed")); } 214 215 if (elementType == null || elementType.isAssignableFrom(obj.getClass())) 216 { 217 StateManager stateManager = this.makeDirty(); 219 220 super.insertElementAt(obj, index); 221 if (removed.remove(obj) == false) 222 added.add(obj); 223 224 this.applyUpdates(stateManager, true); 226 227 } else { 228 throw new JDOUserException(I18NHelper.getMessage(messages, 229 "sco.classcastexception", elementType.getName()), new ClassCastException (), new Object [] {obj}); 231 } 232 233 } 234 235 242 public synchronized void addElement(Object obj) { 243 if (allowNulls == false && obj == null) 244 { 245 throw new JDOUserException(I18NHelper.getMessage(messages, 246 "sco.nulls_not_allowed")); } 248 249 if (elementType == null || elementType.isAssignableFrom(obj.getClass())) 250 { 251 StateManager stateManager = this.makeDirty(); 253 254 super.addElement(obj); 255 if (removed.remove(obj) == false) 256 added.add(obj); 257 258 this.applyUpdates(stateManager, true); 260 261 } else { 262 throw new JDOUserException(I18NHelper.getMessage(messages, 263 "sco.classcastexception", elementType.getName()), new ClassCastException (), new Object [] {obj}); 265 } 266 267 } 268 269 278 public synchronized boolean removeElement(Object obj) { 279 280 283 StateManager stateManager = this.makeDirty(); 285 286 int i = super.indexOf(obj); 287 if (i > -1) { 288 super.removeElementAt(i); 289 290 if (added.remove(obj) == false) 291 removed.add(obj); 292 293 this.applyUpdates(stateManager, true); 295 return true; 296 } 297 return false; 298 299 } 300 301 306 public synchronized void removeAllElements() { 307 StateManager stateManager = this.makeDirty(); 309 310 for (Iterator iter = super.iterator(); iter.hasNext();) { 311 Object o = iter.next(); 312 if (added.remove(o) == false) 313 removed.add(o); 314 } 315 added.clear(); 316 317 super.removeAllElements(); 318 319 this.applyUpdates(stateManager, true); 321 322 } 323 324 325 326 338 public synchronized Object set(int index, Object element) { 339 340 throwUnsupportedOption(); 341 342 if (element == null) 343 { 344 if (allowNulls == false) 345 { 346 throw new JDOUserException(I18NHelper.getMessage(messages, 347 "sco.nulls_not_allowed")); } 349 return this.remove(index); 351 } 352 if (elementType == null || elementType.isAssignableFrom(element.getClass())) 353 { 354 StateManager stateManager = this.makeDirty(); 356 357 Object o = super.set(index, element); 358 359 if (added.remove(o) == false) 360 removed.add(o); 361 362 if (removed.remove(element) == false) 363 added.add(element); 364 365 this.applyUpdates(stateManager, true); 367 368 return o; 369 } else { 370 throw new JDOUserException(I18NHelper.getMessage(messages, 371 "sco.classcastexception", elementType.getName()), new ClassCastException (), new Object [] {element}); 373 } 374 375 } 376 377 378 385 public synchronized boolean add(Object o) { 386 if (allowNulls == false && o == null) 387 { 388 throw new JDOUserException(I18NHelper.getMessage(messages, 389 "sco.nulls_not_allowed")); } 391 392 if (elementType == null || elementType.isAssignableFrom(o.getClass())) 393 { 394 StateManager stateManager = this.makeDirty(); 396 397 if (removed.remove(o) == false) 398 added.add(o); 399 400 boolean modified = super.add(o); 401 402 this.applyUpdates(stateManager, modified); 404 405 return modified; 406 407 } else { 408 throw new JDOUserException(I18NHelper.getMessage(messages, 409 "sco.classcastexception", elementType.getName()), new ClassCastException (), new Object [] {o}); 411 } 412 413 } 414 415 423 public boolean remove(Object o) { 424 return this.removeElement(o); 425 } 426 427 436 public void add(int index, Object element) { 437 this.insertElementAt(element, index); 438 } 439 440 450 public synchronized Object remove(int index) { 451 452 throwUnsupportedOption(); 453 454 StateManager stateManager = this.makeDirty(); 456 457 Object obj = super.remove(index); 458 459 if (added.remove(obj) == false) 460 removed.add(obj); 461 462 this.applyUpdates(stateManager, true); 464 465 return obj; 466 } 467 468 474 public void clear() { 475 this.removeAllElements(); 476 } 477 478 486 public synchronized boolean addAll(Collection c) { 487 if (allowNulls == false && c.contains(null)) 488 { 489 throw new JDOUserException(I18NHelper.getMessage(messages, 490 "sco.nulls_not_allowed")); } 492 493 java.util.Vector errc = new java.util.Vector (); 494 if (elementType != null) 495 { 496 Iterator i = c.iterator(); 498 while (i.hasNext()) 499 { 500 Object o = i.next(); 501 if (!elementType.isAssignableFrom(o.getClass())) 502 errc.add(o); 503 } 504 } 505 if (errc != null && errc.size() > 0) 506 { 507 throw new JDOUserException(I18NHelper.getMessage(messages, 508 "sco.classcastexception", elementType.getName()), new ClassCastException (), errc.toArray()); 510 } 511 512 StateManager stateManager = this.makeDirty(); 514 515 removed.removeAll(c); 516 added.addAll(c); 517 518 boolean modified = super.addAll(c); 519 520 this.applyUpdates(stateManager, modified); 522 523 return modified; 524 } 525 526 533 public synchronized boolean removeAll(Collection c) { 534 boolean modified = false; 535 StateManager stateManager = this.makeDirty(); 537 538 Iterator e = c.iterator(); 539 while (e.hasNext()) { 540 Object o = e.next(); 541 if(super.contains(o)) { 542 removeInternal(o); 543 if (added.remove(o) == false) 544 removed.add(o); 545 modified = true; 546 } 547 } 548 549 this.applyUpdates(stateManager, modified); 551 552 return modified; 553 } 554 555 570 public synchronized boolean addAll(int index, Collection c) { 571 if (allowNulls == false && c.contains(null)) 572 { 573 throw new JDOUserException(I18NHelper.getMessage(messages, 574 "sco.nulls_not_allowed")); } 576 577 java.util.Vector errc = new java.util.Vector (); 578 if (elementType != null) 579 { 580 Iterator i = c.iterator(); 582 while (i.hasNext()) 583 { 584 Object o = i.next(); 585 if (!elementType.isAssignableFrom(o.getClass())) 586 errc.add(o); 587 } 588 } 589 if (errc != null && errc.size() > 0) 590 { 591 throw new JDOUserException(I18NHelper.getMessage(messages, 592 "sco.classcastexception", elementType.getName()), new ClassCastException (), errc.toArray()); 594 } 595 596 StateManager stateManager = this.makeDirty(); 598 599 removed.removeAll(c); 600 added.addAll(c); 601 602 boolean modified = super.addAll(index, c); 603 604 this.applyUpdates(stateManager, modified); 606 607 return modified; 608 } 609 610 617 public synchronized boolean retainAll(Collection c) { 618 boolean modified = false; 619 java.util.Vector v = new java.util.Vector (); 620 621 StateManager stateManager = this.makeDirty(); 623 624 for (Iterator iter = super.iterator(); iter.hasNext();) 625 { 626 Object o = iter.next(); 627 if (!c.contains(o)) 628 { 629 v.add(o); 630 if (added.remove(o) == false) 631 removed.add(o); 632 633 modified = true; 634 } 635 } 636 637 for (Iterator iter = v.iterator(); iter.hasNext();) 639 { 640 removeInternal(iter.next()); 641 } 642 643 this.applyUpdates(stateManager, modified); 645 646 647 return modified; 648 } 649 650 658 public Object clone() 659 { 660 Vector obj = (Vector)super.clone(); 661 obj.unsetOwner(); 662 663 return obj; 664 } 665 666 670 public Object cloneInternal() 671 { 672 return super.clone(); 673 } 674 675 678 public void reset() 679 { 680 added.clear(); 681 removed.clear(); 682 } 683 684 685 public void markDeferred() 686 { 687 } 688 689 public boolean isDeferred() 690 { 691 return false; 692 } 693 694 public void applyDeferredUpdates(Collection c) 695 { 696 super.addAll(c); 697 } 698 699 702 public void addInternal(Object o) 703 { 704 super.addElement(o); 705 } 706 707 708 711 public void addAllInternal(Collection c) 712 { 713 super.addAll(c); 714 } 715 716 719 public void addToBaseCollection(Object o) 720 { 721 super.add(o); 722 } 723 724 727 public void removeAllInternal(Collection c) 728 { 729 super.removeAll(c); 730 } 731 732 737 public Collection getAdded() 738 { 739 return (Collection )added; 740 } 741 742 747 public Collection getRemoved() 748 { 749 return (Collection )removed; 750 } 751 752 755 public void clearInternal() 756 { 757 760 int s = super.size() - 1; 761 762 for (int i = s; i > -1; i--) 764 { 765 super.removeElementAt(i); 766 } 767 768 this.reset(); 769 } 770 771 772 775 public void removeInternal(Object o) 776 { 777 int i = super.indexOf(o); 778 super.remove(i); 779 } 780 781 784 public void unsetOwner() 785 { 786 this.owner = null; 787 this.fieldName = null; 788 this.elementType = null; 789 added.clear(); 790 removed.clear(); 791 } 792 793 798 public Object getOwner() 799 { 800 return this.owner; 801 } 802 803 808 public String getFieldName() 809 { 810 return this.fieldName; 811 } 812 813 816 public StateManager makeDirty() 817 { 818 if (owner != null) 819 { 820 StateManager stateManager = owner.jdoGetStateManager(); 821 if (stateManager != null) 822 { 823 stateManager.makeDirty(fieldName); 824 } 825 826 return stateManager; 827 } 828 return null; 829 } 830 833 public void applyUpdates(StateManager sm, boolean modified) 834 { 835 836 if (modified && sm != null) 837 { 838 sm.applyUpdates(fieldName, this); 839 } 840 } 841 842 845 private void throwUnsupportedOption() 846 { 847 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 850 "sco.not_supported")); } 852 853 861 public void setOwner(Object owner, String fieldName, Class elementType) { 862 863 if (this.owner != null) { 864 throw new JDOUserException(I18NHelper.getMessage( 865 messages1, "core.statemanager.anotherowner"), new Object []{this.owner, this.fieldName}); 867 } 868 if (owner instanceof PersistenceCapable) { 869 this.owner = (PersistenceCapable)owner; 870 this.fieldName = fieldName; 871 this.elementType = elementType; 872 } 873 } 874 } 875 | Popular Tags |