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  package org.apache.jdo.impl.model.java;
18  
19  import java.util.*;
20  import java.math.*;
21  
22  import org.apache.jdo.model.java.JavaType;
23  
24  
25  /***
26   * Instances of this class represent a type that is not a persistence
27   * capable class, but is known by JDO. All JavaModel implementation will
28   * use this implementation to represnet predefined types. Predefined types
29   * include: 
30   * <ul>
31   * <li> java.lang.Object
32   * <li> void
33   * <li> primitive types
34   * <li> Java wrapper class types
35   * <li> immutable and mutable value types, such as java.lang.String,
36   * java.util.Date, etc.
37   * <li> JDO supported collection types, including their superclasses.
38   * <li> JDO supported map types, including their superclasses.
39   * </ul> 
40   * This class provides public static fields for all predefined types. These
41   * constants are convenience for direct access of a JavaType instance
42   * representing a predefined type. The class also manages a map of
43   * predefined types using the type name as key. The constructors
44   * automatically add the new created instance to this map. Please use
45   * method  {@link #getPredefinedType(String name)} to lookup a predefined
46   * type by name. Method {@link #getPredefinedTypes()} returns a view of the
47   * map of predefined types. 
48   *
49   * @author Michael Bouschen
50   * @since JDO 1.0.1
51   */
52  public class PredefinedType
53      extends BaseReflectionJavaType
54  {
55      /*** Map of all predefined types. */
56      private static final Map predefinedTypes = new HashMap();
57  
58      // ===== java.lang.Object =====
59  
60      /*** The JavaType instance for the class java.lang.Object. */
61      public static final PredefinedType objectType = new PredefinedType(Object.class);
62  
63      // ===== void =====
64  
65      /*** The JavaType instance for the type void. */
66      public static final PredefinedType voidType = new PredefinedType(void.class);
67  
68      // ===== primitive types ===== 
69  
70      /*** The JavaType instance for the primitive type boolean. */
71      public static final PrimitiveType booleanType = new PrimitiveType(boolean.class);
72      /*** The JavaType instance for the integral type byte. */
73      public static final IntegralType byteType = new IntegralType(byte.class);
74      /*** The JavaType instance for the integral type short. */
75      public static final IntegralType shortType = new IntegralType(short.class);
76      /*** The JavaType instance for the integral type int. */
77      public static final IntegralType intType = new IntegralType(int.class);
78      /*** The JavaType instance for the integral type long. */
79      public static final IntegralType longType = new IntegralType(long.class);
80      /*** The JavaType instance for the integral type char. */
81      public static final IntegralType charType = new IntegralType(char.class);
82      /*** The JavaType instance for the floating point type float. */
83      public static final FloatingPointType floatType = new FloatingPointType(float.class);
84      /*** The JavaType instance for the floating point type double. */
85      public static final FloatingPointType doubleType = new FloatingPointType(double.class);
86  
87      // ===== Value types ===== 
88  
89      /*** The JavaType instance for the class java.lang.Numer. */
90      public static final ValueClassType numberType = new ValueClassType(Number.class, objectType, false);
91      /*** The JavaType instance for the class java.lang.String. */
92      public static final ValueClassType stringType = new ValueClassType(String.class, objectType, true);
93      /*** The JavaType instance for the class java.lang.Locale. */
94      public static final ValueClassType localeType = new ValueClassType(Locale.class, objectType, false);
95      /*** The JavaType instance for the class java.math.BigDecimal. */
96      public static final ValueClassType bigDecimalType = new ValueClassType(BigDecimal.class, numberType, true);
97      /*** The JavaType instance for the class java.math.BigInteger. */
98      public static final ValueClassType bigIntegerType = new ValueClassType(BigInteger.class, numberType, true);
99  
100     // ===== WrapperClass types ===== 
101 
102     /*** The JavaType instance for the class java.lang.Boolean. */
103     public static final WrapperClassType booleanClassType = new WrapperClassType(Boolean.class, objectType, false);
104     /*** The JavaType instance for the class java.lang.Byte. */
105     public static final WrapperClassType byteClassType = new WrapperClassType(Byte.class, numberType, true);
106     /*** The JavaType instance for the class java.lang.Short. */
107     public static final WrapperClassType shortClassType = new WrapperClassType(Short.class, numberType, true);
108     /*** The JavaType instance for the class java.lang.Integer. */
109     public static final WrapperClassType integerClassType = new WrapperClassType(Integer.class, numberType, true);
110     /*** The JavaType instance for the class java.lang.Long. */
111     public static final WrapperClassType longClassType = new WrapperClassType(Long.class, numberType, true);
112     /*** The JavaType instance for the class java.lang.Character. */
113     public static final WrapperClassType characterClassType = new WrapperClassType(Character.class, numberType, true);
114     /*** The JavaType instance for the class java.lang.Float. */
115     public static final WrapperClassType floatClassType = new WrapperClassType(Float.class, numberType, true);
116     /*** The JavaType instance for the class java.lang.Double. */
117     public static final WrapperClassType doubleClassType = new WrapperClassType(Double.class, numberType, true);
118 
119     // ===== Mutable value types ===== 
120 
121     /*** The JavaType instance for the class java.util.Date. */
122     public static final MutableValueClassType dateType = new MutableValueClassType(Date.class, objectType, true);
123     /*** The JavaType instance for the class java.sql.Date. */
124     public static final MutableValueClassType sqlDateType = new MutableValueClassType(java.sql.Date.class, dateType, true);
125     /*** The JavaType instance for the class java.sql.Time. */
126     public static final MutableValueClassType sqlTimeType = new MutableValueClassType(java.sql.Time.class, dateType, true);
127     /*** The JavaType instance for the class java.sql.Timestamp. */
128     public static final MutableValueClassType sqlTimestampType = new MutableValueClassType(java.sql.Timestamp.class, dateType, true);
129     /*** The JavaType instance for the class java.util.BitSet. */
130     public static final MutableValueClassType bitsetType = new MutableValueClassType(BitSet.class, objectType, false);
131 
132     // ===== JDOSupportedCollection types ===== 
133 
134     /*** The JavaType instance for the interface java.util.Collection. */
135     public static final JDOSupportedCollectionType collectionType = new JDOSupportedCollectionType(Collection.class);
136     /*** The JavaType instance for the interface java.util.Set. */
137     public static final JDOSupportedCollectionType setType = new JDOSupportedCollectionType(Set.class);
138     /*** The JavaType instance for the interface java.util.List. */
139     public static final JDOSupportedCollectionType listType = new JDOSupportedCollectionType(List.class);
140     /*** The JavaType instance for the class java.util.AbstractCollection. */
141     public static final PredefinedType abstractCollectionType = new PredefinedType(AbstractCollection.class, objectType);
142     /*** The JavaType instance for the class java.util.AbstractSet. */
143     public static final PredefinedType abstractSetType = new PredefinedType(AbstractSet.class, abstractCollectionType);
144     /*** The JavaType instance for the class java.util.HashSet. */
145     public static final JDOSupportedCollectionType hashSetType = new JDOSupportedCollectionType(HashSet.class, abstractSetType);
146     /*** The JavaType instance for the class java.util.TreeSet. */
147     public static final JDOSupportedCollectionType treeSetType = new JDOSupportedCollectionType(TreeSet.class, abstractSetType);
148     /*** The JavaType instance for the class java.util.AbstractList. */
149     public static final PredefinedType abstractListType = new PredefinedType(AbstractList.class, abstractCollectionType);
150     /*** The JavaType instance for the class java.util.ArrayList. */
151     public static final JDOSupportedCollectionType arrayListType = new JDOSupportedCollectionType(ArrayList.class, abstractListType);
152     /*** The JavaType instance for the class java.util.LinkedList. */
153     public static final JDOSupportedCollectionType linkedListType = new JDOSupportedCollectionType(LinkedList.class, abstractListType);
154     /*** The JavaType instance for the class java.util.Vector. */
155     public static final JDOSupportedCollectionType vectorType = new JDOSupportedCollectionType(Vector.class, abstractListType);
156     /*** The JavaType instance for the class java.util.Stack. */
157     public static final JDOSupportedCollectionType stackType = new JDOSupportedCollectionType(Stack.class, vectorType);
158 
159     // ===== JDOSupportedMap types =====
160 
161     /*** The JavaType instance for the interface java.util.Map. */
162     public static final JDOSupportedMapType mapType = new JDOSupportedMapType(Map.class);
163     /*** The JavaType instance for the class java.util.AbstractMap. */
164     public static final PredefinedType abstractMapType = new PredefinedType(AbstractMap.class, objectType);
165     /*** The JavaType instance for the class java.util.HashMap. */
166     public static final JDOSupportedMapType hashMapType = new JDOSupportedMapType(HashMap.class, abstractMapType);
167     /*** The JavaType instance for the class java.util.Dictionary. */
168     public static final PredefinedType dictionaryType = new PredefinedType(Dictionary.class, objectType);
169     /*** The JavaType instance for the class java.util.Hashtable. */
170     public static final JDOSupportedMapType hashtableType = new JDOSupportedMapType(Hashtable.class, dictionaryType);
171     /*** The JavaType instance for the class java.util.Properties. */
172     public static final JDOSupportedMapType propertiesType = new JDOSupportedMapType(Properties.class, hashtableType);
173     /*** The JavaType instance for the class java.util.TreeMap. */
174     public static final JDOSupportedMapType treeMapType = new JDOSupportedMapType(TreeMap.class, abstractMapType);
175 
176     /*** 
177      * The static block sets references between the JavaType instances for
178      * primitives types and the JavaType instances for the corresponding
179      * wrapper class.
180      */
181     static
182     {
183         booleanType.setWrapperClassType(booleanClassType);
184         booleanClassType.setWrappedPrimitiveType(booleanType);
185         byteType.setWrapperClassType(byteClassType);
186         byteClassType.setWrappedPrimitiveType(byteType);
187         shortType.setWrapperClassType(shortClassType);
188         shortClassType.setWrappedPrimitiveType(shortType);
189         intType.setWrapperClassType(integerClassType);
190         integerClassType.setWrappedPrimitiveType(intType);
191         longType.setWrapperClassType(longClassType);
192         longClassType.setWrappedPrimitiveType(longType);
193         charType.setWrapperClassType(characterClassType);
194         characterClassType.setWrappedPrimitiveType(charType);
195         floatType.setWrapperClassType(floatClassType);
196         floatClassType.setWrappedPrimitiveType(floatType);
197         doubleType.setWrapperClassType(doubleClassType);
198         doubleClassType.setWrappedPrimitiveType(doubleType);
199     }
200     
201     /***
202      * Constructor taking a Class instance.
203      * It automatically adds a predefined type to the static map of all
204      * predefined types. 
205      * @param clazz the Class instance for this JavaType
206      */
207     protected PredefinedType(Class clazz)
208     {
209         this(clazz, null);
210     }
211     
212     /*** 
213      * Constructor taking a Class instance and a JavaType representing the
214      * superclass of the new JavaType instance. 
215      * It automatically adds a predefined type to the static
216      * map of all predefined types.
217      * @param clazz the Class instance for this JavaType
218      * @param superclass the JavaType representing the superclass or
219      * <code>null</code> if there is no superclass.
220      */
221     protected PredefinedType(Class clazz, JavaType superclass)
222     {
223         super(clazz, superclass);
224         predefinedTypes.put(clazz.getName(), this);
225     }
226 
227     /***
228      * Returns the JavaType instance for a predefined type with the
229      * specified name. The method return <code>null</code> if the specified
230      * name does not denote a predefined type. 
231      * @param name the name of the predefined type.
232      * @return the JavaType instance for the specified predefined type.
233      */
234     public static JavaType getPredefinedType(String name)
235     {
236         return (JavaType)predefinedTypes.get(name);
237     }
238 
239     /*** 
240      * Returns an unmodifiable view of the predefined types map. This map
241      * maps type names to JavaType instances.
242      * @return an unmodifiable view of the predefined types map.
243      */
244     public static Map getPredefinedTypes()
245     {
246         return Collections.unmodifiableMap(predefinedTypes);
247     }
248 
249 }