| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.kns.util; |
| 18 | |
|
| 19 | |
import java.math.BigDecimal; |
| 20 | |
import java.math.BigInteger; |
| 21 | |
import java.util.HashMap; |
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
@SuppressWarnings("unchecked") |
| 30 | 0 | public class TypeUtils { |
| 31 | 0 | private static final Class[] BOOLEAN_CLASSES = { Boolean.class, Boolean.TYPE }; |
| 32 | 0 | private static final Class[] INTEGRAL_CLASSES = { Byte.class, Byte.TYPE, Short.class, Short.TYPE, Integer.class, Integer.TYPE, Long.class, Long.TYPE, BigInteger.class, KualiInteger.class }; |
| 33 | |
|
| 34 | 0 | private static final Class[] DECIMAL_CLASSES = { Float.class, Float.TYPE, Double.class, Double.TYPE, BigDecimal.class, KualiDecimal.class, KualiPercent.class, AbstractKualiDecimal.class }; |
| 35 | 0 | private static final Class[] TEMPORAL_CLASSES = { java.util.Date.class, java.sql.Date.class, java.sql.Timestamp.class }; |
| 36 | 0 | private static final Class[] STRING_CLASSES = { String.class }; |
| 37 | |
|
| 38 | 0 | private static final HashMap<Class,Boolean> isBooleanCache = new HashMap<Class, Boolean>(); |
| 39 | 0 | private static final HashMap<Class,Boolean> isIntegralCache = new HashMap<Class, Boolean>(); |
| 40 | 0 | private static final HashMap<Class,Boolean> isDecimalCache = new HashMap<Class, Boolean>(); |
| 41 | 0 | private static final HashMap<Class,Boolean> isTemporalCache = new HashMap<Class, Boolean>(); |
| 42 | 0 | private static final HashMap<Class,Boolean> isStringCache = new HashMap<Class, Boolean>(); |
| 43 | 0 | private static final HashMap<Class,Boolean> isSimpleCache = new HashMap<Class, Boolean>(); |
| 44 | |
|
| 45 | |
|
| 46 | 0 | public static class JoinType{ |
| 47 | |
|
| 48 | |
} |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public static boolean isJoinClass(Class clazz) { |
| 56 | 0 | return clazz.isAssignableFrom(JoinType.class); |
| 57 | |
} |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
public static boolean isBooleanClass(Class clazz) { |
| 65 | 0 | Boolean result = isBooleanCache.get(clazz); |
| 66 | 0 | if ( result == null ) { |
| 67 | 0 | result = isa(BOOLEAN_CLASSES, clazz); |
| 68 | 0 | synchronized (isBooleanCache) { |
| 69 | 0 | isBooleanCache.put( clazz, result ); |
| 70 | 0 | } |
| 71 | |
} |
| 72 | 0 | return result; |
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
public static boolean isIntegralClass(Class clazz) { |
| 81 | 0 | Boolean result = isIntegralCache.get(clazz); |
| 82 | 0 | if ( result == null ) { |
| 83 | 0 | result = isa(INTEGRAL_CLASSES, clazz); |
| 84 | 0 | synchronized (isIntegralCache) { |
| 85 | 0 | isIntegralCache.put( clazz, result ); |
| 86 | 0 | } |
| 87 | |
} |
| 88 | 0 | return result; |
| 89 | |
} |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
public static boolean isDecimalClass(Class clazz) { |
| 97 | 0 | Boolean result = isDecimalCache.get(clazz); |
| 98 | 0 | if ( result == null ) { |
| 99 | 0 | result = isa(DECIMAL_CLASSES, clazz); |
| 100 | 0 | synchronized (isDecimalCache) { |
| 101 | 0 | isDecimalCache.put( clazz, result ); |
| 102 | 0 | } |
| 103 | |
} |
| 104 | 0 | return result; |
| 105 | |
} |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public static boolean isTemporalClass(Class clazz) { |
| 113 | 0 | Boolean result = isTemporalCache.get(clazz); |
| 114 | 0 | if ( result == null ) { |
| 115 | 0 | result = isa(TEMPORAL_CLASSES, clazz); |
| 116 | 0 | synchronized (isTemporalCache) { |
| 117 | 0 | isTemporalCache.put( clazz, result ); |
| 118 | 0 | } |
| 119 | |
} |
| 120 | 0 | return result; |
| 121 | |
} |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
public static boolean isStringClass(Class clazz) { |
| 129 | 0 | Boolean result = isStringCache.get(clazz); |
| 130 | 0 | if ( result == null ) { |
| 131 | 0 | result = isa(STRING_CLASSES, clazz); |
| 132 | 0 | synchronized (isStringCache) { |
| 133 | 0 | isStringCache.put( clazz, result ); |
| 134 | 0 | } |
| 135 | |
} |
| 136 | 0 | return result; |
| 137 | |
} |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
public static boolean isSimpleType(Class clazz) { |
| 145 | 0 | Boolean result = isSimpleCache.get(clazz); |
| 146 | 0 | if ( result == null ) { |
| 147 | 0 | result = isa(STRING_CLASSES, clazz) || isa(DECIMAL_CLASSES, clazz) || isa(INTEGRAL_CLASSES, clazz) || isa(BOOLEAN_CLASSES, clazz) || isa(TEMPORAL_CLASSES, clazz); |
| 148 | 0 | synchronized (isSimpleCache) { |
| 149 | 0 | isSimpleCache.put( clazz, result ); |
| 150 | 0 | } |
| 151 | |
} |
| 152 | 0 | return result; |
| 153 | |
} |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
private static boolean isa(Class[] types, Class type) { |
| 162 | 0 | if (type == null) { |
| 163 | 0 | throw new IllegalArgumentException("illegal (null) type class"); |
| 164 | |
} |
| 165 | |
|
| 166 | 0 | boolean isa = false; |
| 167 | |
|
| 168 | 0 | for (int i = 0; !isa && (i < types.length); ++i) { |
| 169 | 0 | isa = types[i].isAssignableFrom(type); |
| 170 | |
} |
| 171 | |
|
| 172 | 0 | return isa; |
| 173 | |
} |
| 174 | |
} |