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 18 import javax.jdo.spi.PersistenceCapable; 19 import java.io.Serializable ; 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 import java.util.LinkedList ; 23 import java.util.ListIterator ; 24 25 import com.versant.core.common.BindingSupportImpl; 26 27 30 public final class DetachSCOLinkedList extends LinkedList implements Serializable , VersantSimpleSCO { 31 32 private PersistenceCapable owner; 33 private int fieldNo; 34 private VersantStateManager stateManager; 35 36 public DetachSCOLinkedList(PersistenceCapable owner, VersantStateManager stateManager, 37 VersantFieldMetaData fmd, Object [] originalData) { 38 this.owner = owner; 39 this.fieldNo = fmd.getManagedFieldNo(); 40 this.stateManager = stateManager; 41 int n = originalData == null ? 0 : originalData.length; 42 for (int i = 0; i < n; i++) { 43 Object o = originalData[i]; 44 if (o == null) throw createNPE(); 45 super.add(o); 46 } 47 } 48 49 public Object removeFirst() { 50 Object result = super.removeFirst(); 51 makeDirty(); 52 return result; 53 } 54 55 public Object removeLast() { 56 Object result = super.removeLast(); 57 makeDirty(); 58 return result; 59 } 60 61 private RuntimeException createNPE() { 62 return BindingSupportImpl.getInstance().nullElement("Null element not allowed."); 63 } 64 65 public void addFirst(Object o) { 66 if (o == null) throw createNPE(); 67 super.addFirst(o); 68 makeDirty(); 69 } 70 71 public void addLast(Object o) { 72 if (o == null) throw createNPE(); 73 super.addLast(o); 74 makeDirty(); 75 } 76 77 public boolean addAll(int index, Collection c) { 78 if (super.addAll(index, c)) { 79 makeDirty(); 80 return true; 81 } 82 return false; 83 } 84 85 protected void removeRange(int fromIndex, int toIndex) { 86 super.removeRange(fromIndex, toIndex); 87 makeDirty(); 88 } 89 90 public boolean removeAll(Collection c) { 91 boolean modified = false; 92 Iterator e = super.listIterator(0); 94 while (e.hasNext()) { 95 Object o = e.next(); 96 if (c.contains(o)) { 97 e.remove(); 98 modified = true; 99 } 100 } 101 if (modified) makeDirty(); 102 return modified; 103 104 } 105 106 public boolean retainAll(Collection c) { 107 boolean modified = false; 108 Iterator e = super.listIterator(0); 110 while (e.hasNext()) { 111 Object o = e.next(); 112 if (!c.contains(o)) { 113 e.remove(); 114 modified = true; 115 } 116 } 117 if (modified) makeDirty(); 118 return modified; 119 } 120 121 public Object set(int index, Object element) { 122 if (element == null) throw createNPE(); 123 Object result = super.set(index, element); 124 makeDirty(); 125 return result; 126 } 127 128 public ListIterator listIterator(int index) { 129 return new ListIteratorImp(super.listIterator(index)); 130 } 131 132 138 private class ListIteratorImp implements ListIterator { 139 140 private ListIterator i; 141 142 public void set(Object o) { 143 i.set(o); 144 DetachSCOLinkedList.this.makeDirty(); 145 } 146 147 public void remove() { 148 i.remove(); 149 DetachSCOLinkedList.this.makeDirty(); 150 } 151 152 public void add(Object o) { 153 i.add(o); 154 DetachSCOLinkedList.this.makeDirty(); 155 } 156 157 public ListIteratorImp(ListIterator i) { 158 this.i = i; 159 } 160 161 public boolean hasNext() { 162 return i.hasNext(); 163 } 164 165 public Object next() { 166 return i.next(); 167 } 168 169 public boolean hasPrevious() { 170 return i.hasPrevious(); 171 } 172 173 public Object previous() { 174 return i.previous(); 175 } 176 177 public int nextIndex() { 178 return i.nextIndex(); 179 } 180 181 public int previousIndex() { 182 return i.previousIndex(); 183 } 184 } 185 186 public boolean add(Object o) { 187 if (o == null) throw createNPE(); 188 super.add(o); 189 makeDirty(); 190 return true; 191 } 192 193 public void add(int index, Object element) { 194 if (element == null) throw createNPE(); 195 super.add(index, element); 196 makeDirty(); 197 } 198 199 public Object remove(int index) { 200 Object result = super.remove(index); 201 if (result != null) { 202 makeDirty(); 203 } 204 return result; 205 } 206 207 public boolean remove(Object o) { 208 if (super.remove(o)) { 209 makeDirty(); 210 return true; 211 } 212 return false; 213 } 214 215 public void clear() { 216 super.clear(); 217 makeDirty(); 218 } 219 220 private void makeDirty() { 221 if (stateManager != null && owner != null) { 222 stateManager.makeDirty(owner, fieldNo); 223 } 224 } 225 226 public void makeTransient() { 227 owner = null; 228 stateManager = null; 229 } 230 } 231 | Popular Tags |