1
2
3 package org.virtualmock.util;
4
5 /***
6 * Provides utilities for working with primitives. The
7 * getDefaultPrimitiveValue() method generates default return values for
8 * primitives. The getPrimitiveTypeForWrapperClass method converts a primitive
9 * wrapper class (such as Integer.class) into a primitive class (such as
10 * int.class).
11 *
12 * @author Chad Woolley
13 * @version $Revision: 1.3 $
14 */
15 public class PrimitiveUtils {
16 /***
17 * Returns true if the specified type is assignable to any primitive type.
18 *
19 * @param type the type to check for assignability to a primitive type
20 *
21 * @return Returns true if the specified type is assignable to any
22 * primitive type
23 */
24 public boolean isAssignableToPrimitiveWrapper(Class type) {
25 return Integer.class.isAssignableFrom(type)
26 || Float.class.isAssignableFrom(type)
27 || Double.class.isAssignableFrom(type)
28 || Byte.class.isAssignableFrom(type)
29 || Long.class.isAssignableFrom(type)
30 || Short.class.isAssignableFrom(type)
31 || Boolean.class.isAssignableFrom(type)
32 || Character.class.isAssignableFrom(type);
33 }
34
35 /***
36 * Generate default return values for specified primitive type.
37 *
38 * @param type a Class object representing a primitive
39 *
40 * @return the default value for the primitive
41 *
42 * @throws IllegalArgumentException if the type is not a primitive
43 */
44 public Object getDefaultPrimitiveValue(Class type) {
45 if (!type.isPrimitive()) {
46 throw new IllegalArgumentException(type.getName()
47 + " is not a primitive type");
48 }
49
50 if (int.class.equals(type)) {
51 return new Integer(0);
52 }
53
54 if (float.class.equals(type)) {
55 return new Float(0.0f);
56 }
57
58 if (double.class.equals(type)) {
59 return new Double(0.0d);
60 }
61
62 if (byte.class.equals(type)) {
63 byte byteValue = 0;
64
65 return new Byte(byteValue);
66 }
67
68 if (long.class.equals(type)) {
69 return new Long(0L);
70 }
71
72 if (short.class.equals(type)) {
73 short shortValue = 0;
74
75 return new Short(shortValue);
76 }
77
78 if (boolean.class.equals(type)) {
79 return new Boolean(false);
80 }
81
82
83 return new Character('\u0000');
84 }
85
86 /***
87 * When passed a primitive wrapper type (such as Integer.class), returns
88 * the corresponding primitive type (such as int.class).
89 *
90 * @param primitiveWrapperType The primitive wrapper class to convert to a
91 * primitive class
92 *
93 * @return the primitive class corresponding to the primitive wrapper class
94 *
95 * @throws IllegalArgumentException if the specified type is not one of the
96 * eight primitive wrapper types
97 */
98 public Class getPrimitiveTypeForWrapperClass(Class primitiveWrapperType) {
99 if (Integer.class.equals(primitiveWrapperType)) {
100 return int.class;
101 }
102
103 if (Float.class.equals(primitiveWrapperType)) {
104 return float.class;
105 }
106
107 if (Double.class.equals(primitiveWrapperType)) {
108 return double.class;
109 }
110
111 if (Byte.class.equals(primitiveWrapperType)) {
112 return byte.class;
113 }
114
115 if (Long.class.equals(primitiveWrapperType)) {
116 return long.class;
117 }
118
119 if (Short.class.equals(primitiveWrapperType)) {
120 return short.class;
121 }
122
123 if (Boolean.class.equals(primitiveWrapperType)) {
124 return boolean.class;
125 }
126
127 if (Character.class.equals(primitiveWrapperType)) {
128 return char.class;
129 }
130
131 throw new IllegalArgumentException(primitiveWrapperType.getName()
132 + " is not a primitive wrapper type.");
133 }
134 }