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