KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > type > api > PType


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.type.api;
25
26 import java.io.Serializable JavaDoc;
27
28 /**
29  * The <b>PType</b> class defines the minimal information required to define
30  * a JORM type that belongs to a PTypeSpace. It should be derived for PType
31  * representing JORM classes. This derivation should be different between
32  * classes and generic classes.
33  * @author P. D?chamboux
34  */

35 public class PType implements Serializable JavaDoc {
36     public final static int NOSIZE = -1;
37     public final static short TYPECODE_BOOLEAN = 0;
38     public final static short TYPECODE_CHAR = 1;
39     public final static short TYPECODE_BYTE = 2;
40     public final static short TYPECODE_SHORT = 3;
41     public final static short TYPECODE_INT = 4;
42     public final static short TYPECODE_LONG = 5;
43     public final static short TYPECODE_FLOAT = 6;
44     public final static short TYPECODE_DOUBLE = 7;
45     public final static short TYPECODE_OBJBOOLEAN = 8;
46     public final static short TYPECODE_OBJCHAR = 9;
47     public final static short TYPECODE_OBJBYTE = 10;
48     public final static short TYPECODE_OBJSHORT = 11;
49     public final static short TYPECODE_OBJINT = 12;
50     public final static short TYPECODE_OBJLONG = 13;
51     public final static short TYPECODE_OBJFLOAT = 14;
52     public final static short TYPECODE_OBJDOUBLE = 15;
53     public final static short TYPECODE_STRING = 16;
54     public final static short TYPECODE_DATE = 17;
55     public final static short TYPECODE_CHARARRAY = 18;
56     public final static short TYPECODE_BYTEARRAY = 19;
57     public final static short TYPECODE_SERIALIZED = 20;
58     public final static short TYPECODE_BIGINTEGER = 21;
59     public final static short TYPECODE_BIGDECIMAL = 22;
60     public final static short TYPECODE_REFERENCE = 23;
61
62     // Each PType has a type code
63
private short typeCode;
64     // Each PType has a JORM name
65
private String JavaDoc jormName;
66     // Each PType has an associated java type
67
private String JavaDoc javaName;
68     // The type-related name used in coding functions
69
private String JavaDoc codingName;
70     // Each PType has an associated constant name for prog. access
71
private String JavaDoc progConstName;
72     // Each PType representing a JORM class belongs to a PTypeSpace
73
private PTypeSpace typeSpace;
74
75     /**
76      * It constructs the types that represent basic JORM types defined as
77      * constants within the PTypeSpace interface.
78      * @param typecode The type code as defined within PType.
79      * @param jormname The JORM name of this basic type.
80      * @param javaname The Java associated with this basic type.
81      * @param constname The constant name for prog. access.
82      */

83     protected PType(short typecode, String JavaDoc jormname, String JavaDoc javaname,
84                     String JavaDoc constname, String JavaDoc codingname) {
85         typeCode = typecode;
86         jormName = jormname;
87         javaName = javaname;
88         codingName = codingname;
89         progConstName = constname;
90         typeSpace = null;
91     }
92
93     /**
94      * It constructs the types that represent JORM classes, be it generic or
95      * not.
96      * @param classname The name of the class for which a PType must be
97      * constructed.
98      * @param typespace The type space to which the constructed PType belongs.
99      */

100     protected PType(String JavaDoc classname, PTypeSpace typespace) {
101         typeCode = TYPECODE_REFERENCE;
102         jormName = classname;
103         javaName = "java.lang.Object";
104         typeSpace = typespace;
105     }
106
107     /**
108      * It compares if the current PType conforms to the given PType. In case
109      * of PType representing JORM classes, the comparison is delegated to the
110      * associated PTypeSpace.
111      * @param pt The PType to be compared with.
112      * @return It returns true if this PType conforms to the one passed as
113      * parameter.
114      */

115     public boolean isa(PType pt) {
116         switch (this.typeCode) {
117         case TYPECODE_BOOLEAN:
118             return pt.typeCode == TYPECODE_BOOLEAN;
119         case TYPECODE_CHAR:
120             switch (pt.typeCode) {
121             case TYPECODE_CHAR:
122             case TYPECODE_INT:
123             case TYPECODE_LONG:
124             case TYPECODE_FLOAT:
125             case TYPECODE_DOUBLE:
126                 return true;
127             default :
128                 return false;
129             }
130         case TYPECODE_BYTE:
131             switch (pt.typeCode) {
132             case TYPECODE_BYTE:
133             case TYPECODE_SHORT:
134             case TYPECODE_INT:
135             case TYPECODE_LONG:
136             case TYPECODE_FLOAT:
137             case TYPECODE_DOUBLE:
138                 return true;
139             default :
140                 return false;
141             }
142         case TYPECODE_SHORT:
143             switch (pt.typeCode) {
144             case TYPECODE_SHORT:
145             case TYPECODE_INT:
146             case TYPECODE_LONG:
147             case TYPECODE_FLOAT:
148             case TYPECODE_DOUBLE:
149                 return true;
150             default :
151                 return false;
152             }
153         case TYPECODE_INT:
154             switch (pt.typeCode) {
155             case TYPECODE_INT:
156             case TYPECODE_LONG:
157             case TYPECODE_FLOAT:
158             case TYPECODE_DOUBLE:
159                 return true;
160             default :
161                 return false;
162             }
163         case TYPECODE_LONG:
164             switch (pt.typeCode) {
165             case TYPECODE_LONG:
166             case TYPECODE_FLOAT:
167             case TYPECODE_DOUBLE:
168                 return true;
169             default :
170                 return false;
171             }
172         case TYPECODE_FLOAT:
173             switch (pt.typeCode) {
174             case TYPECODE_FLOAT:
175             case TYPECODE_DOUBLE:
176                 return true;
177             default :
178                 return false;
179             }
180         case TYPECODE_DOUBLE:
181             return pt.typeCode == TYPECODE_DOUBLE;
182         case TYPECODE_OBJBOOLEAN:
183             return (pt.typeCode == TYPECODE_OBJBOOLEAN)
184                     || (pt.typeCode == TYPECODE_SERIALIZED);
185         case TYPECODE_OBJCHAR:
186             switch (pt.typeCode) {
187             case TYPECODE_OBJCHAR:
188             case TYPECODE_OBJINT:
189             case TYPECODE_OBJLONG:
190             case TYPECODE_OBJFLOAT:
191             case TYPECODE_OBJDOUBLE:
192             case TYPECODE_SERIALIZED:
193                 return true;
194             default :
195                 return false;
196             }
197         case TYPECODE_OBJBYTE:
198             switch (pt.typeCode) {
199             case TYPECODE_OBJBYTE:
200             case TYPECODE_OBJSHORT:
201             case TYPECODE_OBJINT:
202             case TYPECODE_OBJLONG:
203             case TYPECODE_OBJFLOAT:
204             case TYPECODE_OBJDOUBLE:
205             case TYPECODE_SERIALIZED:
206                 return true;
207             default :
208                 return false;
209             }
210         case TYPECODE_OBJSHORT:
211             switch (pt.typeCode) {
212             case TYPECODE_OBJSHORT:
213             case TYPECODE_OBJINT:
214             case TYPECODE_OBJLONG:
215             case TYPECODE_OBJFLOAT:
216             case TYPECODE_OBJDOUBLE:
217             case TYPECODE_SERIALIZED:
218                 return true;
219             default :
220                 return false;
221             }
222         case TYPECODE_OBJINT:
223             switch (pt.typeCode) {
224             case TYPECODE_OBJINT:
225             case TYPECODE_OBJLONG:
226             case TYPECODE_OBJFLOAT:
227             case TYPECODE_OBJDOUBLE:
228             case TYPECODE_SERIALIZED:
229                 return true;
230             default :
231                 return false;
232             }
233         case TYPECODE_OBJLONG:
234             switch (pt.typeCode) {
235             case TYPECODE_OBJLONG:
236             case TYPECODE_OBJFLOAT:
237             case TYPECODE_OBJDOUBLE:
238             case TYPECODE_SERIALIZED:
239                 return true;
240             default :
241                 return false;
242             }
243         case TYPECODE_OBJFLOAT:
244             switch (pt.typeCode) {
245             case TYPECODE_OBJFLOAT:
246             case TYPECODE_OBJDOUBLE:
247             case TYPECODE_SERIALIZED:
248                 return true;
249             default :
250                 return false;
251             }
252         case TYPECODE_OBJDOUBLE:
253             return (pt.typeCode == TYPECODE_OBJDOUBLE)
254                     || (pt.typeCode == TYPECODE_SERIALIZED);
255         case TYPECODE_STRING:
256             return (pt.typeCode == TYPECODE_STRING)
257                     || (pt.typeCode == TYPECODE_SERIALIZED);
258         case TYPECODE_DATE:
259             return (pt.typeCode == TYPECODE_DATE)
260                     || (pt.typeCode == TYPECODE_SERIALIZED);
261         case TYPECODE_SERIALIZED:
262             return pt.typeCode == TYPECODE_SERIALIZED;
263         case TYPECODE_BIGDECIMAL:
264             return (pt.typeCode == TYPECODE_BIGDECIMAL)
265                     || (pt.typeCode == TYPECODE_SERIALIZED);
266         case TYPECODE_REFERENCE:
267             if(pt.typeCode != TYPECODE_REFERENCE) {
268                 return false;
269             }
270             PTypeSpace pts = typeSpace;
271             if (pts == null) {
272                 pts = pt.getPTypeSpace();
273             }
274             return pts.isa(this, pt);
275         }
276         return false;
277     }
278
279     public boolean equals(Object JavaDoc o) {
280         if (!(o instanceof PType)) {
281             return false;
282         }
283         PType p = (PType) o;
284         return (p.getTypeCode() == typeCode)
285                 && (typeCode == TYPECODE_REFERENCE
286                 ? jormName.equals(p.getJormName())
287                 : true);
288     }
289
290     /**
291      * It retrieves the PType directly inherited by a PType representing a
292      * JORM class.
293      * @return The array of directly inherited PType.
294      */

295     public PType[] getInheritedPType() {
296         return null;
297     }
298
299     /**
300      * It retrieves the JORM name associated to this PType.
301      * @return It returns the JORM name.
302      */

303     public String JavaDoc getJormName() {
304         return jormName;
305     }
306
307     /**
308      * It retrieves the Java name associated to this PType.
309      * @return It returns the Java name.
310      */

311     public String JavaDoc getJavaName() {
312         return javaName;
313     }
314
315     /**
316      * It retrieves the coding name associated to this PType.
317      * @return It returns the coding name.
318      */

319     public String JavaDoc getCodingName() {
320         return codingName;
321     }
322
323     /**
324      * It retrieves the name of the associated constant for programming access.
325      * @return It returns the Java name.
326      */

327     public String JavaDoc getProgName() {
328         return progConstName;
329     }
330
331     /**
332      * It retrieves the nested PType in case of a generic class PType.
333      * @return The nested PType.
334      */

335     public PType getNestedPType() {
336         return null;
337     }
338
339     /**
340      * It retrieves the type space associated to this PType.
341      * @return The associated type space.
342      */

343     public PTypeSpace getPTypeSpace() {
344         return typeSpace;
345     }
346
347     /**
348      * Retrieves all subtypes of this PType defined within its PTypeSpace.
349      * @return The array of PType containing all these subtypes.
350      */

351     public PType[] getSubTypes() {
352         return typeSpace.getSubTypes(this);
353     }
354
355     /**
356      * Computes the array of direct sub type
357      * @return
358      */

359     public PType[] getDirectSubTypes() {
360         PType[] subtypes = typeSpace.getSubTypes(this);
361         if (subtypes.length == 0) {
362             return subtypes;
363         }
364         int nbOfDirectSubType = 0;
365         for(int i = 0; i<subtypes.length; i++) {
366             if (!subtypes[i].isDirectSubType(this)) {
367                 subtypes[i] = null;
368             } else {
369                 nbOfDirectSubType ++;
370             }
371         }
372         if (nbOfDirectSubType == 0) {
373             return new PType[0];
374         } else if (nbOfDirectSubType == subtypes.length) {
375             return subtypes;
376         } else {
377             PType[] result = new PType[nbOfDirectSubType];
378             for(int i = 0; i<subtypes.length; i++) {
379                 if (subtypes[i] != null) {
380                     result[--nbOfDirectSubType] = subtypes[i];
381                 }
382             }
383             return result;
384         }
385     }
386
387     public boolean isDirectSubType(PType parent) {
388         return isa(parent);
389     }
390
391     /**
392      * It retrieves the code associated to this PType.
393      * @return It returns the Java name.
394      */

395     public int getTypeCode() {
396         return typeCode;
397     }
398 }
399
Popular Tags