KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > utility > JavaTypeHelper


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 in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
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 Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * JavaTypeHelper.java
26  *
27  * Created on January 22, 2002, 3:39 PM
28  */

29
30 package com.sun.jdo.spi.persistence.utility;
31
32 import java.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34
35 /** This is a helper class which provides some basic java type convenience
36  * methods: extraction of a package from a fully qualified class name,
37  * extraction of a short (non-qualified) name from a fully qualified class
38  * name, and various wrapper and primitive type methods.
39  *
40  * @author Rochelle Raccah
41  */

42 public class JavaTypeHelper
43 {
44     /** Map of primitive to wrapper classes */
45     private final static Map JavaDoc _primitiveToWrappers;
46
47     /** Map of primitive names to primitive classes */
48     private final static Map JavaDoc _primitiveNamesToPrimitives;
49
50     /** Map of primitive names to wrapper names */
51     private final static Map JavaDoc _primitiveNamesToWrapperNames;
52
53     /** Map of wrapper classes to primitive names*/
54     private final static Map JavaDoc _wrapperToPrimitiveNames;
55
56     static
57     {
58         _primitiveToWrappers = new HashMap JavaDoc(9);
59         _primitiveToWrappers.put(Boolean.TYPE, Boolean JavaDoc.class);
60         _primitiveToWrappers.put(Byte.TYPE, Byte JavaDoc.class);
61         _primitiveToWrappers.put(Character.TYPE, Character JavaDoc.class);
62         _primitiveToWrappers.put(Double.TYPE, Double JavaDoc.class);
63         _primitiveToWrappers.put(Float.TYPE, Float JavaDoc.class);
64         _primitiveToWrappers.put(Integer.TYPE, Integer JavaDoc.class);
65         _primitiveToWrappers.put(Long.TYPE, Long JavaDoc.class);
66         _primitiveToWrappers.put(Short.TYPE, Short JavaDoc.class);
67         _primitiveToWrappers.put(Void.TYPE, Void JavaDoc.class);
68
69         _primitiveNamesToPrimitives = new HashMap JavaDoc(9);
70         _primitiveNamesToPrimitives.put("boolean", Boolean.TYPE); // NOI18N
71
_primitiveNamesToPrimitives.put("byte", Byte.TYPE); // NOI18N
72
_primitiveNamesToPrimitives.put("char", Character.TYPE); // NOI18N
73
_primitiveNamesToPrimitives.put("double", Double.TYPE); // NOI18N
74
_primitiveNamesToPrimitives.put("float", Float.TYPE); // NOI18N
75
_primitiveNamesToPrimitives.put("int", Integer.TYPE); // NOI18N
76
_primitiveNamesToPrimitives.put("long", Long.TYPE); // NOI18N
77
_primitiveNamesToPrimitives.put("short", Short.TYPE); // NOI18N
78
_primitiveNamesToPrimitives.put("void", Void.TYPE); // NOI18N
79

80         _primitiveNamesToWrapperNames = new HashMap JavaDoc(9);
81         _primitiveNamesToWrapperNames.put("boolean", "Boolean"); // NOI18N
82
_primitiveNamesToWrapperNames.put("byte", "Byte"); // NOI18N
83
_primitiveNamesToWrapperNames.put("char", "Character"); // NOI18N
84
_primitiveNamesToWrapperNames.put("double", "Double"); // NOI18N
85
_primitiveNamesToWrapperNames.put("float", "Float"); // NOI18N
86
_primitiveNamesToWrapperNames.put("int", "Integer"); // NOI18N
87
_primitiveNamesToWrapperNames.put("long", "Long"); // NOI18N
88
_primitiveNamesToWrapperNames.put("short", "Short"); // NOI18N
89
_primitiveNamesToWrapperNames.put("void", "Void"); // NOI18N
90

91         _wrapperToPrimitiveNames = new HashMap JavaDoc(9);
92         _wrapperToPrimitiveNames.put(Boolean JavaDoc.class, "boolean"); // NOI18N
93
_wrapperToPrimitiveNames.put(Byte JavaDoc.class, "byte"); // NOI18N
94
_wrapperToPrimitiveNames.put(Character JavaDoc.class, "char"); // NOI18N
95
_wrapperToPrimitiveNames.put(Double JavaDoc.class, "double"); // NOI18N
96
_wrapperToPrimitiveNames.put(Float JavaDoc.class, "float"); // NOI18N
97
_wrapperToPrimitiveNames.put(Integer JavaDoc.class, "int"); // NOI18N
98
_wrapperToPrimitiveNames.put(Long JavaDoc.class, "long"); // NOI18N
99
_wrapperToPrimitiveNames.put(Short JavaDoc.class, "short"); // NOI18N
100
_wrapperToPrimitiveNames.put(Void JavaDoc.class, "void"); // NOI18N
101
}
102
103     /**
104      * Returns the package portion of the specified class
105      * @param className the name of the class from which to extract the
106      * package
107      * @return package portion of the specified class
108      */

109     public static String JavaDoc getPackageName (final String JavaDoc className)
110     {
111         if (className != null)
112         {
113             final int index = className.lastIndexOf('.');
114
115             return ((index != -1) ?
116                 className.substring(0, index) : ""); // NOI18N
117
}
118
119         return null;
120     }
121
122     /**
123      * Returns the name of a class without the package name. For example: if
124      * input = "java.lang.Object" , then output = "Object".
125      * @param fully qualified classname
126      */

127     public static String JavaDoc getShortClassName (final String JavaDoc className)
128     {
129         if (className != null)
130         {
131             final int index = className.lastIndexOf('.');
132
133             return className.substring(index + 1);
134         }
135         return null;
136     }
137
138     // ================= primitive/wrapper class utilities ====================
139

140     /** Returns the wrapper class associated with the supplied primitive class.
141      * @param primitive the primitive class to be used for lookup.
142      * @return the associated wrapper class.
143      */

144     public static Class JavaDoc getWrapperClass (Class JavaDoc primitive)
145     {
146         return (Class JavaDoc)_primitiveToWrappers.get(primitive);
147     }
148
149     /** Returns the primitive class associated with the supplied primitive
150      * type name.
151      * @param primitiveName the name of the primitive to be used for lookup.
152      * @return the associated primitive class.
153      */

154     public static Class JavaDoc getPrimitiveClass (String JavaDoc primitiveName)
155     {
156         return (Class JavaDoc)_primitiveNamesToPrimitives.get(primitiveName);
157     }
158
159     /** Returns the name of the wrapper class associated with the supplied
160      * primitive type name.
161      * @param primitiveName the name of the primitive to be used for lookup.
162      * @return the associated wrapper class name.
163      */

164     public static String JavaDoc getWrapperName (String JavaDoc primitiveName)
165     {
166         return (String JavaDoc)_primitiveNamesToWrapperNames.get(primitiveName);
167     }
168
169     /** Returns the name of the primitive type associated with the supplied
170      * wrapper class.
171      * @param wrapper the wrapper class to be used for lookup.
172      * @return the associated primitive type name.
173      */

174     public static String JavaDoc getPrimitiveName (Class JavaDoc wrapper)
175     {
176         return (String JavaDoc)_wrapperToPrimitiveNames.get(wrapper);
177     }
178
179     /** Returns the Boolean wrapper object for true or false
180      * corresponding to the supplied argument. This is to provide a
181      * convenience method for this conversion but to prevent calling the
182      * Boolean constructor which has been determined to be unnecessary
183      * and a performance problem. JDK 1.4 provides such a method, but
184      * some of our code still works with JDK 1.3.
185      * @param flag the primitive boolean object to be translated to a
186      * Boolean wrapper.
187      * @return the associated true/false shared wrapper object
188      */

189     public static Boolean JavaDoc valueOf (boolean flag)
190     {
191         return (flag ? Boolean.TRUE : Boolean.FALSE);
192     }
193 }
194
Popular Tags