001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.kuali.core.util;
018:
019: import java.math.BigDecimal;
020: import java.math.BigInteger;
021:
022: /**
023: * This class provides utilities for checking the types of objects.
024: *
025: *
026: */
027:
028: public class TypeUtils {
029: private static final Class[] BOOLEAN_CLASSES = { Boolean.class,
030: Boolean.TYPE };
031: private static final Class[] INTEGRAL_CLASSES = { Byte.class,
032: Byte.TYPE, Short.class, Short.TYPE, Integer.class,
033: Integer.TYPE, Long.class, Long.TYPE, BigInteger.class,
034: KualiInteger.class };
035: private static final Class[] DECIMAL_CLASSES = { Float.class,
036: Float.TYPE, Double.class, Double.TYPE, BigDecimal.class,
037: KualiDecimal.class, KualiPercent.class };
038: private static final Class[] TEMPORAL_CLASSES = {
039: java.util.Date.class, java.sql.Date.class,
040: java.sql.Timestamp.class };
041: private static final Class[] STRING_CLASSES = { String.class };
042:
043: /**
044: * @param clazz
045: * @return true if the given Class is an boolean type
046: * @throws IllegalArgumentException if the given Class is null
047: */
048: public static boolean isBooleanClass(Class clazz) {
049: return isa(BOOLEAN_CLASSES, clazz);
050: }
051:
052: /**
053: * @param clazz
054: * @return true if the given Class is an integral type
055: * @throws IllegalArgumentException if the given Class is null
056: */
057: public static boolean isIntegralClass(Class clazz) {
058: return isa(INTEGRAL_CLASSES, clazz);
059: }
060:
061: /**
062: * @param clazz
063: * @return true if the given Class is a decimal type
064: * @throws IllegalArgumentException if the given Class is null
065: */
066: public static boolean isDecimalClass(Class clazz) {
067: return isa(DECIMAL_CLASSES, clazz);
068: }
069:
070: /**
071: * @param clazz
072: * @return true if the given Class is a temporal type
073: * @throws IllegalArgumentException if the given Class is null
074: */
075: public static boolean isTemporalClass(Class clazz) {
076: return isa(TEMPORAL_CLASSES, clazz);
077: }
078:
079: /**
080: * @param clazz
081: * @return true if the given Class is a string type
082: * @throws IllegalArgumentException if the given Class is null
083: */
084: public static boolean isStringClass(Class clazz) {
085: return isa(STRING_CLASSES, clazz);
086: }
087:
088: /**
089: * @param clazz
090: * @return true if the given Class is a "simple" - one of the primitive types, their wrappers, or a temporal type
091: * @throws IllegalArgumentException if the given Class is null
092: */
093: public static boolean isSimpleType(Class clazz) {
094: return isa(STRING_CLASSES, clazz)
095: || isa(DECIMAL_CLASSES, clazz)
096: || isa(INTEGRAL_CLASSES, clazz)
097: || isa(BOOLEAN_CLASSES, clazz)
098: || isa(TEMPORAL_CLASSES, clazz);
099: }
100:
101: /**
102: * @param types
103: * @param type
104: * @return true if the given Class is assignable from one of the classes in the given Class[]
105: * @throws IllegalArgumentException if the given Class is null
106: */
107: private static boolean isa(Class[] types, Class type) {
108: if (type == null) {
109: throw new IllegalArgumentException(
110: "illegal (null) type class");
111: }
112:
113: boolean isa = false;
114:
115: for (int i = 0; !isa && (i < types.length); ++i) {
116: isa = types[i].isAssignableFrom(type);
117: }
118:
119: return isa;
120: }
121: }
|