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.SCOCollection; 38 import com.sun.jdo.spi.persistence.support.sqlstore.StateManager; 39 import com.sun.jdo.spi.persistence.utility.I18NHelper; 40 41 42 48 public class ArrayList 49 extends java.util.ArrayList 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", ArrayList.class.getClassLoader()); 71 72 private final static ResourceBundle messages1 = I18NHelper.loadBundle( 73 "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", ArrayList.class.getClassLoader()); 75 76 77 86 public ArrayList(Object owner, String fieldName, Class elementType, boolean allowNulls) 87 { 88 super(); 89 if (owner instanceof PersistenceCapable) 90 { 91 this.owner = (PersistenceCapable)owner; 92 this.fieldName = fieldName; 93 } 94 this.elementType = elementType; 95 this.allowNulls = allowNulls; 96 } 97 98 110 public ArrayList(Object owner, String fieldName, 111 Class elementType, boolean allowNulls, int initialCapacity) 112 { 113 super(initialCapacity); 114 if (owner instanceof PersistenceCapable) 115 { 116 this.owner = (PersistenceCapable)owner; 117 this.fieldName = fieldName; 118 } 119 this.elementType = elementType; 120 this.allowNulls = allowNulls; 121 } 122 123 124 125 137 public Object set(int index, Object element) { 138 139 throwUnsupportedOption(); 140 141 if (element == null) 142 { 143 if (allowNulls == false) 144 { 145 throw new JDOUserException(I18NHelper.getMessage(messages, 146 "sco.nulls_not_allowed")); } 148 return this.remove(index); 150 } 151 152 if (elementType == null || elementType.isAssignableFrom(element.getClass())) 153 { 154 StateManager stateManager = this.makeDirty(); 156 157 Object o = super.set(index, element); 158 159 if (added.remove(o) == false) 160 removed.add(o); 161 162 if (removed.remove(element) == false) 163 added.add(element); 164 165 this.applyUpdates(stateManager, true); 167 168 return o; 169 } else { 170 throw new JDOUserException(I18NHelper.getMessage(messages, 171 "sco.classcastexception", elementType.getName()), new ClassCastException (), new Object [] {element}); 173 } 174 175 } 176 177 178 185 public boolean add(Object o) { 186 if (allowNulls == false && o == null) 187 { 188 throw new JDOUserException(I18NHelper.getMessage(messages, 189 "sco.nulls_not_allowed")); } 191 192 if (elementType == null || elementType.isAssignableFrom(o.getClass())) 193 { 194 StateManager stateManager = this.makeDirty(); 196 197 if (removed.remove(o) == false) 198 added.add(o); 199 200 boolean modified = super.add(o); 201 202 this.applyUpdates(stateManager, modified); 204 205 return modified; 206 207 } else { 208 throw new JDOUserException(I18NHelper.getMessage(messages, 209 "sco.classcastexception", elementType.getName()), new ClassCastException (), new Object [] {o}); 211 } 212 } 213 214 222 public boolean remove(Object o) { 223 224 227 StateManager stateManager = this.makeDirty(); 229 230 int i = super.indexOf(o); 231 Object obj = null; 232 if (i > -1) { 233 obj = super.remove(i); 234 235 if (added.remove(obj) == false) 236 removed.add(obj); 237 238 this.applyUpdates(stateManager, true); 240 return true; 241 } 242 return false; 243 } 244 245 254 public void add(int index, Object element) { 255 if (allowNulls == false && element == null) 256 { 257 throw new JDOUserException(I18NHelper.getMessage(messages, 258 "sco.nulls_not_allowed")); } 260 261 if (elementType == null || elementType.isAssignableFrom(element.getClass())) 262 { 263 StateManager stateManager = this.makeDirty(); 265 266 super.add(index, element); 267 if (removed.remove(element) == false) 268 added.add(element); 269 270 this.applyUpdates(stateManager, true); 272 273 } else { 274 throw new JDOUserException(I18NHelper.getMessage(messages, 275 "sco.classcastexception", elementType.getName()), new ClassCastException (), new Object [] {element}); 277 } 278 279 } 280 281 291 public Object remove(int index) { 292 293 throwUnsupportedOption(); 294 295 StateManager stateManager = this.makeDirty(); 297 298 Object obj = super.remove(index); 299 300 if (added.remove(obj) == false) 301 removed.add(obj); 302 303 this.applyUpdates(stateManager, true); 305 306 return obj; 307 } 308 309 315 public void clear() { 316 StateManager stateManager = this.makeDirty(); 318 319 for (Iterator iter = super.iterator(); iter.hasNext();) { 320 Object o = iter.next(); 321 if (added.remove(o) == false) 322 removed.add(o); 323 } 324 added.clear(); 325 super.clear(); 326 327 this.applyUpdates(stateManager, true); 329 } 330 331 341 public boolean addAll(Collection c) { 342 if (allowNulls == false && c.contains(null)) 343 { 344 throw new JDOUserException(I18NHelper.getMessage(messages, 345 "sco.nulls_not_allowed")); } 347 348 java.util.Vector errc = new java.util.Vector (); 349 if (elementType != null) 350 { 351 Iterator i = c.iterator(); 353 while (i.hasNext()) 354 { 355 Object o = i.next(); 356 if (!elementType.isAssignableFrom(o.getClass())) 357 errc.add(o); 358 } 359 } 360 if (errc != null && errc.size() > 0) 361 { 362 throw new JDOUserException(I18NHelper.getMessage(messages, 363 "sco.classcastexception", elementType.getName()), new ClassCastException (), errc.toArray()); 365 } 366 367 StateManager stateManager = this.makeDirty(); 369 370 removed.removeAll(c); 371 added.addAll(c); 372 373 boolean modified = super.addAll(c); 374 375 this.applyUpdates(stateManager, modified); 377 378 return modified; 379 } 380 381 388 public boolean removeAll(Collection c) { 389 boolean modified = false; 390 StateManager stateManager = this.makeDirty(); 392 393 Iterator e = c.iterator(); 394 while (e.hasNext()) { 395 Object o = e.next(); 396 if(super.contains(o)) { 397 removeInternal(o); 398 if (added.remove(o) == false) 399 removed.add(o); 400 modified = true; 401 } 402 } 403 404 this.applyUpdates(stateManager, modified); 406 407 return modified; 408 } 409 410 425 public boolean addAll(int index, Collection c) { 426 if (allowNulls == false && c.contains(null)) 427 { 428 throw new JDOUserException(I18NHelper.getMessage(messages, 429 "sco.nulls_not_allowed")); } 431 432 java.util.Vector errc = new java.util.Vector (); 433 if (elementType != null) 434 { 435 Iterator i = c.iterator(); 437 while (i.hasNext()) 438 { 439 Object o = i.next(); 440 if (!elementType.isAssignableFrom(o.getClass())) 441 errc.add(o); 442 } 443 } 444 if (errc != null && errc.size() > 0) 445 { 446 throw new JDOUserException(I18NHelper.getMessage(messages, 447 "sco.classcastexception", elementType.getName()), new ClassCastException (), errc.toArray()); 449 } 450 451 StateManager stateManager = this.makeDirty(); 453 454 removed.removeAll(c); 455 added.addAll(c); 456 457 boolean modified = super.addAll(index, c); 458 459 this.applyUpdates(stateManager, modified); 461 462 return modified; 463 } 464 465 472 public boolean retainAll(Collection c) 473 { 474 boolean modified = false; 475 java.util.Vector v = new java.util.Vector (); 476 477 StateManager stateManager = this.makeDirty(); 479 480 for (Iterator iter = super.iterator(); iter.hasNext();) 481 { 482 Object o = iter.next(); 483 if (!c.contains(o)) 484 { 485 v.add(o); 486 if (added.remove(o) == false) 487 removed.add(o); 488 489 modified = true; 490 } 491 } 492 493 for (Iterator iter = v.iterator(); iter.hasNext();) 495 { 496 removeInternal(iter.next()); 497 } 498 499 this.applyUpdates(stateManager, modified); 501 502 return modified; 503 } 504 505 513 public Object clone() 514 { 515 ArrayList obj = (ArrayList)super.clone(); 516 obj.unsetOwner(); 517 518 return obj; 519 } 520 521 525 public Object cloneInternal() 526 { 527 return super.clone(); 528 } 529 530 533 public void reset() 534 { 535 added.clear(); 536 removed.clear(); 537 } 538 539 public void markDeferred() 540 { 541 } 542 543 public boolean isDeferred() 544 { 545 return false; 546 } 547 548 public void applyDeferredUpdates(Collection c) 549 { 550 super.addAll(c); 551 } 552 553 556 public void addInternal(Object o) 557 { 558 super.add(o); 559 } 560 561 564 public void addAllInternal(Collection c) 565 { 566 super.addAll(c); 567 } 568 569 572 public void addToBaseCollection(Object o) 573 { 574 super.add(o); 575 } 576 577 580 public void removeAllInternal(Collection c) 581 { 582 super.removeAll(c); 583 } 584 585 590 public Collection getAdded() 591 { 592 return (Collection )added; 593 } 594 595 600 public Collection getRemoved() 601 { 602 return (Collection )removed; 603 } 604 605 606 609 public void clearInternal() 610 { 611 super.clear(); 612 this.reset(); 613 } 614 615 618 public void removeInternal(Object o) 619 { 620 int i = super.indexOf(o); 621 super.remove(i); 622 } 623 624 627 public void unsetOwner() 628 { 629 this.owner = null; 630 this.fieldName = null; 631 this.elementType = null; 632 added.clear(); 633 removed.clear(); 634 } 635 636 641 public Object getOwner() 642 { 643 return this.owner; 644 } 645 646 651 public String getFieldName() 652 { 653 return this.fieldName; 654 } 655 656 659 public StateManager makeDirty() 660 { 661 if (owner != null) 662 { 663 StateManager stateManager = owner.jdoGetStateManager(); 664 if (stateManager != null) 665 { 666 stateManager.makeDirty(fieldName); 667 } 668 669 return stateManager; 670 } 671 return null; 672 } 673 676 public void applyUpdates(StateManager sm, boolean modified) 677 { 678 679 if (modified && sm != null) 680 { 681 sm.applyUpdates(fieldName, this); 682 } 683 } 684 685 688 private void throwUnsupportedOption() 689 { 690 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 693 "sco.not_supported")); } 695 696 704 public void setOwner(Object owner, String fieldName, Class elementType) { 705 706 if (this.owner != null) { 707 throw new JDOUserException(I18NHelper.getMessage( 708 messages1, "core.statemanager.anotherowner"), new Object []{this.owner, this.fieldName}); 710 } 711 if (owner instanceof PersistenceCapable) { 712 this.owner = (PersistenceCapable)owner; 713 this.fieldName = fieldName; 714 this.elementType = elementType; 715 } 716 } 717 } 718 | Popular Tags |