Coverage Report - org.kuali.rice.kns.util.TypeUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
TypeUtils
0%
0/62
0%
0/28
2.75
TypeUtils$JoinType
0%
0/1
N/A
2.75
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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  
  * This class provides utilities for checking the types of objects.
 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  
     // KRACOEUS-1493 : add AbstractKualiDecimal for budgetdecimal
 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  
      * @param clazz
 52  
      * @return true if the given Class is an join type
 53  
      * @throws IllegalArgumentException if the given Class is null
 54  
      */
 55  
     public static boolean isJoinClass(Class clazz) {
 56  0
             return clazz.isAssignableFrom(JoinType.class);
 57  
     }
 58  
 
 59  
     /**
 60  
      * @param clazz
 61  
      * @return true if the given Class is an boolean type
 62  
      * @throws IllegalArgumentException if the given Class is null
 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  
      * @param clazz
 77  
      * @return true if the given Class is an integral type
 78  
      * @throws IllegalArgumentException if the given Class is null
 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  
      * @param clazz
 93  
      * @return true if the given Class is a decimal type
 94  
      * @throws IllegalArgumentException if the given Class is null
 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  
      * @param clazz
 109  
      * @return true if the given Class is a temporal type
 110  
      * @throws IllegalArgumentException if the given Class is null
 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  
      * @param clazz
 125  
      * @return true if the given Class is a string type
 126  
      * @throws IllegalArgumentException if the given Class is null
 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  
      * @param clazz
 141  
      * @return true if the given Class is a "simple" - one of the primitive types, their wrappers, or a temporal type
 142  
      * @throws IllegalArgumentException if the given Class is null
 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  
      * @param types
 157  
      * @param type
 158  
      * @return true if the given Class is assignable from one of the classes in the given Class[]
 159  
      * @throws IllegalArgumentException if the given Class is null
 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  
 }