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.ArrayList ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 25 import com.versant.core.common.BindingSupportImpl; 26 27 30 public final class DetachSCOArrayList extends ArrayList implements Serializable , VersantSimpleSCO { 31 32 private int fieldNo; 33 private VersantStateManager stateManager; 34 private PersistenceCapable owner; 35 36 public DetachSCOArrayList(PersistenceCapable owner, VersantStateManager stateManager, 37 VersantFieldMetaData fmd, Object [] originalData) { 38 this.fieldNo = fmd.getManagedFieldNo(); 39 this.stateManager = stateManager; 40 this.owner = owner; 41 int n = originalData == null ? 0 : originalData.length; 42 if (n > 0) ensureCapacity(n); 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 Object set(int index, Object element) { 55 if (element == null) throw createNPE(); 56 Object result = super.set(index, element); 57 makeDirty(); 58 return result; 59 } 60 61 public boolean add(Object o) { 62 if (o == null) throw createNPE(); 63 super.add(o); 64 makeDirty(); 65 return true; 66 } 67 68 public void add(int index, Object element) { 69 if (element == null) throw createNPE(); 70 super.add(index, element); 71 makeDirty(); 72 } 73 74 public Object remove(int index) { 75 Object result = super.remove(index); 76 if (result == null) return null; makeDirty(); 78 return result; 79 } 80 81 public boolean remove(Object o) { 82 int i = indexOf(o); 87 if (i < 0) return false; 88 remove(i); 89 return true; 90 } 91 92 public boolean addAll(Collection c) { 93 if (super.addAll(c)) { 94 makeDirty(); 95 return true; 96 } 97 return false; 98 } 99 100 public void clear() { 101 super.clear(); 102 makeDirty(); 103 } 104 105 public boolean addAll(int index, Collection c) { 106 boolean result; 107 result = super.addAll(index, c); 108 if (result) makeDirty(); 109 return result; 110 } 111 112 protected void removeRange(int fromIndex, int toIndex) { 113 super.removeRange(fromIndex, toIndex); 114 makeDirty(); 115 } 116 117 public boolean removeAll(Collection c) { 118 if (super.removeAll(c)) { 119 makeDirty(); 120 return true; 121 } 122 return false; 123 } 124 125 public boolean retainAll(Collection c) { 126 boolean modified = false; 127 Iterator e = super.iterator(); 128 while (e.hasNext()) { 129 Object o = e.next(); 130 if (!c.contains(o)) { 131 e.remove(); 132 modified = true; 133 } 134 } 135 if (modified) makeDirty(); 136 return modified; 137 } 138 139 public void makeDirty() { 140 if (stateManager != null && owner != null) { 141 stateManager.makeDirty(owner, fieldNo); 142 } 143 } 144 145 public Iterator iterator() { 146 return new SCOIterator(super.iterator(), stateManager, owner, fieldNo); 147 } 148 149 public void makeTransient() { 150 owner = null; 151 stateManager = null; 152 } 153 } 154 | Popular Tags |