1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.util.Collection ; 22 import java.util.ConcurrentModificationException ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.ListIterator ; 27 import java.util.Set ; 28 29 import javax.jmi.reflect.DuplicateException; 30 import javax.jmi.reflect.RefObject; 31 32 import org.netbeans.mdr.handlers.BaseObjectHandler; 33 import org.netbeans.mdr.persistence.MOFID; 34 import org.netbeans.mdr.persistence.StorageException; 35 import org.netbeans.mdr.storagemodel.StorableClass; 36 import org.netbeans.mdr.storagemodel.StorableFeatured; 37 import org.netbeans.mdr.util.DebugException; 38 import org.openide.ErrorManager; 39 40 public class SemiPersistentAttrList implements List { 41 42 43 private List innerList; 44 private final Set innerSet = new HashSet (); 45 private int modCount = 0; 46 47 private final StorableFeatured mdrObject; 48 private final StorableClass.AttributeDescriptor attrDesc; 49 private final MOFID metaMofId; 50 private final boolean isRefObject; 51 52 private final SemiPersistentElement parent; 53 private final int changeMask; 54 55 56 public SemiPersistentAttrList(StorableFeatured mdrObject, StorableClass.AttributeDescriptor attrDesc, List innerList, SemiPersistentElement parent, int changeMask) { 57 this.mdrObject = mdrObject; 58 this.attrDesc = attrDesc; 59 this.parent = parent; 60 this.changeMask = changeMask; 61 metaMofId = attrDesc.getMofId(); 62 isRefObject = RefObject.class.isAssignableFrom(attrDesc.getType()); 63 setInnerList(innerList); 64 } 65 66 public void setInnerList(List innerList) { 67 modCount++; 68 this.innerList = innerList; 69 innerSet.clear(); 70 Iterator iter = innerList.iterator(); 71 while(iter.hasNext()) { 72 Object obj = iter.next(); 73 if (innerSet.contains(obj)) { 74 throw new DuplicateException(obj, getMetaElement()); 75 } else { 76 innerSet.add(obj); 77 if (isRefObject) { 78 setAttribComposite((RefObject) obj); 79 } 80 } 81 } 82 } 83 84 protected void objectChanged() { 85 if (parent != null) 86 parent.objectChanged(changeMask); 87 } 88 89 91 private void setAttribComposite(RefObject object) { 92 try { 93 DeferredObject storable = (DeferredObject) ((BaseObjectHandler) object)._getDelegate(); 94 storable.setComposite(mdrObject, storable.getMofId(), metaMofId); 95 } catch (StorageException e) { 96 ErrorManager.getDefault().notify(e); 97 } 98 } 99 100 private void clearAttribComposite(RefObject object) { 101 try { 102 DeferredObject storable = (DeferredObject) ((BaseObjectHandler) object)._getDelegate(); 103 storable.clearComposite(); 104 } catch (StorageException e) { 105 ErrorManager.getDefault().notify(e); 106 } 107 } 108 109 private RefObject getMetaElement() { 110 try { 111 return (RefObject) mdrObject.getMdrStorage().getRepository().getHandler(mdrObject.getMdrStorage().getObject(metaMofId)); 112 } catch (Exception e) { 113 return null; 114 } 115 } 116 117 119 public boolean remove(Object obj) { 120 boolean res = innerList.remove(obj); 121 if (res) { 122 innerSet.remove(obj); 123 if (isRefObject) { 124 clearAttribComposite((RefObject) obj); 125 } 126 objectChanged(); 127 } 128 return res; 129 } 130 131 public Object set(int param, Object obj) { 132 objectChanged(); 133 Object old = innerList.set(param, obj); 134 if (isRefObject) { 135 clearAttribComposite((RefObject) old); 136 setAttribComposite((RefObject) obj); 137 } 138 innerSet.remove(old); 139 if (!innerSet.add(obj)) { 140 throw new DuplicateException(obj, getMetaElement()); 141 } 142 return old; 143 } 144 145 public Object remove(int param) { 146 objectChanged(); 147 Object old = innerList.remove(param); 148 if (isRefObject) { 149 clearAttribComposite ((RefObject) old); 150 } 151 innerSet.remove(old); 152 return old; 153 } 154 155 public void add(int param, Object obj) { 156 objectChanged(); 157 innerList.add(param, obj); 158 if (isRefObject) { 159 setAttribComposite((RefObject) obj); 160 } 161 if (!innerSet.add(obj)) { 162 throw new DuplicateException(obj, getMetaElement()); 163 } 164 } 165 166 public boolean add(Object obj) { 167 objectChanged(); 168 boolean res = innerList.add(obj); 169 if (isRefObject && res) { 170 setAttribComposite((RefObject) obj); 171 } 172 if (!innerSet.add(obj)) { 173 throw new DuplicateException(obj, getMetaElement()); 174 } 175 return res; 176 } 177 178 public ListIterator listIterator(int param) { 179 return new SemiPersistentAttrListIterator(innerList.listIterator(param)); 180 } 181 182 public Iterator iterator() { 183 return new SemiPersistentAttrListIterator(innerList.listIterator()); 184 } 185 186 public ListIterator listIterator() { 187 return new SemiPersistentAttrListIterator(innerList.listIterator()); 188 } 189 190 public List subList(int param, int param1) { 191 return innerList.subList(param, param1); 192 } 193 194 public boolean contains(Object obj) { 195 return innerSet.contains(obj); 196 } 197 198 public boolean containsAll(Collection collection) { 199 return innerSet.containsAll(collection); 200 } 201 202 public boolean addAll(Collection c) { 203 throw new DebugException(); 205 } 206 207 public void clear() { 208 throw new DebugException(); 210 } 211 212 public boolean isEmpty() { 213 return innerList.isEmpty(); 214 } 215 216 public boolean removeAll(Collection c) { 217 throw new DebugException(); 219 } 220 221 public boolean retainAll(Collection c) { 222 throw new DebugException(); 224 } 225 226 public int size() { 227 return innerList.size(); 228 } 229 230 public Object [] toArray() { 231 return innerList.toArray(); 232 } 233 234 public Object [] toArray(Object [] a) { 235 return innerList.toArray(a); 236 } 237 238 public boolean addAll(int index, Collection c) { 239 throw new DebugException(); 241 } 242 243 public Object get(int index) { 244 return innerList.get(index); 245 } 246 247 public int indexOf(Object o) { 248 return innerList.indexOf(o); 249 } 250 251 public int lastIndexOf(Object o) { 252 return innerList.lastIndexOf(o); 253 } 254 255 class SemiPersistentAttrListIterator implements ListIterator { 257 258 private Object lastRead; 259 private ListIterator innerIterator; 260 private final int modCount; 261 262 SemiPersistentAttrListIterator(ListIterator iterator) { 263 this.innerIterator = iterator; 264 this.modCount = getParentModCount(); 265 } 266 267 private int getParentModCount() { 268 return SemiPersistentAttrList.this.modCount; 269 } 270 271 private void testModCount() throws ConcurrentModificationException { 272 if (modCount != getParentModCount()) 273 throw new ConcurrentModificationException (); 274 } 275 276 public void remove() { 277 testModCount(); 278 objectChanged(); 279 innerIterator.remove(); 280 innerSet.remove(lastRead); 281 if (isRefObject) { 282 clearAttribComposite((RefObject) lastRead); 283 } 284 } 285 286 public void add(Object obj) { 287 testModCount(); 288 objectChanged(); 289 innerIterator.add(obj); 290 if (!innerSet.add(obj)) { 291 throw new DuplicateException(obj, getMetaElement()); 292 } 293 if (isRefObject) { 294 setAttribComposite((RefObject) obj); 295 } 296 } 297 298 public void set(Object obj) { 299 testModCount(); 300 objectChanged(); 301 innerIterator.set(obj); 302 innerSet.remove(lastRead); 303 if (!innerSet.add(obj)) { 304 throw new DuplicateException(obj, getMetaElement()); 305 } 306 if (isRefObject) { 307 clearAttribComposite((RefObject) lastRead); 308 setAttribComposite((RefObject) obj); 309 } 310 } 311 312 public boolean hasNext() { 313 testModCount(); 314 return innerIterator.hasNext(); 315 } 316 317 public boolean hasPrevious() { 318 testModCount(); 319 return innerIterator.hasPrevious(); 320 } 321 322 public Object next() { 323 testModCount(); 324 return lastRead = innerIterator.next(); 325 } 326 327 public int nextIndex() { 328 testModCount(); 329 return innerIterator.nextIndex(); 330 } 331 332 public Object previous() { 333 testModCount(); 334 return lastRead = innerIterator.previous(); 335 } 336 337 public int previousIndex() { 338 testModCount(); 339 return innerIterator.previousIndex(); 340 } 341 } 342 } 343 | Popular Tags |