1 23 24 29 30 package com.sun.jdo.spi.persistence.support.sqlstore.query.util.type; 31 32 import java.util.*; 33 import java.math.*; 34 35 import com.sun.jdo.api.persistence.model.Model; 36 import com.sun.jdo.spi.persistence.utility.FieldTypeEnumeration; 37 38 44 public class TypeTable 45 { 46 49 public static final NullType nullType = new NullType(); 50 51 54 public static final ErrorType errorType = new ErrorType(); 55 56 59 public BooleanType booleanType; 60 61 64 public IntegralType charType; 65 66 69 public IntegralType byteType; 70 71 74 public IntegralType shortType; 75 76 79 public IntegralType intType; 80 81 84 public IntegralType longType; 85 86 89 public FloatingPointType floatType; 90 91 94 public FloatingPointType doubleType; 95 96 99 public StringType stringType; 100 101 104 public MathType bigDecimalType; 105 106 109 public MathType bigIntegerType; 110 111 114 protected Model model; 115 116 119 protected ClassLoader classLoader; 120 121 124 protected Map types = new HashMap(); 125 126 127 private static Map typeTables = new HashMap(); 128 129 130 public static TypeTable getInstance(ClassLoader classLoader) 131 { 132 synchronized (typeTables) { 133 TypeTable typeTable = (TypeTable)typeTables.get(classLoader); 134 if (typeTable == null) { 135 typeTable = new TypeTable(classLoader); 136 typeTables.put(classLoader, typeTable); 137 } 138 return typeTable; 139 } 140 } 141 142 143 public static void removeInstance(ClassLoader classLoader) 144 { 145 synchronized (typeTables) { 146 typeTables.remove(classLoader); 147 } 148 } 149 150 153 private TypeTable(ClassLoader classLoader) 154 { 155 this.model = Model.RUNTIME; 158 159 this.classLoader = classLoader; 160 161 booleanType = new BooleanType(); 162 types.put(booleanType.getName(), booleanType); 163 charType = new IntegralType("char", char.class, FieldTypeEnumeration.CHARACTER_PRIMITIVE); types.put(charType.getName(), charType); 165 byteType = new IntegralType("byte", byte.class, FieldTypeEnumeration.BYTE_PRIMITIVE); types.put(byteType.getName(), byteType); 167 shortType = new IntegralType("short", short.class, FieldTypeEnumeration.SHORT_PRIMITIVE); types.put(shortType.getName(), shortType); 169 intType = new IntegralType("int", int.class, FieldTypeEnumeration.INTEGER_PRIMITIVE); types.put(intType.getName(), intType); 171 longType = new IntegralType("long", long.class, FieldTypeEnumeration.LONG_PRIMITIVE); types.put(longType.getName(), longType); 173 floatType = new FloatingPointType("float", float.class, FieldTypeEnumeration.FLOAT_PRIMITIVE); types.put(floatType.getName(), floatType); 175 doubleType = new FloatingPointType("double", double.class, FieldTypeEnumeration.DOUBLE_PRIMITIVE); types.put(doubleType.getName(), doubleType); 177 178 stringType = new StringType(this); 179 types.put(stringType.getName(), stringType); 180 181 WrapperClassType booleanClassType = 182 new WrapperClassType("java.lang.Boolean", Boolean .class, FieldTypeEnumeration.BOOLEAN, booleanType, this); types.put(booleanClassType.getName(), booleanClassType); 184 NumericWrapperClassType byteClassType = 185 new NumericWrapperClassType("java.lang.Byte", Byte .class, FieldTypeEnumeration.BYTE, byteType, this); types.put(byteClassType.getName(), byteClassType); 187 NumericWrapperClassType shortClassType = 188 new NumericWrapperClassType("java.lang.Short", Short .class, FieldTypeEnumeration.SHORT, shortType, this); types.put(shortClassType.getName(), shortClassType); 190 NumericWrapperClassType intClassType = 191 new NumericWrapperClassType("java.lang.Integer", Integer .class, FieldTypeEnumeration.INTEGER, intType, this); types.put(intClassType.getName(), intClassType); 193 NumericWrapperClassType longClassType = 194 new NumericWrapperClassType("java.lang.Long", Long .class, FieldTypeEnumeration.LONG, longType, this); types.put(longClassType.getName(), longClassType); 196 NumericWrapperClassType charClassType = 197 new NumericWrapperClassType("java.lang.Character", Character .class, FieldTypeEnumeration.CHARACTER, charType, this); types.put(charClassType.getName(), charClassType); 199 NumericWrapperClassType floatClassType = 200 new NumericWrapperClassType("java.lang.Float", Float .class, FieldTypeEnumeration.FLOAT, floatType, this); types.put(floatClassType.getName(), floatClassType); 202 NumericWrapperClassType doubleClassType = 203 new NumericWrapperClassType("java.lang.Double", Double .class, FieldTypeEnumeration.DOUBLE, doubleType, this); types.put(doubleClassType.getName(), doubleClassType); 205 206 booleanType.setWrapper(booleanClassType); 207 byteType.setWrapper(byteClassType); 208 shortType.setWrapper(shortClassType); 209 intType.setWrapper(intClassType); 210 longType.setWrapper(longClassType); 211 charType.setWrapper(charClassType); 212 floatType.setWrapper(floatClassType); 213 doubleType.setWrapper(doubleClassType); 214 215 bigDecimalType = new MathType("java.math.BigDecimal", BigDecimal.class, FieldTypeEnumeration.BIGDECIMAL, this); types.put(bigDecimalType.getName(), bigDecimalType); 217 bigIntegerType = new MathType("java.math.BigInteger", BigInteger.class, FieldTypeEnumeration.BIGINTEGER, this); types.put(bigIntegerType.getName(), bigIntegerType); 219 220 DateType dateType = new DateType("java.util.Date", java.util.Date .class, FieldTypeEnumeration.UTIL_DATE, this); 222 types.put(dateType.getName(), dateType); 223 DateType sqldateType = new DateType("java.sql.Date", java.sql.Date .class, FieldTypeEnumeration.SQL_DATE, this); 224 types.put(sqldateType.getName(), sqldateType); 225 DateType sqlTimeType = new DateType("java.sql.Time", java.sql.Time .class, FieldTypeEnumeration.SQL_TIME, this); 226 types.put(sqlTimeType.getName(), sqlTimeType); 227 DateType sqlTimestampType = new DateType("java.sql.Timestamp", java.sql.Timestamp .class, FieldTypeEnumeration.SQL_TIMESTAMP, this); 228 types.put(sqlTimestampType.getName(), sqlTimestampType); 229 } 230 231 244 public Type checkType(String name) 245 { 246 synchronized(types) 247 { 248 Type result = (Type)types.get(name); 249 if (result == null) 250 { 251 try 253 { 254 Class clazz = Class.forName(name, true, classLoader); 255 result = new ClassType(name, clazz, this); 256 types.put(name, result); 257 } 258 catch (ClassNotFoundException ex) 259 { 260 } 262 } 263 return result; 264 } 265 } 266 267 280 public Type checkType(Class clazz) 281 { 282 if (clazz == null) 283 return null; 284 String name = clazz.getName(); 285 synchronized (types) 286 { 287 Type result = (Type)types.get(name); 288 289 if (result == null) 290 { 291 result = new ClassType(name, clazz, this); 293 types.put(name, result); 294 } 295 return result; 296 } 297 } 298 299 302 public Type binaryNumericPromotion(Type left, Type right) 303 { 304 if ((left instanceof NumericType) && (right instanceof NumericType)) 305 { 306 if (left.equals(doubleType) || right.equals(doubleType)) 307 return doubleType; 308 else if (left.equals(floatType) || right.equals(floatType)) 309 return floatType; 310 else if (left.equals(longType) || right.equals(longType)) 311 return longType; 312 else 313 return intType; 314 } 315 else 316 { 317 return errorType; 318 } 319 } 320 321 324 public Type unaryNumericPromotion(Type type) 325 { 326 if (type instanceof NumericType) 327 { 328 if (type.equals(byteType) || type.equals(shortType) || type.equals(charType)) 329 { 330 return intType; 331 } 332 else 333 { 334 return type; 335 } 336 } 337 else 338 { 339 return errorType; 340 } 341 } 342 343 346 public boolean isNumberType(Type type) 347 { 348 Type numberType = checkType("java.lang.Number"); Type characterType = checkType("java.lang.Character"); return (type instanceof NumericType) || 351 (type.isCompatibleWith(numberType)) || 352 (type.isCompatibleWith(characterType)); 353 } 354 355 359 public boolean isIntegralType(Type type) 360 { 361 if (type instanceof IntegralType) 362 return true; 363 else if (type instanceof NumericWrapperClassType) 364 return ((NumericWrapperClassType)type).getPrimitiveType() instanceof IntegralType; 365 return false; 366 } 367 368 372 public boolean isFloatingPointType(Type type) 373 { 374 if (type instanceof FloatingPointType) 375 return true; 376 else if (type instanceof NumericWrapperClassType) 377 return ((NumericWrapperClassType)type).getPrimitiveType() instanceof FloatingPointType; 378 return false; 379 } 380 381 384 public boolean isDoubleType(Type type) 385 { 386 return (type.equals(doubleType) || type.equals(doubleType.getWrapper())); 387 } 388 389 392 public boolean isIntType(Type type) 393 { 394 return (type.equals(intType) || type.equals(intType.getWrapper())); 395 } 396 397 400 public boolean isCharType(Type type) 401 { 402 return (type.equals(charType) || type.equals(charType.getWrapper())); 403 } 404 405 408 public boolean isBooleanType(Type type) 409 { 410 return (type.equals(booleanType) || type.equals(booleanType.getWrapper())); 411 } 412 413 418 public boolean isPersistenceCapableType(Type type) 419 { 420 return ((type instanceof ClassType) && 421 ((ClassType)type).isPersistenceCapable()); 422 } 423 424 429 public boolean isCollectionType(Type type) 430 { 431 Type collectionType = checkType("java.util.Collection"); return (type instanceof ClassType) && type.isCompatibleWith(collectionType); 433 } 434 435 440 public boolean isJavaLangMathType(Type type) 441 { 442 Type mathType = checkType("java.lang.Math"); return (type instanceof ClassType) && type.isCompatibleWith(mathType); 444 } 445 446 450 public Type getSumReturnType(Type type) { 451 if (isFloatingPointType(type)) { 452 return doubleType.getWrapper(); 453 } else if (bigDecimalType.equals(type)) { 454 return bigDecimalType; 455 } else if (bigIntegerType.equals(type)) { 456 return bigIntegerType; 457 } else if (isNumberType(type)) { 458 return longType.getWrapper(); 459 } else { 460 return type; 461 } 462 } 463 464 468 public Type getAvgReturnType(Type type) { 469 if (bigDecimalType.equals(type)) { 470 return bigDecimalType; 471 } else if (bigIntegerType.equals(type)) { 472 return bigIntegerType; 473 } else if (isNumberType(type)) { 474 return doubleType.getWrapper(); 475 } else { 476 return type; 477 } 478 } 479 480 484 public Type getMinMaxReturnType(Type type) { 485 if (isFloatingPointType(type)) { 486 return doubleType.getWrapper(); 487 } else if (isCharType(type)) { 488 return charType.getWrapper(); 489 } else if (bigDecimalType.equals(type)) { 490 return bigDecimalType; 491 } else if (bigIntegerType.equals(type)) { 492 return bigIntegerType; 493 } else if (isNumberType(type)) { 494 return longType.getWrapper(); 495 } else { 496 return type; 497 } 498 } 499 } 500 | Popular Tags |