KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > TypeLib


1 package prefuse.util;
2
3 /**
4  * Library routines dealing with Java Class types.
5  *
6  * @author <a HREF="http://jheer.org">jeffrey heer</a>
7  */

8 public class TypeLib {
9
10     private TypeLib() {
11         // prevent instantiation
12
}
13     
14     // ------------------------------------------------------------------------
15

16     /**
17      * Check if an object is an instance of a given class, or, if the class
18      * is a primitive type, if the Object is an instance of the wrapper class
19      * for that primitive (e.g., as Integer is a wrapper for int).
20      * @param type the Class type
21      * @param instance the Object instance
22      * @return true if the object is an instance of the given class, or if
23      * of the appropriate primitive wrapper type.
24      */

25     public static boolean typeCheck(Class JavaDoc type, Object JavaDoc instance) {
26         return type.isAssignableFrom(instance.getClass()) ||
27             isWrapperInstance(type, instance);
28     }
29     
30     /**
31      * Get the nearest shared ancestor class of two objects. Note: this
32      * currently does not compute the actual least common ancestor, but
33      * only looks up one level in the inheritance tree and quits if
34      * it does not find a match.
35      * @param o1 the first object
36      * @param o2 the second object
37      * @return the nearest class instance of which both objects
38      * are instances
39      */

40     public static Class JavaDoc getSharedType(Object JavaDoc o1, Object JavaDoc o2) {
41         return getSharedType(o1.getClass(), o2.getClass());
42     }
43     
44     /**
45      * Get the nearest shared ancestor class of two classes. Note: this
46      * currently does not compute the actual least common ancestor, but
47      * only looks up one level in the inheritance tree and quits if
48      * it does not find a match.
49      * @param type1 the first type
50      * @param type2 the second type
51      * @return the nearest class instance which is equal to or a
52      * superclass of the two class instances
53      */

54     public static Class JavaDoc getSharedType(Class JavaDoc type1, Class JavaDoc type2) {
55         if ( type1 == type2 ) {
56             return type1;
57         } else if ( type1.isAssignableFrom(type2) ) {
58             return type1;
59         } else if ( type2.isAssignableFrom(type1) ) {
60             return type2;
61         } else {
62             return null;
63         }
64     }
65     
66     /**
67      * Indicates if an object is an instance of a wrapper class for a given
68      * primitive type.
69      * @param type the primitive Class type
70      * @param instance the object to test as wrapper (e.g., as Integer is a
71      * wrapper type for int)
72      * @return true if the object is a wrapper instance of the given
73      * primitive type
74      */

75     public static boolean isWrapperInstance(Class JavaDoc type, Object JavaDoc instance) {
76         if ( !type.isPrimitive() )
77             throw new IllegalArgumentException JavaDoc("Input type must be a primitive");
78         
79         if ( int.class == type && instance instanceof Integer JavaDoc ) {
80             return true;
81         } else if ( long.class == type && instance instanceof Long JavaDoc ) {
82             return true;
83         } else if ( float.class == type && instance instanceof Float JavaDoc ) {
84             return true;
85         } else if ( double.class == type && instance instanceof Double JavaDoc ) {
86             return true;
87         } else if ( boolean.class == type && instance instanceof Boolean JavaDoc ) {
88             return true;
89         } else if ( short.class == type && instance instanceof Short JavaDoc ) {
90             return true;
91         } else if ( byte.class == type && instance instanceof Byte JavaDoc ) {
92             return true;
93         } else if ( char.class == type && instance instanceof Character JavaDoc ) {
94             return true;
95         } else {
96             return false;
97         }
98     }
99     
100     /**
101      * Given a numeric (byte, short, int, long, float, or double) class type or
102      * associated wrapper class type, return the primitive class type
103      * @param type the type to look up, must be a numerical type, but can be
104      * either primitive or a wrapper.
105      * @return the primitive class type
106      */

107     public static Class JavaDoc getPrimitiveType(Class JavaDoc type) {
108         if ( Integer JavaDoc.class.equals(type) || type == int.class ) {
109             return int.class;
110         } else if ( Long JavaDoc.class.equals(type) || type == long.class ) {
111             return long.class;
112         } else if ( Float JavaDoc.class.equals(type) || type == float.class ) {
113             return float.class;
114         } else if ( Double JavaDoc.class.equals(type) || type == double.class ) {
115             return double.class;
116         } else if ( Byte JavaDoc.class.equals(type) || type == byte.class ) {
117             return byte.class;
118         } else if ( Short JavaDoc.class.equals(type) || type == short.class ) {
119             return short.class;
120         } else {
121             throw new IllegalArgumentException JavaDoc(
122                 "Input class must be a numeric type");
123         }
124     }
125     
126     /**
127      * Get the wrapper class type for a primitive class type.
128      * @param type a class type
129      * @return the wrapper class for the input type if it is a
130      * primitive class type, otherwise returns the input type
131      */

132     public static Class JavaDoc getWrapperType(Class JavaDoc type) {
133         if ( !type.isPrimitive() ) {
134             return type;
135         } else if ( int.class == type ) {
136             return Integer JavaDoc.class;
137         } else if ( long.class == type ) {
138             return Long JavaDoc.class;
139         } else if ( float.class == type ) {
140             return Float JavaDoc.class;
141         } else if ( double.class == type ) {
142             return Double JavaDoc.class;
143         } else if ( boolean.class == type ) {
144             return Boolean JavaDoc.class;
145         } else if ( short.class == type ) {
146             return Short JavaDoc.class;
147         } else if ( char.class == type ) {
148             return Character JavaDoc.class;
149         } else if ( byte.class == type ) {
150             return Byte JavaDoc.class;
151         } else if ( short.class == type ) {
152             return Short JavaDoc.class;
153         } else {
154             throw new IllegalArgumentException JavaDoc();
155         }
156     }
157     
158     /**
159      * Indicates if a given class type is a primitive integer type
160      * (one of byte, short, int, or long).
161      * @param type the type to check
162      * @return true if it is a primitive numeric type, false otherwise
163      */

164     public static boolean isIntegerType(Class JavaDoc type) {
165         return ( type == byte.class || type == short.class ||
166                  type == int.class || type == long.class);
167     }
168     
169     /**
170      * Indicates if a given class type is a primitive numeric one type
171      * (one of byte, short, int, long, float, or double).
172      * @param type the type to check
173      * @return true if it is a primitive numeric type, false otherwise
174      */

175     public static boolean isNumericType(Class JavaDoc type) {
176         return ( type == byte.class || type == short.class ||
177                  type == int.class || type == long.class ||
178                  type == double.class || type == float.class );
179     }
180     
181     /**
182      * Get a compatible numeric type for two primitive numeric
183      * class types. Any of (byte, short, int) will resolve to int.
184      * @param c1 a numeric primitive class type (int, long, float, or double)
185      * @param c2 a numeric primitive class type (int, long, float, or double)
186      * @return the compatible numeric type for binary operations involving
187      * both types.
188      */

189     public static Class JavaDoc getNumericType(Class JavaDoc c1, Class JavaDoc c2) {
190         if ( !isNumericType(c1) || !isNumericType(c2) ) {
191             throw new IllegalArgumentException JavaDoc(
192                 "Input types must be primitive number types");
193         }
194         if ( c1 == double.class || c2 == double.class ) {
195             return double.class;
196         } else if ( c1 == float.class || c1 == float.class ) {
197             return float.class;
198         } else if ( c1 == long.class || c2 == long.class ) {
199             return long.class;
200         } else {
201             return int.class;
202         }
203     }
204     
205 } // end of class TypeLib
206
Popular Tags