1 2 12 package com.versant.core.jdo.sco.detached; 13 14 import com.versant.core.jdo.VersantStateManager; 15 import com.versant.core.common.VersantFieldMetaData; 16 import com.versant.core.jdo.sco.VersantSimpleSCO; 17 import com.versant.core.jdo.sco.SCOIterator; 18 19 import javax.jdo.spi.PersistenceCapable; 20 import java.io.Serializable ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.TreeSet ; 24 25 import com.versant.core.common.BindingSupportImpl; 26 27 30 public class DetachSCOTreeSet extends TreeSet implements Serializable , VersantSimpleSCO { 31 32 private PersistenceCapable owner; 33 private VersantStateManager stateManager; 34 private int fieldNo; 35 36 public DetachSCOTreeSet(PersistenceCapable owner, VersantStateManager stateManager, 37 VersantFieldMetaData fmd, Object [] originalData) { 38 super(fmd.getComparator()); 39 this.owner = owner; 40 this.stateManager = stateManager; 41 this.fieldNo = fmd.getManagedFieldNo(); 42 int n = originalData == null ? 0 : originalData.length; 43 if (!owner.jdoIsNew()) { 44 for (int i = 0; i < n; i++) { 45 Object o = originalData[i]; 46 if (o == null) break; 47 super.add(o); 48 } 49 } else { 50 for (int i = 0; i < n; i++) { 51 Object o = originalData[i]; 52 if (o == null) throw createNPE(); 53 super.add(o); 54 } 55 } 56 } 57 58 private RuntimeException createNPE() { 59 return BindingSupportImpl.getInstance().nullElement("Null element not allowed."); 60 } 61 62 public boolean add(Object o) { 63 if (o == null) throw createNPE(); 64 boolean result = super.add(o); 65 if (result) makeDirty(); 66 return result; 67 } 68 69 public boolean remove(Object o) { 70 boolean result = super.remove(o); 71 if (result) { 72 makeDirty(); 73 } 74 return result; 75 } 76 77 public void clear() { 78 super.clear(); 79 makeDirty(); 80 } 81 82 public boolean retainAll(Collection c) { 83 if (super.retainAll(c)) { 84 makeDirty(); 85 return true; 86 } 87 return false; 88 } 89 90 public boolean removeAll(Collection c) { 91 if (super.removeAll(c)) { 92 makeDirty(); 93 return true; 94 } 95 return false; 96 } 97 98 public boolean addAll(Collection c) { 99 if (super.addAll(c)) { 100 makeDirty(); 101 return true; 102 } 103 return false; 104 105 } 106 107 public Iterator iterator() { 108 return new SCOIterator(super.iterator(), stateManager, owner, fieldNo); 109 } 110 111 private void makeDirty() { 112 if (stateManager != null && owner != null) { 113 stateManager.makeDirty(owner, fieldNo); 114 } 115 } 116 117 public void makeTransient() { 118 owner = null; 119 stateManager = null; 120 } 121 } 122 | Popular Tags |