1 21 package oracle.toplink.essentials.internal.helper; 23 24 import java.math.BigDecimal ; 25 import java.math.BigInteger ; 26 27 import java.util.Date ; 28 import java.util.HashSet ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 38 public class BasicTypeHelperImpl { 39 40 41 private static Set integralTypes = new HashSet (); 42 43 private static Set floatingPointTypes = new HashSet (); 44 45 private static Set dateClasses = new HashSet (); 46 47 private static Map primitiveToWrapper = new HashMap (); 48 49 private static Map wrapperToPrimitive = new HashMap (); 50 51 static { 52 integralTypes.add(byte.class); 54 integralTypes.add(Byte .class); 55 integralTypes.add(short.class); 56 integralTypes.add(Short .class); 57 integralTypes.add(char.class); 58 integralTypes.add(Character .class); 59 integralTypes.add(int.class); 60 integralTypes.add(Integer .class); 61 integralTypes.add(long.class); 62 integralTypes.add(Long .class); 63 64 floatingPointTypes.add(float.class); 66 floatingPointTypes.add(Float .class); 67 floatingPointTypes.add(double.class); 68 floatingPointTypes.add(Double .class); 69 70 dateClasses.add(java.util.Date .class); 72 dateClasses.add(java.util.Calendar .class); 73 dateClasses.add(java.sql.Date .class); 74 dateClasses.add(java.sql.Time .class); 75 dateClasses.add(java.sql.Timestamp .class); 76 77 primitiveToWrapper.put(boolean.class, Boolean .class); 79 primitiveToWrapper.put(byte.class, Byte .class); 80 primitiveToWrapper.put(short.class, Short .class); 81 primitiveToWrapper.put(char.class, Character .class); 82 primitiveToWrapper.put(int.class, Integer .class); 83 primitiveToWrapper.put(long.class, Long .class); 84 primitiveToWrapper.put(float.class, Float .class); 85 primitiveToWrapper.put(double.class, Double .class); 86 87 wrapperToPrimitive.put(Boolean .class, boolean.class); 89 wrapperToPrimitive.put(Byte .class, byte.class); 90 wrapperToPrimitive.put(Short .class, short.class); 91 wrapperToPrimitive.put(Character .class, char.class); 92 wrapperToPrimitive.put(Integer .class, int.class); 93 wrapperToPrimitive.put(Long .class, long.class); 94 wrapperToPrimitive.put(Float .class, float.class); 95 wrapperToPrimitive.put(Double .class, double.class); 96 } 97 98 99 private static final BasicTypeHelperImpl singleton = new BasicTypeHelperImpl(); 100 101 102 public static BasicTypeHelperImpl getInstance() { 103 return singleton; 104 } 105 106 107 public Object getObjectType() { 108 return Object .class; 109 } 110 111 112 public Object getBooleanType() { 113 return boolean.class; 114 } 115 116 117 public Object getBooleanClassType() { 118 return Boolean .class; 119 } 120 121 122 public Object getCharType() { 123 return char.class; 124 } 125 126 127 public Object getCharacterClassType() { 128 return Character .class; 129 } 130 131 132 public Object getByteType() { 133 return byte.class; 134 } 135 136 137 public Object getByteClassType() { 138 return Byte .class; 139 } 140 141 142 public Object getShortType() { 143 return short.class; 144 } 145 146 147 public Object getShortClassType() { 148 return Short .class; 149 } 150 151 152 public Object getIntType() { 153 return int.class; 154 } 155 156 157 public Object getIntegerClassType() { 158 return Integer .class; 159 } 160 161 162 public Object getLongType() { 163 return long.class; 164 } 165 166 167 public Object getLongClassType() { 168 return Long .class; 169 } 170 171 172 public Object getFloatType() { 173 return float.class; 174 } 175 176 177 public Object getFloatClassType() { 178 return Float .class; 179 } 180 181 182 public Object getDoubleType() { 183 return double.class; 184 } 185 186 187 public Object getDoubleClassType() { 188 return Double .class; 189 } 190 191 192 public Object getStringType() { 193 return String .class; 194 } 195 196 197 public Object getBigIntegerType() { 198 return BigInteger .class; 199 } 200 201 202 public Object getBigDecimalType() { 203 return BigDecimal .class; 204 } 205 206 207 public Object getDateType() { 208 return Date .class; 209 } 210 211 212 public boolean isEnumType(Object type) { 213 Class clazz = (Class )type; 214 return (clazz != null) && (clazz.isEnum()); 215 } 216 217 218 public boolean isNumericType(Object type) { 219 return isIntegralType(type) || isFloatingPointType(type) || 220 isBigIntegerType(type) || isBigDecimalType(type); 221 } 222 223 227 public boolean isIntegralType(Object type) { 228 return integralTypes.contains(type); 229 } 230 231 235 public boolean isFloatingPointType(Object type) { 236 return floatingPointTypes.contains(type); 237 } 238 239 240 public boolean isWrapperClass(Object type) { 241 return wrapperToPrimitive.containsKey(type); 242 } 243 244 247 public boolean isBooleanType(Object type) { 248 return (type == getBooleanType()) || (type == getBooleanClassType()); 249 } 250 251 254 public boolean isCharacterType(Object type) { 255 return (type == getCharType()) || (type == getCharacterClassType()); 256 } 257 258 261 public boolean isByteType(Object type) { 262 return (type == getByteType()) || (type == getByteClassType()); 263 } 264 265 268 public boolean isShortType(Object type) { 269 return (type == getShortType()) || (type == getShortClassType()); 270 } 271 272 275 public boolean isIntType(Object type) { 276 return (type == getIntType()) || (type == getIntegerClassType()); 277 } 278 279 282 public boolean isLongType(Object type) { 283 return (type == getLongType()) || (type == getLongClassType()); 284 } 285 286 289 public boolean isFloatType(Object type) { 290 return (type == getFloatType()) || (type == getFloatClassType()); 291 } 292 293 296 public boolean isDoubleType(Object type) { 297 return (type == getDoubleType()) || (type == getDoubleClassType()); 298 } 299 300 301 public boolean isStringType(Object type) { 302 return type == getStringType(); 303 } 304 305 306 public boolean isDateClass(Object type) { 307 return dateClasses.contains(type); 308 } 309 310 311 public boolean isBigIntegerType(Object type) { 312 return type == getBigIntegerType(); 313 } 314 315 316 public boolean isBigDecimalType(Object type) { 317 return type == getBigDecimalType(); 318 } 319 320 321 public boolean isOrderableType(Object type) { 322 return isNumericType(type) || isStringType(type) || 323 isDateClass(type) || isEnumType(type); 324 } 325 326 327 public boolean isAssignableFrom(Object left, Object right) { 328 if ((left == null) || (right == null)) { 329 return false; 330 } 331 if (left == right) { 333 return true; 334 } 335 Object promoted = extendedBinaryNumericPromotion(left, right); 337 if (promoted != null) { 338 return true; 339 } 340 if (isDateClass(left) && isDateClass(right)) { 342 return true; 343 } 344 if (isBooleanType(left) && isBooleanType(right)) { 346 return true; 347 } 348 return (((Class )left).isAssignableFrom((Class )right)); 350 } 351 352 354 public Object extendedBinaryNumericPromotion(Object left, Object right) { 355 if ((left == null) || (right == null) || 356 !isNumericType(left) || !isNumericType(right)) { 357 return null; 358 } 359 360 if (isBigDecimalType(left) || isBigDecimalType(right)) { 362 return getBigDecimalType(); 363 } 364 365 if (isBigIntegerType(left)) { 367 return isFloatingPointType(right) ? right : getBigIntegerType(); 368 } 369 if (isBigIntegerType(right)) { 370 return isFloatingPointType(left) ? left : getBigIntegerType(); 371 } 372 373 boolean wrapper = false; 375 if (isWrapperClass(left)) { 376 wrapper = true; 377 left = getPrimitiveType(left); 378 } 379 if (isWrapperClass(right)) { 380 wrapper = true; 381 right = getPrimitiveType(right); 382 } 383 384 Object promoted = binaryNumericPromotion(left, right); 385 if (wrapper && promoted != null) { 386 promoted = getWrapperClass(promoted); 387 } 388 return promoted; 389 } 390 391 393 394 protected Object getPrimitiveType(Object wrapper) { 395 return wrapperToPrimitive.get(wrapper); 396 } 397 398 399 protected Object getWrapperClass(Object primitive) { 400 return primitiveToWrapper.get(primitive); 401 } 402 403 404 protected Object binaryNumericPromotion(Object left, Object right) { 405 if ((left == null) || (right == null)) { 406 return null; 407 } 408 Object type = null; 409 410 if (left == getDoubleType() || right == getDoubleType()) { 411 type = getDoubleType(); 412 } else if (left == getFloatType() || right == getFloatType()) { 413 type = getFloatType(); 414 } else if (left == getLongType() || right == getLongType()) { 415 type = getLongType(); 416 } else if (isIntegralType(left) && isIntegralType(right)) { 417 type = getIntType(); 418 } 419 return type; 420 } 421 422 } 423 424 | Popular Tags |