KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > facility > naming > basidir > BasidName


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.facility.naming.basidir;
25
26 import org.objectweb.jorm.metainfo.api.Class;
27 import org.objectweb.jorm.naming.api.PName;
28 import org.objectweb.jorm.naming.api.PNamingContext;
29 import org.objectweb.jorm.naming.api.PExceptionNaming;
30 import org.objectweb.jorm.naming.api.PNameGetter;
31 import org.objectweb.jorm.naming.api.PNameCoder;
32 import org.objectweb.jorm.naming.api.PNameManager;
33 import org.objectweb.jorm.naming.api.PolymorphicPName;
34 import org.objectweb.jorm.naming.api.PolymorphicPNamingContext;
35 import org.objectweb.jorm.naming.lib.BasicPolymorphicPName;
36 import org.objectweb.jorm.naming.lib.BasicPName;
37 import org.objectweb.jorm.api.PException;
38
39 import java.util.Date JavaDoc;
40 import java.text.DateFormat JavaDoc;
41 import java.math.BigInteger JavaDoc;
42 import java.math.BigDecimal JavaDoc;
43
44 /**
45  *
46  * @author Sebastien Chassande-Barrioz
47  */

48 public class BasidName extends BasicPolymorphicPName implements PNameGetter {
49     protected Object JavaDoc o = null;
50     protected long p = 0;
51
52     public BasidName(Object JavaDoc o, BasidBinder binder) throws PExceptionNaming {
53         this(o, binder, binder.codingType);
54     }
55     
56     public BasidName(Object JavaDoc o, PNameManager binder, int codingType) throws PExceptionNaming {
57         this.pnc = binder;
58         if (o instanceof BasidName) {
59             this.o = ((BasidName) o).o;
60             this.p = ((BasidName) o).p;
61
62         } else if (o instanceof PName) {
63             switch (codingType) {
64             case PNamingContext.CTBYTE:
65                 p = ((PName) o).encodeByte();
66                 break;
67             case PNamingContext.CTOBYTE:
68                 o = ((PName) o).encodeObyte();
69                 break;
70             case PNamingContext.CTCHAR:
71                 p = ((PName) o).encodeChar();
72                 break;
73             case PNamingContext.CTOCHAR:
74                 o = ((PName) o).encodeOchar();
75                 break;
76             case PNamingContext.CTINT:
77                 p = ((PName) o).encodeInt();
78                 break;
79             case PNamingContext.CTOINT:
80                 o = ((PName) o).encodeOint();
81                 break;
82             case PNamingContext.CTLONG:
83                 p = ((PName) o).encodeLong();
84                 break;
85             case PNamingContext.CTOLONG:
86                 o = ((PName) o).encodeOlong();
87                 break;
88             case PNamingContext.CTSHORT:
89                 p = ((PName) o).encodeShort();
90                 break;
91             case PNamingContext.CTOSHORT:
92                 o = ((PName) o).encodeOshort();
93                 break;
94             case PNamingContext.CTDATE:
95                 o = ((PName) o).encodeDate();
96                 break;
97             case PNamingContext.CTSTRING:
98                 o = ((PName) o).encodeString();
99                 break;
100             case PNamingContext.CTBYTEARRAY:
101                 o = ((PName) o).encode();
102                 break;
103             case PNamingContext.CTCHARARRAY:
104                 o = ((PName) o).encodeCharArray();
105                 break;
106             default:
107                 throw new PExceptionNaming(
108                         "BasidName does not support this coding type");
109             }
110
111         } else if (o instanceof PNameGetter) {
112             try {
113                 switch (codingType) {
114                 case PNameCoder.CTBYTE:
115                     p = ((PNameGetter) o).pngetByteField(null, null);
116                     break;
117                 case PNameCoder.CTOBYTE:
118                     o = ((PNameGetter) o).pngetObyteField(null, null);
119                     break;
120                 case PNameCoder.CTCHAR:
121                     p = ((PNameGetter) o).pngetCharField(null, null);
122                     break;
123                 case PNameCoder.CTOCHAR:
124                     o = ((PNameGetter) o).pngetOcharField(null, null);
125                     break;
126                 case PNameCoder.CTINT:
127                     p = ((PNameGetter) o).pngetIntField(null, null);
128                     break;
129                 case PNameCoder.CTOINT:
130                     o = ((PNameGetter) o).pngetOintField(null, null);
131                     break;
132                 case PNameCoder.CTLONG:
133                     p = ((PNameGetter) o).pngetLongField(null, null);
134                     break;
135                 case PNameCoder.CTOLONG:
136                     o = ((PNameGetter) o).pngetOlongField(null, null);
137                     break;
138                 case PNameCoder.CTSHORT:
139                     p = ((PNameGetter) o).pngetShortField(null, null);
140                     break;
141                 case PNameCoder.CTOSHORT:
142                     o = ((PNameGetter) o).pngetOshortField(null, null);
143                     break;
144                 case PNameCoder.CTDATE:
145                     o = ((PNameGetter) o).pngetDateField(null, null);
146                     break;
147                 case PNameCoder.CTSTRING:
148                     o = ((PNameGetter) o).pngetStringField(null, null);
149                     break;
150                 case PNameCoder.CTBYTEARRAY:
151                     o = ((PNameGetter) o).pngetByteArrayField(null, null);
152                     break;
153                 case PNameCoder.CTCHARARRAY:
154                     o = ((PNameGetter) o).pngetCharArrayField(null, null);
155                     break;
156                 default:
157                     throw new PExceptionNaming(
158                             "BasidName does not support this coding type");
159                 }
160             } catch (PExceptionNaming e) {
161                 throw e;
162             } catch (PException e) {
163                 throw new PExceptionNaming(e);
164             }
165         } else {
166             switch (codingType) {
167             case PNamingContext.CTBYTE:
168                 if (o instanceof Byte JavaDoc) {
169                     this.p = ((Byte JavaDoc) o).byteValue();
170                 } else if (o instanceof String JavaDoc) {
171                     this.p = Byte.parseByte((String JavaDoc) o);
172                 } else if (o instanceof Short JavaDoc) {
173                     this.p = ((Short JavaDoc) o).byteValue();
174                 } else if (o instanceof Integer JavaDoc) {
175                     this.p = ((Integer JavaDoc) o).byteValue();
176                 } else if (o instanceof Long JavaDoc) {
177                     this.p = ((Long JavaDoc) o).byteValue();
178                 } else {
179                     throw new PExceptionNaming("Cannot initalize BasidName with null value (Byte object value expected)");
180                 }
181                 break;
182             case PNamingContext.CTCHAR:
183                 if (o instanceof Character JavaDoc) {
184                     this.p = ((Character JavaDoc) o).charValue();
185                 } else if (o instanceof String JavaDoc) {
186                     this.p = ((String JavaDoc) o).charAt(0);
187                 } else {
188                     throw new PExceptionNaming("Cannot initalize BasidName with null value (Character object value expected)");
189                 }
190                 break;
191             case PNamingContext.CTSHORT:
192                 if (o instanceof Byte JavaDoc) {
193                     this.p = ((Byte JavaDoc) o).byteValue();
194                 } else if (o instanceof String JavaDoc) {
195                     this.p = Short.parseShort((String JavaDoc) o);
196                 } else if (o instanceof Short JavaDoc) {
197                     this.p = ((Short JavaDoc) o).shortValue();
198                 } else if (o instanceof Integer JavaDoc) {
199                     this.p = ((Integer JavaDoc) o).shortValue();
200                 } else if (o instanceof Long JavaDoc) {
201                     this.p = ((Long JavaDoc) o).shortValue();
202                 } else {
203                     throw new PExceptionNaming("Cannot initalize BasidName with null value (Short object value expected)");
204                 }
205                 break;
206             case PNamingContext.CTINT:
207                 if (o instanceof Byte JavaDoc) {
208                     this.p = ((Byte JavaDoc) o).byteValue();
209                 } else if (o instanceof String JavaDoc) {
210                     this.p = Integer.parseInt((String JavaDoc) o);
211                 } else if (o instanceof Short JavaDoc) {
212                     this.p = ((Short JavaDoc) o).shortValue();
213                 } else if (o instanceof Integer JavaDoc) {
214                     this.p = ((Integer JavaDoc) o).intValue();
215                 } else if (o instanceof Long JavaDoc) {
216                     this.p = ((Long JavaDoc) o).intValue();
217                 } else {
218                     throw new PExceptionNaming("Cannot initalize BasidName with null value (Integer object value expected)");
219                 }
220                 break;
221             case PNamingContext.CTLONG:
222                 if (o instanceof Byte JavaDoc) {
223                     this.p = ((Byte JavaDoc) o).byteValue();
224                 } else if (o instanceof String JavaDoc) {
225                     this.p = Long.parseLong((String JavaDoc) o);
226                 } else if (o instanceof Short JavaDoc) {
227                     this.p = ((Short JavaDoc) o).shortValue();
228                 } else if (o instanceof Integer JavaDoc) {
229                     this.p = ((Integer JavaDoc) o).intValue();
230                 } else if (o instanceof Long JavaDoc) {
231                     this.p = ((Long JavaDoc) o).longValue();
232                 } else {
233                     throw new PExceptionNaming("Cannot initalize BasidName with null value (Long object value expected)");
234                 }
235                 break;
236             case PNamingContext.CTOCHAR:
237                 if (o == null || o instanceof Character JavaDoc) {
238                     this.o = o;
239                 } else if (o instanceof String JavaDoc) {
240                     this.o = new Character JavaDoc(((String JavaDoc) o).charAt(0));
241                 } else {
242                     throw new PExceptionNaming("Cannot initalize BasidName with null value (Character object value expected)");
243                 }
244                 break;
245             case PNamingContext.CTOBYTE:
246                 if (o == null || o instanceof Byte JavaDoc) {
247                     this.o = o;
248                 } else if (o instanceof String JavaDoc) {
249                     this.o = Byte.decode((String JavaDoc) o);
250                 } else if (o instanceof Short JavaDoc) {
251                     this.o = new Byte JavaDoc(((Short JavaDoc) o).byteValue());
252                 } else if (o instanceof Integer JavaDoc) {
253                     this.o = new Byte JavaDoc(((Integer JavaDoc) o).byteValue());
254                 } else if (o instanceof Long JavaDoc) {
255                     this.o = new Byte JavaDoc(((Long JavaDoc) o).byteValue());
256                 } else {
257                     throw new PExceptionNaming("Cannot initalize BasidName with null value (Byte object value expected)");
258                 }
259                 break;
260             case PNamingContext.CTOSHORT:
261                 if (o == null || o instanceof Short JavaDoc) {
262                     this.o = o;
263                 } else if (o instanceof String JavaDoc) {
264                     this.o = Short.decode((String JavaDoc) o);
265                 } else if (o instanceof Byte JavaDoc) {
266                     this.o = new Short JavaDoc(((Byte JavaDoc) o).byteValue());
267                 } else if (o instanceof Integer JavaDoc) {
268                     this.o = new Short JavaDoc(((Integer JavaDoc) o).shortValue());
269                 } else if (o instanceof Long JavaDoc) {
270                     this.o = new Short JavaDoc(((Long JavaDoc) o).shortValue());
271                 } else {
272                     throw new PExceptionNaming("Cannot initalize BasidName with null value (Byte object value expected)");
273                 }
274                 break;
275             case PNamingContext.CTOINT:
276                 if (o == null || o instanceof Integer JavaDoc) {
277                     this.o = o;
278                 } else if (o instanceof String JavaDoc) {
279                     this.o = Integer.decode((String JavaDoc) o);
280                 } else if (o instanceof Byte JavaDoc) {
281                     this.o = new Integer JavaDoc(((Byte JavaDoc) o).byteValue());
282                 } else if (o instanceof Short JavaDoc) {
283                     this.o = new Integer JavaDoc(((Short JavaDoc) o).shortValue());
284                 } else if (o instanceof Long JavaDoc) {
285                     this.o = new Integer JavaDoc(((Long JavaDoc) o).intValue());
286                 } else {
287                     throw new PExceptionNaming("Cannot initalize BasidName with null value (Integer object value expected)");
288                 }
289                 break;
290             case PNamingContext.CTOLONG:
291                 if (o == null || o instanceof Long JavaDoc) {
292                     this.o = o;
293                 } else if (o instanceof String JavaDoc) {
294                     this.o = Long.decode((String JavaDoc) o);
295                 } else if (o instanceof Byte JavaDoc) {
296                     this.o = new Long JavaDoc(((Byte JavaDoc) o).byteValue());
297                 } else if (o instanceof Short JavaDoc) {
298                     this.o = new Long JavaDoc(((Short JavaDoc) o).shortValue());
299                 } else if (o instanceof Integer JavaDoc) {
300                     this.o = new Long JavaDoc(((Integer JavaDoc) o).intValue());
301                 } else {
302                     throw new PExceptionNaming("Cannot initalize BasidName with null value (Long object value expected)");
303                 }
304                 break;
305             case PNamingContext.CTDATE:
306                 if (o == null || o instanceof Date JavaDoc) {
307                     this.o = o;
308                 } else if (o instanceof Long JavaDoc) {
309                     this.o = new Date JavaDoc(((Long JavaDoc) o).longValue());
310                 } else if (o instanceof String JavaDoc) {
311                     try {
312                         this.o = new Date JavaDoc(Long.decode((String JavaDoc) o).longValue());
313                     } catch (Exception JavaDoc e) {
314                         try {
315                             this.o = DateFormat.getInstance().parse((String JavaDoc) o);
316                         } catch (Exception JavaDoc e2) {
317                             throw new PExceptionNaming(e2, "Cannot initalize BasidName with null value (Date object value expected)");
318                         }
319                     }
320                 } else {
321                     throw new PExceptionNaming("Cannot initalize BasidName with null value (Date object value expected)");
322                 }
323             default:
324                 this.o = o;
325             }
326         }
327     }
328
329     public BasidName(long p, BasidBinder binder) {
330         this.pnc = binder;
331         this.p = p;
332     }
333
334     // IMPLEMENTATION OF METHODS OF THE Object INTERFACE
335

336     public boolean equals(Object JavaDoc obj) {
337         if (obj instanceof BasidName) {
338             BasidName pn = (BasidName) obj;
339             PolymorphicPName polymThis = (PolymorphicPName) this;
340             PolymorphicPName polymObj = (PolymorphicPName) pn;
341             //this or obj can be polymorphic
342
if (polymThis.isPolymorphic() || polymObj.isPolymorphic()) {
343                 return (o != null ? o.equals(pn.o) : p == pn.p);
344             }
345         }
346         return (this == obj)
347                 || ((obj instanceof BasidName)
348                 && pnc == ((BasidName) obj).pnc
349                 && (o != null
350                 ? o.equals(((BasidName) obj).o)
351                 : p == ((BasidName) obj).p));
352     }
353
354     public int hashCode() {
355         if (o != null)
356             return o.hashCode();
357         return (int) p;
358     }
359
360     public String JavaDoc toString() {
361         BasidBinder basidBinder = null;
362         if (pnc instanceof PolymorphicPNamingContext) {
363             PolymorphicPNamingContext polymophicPNC = (PolymorphicPNamingContext) pnc;
364             
365             basidBinder = (BasidBinder) polymophicPNC.getDelegatedBinder();
366         } else {
367             basidBinder = (BasidBinder) pnc;
368         }
369         switch (basidBinder.codingType) {
370         case PNamingContext.CTBYTE:
371             return super.toString() + " / type=byte / value=" + p;
372         case PNamingContext.CTCHAR:
373             return super.toString() + " / type=char / value=" + (char) p;
374         case PNamingContext.CTINT:
375             return super.toString() + " / type=int / value=" + p;
376         case PNamingContext.CTLONG:
377             return super.toString() + " / type=long / value=" + p;
378         case PNamingContext.CTSHORT:
379             return super.toString() + " / type=short / value=" + p;
380         default:
381             return super.toString() + " / type=" + basidBinder.codingType
382                     + " / value=" + o;
383         }
384     }
385
386     // IMPLEMENTATION OF METHODS OF THE PName INTERFACE
387

388     public boolean isNull() {
389         BasidBinder basidBinder = null;
390         if (pnc instanceof PolymorphicPNamingContext) {
391             PolymorphicPNamingContext polymophicPNC = (PolymorphicPNamingContext) pnc;
392             
393             basidBinder = (BasidBinder) polymophicPNC.getDelegatedBinder();
394         } else {
395             basidBinder = (BasidBinder) pnc;
396         }
397         switch (basidBinder.codingType) {
398         case PNamingContext.CTBYTE:
399         case PNamingContext.CTCHAR:
400         case PNamingContext.CTINT:
401         case PNamingContext.CTLONG:
402         case PNamingContext.CTSHORT:
403             return p == basidBinder.nullValue;
404         default:
405             return o == basidBinder.nullOValue;
406         }
407     }
408
409     public byte pngetByteField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
410         return (byte) p;
411     }
412
413     public Byte JavaDoc pngetObyteField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
414         return (Byte JavaDoc) o;
415     }
416
417     public char pngetCharField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
418         return (char) p;
419     }
420
421     public Character JavaDoc pngetOcharField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
422         return (Character JavaDoc) o;
423     }
424
425     public short pngetShortField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
426         return (short) p;
427     }
428
429     public Short JavaDoc pngetOshortField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
430         return (Short JavaDoc) o;
431     }
432
433     public int pngetIntField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
434         return (int) p;
435     }
436
437     public Integer JavaDoc pngetOintField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
438         return (Integer JavaDoc) o;
439     }
440
441     public long pngetLongField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
442         return p;
443     }
444
445     public Long JavaDoc pngetOlongField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
446         return (Long JavaDoc) o;
447     }
448
449     public String JavaDoc pngetStringField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
450         return (String JavaDoc) o;
451     }
452
453     public byte[] pngetByteArrayField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
454         return (byte[]) o;
455     }
456
457     public char[] pngetCharArrayField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
458         return (char[]) o;
459     }
460
461     public Date JavaDoc pngetDateField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
462         return (Date JavaDoc) o;
463     }
464
465     public BigInteger JavaDoc pngetBigIntegerField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
466         return (BigInteger JavaDoc) o;
467     }
468
469     public BigDecimal JavaDoc pngetBigDecimalField(String JavaDoc fn, Object JavaDoc ctxt) throws PException {
470         return (BigDecimal JavaDoc) o;
471     }
472 }
473
Popular Tags