KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Serializable JavaDoc;
21 import java.math.BigDecimal JavaDoc;
22 import java.math.BigInteger JavaDoc;
23 import java.util.Date JavaDoc;
24
25 import javax.jdo.PersistenceManager;
26
27 import org.objectweb.jorm.api.PException;
28 import org.objectweb.jorm.api.PIndexedElem;
29 import org.objectweb.jorm.naming.api.PName;
30 import org.objectweb.jorm.naming.api.PNamingContext;
31 import org.objectweb.jorm.type.api.PExceptionTyping;
32 import org.objectweb.speedo.genclass.GenClassAccessor;
33 import org.objectweb.speedo.genclass.GenClassElement;
34 import org.objectweb.speedo.mim.api.SpeedoAccessor;
35 import org.objectweb.speedo.mim.api.SpeedoProxy;
36 import org.objectweb.util.monolog.api.BasicLevel;
37 import org.objectweb.util.monolog.api.Logger;
38
39 /**
40  * @author P. D?chamboux
41  */

42 public class CollectionElem implements PIndexedElem, GenClassElement {
43
44     /**
45      * The error message thrown when an accessor method associated to a
46      * primitive element type is called. Indeed this implementation supports
47      * only object elements.
48      */

49     public final static String JavaDoc ERROR_MESSAGE_BAD_FIELD_NAME =
50             "Wrong index field name";
51
52     /**
53      * The error message thrown when an accessor method associated to a
54      * primitive element type is called. Indeed this implementation supports
55      * only object elements.
56      */

57     public final static String JavaDoc ERROR_MESSAGE_NO_NULL_INDEX =
58             "Null value not supported for index field";
59
60     /**
61      * This constant is the name of the index field. This value must be use with
62      * the methods associated to the management of the index.
63      */

64     public final static String JavaDoc INDEX_FIELD_NAME = "idx";
65
66     /**
67      * This field is the element (or its PName for a reference).
68      */

69     protected Object JavaDoc element;
70
71     /**
72      * This field can be the index. The index type is known at instanciation
73      * time.
74      */

75     protected Object JavaDoc index;
76
77     /**
78      * This field represents the satus of the PIndexedEleme. The possible value
79      * are ELEM_CREATED, ELEM_DELETED, ELEM_MODIFIED, ELEM_UNMODIFIED
80      */

81     protected byte status = ELEM_CREATED;
82
83     protected GenClassAccessor gca;
84
85     public CollectionElem(GenClassAccessor gca) {
86         this.gca = gca;
87     }
88
89     
90     public GenClassElement cloneGCE() {
91         return cloneGCE(new CollectionElem(gca));
92     }
93     
94     public GenClassElement cloneGCE(GenClassElement gce) {
95         ((CollectionElem) gce).status = status;
96         ((CollectionElem) gce).element = element;
97         ((CollectionElem) gce).index = index;
98         return gce;
99     }
100     
101     // IMPLEMENTATION OF THE GenClassElement INTERFACE //
102
// ------------------------------------------------//
103

104     public Object JavaDoc getIndex() {
105         return index;
106     }
107
108     public void setIndex(Object JavaDoc index) {
109         this.index = index;
110     }
111
112     /**
113      * @return the element of the gen class. It is a user object.
114      */

115     public synchronized Object JavaDoc getElement() {
116         return getElement(null);
117     }
118
119     /**
120      * Assignes the element of the gen class. It is a user object.
121      * @param element to add
122      */

123     public void setElement(Object JavaDoc element) {
124         this.element = element;
125     }
126
127     /**
128      * @param pm is the persistence manager which permits to resolve the PName
129      * into a java reference.
130      * @return the element of the gen class. The element is a reference
131      * (SpeedoProxy).
132      */

133     public Object JavaDoc getElement(PersistenceManager pm) {
134         Object JavaDoc res = element;
135         if (res instanceof PName) {
136             synchronized(this) {
137                 if (res instanceof PName) {
138                     if (pm == null) {
139                         pm = gca.jdoProxy.jdoGetPersistenceManager();
140                     }
141                     res = pm.getObjectById(res, false);
142                 }
143                 if (status != PIndexedElem.ELEM_DELETED) {
144                     //when an element is deleted, it will be unbound from its
145
// identifier. Then all futur GenClassElement comparaison
146
// will be impossible if we do not have the identifier
147
element = res;
148                 }
149             }
150         }
151         return res;
152     }
153
154     public SpeedoAccessor getSpeedoAccessor() {
155         return gca;
156     }
157
158     public void unSwizzle() {
159         if (element instanceof SpeedoProxy) {
160             synchronized(this) {
161                 if (element instanceof SpeedoProxy) {
162                     element = ((SpeedoProxy) element).getPName();
163                 }
164             }
165         }
166     }
167
168     byte statusForMerge = PIndexedElem.ELEM_UNMODIFIED;
169     
170     public void cleanStatusForMerge() {
171         statusForMerge = PIndexedElem.ELEM_UNMODIFIED;
172     }
173     public byte getStatusForMerge() {
174         return statusForMerge;
175     }
176     public byte retainStatusForMerge() {
177         statusForMerge = status;
178         return statusForMerge;
179     }
180     
181     // IMPLEMENTATION OF THE PIndexedElem INTERFACE //
182
// ---------------------------------------------//
183

184     public byte getElemStatus() {
185         return status;
186     }
187
188     public String JavaDoc pieGetStringElem() throws PExceptionTyping {
189         return (String JavaDoc) element;
190     }
191
192     public Date JavaDoc pieGetDateElem() throws PExceptionTyping {
193         return (Date JavaDoc) element;
194     }
195
196     public char[] pieGetCharArrayElem() throws PExceptionTyping {
197         return (char[]) element;
198     }
199
200     public byte[] pieGetByteArrayElem() throws PExceptionTyping {
201         return (byte[]) element;
202     }
203
204     public Serializable JavaDoc pieGetSerializedElem() throws PExceptionTyping {
205         return (Serializable JavaDoc) element;
206     }
207
208     public PName pieGetRefElem() {
209         PName pn = (PName) (element instanceof SpeedoProxy
210             ? ((SpeedoProxy) element).getPName()
211             : element);
212         try {
213             pn = ((PNamingContext) gca.getSpeedoProxy().getPClassMapping()
214                     .getPNameCoder()).export(null, pn);
215         } catch (Exception JavaDoc e) {
216         }
217         return pn;
218     }
219
220     public boolean pieGetBooleanElem() throws PExceptionTyping {
221         return ((Boolean JavaDoc) element).booleanValue();
222     }
223
224     public Boolean JavaDoc pieGetObooleanElem() throws PExceptionTyping {
225         return (Boolean JavaDoc) element;
226     }
227
228     public byte pieGetByteElem() throws PExceptionTyping {
229         return ((Byte JavaDoc) element).byteValue();
230     }
231
232     public Byte JavaDoc pieGetObyteElem() throws PExceptionTyping {
233         return (Byte JavaDoc) element;
234     }
235
236     public char pieGetCharElem() throws PExceptionTyping {
237         return ((Character JavaDoc) element).charValue();
238     }
239
240     public Character JavaDoc pieGetOcharElem() throws PExceptionTyping {
241         return (Character JavaDoc) element;
242     }
243
244     public short pieGetShortElem() throws PExceptionTyping {
245         return ((Short JavaDoc) element).shortValue();
246     }
247
248     public Short JavaDoc pieGetOshortElem() throws PExceptionTyping {
249         return (Short JavaDoc) element;
250     }
251
252     public int pieGetIntElem() throws PExceptionTyping {
253         return ((Integer JavaDoc) element).intValue();
254     }
255
256     public Integer JavaDoc pieGetOintElem() throws PExceptionTyping {
257         return (Integer JavaDoc) element;
258     }
259
260     public long pieGetLongElem() throws PExceptionTyping {
261         return ((Long JavaDoc) element).longValue();
262     }
263
264     public Long JavaDoc pieGetOlongElem() throws PExceptionTyping {
265         return (Long JavaDoc) element;
266     }
267
268     public float pieGetFloatElem() throws PExceptionTyping {
269         return ((Float JavaDoc) element).floatValue();
270     }
271
272     public Float JavaDoc pieGetOfloatElem() throws PExceptionTyping {
273         return (Float JavaDoc) element;
274     }
275
276     public double pieGetDoubleElem() throws PExceptionTyping {
277         return ((Double JavaDoc) element).doubleValue();
278     }
279
280     public Double JavaDoc pieGetOdoubleElem() throws PExceptionTyping {
281         return (Double JavaDoc) element;
282     }
283
284     public BigDecimal JavaDoc pieGetBigDecimalElem() throws PException {
285         return (BigDecimal JavaDoc) element;
286     }
287
288     public BigInteger JavaDoc pieGetBigIntegerElem() throws PException {
289         return (BigInteger JavaDoc) element;
290     }
291
292     public void pieSetStringElem(String JavaDoc value) throws PExceptionTyping {
293         element = value;
294     }
295
296     public void pieSetDateElem(Date JavaDoc value) throws PExceptionTyping {
297         element = value;
298     }
299
300     public void pieSetCharArrayElem(char[] value) throws PExceptionTyping {
301         element = value;
302     }
303
304     public void pieSetByteArrayElem(byte[] value) throws PExceptionTyping {
305         element = value;
306     }
307
308     public void pieSetSerializedElem(Serializable JavaDoc value) throws PExceptionTyping {
309         element = value;
310     }
311
312     public void pieSetRefElem(PName value) throws PExceptionTyping {
313         element = value;
314         /*
315         try {
316             PersistenceManager pm = gca.jdoProxy.jdoGetPersistenceManager();
317             Transaction tx = (Transaction) pm.currentTransaction();
318             element = value.resolve(tx.getConnectionHolder());
319         } catch (Exception e) {
320             ExceptionHelper.getNested(e).printStackTrace();
321             element = value;
322         }
323         */

324     }
325
326     public void pieSetBooleanElem(boolean value) throws PExceptionTyping {
327         element = new Boolean JavaDoc(value);
328     }
329
330     public void pieSetObooleanElem(Boolean JavaDoc value) throws PExceptionTyping {
331         element = value;
332     }
333
334     public void pieSetByteElem(byte value) throws PExceptionTyping {
335         element = new Byte JavaDoc(value);
336     }
337
338     public void pieSetObyteElem(Byte JavaDoc value) throws PExceptionTyping {
339         element = value;
340     }
341
342     public void pieSetCharElem(char value) throws PExceptionTyping {
343         element = new Character JavaDoc(value);
344     }
345
346     public void pieSetOcharElem(Character JavaDoc value) throws PExceptionTyping {
347         element = value;
348     }
349
350     public void pieSetShortElem(short value) throws PExceptionTyping {
351         element = new Short JavaDoc(value);
352     }
353
354     public void pieSetOshortElem(Short JavaDoc value) throws PExceptionTyping {
355         element = value;
356     }
357
358     public void pieSetIntElem(int value) throws PExceptionTyping {
359         element = new Integer JavaDoc(value);
360     }
361
362     public void pieSetOintElem(Integer JavaDoc value) throws PExceptionTyping {
363         element = value;
364     }
365
366     public void pieSetLongElem(long value) throws PExceptionTyping {
367         element = new Long JavaDoc(value);
368     }
369
370     public void pieSetOlongElem(Long JavaDoc value) throws PExceptionTyping {
371         element = value;
372     }
373
374     public void pieSetFloatElem(float value) throws PExceptionTyping {
375         element = new Float JavaDoc(value);
376     }
377
378     public void pieSetOfloatElem(Float JavaDoc value) throws PExceptionTyping {
379         element = value;
380     }
381
382     public void pieSetDoubleElem(double value) throws PExceptionTyping {
383         element = new Double JavaDoc(value);
384     }
385
386     public void pieSetOdoubleElem(Double JavaDoc value) throws PExceptionTyping {
387         element = value;
388     }
389
390     public void pieSetBigDecimalElem(BigDecimal JavaDoc bigDecimal) throws PException {
391         element = bigDecimal;
392     }
393
394     public void pieSetBigIntegerElem(BigInteger JavaDoc value) throws PException {
395         element = value;
396     }
397     // ---------------------------------------------------------------------------
398
// Index accessor
399
// ---------------------------------------------------------------------------
400

401     public void pieSetByteIndexField(String JavaDoc fn, byte value) throws PExceptionTyping {
402         if (!INDEX_FIELD_NAME.equals(fn))
403             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
404         index = new Byte JavaDoc(value);
405     }
406
407     public void pieSetObyteIndexField(String JavaDoc fn, Byte JavaDoc value) throws PExceptionTyping {
408         if (!INDEX_FIELD_NAME.equals(fn))
409             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
410         if (value == null)
411             throw new PExceptionTyping(ERROR_MESSAGE_NO_NULL_INDEX);
412         index = value;
413     }
414
415     public void pieSetCharIndexField(String JavaDoc fn, char value) throws PExceptionTyping {
416         if (!INDEX_FIELD_NAME.equals(fn))
417             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
418         index = new Character JavaDoc(value);
419     }
420
421     public void pieSetOcharIndexField(String JavaDoc fn, Character JavaDoc value) throws PExceptionTyping {
422         if (!INDEX_FIELD_NAME.equals(fn))
423             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
424         if (value == null)
425             throw new PExceptionTyping(ERROR_MESSAGE_NO_NULL_INDEX);
426         index = value;
427     }
428
429     public void pieSetShortIndexField(String JavaDoc fn, short value) throws PExceptionTyping {
430         if (!INDEX_FIELD_NAME.equals(fn))
431             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
432         index = new Short JavaDoc(value);
433     }
434
435     public void pieSetOshortIndexField(String JavaDoc fn, Short JavaDoc value) throws PExceptionTyping {
436         if (!INDEX_FIELD_NAME.equals(fn))
437             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
438         if (value == null)
439             throw new PExceptionTyping(ERROR_MESSAGE_NO_NULL_INDEX);
440         index = value;
441     }
442
443     public void pieSetIntIndexField(String JavaDoc fn, int value) throws PExceptionTyping {
444         if (!INDEX_FIELD_NAME.equals(fn))
445             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
446         index = new Integer JavaDoc(value);
447     }
448
449     public void pieSetOintIndexField(String JavaDoc fn, Integer JavaDoc value) throws PExceptionTyping {
450         if (!INDEX_FIELD_NAME.equals(fn))
451             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
452         if (value == null)
453             throw new PExceptionTyping(ERROR_MESSAGE_NO_NULL_INDEX);
454         index = value;
455     }
456
457     public void pieSetLongIndexField(String JavaDoc fn, long value) throws PExceptionTyping {
458         if (!INDEX_FIELD_NAME.equals(fn))
459             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
460         index = new Long JavaDoc(value);
461     }
462
463     public void pieSetOlongIndexField(String JavaDoc fn, Long JavaDoc value) throws PExceptionTyping {
464         if (!INDEX_FIELD_NAME.equals(fn))
465             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
466         if (value == null)
467             throw new PExceptionTyping(ERROR_MESSAGE_NO_NULL_INDEX);
468         index = value;
469     }
470
471     public void pieSetStringIndexField(String JavaDoc fn, String JavaDoc value) throws PExceptionTyping {
472         if (!INDEX_FIELD_NAME.equals(fn))
473             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
474         if (value == null)
475             throw new PExceptionTyping(ERROR_MESSAGE_NO_NULL_INDEX);
476         index = value;
477     }
478
479     public void pieSetDateIndexField(String JavaDoc fn, Date JavaDoc value) throws PExceptionTyping {
480         if (!INDEX_FIELD_NAME.equals(fn))
481             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
482         if (value == null)
483             throw new PExceptionTyping(ERROR_MESSAGE_NO_NULL_INDEX);
484         index = value;
485     }
486
487     public short pieGetShortIndexField(String JavaDoc fn) throws PExceptionTyping {
488         if (!INDEX_FIELD_NAME.equals(fn))
489             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
490         return ((Short JavaDoc) index).shortValue();
491     }
492
493     public Short JavaDoc pieGetOshortIndexField(String JavaDoc fn) throws PExceptionTyping {
494         if (!INDEX_FIELD_NAME.equals(fn))
495             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
496         return (Short JavaDoc) index;
497     }
498
499     public long pieGetLongIndexField(String JavaDoc fn) throws PExceptionTyping {
500         if (!INDEX_FIELD_NAME.equals(fn))
501             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
502         return ((Long JavaDoc) index).longValue();
503     }
504
505     public Long JavaDoc pieGetOlongIndexField(String JavaDoc fn) throws PExceptionTyping {
506         if (!INDEX_FIELD_NAME.equals(fn))
507             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
508         return (Long JavaDoc) index;
509     }
510
511     public int pieGetIntIndexField(String JavaDoc fn) throws PExceptionTyping {
512         if (!INDEX_FIELD_NAME.equals(fn))
513             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
514         return ((Integer JavaDoc) index).intValue();
515     }
516
517     public Integer JavaDoc pieGetOintIndexField(String JavaDoc fn) throws PExceptionTyping {
518         if (!INDEX_FIELD_NAME.equals(fn))
519             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
520         return (Integer JavaDoc) index;
521     }
522
523     public String JavaDoc pieGetStringIndexField(String JavaDoc fn) throws PExceptionTyping {
524         if (!INDEX_FIELD_NAME.equals(fn))
525             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
526         return (String JavaDoc) index;
527     }
528
529     public Date JavaDoc pieGetDateIndexField(String JavaDoc fn) throws PExceptionTyping {
530         if (!INDEX_FIELD_NAME.equals(fn))
531             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
532         return (Date JavaDoc) index;
533     }
534
535     public byte pieGetByteIndexField(String JavaDoc fn) throws PExceptionTyping {
536         if (!INDEX_FIELD_NAME.equals(fn))
537             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
538         return ((Byte JavaDoc) index).byteValue();
539     }
540
541     public Byte JavaDoc pieGetObyteIndexField(String JavaDoc fn) throws PExceptionTyping {
542         if (!INDEX_FIELD_NAME.equals(fn))
543             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
544         return (Byte JavaDoc) index;
545     }
546
547     public char pieGetCharIndexField(String JavaDoc fn) throws PExceptionTyping {
548         if (!INDEX_FIELD_NAME.equals(fn))
549             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
550         return ((Character JavaDoc) index).charValue();
551     }
552
553     public Character JavaDoc pieGetOcharIndexField(String JavaDoc fn) throws PExceptionTyping {
554         if (!INDEX_FIELD_NAME.equals(fn))
555             throw new PExceptionTyping(ERROR_MESSAGE_BAD_FIELD_NAME);
556         return (Character JavaDoc) index;
557     }
558
559     // ---------------------------------------------------------------------------
560
// Other methods
561
// ---------------------------------------------------------------------------
562

563     public void setStatus(byte s) {
564         status = s;
565         if (status == PIndexedElem.ELEM_DELETED) {
566             //One of the reason to remove an element from the collection, is
567
//that the element is deleted. So when the element is deleted, it
568
// is not no more bound to a PName. Then here we decide to fetch the
569
// PName in advance but when it is possible.
570
unSwizzle();
571         }
572     }
573
574     public boolean equals(Object JavaDoc o) {
575         if (!(o instanceof CollectionElem)) {
576             return false;
577         }
578         Object JavaDoc _index = ((CollectionElem) o).getIndex();
579         if ((index == null && _index != null) ||
580                 (index != null && !index.equals(_index))) {
581             return false;
582         }
583         //Store the element value in a local variable in order to avoid
584
// syncrhonization
585
Object JavaDoc _element = element;
586         //Fetch the real element without causing the loading of the persistent
587
//instance: do not use the getElement() methods, but direct access to
588
// the field.
589
Object JavaDoc ceElem = ((CollectionElem) o).element;
590         if (_element == null) {
591             return ceElem == null;
592         }
593         if (ceElem == null) {
594             return false;
595         }
596         if (_element instanceof PName) {
597             if (ceElem instanceof PName) {
598                 return _element.equals(ceElem);
599             } else if (ceElem instanceof SpeedoProxy) {
600                 return _element.equals(((SpeedoProxy) ceElem).getPName());
601             } else {
602                 return false;
603             }
604         } else if (_element instanceof SpeedoProxy) {
605             if (ceElem instanceof PName) {
606                 Object JavaDoc pn = ((SpeedoProxy) _element).getPName();
607                 if (pn == null) {
608                     Logger l = gca.getLogger();
609                     if (l != null) {
610                         l.log(BasicLevel.WARN,
611                                 "Internal error: a collection/set/map elemnent is no more persistent and it is impossible to compare it: ",
612                                 new Exception JavaDoc());
613                     }
614                     return false;
615                 } else {
616                     return pn.equals(ceElem);
617                 }
618             } else if (ceElem instanceof SpeedoProxy) {
619                 return _element == ceElem;
620             } else {
621                 return false;
622             }
623         } else {
624             return _element.equals(ceElem);
625         }
626     }
627 }
628
Popular Tags