KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > zeus > util > ClassUtils


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  */

19 package org.enhydra.zeus.util;
20
21 import java.util.HashSet JavaDoc;
22
23 // Zeus imports
24
import org.enhydra.zeus.InvalidCollectionTypeException;
25
26 /**
27  * <p>
28  * <code>ClassUtils</code> is a Zeus utility class that provides
29  * methods that deal with the various classes and types that
30  * may be used in data binding. Specifically, this will deal
31  * with the <code>String</code> Java types that
32  * <code>{@link org.enhydra.zeus.Generator}</code>s deal with,
33  * and relate them to Java <code>Class</code> objects.
34  * </p>
35  *
36  * @author Brett McLaughlin
37  */

38 public class ClassUtils {
39     
40     /** A hashed set of the Java primitive types. */
41     private static final HashSet JavaDoc primitives;
42     
43     /** Constant for using List as the default collection type */
44     public static final int COLLECTION_TYPE_LIST = 0;
45     
46     /** Constant for using Array as the default collection type */
47     public static final int COLLECTION_TYPE_ARRAY = 1;
48     
49     static {
50         /** Initialize the primitives hashed set. */
51         primitives = new HashSet JavaDoc();
52         primitives.add("boolean");
53         primitives.add("byte");
54         primitives.add("char");
55         primitives.add("double");
56         primitives.add("float");
57         primitives.add("int");
58         primitives.add("long");
59         primitives.add("short");
60         primitives.add("String");
61         primitives.add("java.lang.String");
62     }
63     
64     /**
65      * <p>
66      * This will indicate whether the supplied Java type is a Java
67      * primitive.
68      * </p>
69      *
70      * @param javaType the Java type to check against Java primitives
71      * @return <code>boolean</code> whether the supplied type is a Java
72      * primitive
73      */

74     public static boolean isJavaPrimitive(String JavaDoc javaType) {
75         if (javaType == null) {
76             throw new IllegalArgumentException JavaDoc("A non-null String must be " +
77                 "supplied to ClassUtils methods.");
78         }
79         
80         return primitives.contains(javaType);
81     }
82
83     /**
84      * <p>
85      * This will determine if the supplied Java type (in
86      * <code>String</code> form) is one of the Java
87      * <code>Collection</code> classes. The type supplied <i>must</i>
88      * be a fully-qualified class name, like <code>java.util.List</code>.
89      * </p>
90      *
91      * @param javaType the <code>String</code> name of the Java
92      * type to check for.
93      * @return <code>boolean</code> - whether the class is a
94      * Java <code>Collection</code>.
95      */

96     public static boolean isCollectionClass(String JavaDoc javaType) {
97         if (javaType == null) {
98             throw new IllegalArgumentException JavaDoc("A non-null String must be " +
99                 "supplied to ClassUtils methods.");
100         }
101         
102         // Since all classes are fully qualified, check for java.util package
103
if (javaType.indexOf("java.util") == -1) {
104             return false;
105         }
106         
107         // Check for the specific Collection names, in order of commonality
108
if (javaType.indexOf("Collection") != -1) {
109             return true;
110         } else if (javaType.indexOf("List") != -1) {
111             return true;
112         } else if (javaType.indexOf("LinkedList") != -1) {
113             return true;
114         } else if (javaType.indexOf("ArrayList") != -1) {
115             return true;
116         } else if (javaType.indexOf("Vector") != -1) {
117             return true;
118         } else if (javaType.indexOf("Stack") != -1) {
119             return true;
120         } else if (javaType.indexOf("Set") != -1) {
121             return true;
122         } else if (javaType.indexOf("SortedSet") != -1) {
123             return true;
124         } else if (javaType.indexOf("HashSet") != -1) {
125             return true;
126         } else if (javaType.indexOf("TreeSet") != -1) {
127             return true;
128         }
129         
130         return false;
131     }
132     
133     /**
134      * <p>
135      * For a supplied <code>Collection</code> class, this will return an
136      * implementation of that class. For example, specifying
137      * <code>java.util.List</code> to this method would return
138      * <code>java.util.LinkedList</code>, which is a concrete class. If the
139      * supplied class is already a concrete class, it is returned unchanged.
140      * </p>
141      *
142      * @param collectionClass <code>String</code> name of collection
143      * class/interface
144      * @return <code>String</code> - fully qualified concrete class to use.
145      */

146     public static String JavaDoc getCollectionImplClass(String JavaDoc collectionClass) {
147         if (collectionClass == null) {
148             throw new IllegalArgumentException JavaDoc("A non-null String must be " +
149                 "supplied to ClassUtils methods.");
150         }
151         
152         // Check for the specific Collection names, in order of commonality
153
if (collectionClass.indexOf("Collection") != -1) {
154             return "java.util.LinkedList";
155         } else if (collectionClass.indexOf("LinkedList") != -1) {
156             return collectionClass;
157         } else if (collectionClass.indexOf("ArrayList") != -1) {
158             return collectionClass;
159         } else if (collectionClass.indexOf("Vector") != -1) {
160             return collectionClass;
161         } else if (collectionClass.indexOf("Stack") != -1) {
162             return collectionClass;
163         } else if (collectionClass.indexOf("SortedSet") != -1) {
164             return collectionClass;
165         } else if (collectionClass.indexOf("HashSet") != -1) {
166             return collectionClass;
167         } else if (collectionClass.indexOf("TreeSet") != -1) {
168             return collectionClass;
169         } else if (collectionClass.indexOf("Set") != -1) {
170             return "java.util.HashSet";
171         } else if (collectionClass.indexOf("List") != -1) {
172             return "java.util.LinkedList";
173         } else {
174             throw new IllegalArgumentException JavaDoc(
175                 "A non Collection class was specified.");
176         }
177     }
178     
179     /**
180      * <p>
181      * This checks to see if the supplied <code>int</code> constant is a
182      * legal collection type, as defined in this class.
183      * </p>
184      *
185      * @param collectionType <code>int</code> to check against constants.
186      * @return <code>boolean</code> - whether the supplied type is a legal
187      * collection type constant.
188      */

189     public static boolean isCollectionConstant(int collectionType) {
190         // Make sure the constant is within the correct boundary points
191
if ((collectionType < COLLECTION_TYPE_LIST) ||
192             (collectionType > COLLECTION_TYPE_ARRAY)) {
193                 
194             return false;
195         }
196             
197         return true;
198     }
199     
200     /**
201      * <p>
202      * This will convert a <code>String</code> representation of a supplied
203      * collection type, and convert it to one of the <code>int</code>
204      * constants specified in this class.
205      * </p>
206      *
207      * @param collectionTypeString the collection type to convert to a constant.
208      * @return <code>int</code> - the constant representation of the type.
209      * @throws <code>InvalidCollectionTypeException</code> - when an illegal
210      * collection type is supplied.
211      * @see <code>{@link #COLLECTION_TYPE_LIST}</code>
212      * @see <code>{@link #COLLECTION_TYPE_ARRAY}</code>
213      */

214     public static int getCollectionTypeAsInt(String JavaDoc collectionTypeString)
215         throws InvalidCollectionTypeException {
216         
217         if ((collectionTypeString.equals("List")) ||
218             (collectionTypeString.equals("java.util.List"))) {
219             
220             return COLLECTION_TYPE_LIST;
221         } else if ((collectionTypeString.equals("Array")) ||
222                    (collectionTypeString.equals("java.util.Array"))) {
223                        
224             return COLLECTION_TYPE_ARRAY;
225         } else {
226             throw new InvalidCollectionTypeException(collectionTypeString);
227         }
228     }
229     
230     /**
231      * <p>
232      * This returns the currently selected default collection type, as a
233      * <code>String</code> that maps to a Java class. For example, if
234      * the default collection type is currently "List", this would return
235      * the value "java.util.List". The class name returned here is always
236      * a fully qualified class name.
237      * </p>
238      *
239      * @return <code>String</code> - the default collection type as a Java class
240      * name.
241      */

242     public static String JavaDoc getCollectionTypeAsString(int collectionType) {
243         if (collectionType == COLLECTION_TYPE_LIST) {
244             return "java.util.List";
245         } else if (collectionType == COLLECTION_TYPE_ARRAY) {
246             return "java.util.Array";
247         }
248         
249         // We should never get here
250
return null;
251     }
252     
253     /**
254      * <p>
255      * This will take the <code>String</code> value supplied and convert it
256      * to an <code>Object</code> of the type specified in
257      * <code>paramType</code>.
258      * </p>
259      *
260      * @param value <code>String</code> value to convert.
261      * @param paramType <code>Class</code> with type to convert to.
262      * @return <code>Object</code> - value in correct type.
263      */

264     public static Object JavaDoc getParameter(String JavaDoc value, Class JavaDoc paramType) {
265         if (value == null) {
266             throw new IllegalArgumentException JavaDoc("A non-null String must be " +
267                 "supplied to ClassUtils methods.");
268         }
269         if (paramType == null) {
270             throw new IllegalArgumentException JavaDoc("A non-null Class must be " +
271                 "supplied to ClassUtils methods.");
272         }
273         
274         Object JavaDoc ob = null;
275         String JavaDoc type = paramType.getName();
276
277         if (type.equals("java.lang.String")) {
278             ob = value;
279         } else if ((type.equals("int")) ||
280                    (type.equals("java.lang.Integer"))) {
281             ob = Integer.valueOf(value);
282         } else if ((type.equals("long")) ||
283                    (type.equals("java.lang.Long"))) {
284             ob = Long.valueOf(value);
285         } else if ((type.equals("float")) ||
286                    (type.equals("java.lang.Float"))) {
287             ob = Float.valueOf(value);
288         } else if ((type.equals("double")) ||
289                    (type.equals("java.lang.Double"))) {
290             ob = Double.valueOf(value);
291         } else if ((type.equals("boolean")) ||
292                    (type.equals("java.lang.Boolean"))) {
293             ob = Boolean.valueOf(value);
294         }
295
296         return ob;
297     }
298 }
299
Popular Tags