1 19 package org.enhydra.zeus.util; 20 21 import java.util.HashSet ; 22 23 import org.enhydra.zeus.InvalidCollectionTypeException; 25 26 38 public class ClassUtils { 39 40 41 private static final HashSet primitives; 42 43 44 public static final int COLLECTION_TYPE_LIST = 0; 45 46 47 public static final int COLLECTION_TYPE_ARRAY = 1; 48 49 static { 50 51 primitives = new HashSet (); 52 primitives.add("boolean"); 53 primitives.add("byte"); 54 primitives.add("char"); 55 primitives.add("double"); 56 primitives.add("float"); 57 primitives.add("int"); 58 primitives.add("long"); 59 primitives.add("short"); 60 primitives.add("String"); 61 primitives.add("java.lang.String"); 62 } 63 64 74 public static boolean isJavaPrimitive(String javaType) { 75 if (javaType == null) { 76 throw new IllegalArgumentException ("A non-null String must be " + 77 "supplied to ClassUtils methods."); 78 } 79 80 return primitives.contains(javaType); 81 } 82 83 96 public static boolean isCollectionClass(String javaType) { 97 if (javaType == null) { 98 throw new IllegalArgumentException ("A non-null String must be " + 99 "supplied to ClassUtils methods."); 100 } 101 102 if (javaType.indexOf("java.util") == -1) { 104 return false; 105 } 106 107 if (javaType.indexOf("Collection") != -1) { 109 return true; 110 } else if (javaType.indexOf("List") != -1) { 111 return true; 112 } else if (javaType.indexOf("LinkedList") != -1) { 113 return true; 114 } else if (javaType.indexOf("ArrayList") != -1) { 115 return true; 116 } else if (javaType.indexOf("Vector") != -1) { 117 return true; 118 } else if (javaType.indexOf("Stack") != -1) { 119 return true; 120 } else if (javaType.indexOf("Set") != -1) { 121 return true; 122 } else if (javaType.indexOf("SortedSet") != -1) { 123 return true; 124 } else if (javaType.indexOf("HashSet") != -1) { 125 return true; 126 } else if (javaType.indexOf("TreeSet") != -1) { 127 return true; 128 } 129 130 return false; 131 } 132 133 146 public static String getCollectionImplClass(String collectionClass) { 147 if (collectionClass == null) { 148 throw new IllegalArgumentException ("A non-null String must be " + 149 "supplied to ClassUtils methods."); 150 } 151 152 if (collectionClass.indexOf("Collection") != -1) { 154 return "java.util.LinkedList"; 155 } else if (collectionClass.indexOf("LinkedList") != -1) { 156 return collectionClass; 157 } else if (collectionClass.indexOf("ArrayList") != -1) { 158 return collectionClass; 159 } else if (collectionClass.indexOf("Vector") != -1) { 160 return collectionClass; 161 } else if (collectionClass.indexOf("Stack") != -1) { 162 return collectionClass; 163 } else if (collectionClass.indexOf("SortedSet") != -1) { 164 return collectionClass; 165 } else if (collectionClass.indexOf("HashSet") != -1) { 166 return collectionClass; 167 } else if (collectionClass.indexOf("TreeSet") != -1) { 168 return collectionClass; 169 } else if (collectionClass.indexOf("Set") != -1) { 170 return "java.util.HashSet"; 171 } else if (collectionClass.indexOf("List") != -1) { 172 return "java.util.LinkedList"; 173 } else { 174 throw new IllegalArgumentException ( 175 "A non Collection class was specified."); 176 } 177 } 178 179 189 public static boolean isCollectionConstant(int collectionType) { 190 if ((collectionType < COLLECTION_TYPE_LIST) || 192 (collectionType > COLLECTION_TYPE_ARRAY)) { 193 194 return false; 195 } 196 197 return true; 198 } 199 200 214 public static int getCollectionTypeAsInt(String collectionTypeString) 215 throws InvalidCollectionTypeException { 216 217 if ((collectionTypeString.equals("List")) || 218 (collectionTypeString.equals("java.util.List"))) { 219 220 return COLLECTION_TYPE_LIST; 221 } else if ((collectionTypeString.equals("Array")) || 222 (collectionTypeString.equals("java.util.Array"))) { 223 224 return COLLECTION_TYPE_ARRAY; 225 } else { 226 throw new InvalidCollectionTypeException(collectionTypeString); 227 } 228 } 229 230 242 public static String getCollectionTypeAsString(int collectionType) { 243 if (collectionType == COLLECTION_TYPE_LIST) { 244 return "java.util.List"; 245 } else if (collectionType == COLLECTION_TYPE_ARRAY) { 246 return "java.util.Array"; 247 } 248 249 return null; 251 } 252 253 264 public static Object getParameter(String value, Class paramType) { 265 if (value == null) { 266 throw new IllegalArgumentException ("A non-null String must be " + 267 "supplied to ClassUtils methods."); 268 } 269 if (paramType == null) { 270 throw new IllegalArgumentException ("A non-null Class must be " + 271 "supplied to ClassUtils methods."); 272 } 273 274 Object ob = null; 275 String type = paramType.getName(); 276 277 if (type.equals("java.lang.String")) { 278 ob = value; 279 } else if ((type.equals("int")) || 280 (type.equals("java.lang.Integer"))) { 281 ob = Integer.valueOf(value); 282 } else if ((type.equals("long")) || 283 (type.equals("java.lang.Long"))) { 284 ob = Long.valueOf(value); 285 } else if ((type.equals("float")) || 286 (type.equals("java.lang.Float"))) { 287 ob = Float.valueOf(value); 288 } else if ((type.equals("double")) || 289 (type.equals("java.lang.Double"))) { 290 ob = Double.valueOf(value); 291 } else if ((type.equals("boolean")) || 292 (type.equals("java.lang.Boolean"))) { 293 ob = Boolean.valueOf(value); 294 } 295 296 return ob; 297 } 298 } 299 | Popular Tags |