1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.tomgibara.pronto.util;
19
20 /**
21 * Collects together utility methods for handling class objects.
22 *
23 * @author Tom Gibara
24 */
25
26 public final class Classes {
27
28 /**
29 * Identifies the object class which corresponds to a specified primitive
30 * class, or throws an exception.
31 *
32 * @param clss
33 * the class
34 * @return the object class for the supplied primitive class, never null
35 * @throws IllegalArgumentException
36 * if the supplied class is null or does not represent a
37 * primitive type
38 */
39
40 public static Class classForPrimitive(final Class clss) throws IllegalArgumentException {
41 if (clss == Boolean.TYPE) return Boolean.class;
42 if (clss == Integer.TYPE) return Integer.class;
43 if (clss == Double.TYPE) return Double.class;
44 if (clss == Long.TYPE) return Long.class;
45 if (clss == Float.TYPE) return Float.class;
46 if (clss == Short.TYPE) return Short.class;
47 if (clss == Byte.TYPE) return Byte.class;
48 if (clss == Character.TYPE) return Character.class;
49 if (clss == Void.TYPE) return Void.class;
50 throw new IllegalArgumentException(String.format("Class %s is not primitive.", clss.getName()));
51 }
52
53 /**
54 * Whether the class is strictly primitive - not including the Void.TYPE.
55 *
56 * @param clss
57 * the class
58 * @return true if and only if the class is primitive and not void
59 */
60
61 public static boolean isNonVoidPrimitive(final Class clss) {
62 return clss.isPrimitive() && clss != Void.TYPE;
63 }
64
65 /**
66 * Whether the supplied class represents a number primitive.
67 *
68 * @param clss
69 * the class
70 * @return true if and only if clss equals the TYPE of some extension of
71 * Number
72 */
73
74 public static boolean isNumberPrimitive(final Class clss) {
75
76 return clss == Integer.TYPE || clss == Double.TYPE || clss == Long.TYPE || clss == Float.TYPE
77 || clss == Short.TYPE || clss == Byte.TYPE;
78 }
79
80 /**
81 * Whether the supplied class represents the boolean primitive.
82 *
83 * @param clss
84 * the class
85 * @return true if and only if clss == Boolean.TYPE
86 */
87
88 public static boolean isBooleanPrimitive(final Class clss) {
89 return clss == Boolean.TYPE;
90 }
91
92 private Classes() { }
93
94 }