KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > helper > BasicTypeHelperImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.helper;
23
24 import java.math.BigDecimal JavaDoc;
25 import java.math.BigInteger JavaDoc;
26
27 import java.util.Date JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 /**
34  * INTERNAL
35  * This class is a helper class providing type information.
36  * Its implementation uses Java reflection to calculate the type information.
37  */

38 public class BasicTypeHelperImpl {
39
40     /** Set of intergral types and its wrapper classes. */
41     private static Set JavaDoc integralTypes = new HashSet JavaDoc();
42     /** Set of floating point types and its wrapper classes. */
43     private static Set JavaDoc floatingPointTypes = new HashSet JavaDoc();
44     /** Set of date classes. */
45     private static Set JavaDoc dateClasses = new HashSet JavaDoc();
46     /** Maps primtives types to their wrapper classes. */
47     private static Map JavaDoc primitiveToWrapper = new HashMap JavaDoc();
48     /** Maps wrapper classes to their primitive types. */
49     private static Map JavaDoc wrapperToPrimitive = new HashMap JavaDoc();
50
51     static {
52         // Inilialize set of integral types plus their wrapper classes
53
integralTypes.add(byte.class);
54         integralTypes.add(Byte JavaDoc.class);
55         integralTypes.add(short.class);
56         integralTypes.add(Short JavaDoc.class);
57         integralTypes.add(char.class);
58         integralTypes.add(Character JavaDoc.class);
59         integralTypes.add(int.class);
60         integralTypes.add(Integer JavaDoc.class);
61         integralTypes.add(long.class);
62         integralTypes.add(Long JavaDoc.class);
63         
64         // Inilialize set of floating point types plus their wrapper classes
65
floatingPointTypes.add(float.class);
66         floatingPointTypes.add(Float JavaDoc.class);
67         floatingPointTypes.add(double.class);
68         floatingPointTypes.add(Double JavaDoc.class);
69
70         // Inilialize set of floating point types plus their wrapper classes
71
dateClasses.add(java.util.Date JavaDoc.class);
72         dateClasses.add(java.util.Calendar JavaDoc.class);
73         dateClasses.add(java.sql.Date JavaDoc.class);
74         dateClasses.add(java.sql.Time JavaDoc.class);
75         dateClasses.add(java.sql.Timestamp JavaDoc.class);
76
77         // Inilialize mapping primitives to their wrapper classes
78
primitiveToWrapper.put(boolean.class, Boolean JavaDoc.class);
79         primitiveToWrapper.put(byte.class, Byte JavaDoc.class);
80         primitiveToWrapper.put(short.class, Short JavaDoc.class);
81         primitiveToWrapper.put(char.class, Character JavaDoc.class);
82         primitiveToWrapper.put(int.class, Integer JavaDoc.class);
83         primitiveToWrapper.put(long.class, Long JavaDoc.class);
84         primitiveToWrapper.put(float.class, Float JavaDoc.class);
85         primitiveToWrapper.put(double.class, Double JavaDoc.class);
86
87         // Inilialize mapping wrapper classes to their primitives
88
wrapperToPrimitive.put(Boolean JavaDoc.class, boolean.class);
89         wrapperToPrimitive.put(Byte JavaDoc.class, byte.class);
90         wrapperToPrimitive.put(Short JavaDoc.class, short.class);
91         wrapperToPrimitive.put(Character JavaDoc.class, char.class);
92         wrapperToPrimitive.put(Integer JavaDoc.class, int.class);
93         wrapperToPrimitive.put(Long JavaDoc.class, long.class);
94         wrapperToPrimitive.put(Float JavaDoc.class, float.class);
95         wrapperToPrimitive.put(Double JavaDoc.class, double.class);
96     }
97
98     /** A singleton for this class */
99     private static final BasicTypeHelperImpl singleton = new BasicTypeHelperImpl();
100
101     /** Gets instance of this class */
102     public static BasicTypeHelperImpl getInstance() {
103         return singleton;
104     }
105
106     /** Returns the Object type representation.*/
107     public Object JavaDoc getObjectType() {
108         return Object JavaDoc.class;
109     }
110
111     /** Returns the boolean type representation.*/
112     public Object JavaDoc getBooleanType() {
113         return boolean.class;
114     }
115
116     /** Returns the Boolean class representation.*/
117     public Object JavaDoc getBooleanClassType() {
118         return Boolean JavaDoc.class;
119     }
120
121     /** Returns the char type representation.*/
122     public Object JavaDoc getCharType() {
123         return char.class;
124     }
125
126     /** Returns the Character class representation.*/
127     public Object JavaDoc getCharacterClassType() {
128         return Character JavaDoc.class;
129     }
130
131     /** Returns the byte type representation.*/
132     public Object JavaDoc getByteType() {
133         return byte.class;
134     }
135
136     /** Returns the Byte class representation.*/
137     public Object JavaDoc getByteClassType() {
138         return Byte JavaDoc.class;
139     }
140
141     /** Returns the short type representation.*/
142     public Object JavaDoc getShortType() {
143         return short.class;
144     }
145
146     /** Returns the Short class representation.*/
147     public Object JavaDoc getShortClassType() {
148         return Short JavaDoc.class;
149     }
150
151     /** Returns the int type representation.*/
152     public Object JavaDoc getIntType() {
153         return int.class;
154     }
155
156     /** Returns the Inter class representation.*/
157     public Object JavaDoc getIntegerClassType() {
158         return Integer JavaDoc.class;
159     }
160
161     /** Returns the long type representation.*/
162     public Object JavaDoc getLongType() {
163         return long.class;
164     }
165
166     /** Returns the type representation of class Long.*/
167     public Object JavaDoc getLongClassType() {
168         return Long JavaDoc.class;
169     }
170
171     /** Returns the float type representation.*/
172     public Object JavaDoc getFloatType() {
173         return float.class;
174     }
175
176     /** Returns the type representation of class Float.*/
177     public Object JavaDoc getFloatClassType() {
178         return Float JavaDoc.class;
179     }
180
181     /** Returns the double type representation.*/
182     public Object JavaDoc getDoubleType() {
183         return double.class;
184     }
185
186     /** Returns the type representation of class Double.*/
187     public Object JavaDoc getDoubleClassType() {
188         return Double JavaDoc.class;
189     }
190
191     /** Returns the String type representation.*/
192     public Object JavaDoc getStringType() {
193         return String JavaDoc.class;
194     }
195
196     /** Returns the BigInteger type representation.*/
197     public Object JavaDoc getBigIntegerType() {
198         return BigInteger JavaDoc.class;
199     }
200
201     /** Returns the BigDecimal type representation.*/
202     public Object JavaDoc getBigDecimalType() {
203         return BigDecimal JavaDoc.class;
204     }
205
206     /** Returns the java.util.Date type representation.*/
207     public Object JavaDoc getDateType() {
208         return Date JavaDoc.class;
209     }
210
211     /** */
212     public boolean isEnumType(Object JavaDoc type) {
213         Class JavaDoc clazz = (Class JavaDoc)type;
214         return (clazz != null) && (clazz.isEnum());
215     }
216
217     /** */
218     public boolean isNumericType(Object JavaDoc type) {
219         return isIntegralType(type) || isFloatingPointType(type) ||
220             isBigIntegerType(type) || isBigDecimalType(type);
221     }
222
223     /**
224      * Returns true if the specified type represents an
225      * integral type or a wrapper class of an integral type.
226      */

227     public boolean isIntegralType(Object JavaDoc type) {
228         return integralTypes.contains(type);
229     }
230     
231     /**
232      * Returns true if the specified type represents an
233      * floating point type or a wrapper class of an floating point type.
234      */

235     public boolean isFloatingPointType(Object JavaDoc type) {
236         return floatingPointTypes.contains(type);
237     }
238
239     /** Returns true if the specified type is a wrapper class. */
240     public boolean isWrapperClass(Object JavaDoc type) {
241         return wrapperToPrimitive.containsKey(type);
242     }
243
244     /**
245      * Returns true if type is the boolean primitive type or the Boolean wrapper class
246      */

247     public boolean isBooleanType(Object JavaDoc type) {
248         return (type == getBooleanType()) || (type == getBooleanClassType());
249     }
250
251     /**
252      * Returns true if type is the char primitive type or the Character wrapper class
253      */

254     public boolean isCharacterType(Object JavaDoc type) {
255         return (type == getCharType()) || (type == getCharacterClassType());
256     }
257
258     /**
259      * Returns true if type is the byte primitive type or the Byte wrapper class
260      */

261     public boolean isByteType(Object JavaDoc type) {
262         return (type == getByteType()) || (type == getByteClassType());
263     }
264
265     /**
266      * Returns true if type is the short primitive type or the Short wrapper class
267      */

268     public boolean isShortType(Object JavaDoc type) {
269         return (type == getShortType()) || (type == getShortClassType());
270     }
271
272     /**
273      * Returns true if type is the int primitive type or the Integer wrapper class
274      */

275     public boolean isIntType(Object JavaDoc type) {
276         return (type == getIntType()) || (type == getIntegerClassType());
277     }
278
279     /**
280      * Returns true if type is the long primitive type or the Long wrapper class
281      */

282     public boolean isLongType(Object JavaDoc type) {
283         return (type == getLongType()) || (type == getLongClassType());
284     }
285
286     /**
287      * Returns true if type is the float primitive type or the Float wrapper class
288      */

289     public boolean isFloatType(Object JavaDoc type) {
290         return (type == getFloatType()) || (type == getFloatClassType());
291     }
292
293     /**
294      * Returns true if type is the double primitive type or the Double wrapper class
295      */

296     public boolean isDoubleType(Object JavaDoc type) {
297         return (type == getDoubleType()) || (type == getDoubleClassType());
298     }
299
300     /** Returns true if the specified type represents java.lang.String. */
301     public boolean isStringType(Object JavaDoc type) {
302         return type == getStringType();
303     }
304
305     /** */
306     public boolean isDateClass(Object JavaDoc type) {
307         return dateClasses.contains(type);
308     }
309
310     /** */
311     public boolean isBigIntegerType(Object JavaDoc type) {
312         return type == getBigIntegerType();
313     }
314
315     /** */
316     public boolean isBigDecimalType(Object JavaDoc type) {
317         return type == getBigDecimalType();
318     }
319
320     /** Returns true if the specified type denotes an orable type */
321     public boolean isOrderableType(Object JavaDoc type) {
322         return isNumericType(type) || isStringType(type) ||
323             isDateClass(type) || isEnumType(type);
324     }
325
326     /** */
327     public boolean isAssignableFrom(Object JavaDoc left, Object JavaDoc right) {
328         if ((left == null) || (right == null)) {
329             return false;
330         }
331         // chec for identical types
332
if (left == right) {
333             return true;
334         }
335         // numeric types are compatible
336
Object JavaDoc promoted = extendedBinaryNumericPromotion(left, right);
337         if (promoted != null) {
338             return true;
339         }
340         // date types are compatible
341
if (isDateClass(left) && isDateClass(right)) {
342             return true;
343         }
344         // handle boolean and Boolean
345
if (isBooleanType(left) && isBooleanType(right)) {
346             return true;
347         }
348         // check for inheritance and implements
349
return (((Class JavaDoc)left).isAssignableFrom((Class JavaDoc)right));
350     }
351
352     /** Implements binary numeric promotion as defined in JLS extended by
353      * wrapper classes, BigDecimal and BigInteger. */

354     public Object JavaDoc extendedBinaryNumericPromotion(Object JavaDoc left, Object JavaDoc right) {
355         if ((left == null) || (right == null) ||
356             !isNumericType(left) || !isNumericType(right)) {
357             return null;
358         }
359
360         // handle BigDecimal
361
if (isBigDecimalType(left) || isBigDecimalType(right)) {
362             return getBigDecimalType();
363         }
364         
365         // handle BigInteger
366
if (isBigIntegerType(left)) {
367             return isFloatingPointType(right) ? right : getBigIntegerType();
368         }
369         if (isBigIntegerType(right)) {
370             return isFloatingPointType(left) ? left : getBigIntegerType();
371         }
372
373         // check wrapper classes
374
boolean wrapper = false;
375         if (isWrapperClass(left)) {
376             wrapper = true;
377             left = getPrimitiveType(left);
378         }
379         if (isWrapperClass(right)) {
380             wrapper = true;
381             right = getPrimitiveType(right);
382         }
383         
384         Object JavaDoc promoted = binaryNumericPromotion(left, right);
385         if (wrapper && promoted != null) {
386             promoted = getWrapperClass(promoted);
387         }
388         return promoted;
389     }
390     
391     // Helper methods
392

393     /** Returns the primitive for the specified wrapper class. */
394     protected Object JavaDoc getPrimitiveType(Object JavaDoc wrapper) {
395         return wrapperToPrimitive.get(wrapper);
396     }
397     
398     /** Returns the wrapper class for the specified primitive. */
399     protected Object JavaDoc getWrapperClass(Object JavaDoc primitive) {
400         return primitiveToWrapper.get(primitive);
401     }
402
403     /** Implements binary numeric promotion as defined in JLS. */
404     protected Object JavaDoc binaryNumericPromotion(Object JavaDoc left, Object JavaDoc right) {
405         if ((left == null) || (right == null)) {
406             return null;
407         }
408         Object JavaDoc type = null;
409         
410         if (left == getDoubleType() || right == getDoubleType()) {
411             type = getDoubleType();
412         } else if (left == getFloatType() || right == getFloatType()) {
413             type = getFloatType();
414         } else if (left == getLongType() || right == getLongType()) {
415             type = getLongType();
416         } else if (isIntegralType(left) && isIntegralType(right)) {
417             type = getIntType();
418         }
419         return type;
420     }
421
422 }
423
424
Popular Tags