KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > genclass > collection > CollectionAccessor


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.genclass.collection;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
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 /**
41  * @author S.Chassande-Barrioz
42  */

43 public class CollectionAccessor
44         extends GenClassAccessor
45         implements Collection JavaDoc {
46
47     /**
48      * is the internal collection used when the gen class is not active.
49      * When the proxy is active the 'elements' field is used.
50      */

51     protected Collection JavaDoc collection = null;
52
53
54     /**
55      * Instanciates and initializes a new collection with an initial size.
56      */

57     public CollectionAccessor(SpeedoGenClassProxy _jdoProxy) {
58         super(_jdoProxy);
59         collection = (Collection JavaDoc) _jdoProxy.createGenClass();
60     }
61
62     public String JavaDoc toString() {
63         return "CollectionAccessor: id=" + jdoProxy.jdoGetGenClassId();
64     }
65
66     protected boolean duplicatAllowed() {
67         return true;
68     }
69
70     public GenClassElement speedoRemove2(Object JavaDoc o) {
71         tmpelem.setElement(o); // tmp type = etype.
72
Iterator JavaDoc 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 JavaDoc o) {
106         if (Debug.ON && getLogger() != null) {
107             logger.log(BasicLevel.DEBUG, "setElements()");
108         }
109         clear();
110         if (o != null) {
111             addAll(((Collection JavaDoc) 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 JavaDoc map, SpeedoAccessor fieldsClone, Collection JavaDoc fgHints){
129         ((CollectionAccessor)fieldsClone).loadFieldsFromAccessor(this);
130         //copy the elements in the collection: collection is no more available when the proxy is not active
131
((CollectionAccessor)fieldsClone).collection.clear();
132         //get an interator on the PIndexed elements
133
Iterator JavaDoc it = ((GenClassAccessor) fieldsClone).elements.iterator();
134         while(it.hasNext()){
135             //for each element of the collection, send the list of fields to load passed as a parameter
136
//indeed, the list sent will be modified (elements processed are removed)
137
Iterator JavaDoc itFgHints = fgHints.iterator();
138             Collection JavaDoc currentFgHints = new ArrayList JavaDoc();
139             while (itFgHints.hasNext()) {
140                 currentFgHints.add((String JavaDoc)itFgHints.next());
141             }
142             //get the PIndexElement
143
CollectionElem iElem = (CollectionElem) it.next();
144             //get the speedo proxy associated
145
SpeedoProxy sp = (SpeedoProxy) iElem.getElement(pm);
146             SpeedoProxy clone = null;
147             if(map != null)
148                 clone = (SpeedoProxy) map.get(sp);
149             synchronized(currentFgHints){
150                 //get the clone of the speedo proxy
151
clone = (SpeedoProxy) pm.speedoDetachCopy(sp, map, currentFgHints);
152             }
153             //add it in the collection of elements reachable when the proxy is no more active
154
((CollectionAccessor)fieldsClone).collection.add(clone);
155         }
156         ((GenClassAccessor) fieldsClone).jdoSetStatus(LifeCycle.TRANSIENT);
157     }
158     
159     public void attachCopy(ProxyManager pm, Map JavaDoc map, SpeedoAccessor fieldsClone, boolean makeTransactional) {
160         CollectionAccessor ca = (CollectionAccessor) fieldsClone;
161         //get an iterator on the PIndexed elements of the collection
162
Iterator JavaDoc it = ca.collection.iterator();
163         while(it.hasNext()){
164             //get the detached speedo proxy
165
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             //put the element of the collection attribute in the elements attribute (the opposite of the jdoDetachCopy method)
172
speedoAdd(sp, false);
173         }
174     }
175     
176     public void refresh(ProxyManager pm, Map JavaDoc map, Collection JavaDoc fgHints){
177         commonRefreshRetrieve(pm, map, fgHints, true);
178     }
179     
180     public void retrieve(ProxyManager pm, Map JavaDoc map, Collection JavaDoc fgHints){
181         commonRefreshRetrieve(pm, map, fgHints, false);
182     }
183     
184     /**
185      *
186      * @param pm
187      * @param map
188      * @param fgHints
189      * @param refresh: if true, call refresh, else call retrieve
190      */

191     private void commonRefreshRetrieve(ProxyManager pm, Map JavaDoc map, Collection JavaDoc fgHints, boolean refresh){
192         //get an interator on the PIndexed elements
193
Iterator JavaDoc it = elements.iterator();
194         while(it.hasNext()){
195             //get the PIndexElement
196
CollectionElem iElem = (CollectionElem) it.next();
197             //get the speedo proxy associated
198
SpeedoProxy sp = (SpeedoProxy) iElem.getElement(pm);
199             if(map != null && !map.containsKey(sp.getPName())){
200                 synchronized(fgHints){
201                     if (refresh) {
202                         //refresh
203
pm.speedoRefresh(sp, map, fgHints);
204                     } else {
205                         //retrieve
206
pm.speedoRetrieve(sp, map, fgHints);
207                     }
208                     
209                 }
210             }
211         }
212     }
213     
214     // ------------------------------------------------------------------------
215
// IMPLEMENTATION OF THE Collection INTERFACE
216
// ------------------------------------------------------------------------
217

218     public boolean add(Object JavaDoc o) {
219         if (Debug.ON && getLogger() != null) {
220             logger.log(BasicLevel.DEBUG, "add()");
221         }
222         GenClassElement gce = speedoAdd(o, false);
223
224         //Send the event about the new element adding
225
if (gce != null) {
226             Object JavaDoc _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 JavaDoc c) {
238         Iterator JavaDoc 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 JavaDoc c = new ArrayList JavaDoc(this);
252             Iterator JavaDoc i = c.iterator();
253             while (i.hasNext()) {
254                 remove(i.next());
255             }
256             elements.clear(); // efficient, but does not work with relations
257
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 JavaDoc o) {
266         tmpelem.setElement(o);
267         Iterator JavaDoc 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 JavaDoc c) {
284         Iterator JavaDoc 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 JavaDoc o) {
294         return (o instanceof Collection JavaDoc) &&
295                 ((Collection JavaDoc) o).size() == size() &&
296                 containsAll((Collection JavaDoc) o);
297     }
298
299     public boolean isEmpty() {
300         if (elements.isEmpty()) {
301             return true;
302         }
303         Iterator JavaDoc it = elements.iterator();
304         // The set is empty if it contains only deleted indexed elements
305
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 JavaDoc iterator() {
314         return new PIndexedElemIterator(elements, this,
315                 jdoProxy.jdoGetPersistenceManager(), ((Loggable) jdoProxy).getLogger());
316     }
317
318     public boolean remove(Object JavaDoc 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 JavaDoc _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 JavaDoc c) {
333         Iterator JavaDoc 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 JavaDoc c) {
342         throw new UnsupportedOperationException JavaDoc();
343     }
344
345     public int size() {
346         Iterator JavaDoc 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 JavaDoc[] toArray() {
357         ArrayList JavaDoc al = new ArrayList JavaDoc(elements.size());
358         Iterator JavaDoc 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 JavaDoc[] toArray(Object JavaDoc[] a) {
369         ArrayList JavaDoc al = new ArrayList JavaDoc(elements.size());
370         Iterator JavaDoc 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     // ------------------------------------------------------------------------
381
// IMPLEMENTATION OF THE PGenClassAccessor INTERFACE
382
// ------------------------------------------------------------------------
383

384     public PIndexedElem createPIndexedElem(GenClassAccessor gca) {
385         return new CollectionElem(gca);
386     }
387
388     // ------------------------------------------------------------------------
389
// IMPLEMENTATION OF THE SpeedoGenClassCoherence INTERFACE
390
// ------------------------------------------------------------------------
391

392     public boolean speedoAdd(Object JavaDoc o, Object JavaDoc hints) {
393         return speedoAdd(o, false) != null;
394     }
395
396     protected GenClassElement speedoAdd(Object JavaDoc 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             // Put it in the "elements" list
416
GenClassElement element = (GenClassElement) createPIndexedElem();
417             element.setElement(o);
418             return (elements.add(element) ? element : null);
419         }
420     }
421
422     public boolean speedoRemove(Object JavaDoc o, Object JavaDoc 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