1 18 package org.objectweb.speedo.genclass.collection; 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import org.objectweb.jorm.api.PIndexedElem; 26 import org.objectweb.jorm.naming.api.PName; 27 import org.objectweb.jorm.util.api.Loggable; 28 import org.objectweb.speedo.api.Debug; 29 import org.objectweb.speedo.genclass.GenClassAccessor; 30 import org.objectweb.speedo.genclass.GenClassElement; 31 import org.objectweb.speedo.genclass.PIndexedElemIterator; 32 import org.objectweb.speedo.genclass.SpeedoGenClassHome; 33 import org.objectweb.speedo.genclass.api.SpeedoGenClassProxy; 34 import org.objectweb.speedo.mim.api.LifeCycle; 35 import org.objectweb.speedo.mim.api.SpeedoAccessor; 36 import org.objectweb.speedo.mim.api.SpeedoProxy; 37 import org.objectweb.speedo.pm.api.ProxyManager; 38 import org.objectweb.util.monolog.api.BasicLevel; 39 40 43 public class CollectionAccessor 44 extends GenClassAccessor 45 implements Collection { 46 47 51 protected Collection collection = null; 52 53 54 57 public CollectionAccessor(SpeedoGenClassProxy _jdoProxy) { 58 super(_jdoProxy); 59 collection = (Collection ) _jdoProxy.createGenClass(); 60 } 61 62 public String toString() { 63 return "CollectionAccessor: id=" + jdoProxy.jdoGetGenClassId(); 64 } 65 66 protected boolean duplicatAllowed() { 67 return true; 68 } 69 70 public GenClassElement speedoRemove2(Object o) { 71 tmpelem.setElement(o); Iterator it = elements.iterator(); 73 while (it.hasNext()) { 74 CollectionElem e = (CollectionElem) it.next(); 75 if (e.equals(tmpelem) && e.getElemStatus() != PIndexedElem.ELEM_DELETED) { 76 e.setStatus(PIndexedElem.ELEM_DELETED); 77 if (Debug.ON && getLogger() != null) { 78 logger.log(BasicLevel.DEBUG, "speedoRemove2(): removed"); 79 } 80 return e; 81 } 82 } 83 if (Debug.ON && getLogger() != null) { 84 logger.log(BasicLevel.DEBUG, "speedoRemove2(): not found"); 85 } 86 return null; 87 } 88 89 public void makePersistent(ProxyManager pm) { 90 if (Debug.ON && getLogger() != null) { 91 logger.log(BasicLevel.DEBUG, "makePersistent"); 92 } 93 if (collection.size() > 0) { 94 ((SpeedoGenClassHome) getSpeedoProxy().getSpeedoHome()) 95 .makePersistent(pm, collection.iterator(), null); 96 } 97 } 98 public void deletePersistent(ProxyManager pm) { 99 if (Debug.ON && getLogger() != null) { 100 logger.log(BasicLevel.DEBUG, "deletePersistent"); 101 } 102 clear(); 103 } 104 105 public void setElements(Object o) { 106 if (Debug.ON && getLogger() != null) { 107 logger.log(BasicLevel.DEBUG, "setElements()"); 108 } 109 clear(); 110 if (o != null) { 111 addAll(((Collection ) o)); 112 } 113 } 114 115 public void loadFieldsFromAccessor(SpeedoAccessor sa) { 116 if (Debug.ON && getLogger() != null) { 117 logger.log(BasicLevel.DEBUG, "jdoLoadFieldsFromAccessor()"); 118 } 119 CollectionAccessor ca = (CollectionAccessor) sa; 120 collection.clear(); 121 collection.addAll(ca.collection); 122 supportDelta = ca.supportDelta; 123 elements.clear(); 124 elements.addAll(ca.elements); 125 jdoSetStatus(ca.jdoGetStatus()); 126 } 127 128 public void detachCopy(ProxyManager pm, Map map, SpeedoAccessor fieldsClone, Collection fgHints){ 129 ((CollectionAccessor)fieldsClone).loadFieldsFromAccessor(this); 130 ((CollectionAccessor)fieldsClone).collection.clear(); 132 Iterator it = ((GenClassAccessor) fieldsClone).elements.iterator(); 134 while(it.hasNext()){ 135 Iterator itFgHints = fgHints.iterator(); 138 Collection currentFgHints = new ArrayList (); 139 while (itFgHints.hasNext()) { 140 currentFgHints.add((String )itFgHints.next()); 141 } 142 CollectionElem iElem = (CollectionElem) it.next(); 144 SpeedoProxy sp = (SpeedoProxy) iElem.getElement(pm); 146 SpeedoProxy clone = null; 147 if(map != null) 148 clone = (SpeedoProxy) map.get(sp); 149 synchronized(currentFgHints){ 150 clone = (SpeedoProxy) pm.speedoDetachCopy(sp, map, currentFgHints); 152 } 153 ((CollectionAccessor)fieldsClone).collection.add(clone); 155 } 156 ((GenClassAccessor) fieldsClone).jdoSetStatus(LifeCycle.TRANSIENT); 157 } 158 159 public void attachCopy(ProxyManager pm, Map map, SpeedoAccessor fieldsClone, boolean makeTransactional) { 160 CollectionAccessor ca = (CollectionAccessor) fieldsClone; 161 Iterator it = ca.collection.iterator(); 163 while(it.hasNext()){ 164 SpeedoProxy spDetached = (SpeedoProxy) it.next(); 166 SpeedoProxy sp = (SpeedoProxy) map.get(spDetached); 167 if(sp == null){ 168 map.put(spDetached, sp); 169 sp = (SpeedoProxy) pm.speedoAttachCopy(spDetached, makeTransactional, map); 170 } 171 speedoAdd(sp, false); 173 } 174 } 175 176 public void refresh(ProxyManager pm, Map map, Collection fgHints){ 177 commonRefreshRetrieve(pm, map, fgHints, true); 178 } 179 180 public void retrieve(ProxyManager pm, Map map, Collection fgHints){ 181 commonRefreshRetrieve(pm, map, fgHints, false); 182 } 183 184 191 private void commonRefreshRetrieve(ProxyManager pm, Map map, Collection fgHints, boolean refresh){ 192 Iterator it = elements.iterator(); 194 while(it.hasNext()){ 195 CollectionElem iElem = (CollectionElem) it.next(); 197 SpeedoProxy sp = (SpeedoProxy) iElem.getElement(pm); 199 if(map != null && !map.containsKey(sp.getPName())){ 200 synchronized(fgHints){ 201 if (refresh) { 202 pm.speedoRefresh(sp, map, fgHints); 204 } else { 205 pm.speedoRetrieve(sp, map, fgHints); 207 } 208 209 } 210 } 211 } 212 } 213 214 218 public boolean add(Object o) { 219 if (Debug.ON && getLogger() != null) { 220 logger.log(BasicLevel.DEBUG, "add()"); 221 } 222 GenClassElement gce = speedoAdd(o, false); 223 224 if (gce != null) { 226 Object _o = o; 227 if (_o instanceof PName) { 228 _o = gce.getElement(jdoProxy.jdoGetPersistenceManager()); 229 } 230 if (_o instanceof SpeedoProxy) { 231 jdoProxy.fireSpeedoElementAdded(_o); 232 } 233 } 234 return gce != null; 235 } 236 237 public boolean addAll(Collection c) { 238 Iterator it = c.iterator(); 239 boolean changed = false; 240 while (it.hasNext()) { 241 changed |= add(it.next()); 242 } 243 return changed; 244 } 245 246 public void clear() { 247 if (elements.size() > 0) { 248 if (Debug.ON && getLogger() != null) { 249 logger.log(BasicLevel.DEBUG, "clear()"); 250 } 251 Collection c = new ArrayList (this); 252 Iterator i = c.iterator(); 253 while (i.hasNext()) { 254 remove(i.next()); 255 } 256 elements.clear(); supportDelta = false; 258 } else { 259 if (Debug.ON && getLogger() != null) { 260 logger.log(BasicLevel.DEBUG, "clear(): empty collection"); 261 } 262 } 263 } 264 265 public boolean contains(Object o) { 266 tmpelem.setElement(o); 267 Iterator it = elements.iterator(); 268 while (it.hasNext()) { 269 PIndexedElem e = (PIndexedElem) it.next(); 270 if (e.equals(tmpelem) && e.getElemStatus() != PIndexedElem.ELEM_DELETED) { 271 if (Debug.ON && getLogger() != null) { 272 logger.log(BasicLevel.DEBUG, "contains(): yes"); 273 } 274 return true; 275 } 276 } 277 if (Debug.ON && getLogger() != null) { 278 logger.log(BasicLevel.DEBUG, "contains(): no"); 279 } 280 return false; 281 } 282 283 public boolean containsAll(Collection c) { 284 Iterator it = c.iterator(); 285 while (it.hasNext()) { 286 if (!contains(it.next())) { 287 return false; 288 } 289 } 290 return true; 291 } 292 293 public boolean equals(Object o) { 294 return (o instanceof Collection ) && 295 ((Collection ) o).size() == size() && 296 containsAll((Collection ) o); 297 } 298 299 public boolean isEmpty() { 300 if (elements.isEmpty()) { 301 return true; 302 } 303 Iterator it = elements.iterator(); 304 while (it.hasNext()) { 306 if (((PIndexedElem) it.next()).getElemStatus() != PIndexedElem.ELEM_DELETED) { 307 return false; 308 } 309 } 310 return true; 311 } 312 313 public Iterator iterator() { 314 return new PIndexedElemIterator(elements, this, 315 jdoProxy.jdoGetPersistenceManager(), ((Loggable) jdoProxy).getLogger()); 316 } 317 318 public boolean remove(Object o) { 319 GenClassElement gce = speedoRemove2(o); 320 if (Debug.ON && getLogger() != null) { 321 logger.log(BasicLevel.DEBUG, "remove: gce=" + gce); 322 } 323 if (gce != null) { 324 Object _o = gce.getElement(jdoProxy.jdoGetPersistenceManager()); 325 if (_o != null) { 326 jdoProxy.fireSpeedoElementRemoved(_o); 327 } 328 } 329 return gce != null; 330 } 331 332 public boolean removeAll(Collection c) { 333 Iterator it = c.iterator(); 334 boolean removed = true; 335 while (it.hasNext()) { 336 removed &= remove(it.next()); 337 } 338 return removed; 339 } 340 341 public boolean retainAll(Collection c) { 342 throw new UnsupportedOperationException (); 343 } 344 345 public int size() { 346 Iterator it = elements.iterator(); 347 int size = 0; 348 while (it.hasNext()) { 349 if (((PIndexedElem) it.next()).getElemStatus() != PIndexedElem.ELEM_DELETED) { 350 size++; 351 } 352 } 353 return size; 354 } 355 356 public Object [] toArray() { 357 ArrayList al = new ArrayList (elements.size()); 358 Iterator it = elements.iterator(); 359 while (it.hasNext()) { 360 GenClassElement gce = (GenClassElement) it.next(); 361 if (gce.getElemStatus() != PIndexedElem.ELEM_DELETED) { 362 al.add(gce.getElement()); 363 } 364 } 365 return al.toArray(); 366 } 367 368 public Object [] toArray(Object [] a) { 369 ArrayList al = new ArrayList (elements.size()); 370 Iterator it = elements.iterator(); 371 while (it.hasNext()) { 372 GenClassElement gce = (GenClassElement) it.next(); 373 if (gce.getElemStatus() != PIndexedElem.ELEM_DELETED) { 374 al.add(gce.getElement()); 375 } 376 } 377 return al.toArray(a); 378 } 379 380 384 public PIndexedElem createPIndexedElem(GenClassAccessor gca) { 385 return new CollectionElem(gca); 386 } 387 388 392 public boolean speedoAdd(Object o, Object hints) { 393 return speedoAdd(o, false) != null; 394 } 395 396 protected GenClassElement speedoAdd(Object o, boolean allowDuplicates) { 397 if (Debug.ON && getLogger() != null) { 398 logger.log(BasicLevel.DEBUG, "speedoAdd(, " + allowDuplicates + ")"); 399 } 400 if (o instanceof SpeedoProxy && !((SpeedoProxy) o).jdoIsActive()) { 401 ((SpeedoGenClassHome) getSpeedoProxy().getSpeedoHome()) 402 .makePersistent(null, (SpeedoProxy) o, 403 (SpeedoGenClassProxy) getSpeedoProxy()); 404 } 405 synchronized (this) { 406 if (!allowDuplicates && contains(o)) { 407 if (Debug.ON && getLogger() != null) { 408 logger.log(BasicLevel.DEBUG, "speedoAdd(, " + allowDuplicates + "): already exist"); 409 } 410 return null; 411 } 412 if (Debug.ON && getLogger() != null) { 413 logger.log(BasicLevel.DEBUG, "speedoAdd(, " + allowDuplicates + "): adding"); 414 } 415 GenClassElement element = (GenClassElement) createPIndexedElem(); 417 element.setElement(o); 418 return (elements.add(element) ? element : null); 419 } 420 } 421 422 public boolean speedoRemove(Object o, Object hints) { 423 if (Debug.ON && getLogger() != null) { 424 logger.log(BasicLevel.DEBUG, "speedoRemove()"); 425 } 426 return speedoRemove2(o) != null; 427 } 428 429 public void forceDetachedDirty() { 430 } 431 432 public void restoreDetachedNone() { 433 } 434 435 } 436 | Popular Tags |