View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  /***
18   * TypeSupport.java
19   *
20   */
21  
22  package org.apache.jdo.impl.model.jdo.util;
23  
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.HashMap;
27  import java.util.HashSet;
28  
29  import org.apache.jdo.model.java.JavaModel;
30  import org.apache.jdo.model.java.JavaType;
31  import org.apache.jdo.model.jdo.JDOModel;
32  
33  /***
34   *
35   */
36  public class TypeSupport
37  {
38      /*** */
39      private final static Set primitiveTypeNames = new HashSet();
40  
41      /*** */
42      private final static Map singleFieldObjectIdClassNames = new HashMap();
43  
44      static 
45      {
46          // initialize set of names of primitive types
47          primitiveTypeNames.add("byte"); // NOI18N
48          primitiveTypeNames.add("short"); // NOI18N
49          primitiveTypeNames.add("int"); // NOI18N
50          primitiveTypeNames.add("long"); // NOI18N
51          primitiveTypeNames.add("char"); // NOI18N
52          primitiveTypeNames.add("float"); // NOI18N
53          primitiveTypeNames.add("double"); // NOI18N
54          primitiveTypeNames.add("boolean"); // NOI18N
55  
56          // initialize map of singleFieldObjectIdClassNames
57          singleFieldObjectIdClassNames.put(
58              "byte", //NOI18N
59              "javax.jdo.identity.ByteIdentity"); //NOI18N
60          singleFieldObjectIdClassNames.put(
61              "java.lang.Byte", //NOI18N
62              "javax.jdo.identity.ByteIdentity"); //NOI18N
63          singleFieldObjectIdClassNames.put(
64              "char", //NOI18N
65              "javax.jdo.identity.CharIdentity"); //NOI18N
66          singleFieldObjectIdClassNames.put(
67              "java.lang.Char", //NOI18N
68              "javax.jdo.identity.CharIdentity"); //NOI18N
69          singleFieldObjectIdClassNames.put(
70              "int", //NOI18N
71              "javax.jdo.identity.IntIdentity"); //NOI18N
72          singleFieldObjectIdClassNames.put(
73              "java.lang.Integer",  //NOI18N
74              "javax.jdo.identity.IntIdentity"); //NOI18N
75          singleFieldObjectIdClassNames.put(
76              "long",  //NOI18N
77              "javax.jdo.identity.LongIdentity"); //NOI18N
78          singleFieldObjectIdClassNames.put(
79              "java.lang.Long",  //NOI18N
80              "javax.jdo.identity.LongIdentity"); //NOI18N
81          singleFieldObjectIdClassNames.put(
82              "short", //NOI18N
83              "javax.jdo.identity.ShortIdentity"); //NOI18N
84          singleFieldObjectIdClassNames.put(
85              "java.lang.Short", //NOI18N
86              "javax.jdo.identity.ShortIdentity"); //NOI18N
87          singleFieldObjectIdClassNames.put(
88              "java.lang.String", //NOI18N
89              "javax.jdo.identity.StringIdentity"); //NOI18N
90      }
91  
92      /*** Fully qualified class name of the ObjectIdentity class. */
93      private static final String OBJECT_IDENTITY_NAME =  
94          "javax.jdo.identity.ObjectIdentity"; //NOI18N
95      
96      /*** 
97       * Returns <code>true</code> if the persistence-modifier of a field
98       * having the specified type defaults to <code>true</code>. 
99       * @param type the type to be checked
100      * @return <code>true</code> if type is a value type; 
101      * <code>false</code> otherwise
102      */
103     public static boolean isPersistenceFieldType(JavaType type)
104     {
105         return type.isValue() ||
106                type.isJDOSupportedCollection() ||
107                type.isJDOSupportedMap() ||
108                type.isPersistenceCapable() ||
109                isPersistenceArrayType(type);
110     }
111      
112     /*** 
113      * Returns <code>true</code> if the embedded-element property of a field 
114      * having the specified type defaults to <code>true</code>.
115      * @param type the type to be checked
116      * @return <code>true</code> if type is a embedded-element type; 
117      * <code>false</code> otherwise
118      */
119     public static boolean isEmbeddedElementType(JavaType type)
120     {
121         return !type.isPersistenceCapable() && !type.isInterface();
122     }
123 
124     /*** 
125      * Returns <code>true</code> if the embedded property of a field having 
126      * the specified type defaults to <code>true</code>.
127      * @param type the type to be checked
128      * @return <code>true</code> if type is a embedded type; 
129      * <code>false</code> otherwise
130      */
131     public static boolean isEmbeddedFieldType(JavaType type)
132     {
133         return type.isValue() ||
134                isPersistenceArrayType(type);
135     }
136 
137     /***
138      * Returns a JavaType representation for the specified type name. 
139      * The method delegates the request to the JavaModel attached to the
140      * specified JDOModel. An unqualified name is qualified using first the 
141      * specified packagePrefix and then "java.lang.", but only if the type
142      * name is the the name of a primitive type. If the method still does
143      * not find a valid type, then it returns <code>null</code>.
144      * @param jdoModel the owning JDOModel
145      * @param typeName the name of the type to be checked
146      * @param packagePrefix the package prefix used to qualify the type name
147      * @return the JavaType representation of the specified type name or
148      * <code>null</code> if it cannot be resolved.
149      */
150     public static JavaType resolveType(JDOModel jdoModel, String typeName, 
151                                        String packagePrefix)
152     {
153         JavaType type = null;
154         JavaModel javaModel = jdoModel.getJavaModel();
155         if (primitiveTypeNames.contains(typeName) ||
156             (typeName.indexOf('.') != -1) || 
157             (packagePrefix == null) || (packagePrefix.length() == 0)) {
158             // Take the typeName as specified, 
159             // if typeName denotes a primitive type or is a qualified name
160             // or if there is no packagePrefix (default package)
161             type = javaModel.getJavaType(typeName);
162         }
163         else {
164             // Not a primitive type and not qualified and packagePrefix
165             // specified => qualify using packagePrefix
166             type = javaModel.getJavaType(packagePrefix + typeName);
167             if (type == null) {
168                 // If type could not be resolved => 
169                 // use java.lang. package prefix as qualifier 
170                 type = javaModel.getJavaType("java.lang." + typeName); //NOI18N
171             }
172         }
173         return type;
174     }
175 
176     /***
177      * Returns <code>true</code> if the specified type represents an array
178      * and its element type is a value type.
179      * @param type the JavaType to be checked
180      * @return <code>true</code> if type is a value array; 
181      * <code>false</code> otherwise.
182      */
183     public static boolean isValueArrayType(JavaType type)
184     {
185         if (type.isArray()) {
186             JavaType elementType = type.getArrayComponentType();
187             return elementType.isValue();
188         }
189         return false;
190     }
191     
192     /*** 
193      * Returns the name of a single field ObjectId class, if the specified
194      * type name denotes a type that is suitable for single field identity.
195      * @param typeName the type to be checked
196      */
197     public static String getSingleFieldObjectIdClassName(String typeName) {
198         if (typeName == null)
199             return null;
200         String singleFieldObjectIdClassName = 
201             (String) singleFieldObjectIdClassNames.get(typeName);
202         if (singleFieldObjectIdClassName == null)
203             singleFieldObjectIdClassName = OBJECT_IDENTITY_NAME;
204         return singleFieldObjectIdClassName;
205     }
206 
207     //========= Internal helper methods ==========
208     
209     /***
210      * Returns <code>true</code> if the specified type represents an array
211      * and its element type is a persistence capable class.
212      * @param type the JavaType to be checked
213      * @return <code>true</code> if type is a persistent array; 
214      * <code>false</code> otherwise.
215      */
216     private static boolean isPersistenceArrayType(JavaType type)
217     {
218          if (type.isArray()) {
219             JavaType elementType = type.getArrayComponentType();
220             return elementType.isValue() ||
221                    elementType.isPersistenceCapable();
222         }
223         return false;
224     }
225     
226 }