1 25 package com.sun.el.lang; 26 27 import java.beans.PropertyEditor ; 28 import java.beans.PropertyEditorManager ; 29 import java.math.BigDecimal ; 30 import java.math.BigInteger ; 31 32 import javax.el.ELException; 33 import javax.el.PropertyNotFoundException; 34 35 import com.sun.el.util.MessageFactory; 36 37 43 public class ELSupport { 44 45 private final static ELSupport REF = new ELSupport(); 46 47 private final static Long ZERO = new Long (0L); 48 49 public final static void throwUnhandled(Object base, Object property) 50 throws ELException { 51 if (base == null) { 52 throw new PropertyNotFoundException(MessageFactory.get( 53 "error.resolver.unhandled.null", property)); 54 } else { 55 throw new PropertyNotFoundException(MessageFactory.get( 56 "error.resolver.unhandled", base.getClass(), property)); 57 } 58 } 59 60 66 public final static int compare(final Object obj0, final Object obj1) 67 throws ELException { 68 if (obj0 == obj1 || equals(obj0, obj1)) { 69 return 0; 70 } 71 if (isBigDecimalOp(obj0, obj1)) { 72 BigDecimal bd0 = (BigDecimal ) coerceToNumber(obj0, BigDecimal .class); 73 BigDecimal bd1 = (BigDecimal ) coerceToNumber(obj1, BigDecimal .class); 74 return bd0.compareTo(bd1); 75 } 76 if (isDoubleOp(obj0, obj1)) { 77 Double d0 = (Double ) coerceToNumber(obj0, Double .class); 78 Double d1 = (Double ) coerceToNumber(obj1, Double .class); 79 return d0.compareTo(d1); 80 } 81 if (isBigIntegerOp(obj0, obj1)) { 82 BigInteger bi0 = (BigInteger ) coerceToNumber(obj0, BigInteger .class); 83 BigInteger bi1 = (BigInteger ) coerceToNumber(obj1, BigInteger .class); 84 return bi0.compareTo(bi1); 85 } 86 if (isLongOp(obj0, obj1)) { 87 Long l0 = (Long ) coerceToNumber(obj0, Long .class); 88 Long l1 = (Long ) coerceToNumber(obj1, Long .class); 89 return l0.compareTo(l1); 90 } 91 if (obj0 instanceof String || obj1 instanceof String ) { 92 return coerceToString(obj0).compareTo(coerceToString(obj1)); 93 } 94 if (obj0 instanceof Comparable ) { 95 return (obj1 != null) ? ((Comparable ) obj0).compareTo(obj1) : 1; 96 } 97 if (obj1 instanceof Comparable ) { 98 return (obj0 != null) ? -((Comparable ) obj1).compareTo(obj0) : -1; 99 } 100 throw new ELException(MessageFactory.get("error.compare", obj0, obj1)); 101 } 102 103 109 public final static boolean equals(final Object obj0, final Object obj1) 110 throws ELException { 111 if (obj0 == obj1) { 112 return true; 113 } 114 if (obj0 == null || obj1 == null) { 115 return false; 116 } 117 if (obj0 instanceof Boolean || obj1 instanceof Boolean ) { 118 return coerceToBoolean(obj0).equals(coerceToBoolean(obj1)); 119 } 120 if (obj0.getClass().isEnum()) { 121 return obj0.equals(coerceToEnum(obj1, obj0.getClass())); 122 } 123 if (obj1.getClass().isEnum()) { 124 return obj1.equals(coerceToEnum(obj0, obj1.getClass())); 125 } 126 if (obj0 instanceof String || obj1 instanceof String ) { 127 return coerceToString(obj0).equals(coerceToString(obj1)); 128 } 129 if (isBigDecimalOp(obj0, obj1)) { 130 BigDecimal bd0 = (BigDecimal ) coerceToNumber(obj0, BigDecimal .class); 131 BigDecimal bd1 = (BigDecimal ) coerceToNumber(obj1, BigDecimal .class); 132 return bd0.equals(bd1); 133 } 134 if (isDoubleOp(obj0, obj1)) { 135 Double d0 = (Double ) coerceToNumber(obj0, Double .class); 136 Double d1 = (Double ) coerceToNumber(obj1, Double .class); 137 return d0.equals(d1); 138 } 139 if (isBigIntegerOp(obj0, obj1)) { 140 BigInteger bi0 = (BigInteger ) coerceToNumber(obj0, BigInteger .class); 141 BigInteger bi1 = (BigInteger ) coerceToNumber(obj1, BigInteger .class); 142 return bi0.equals(bi1); 143 } 144 if (isLongOp(obj0, obj1)) { 145 Long l0 = (Long ) coerceToNumber(obj0, Long .class); 146 Long l1 = (Long ) coerceToNumber(obj1, Long .class); 147 return l0.equals(l1); 148 } else { 149 return obj0.equals(obj1); 150 } 151 } 152 153 157 public final static Boolean coerceToBoolean(final Object obj) 158 throws IllegalArgumentException { 159 if (obj == null || "".equals(obj)) { 160 return Boolean.FALSE; 161 } 162 if (obj instanceof Boolean || obj.getClass() == Boolean.TYPE) { 163 return (Boolean ) obj; 164 } 165 if (obj instanceof String ) { 166 return Boolean.valueOf((String ) obj); 167 } 168 169 throw new IllegalArgumentException (MessageFactory.get("error.convert", 170 obj, obj.getClass(), Boolean .class)); 171 } 172 173 public final static Enum coerceToEnum(final Object obj, Class type) 174 throws IllegalArgumentException { 175 if (obj == null || "".equals(obj)) { 176 return null; 177 } 178 if (type.isInstance(obj)) { 179 return (Enum )obj; 180 } 181 if (obj instanceof String ) { 182 return Enum.valueOf(type, (String ) obj); 183 } 184 185 throw new IllegalArgumentException (MessageFactory.get("error.convert", 186 obj, obj.getClass(), type)); 187 } 188 189 public final static Character coerceToCharacter(final Object obj) 190 throws IllegalArgumentException { 191 if (obj == null || "".equals(obj)) { 192 return new Character ((char) 0); 193 } 194 if (obj instanceof String ) { 195 return new Character (((String ) obj).charAt(0)); 196 } 197 if (ELArithmetic.isNumber(obj)) { 198 return new Character ((char) ((Number ) obj).shortValue()); 199 } 200 Class objType = obj.getClass(); 201 if (obj instanceof Character || objType == Character.TYPE) { 202 return (Character ) obj; 203 } 204 205 throw new IllegalArgumentException (MessageFactory.get("error.convert", 206 obj, objType, Character .class)); 207 } 208 209 public final static Number coerceToNumber(final Object obj) { 210 if (obj == null) { 211 return ZERO; 212 } else if (obj instanceof Number ) { 213 return (Number ) obj; 214 } else { 215 String str = coerceToString(obj); 216 if (isStringFloat(str)) { 217 return toFloat(str); 218 } else { 219 return toNumber(str); 220 } 221 } 222 } 223 224 protected final static Number coerceToNumber(final Number number, 225 final Class type) throws IllegalArgumentException { 226 if (Long.TYPE == type || Long .class.equals(type)) { 227 return new Long (number.longValue()); 228 } 229 if (Double.TYPE == type || Double .class.equals(type)) { 230 return new Double (number.doubleValue()); 231 } 232 if (Integer.TYPE == type || Integer .class.equals(type)) { 233 return new Integer (number.intValue()); 234 } 235 if (BigInteger .class.equals(type)) { 236 if (number instanceof BigDecimal ) { 237 return ((BigDecimal ) number).toBigInteger(); 238 } 239 return BigInteger.valueOf(number.longValue()); 240 } 241 if (BigDecimal .class.equals(type)) { 242 if (number instanceof BigInteger ) { 243 return new BigDecimal ((BigInteger ) number); 244 } 245 return new BigDecimal (number.doubleValue()); 247 } 248 if (Byte.TYPE == type || Byte .class.equals(type)) { 249 return new Byte (number.byteValue()); 250 } 251 if (Short.TYPE == type || Short .class.equals(type)) { 252 return new Short (number.shortValue()); 253 } 254 if (Float.TYPE == type || Float .class.equals(type)) { 255 return new Float (number.floatValue()); 256 } 257 258 throw new IllegalArgumentException (MessageFactory.get("error.convert", 259 number, number.getClass(), type)); 260 } 261 262 public final static Number coerceToNumber(final Object obj, final Class type) 263 throws IllegalArgumentException { 264 if (obj == null || "".equals(obj)) { 265 return coerceToNumber(ZERO, type); 266 } 267 if (obj instanceof String ) { 268 return coerceToNumber((String ) obj, type); 269 } 270 if (ELArithmetic.isNumber(obj)) { 271 return coerceToNumber((Number ) obj, type); 272 } 273 274 Class objType = obj.getClass(); 275 if (Character .class.equals(objType) || Character.TYPE == objType) { 276 return coerceToNumber(new Short ((short) ((Character ) obj) 277 .charValue()), type); 278 } 279 280 throw new IllegalArgumentException (MessageFactory.get("error.convert", 281 obj, objType, type)); 282 } 283 284 protected final static Number coerceToNumber(final String val, 285 final Class type) throws IllegalArgumentException { 286 if (Long.TYPE == type || Long .class.equals(type)) { 287 return Long.valueOf(val); 288 } 289 if (Integer.TYPE == type || Integer .class.equals(type)) { 290 return Integer.valueOf(val); 291 } 292 if (Double.TYPE == type || Double .class.equals(type)) { 293 return Double.valueOf(val); 294 } 295 if (BigInteger .class.equals(type)) { 296 return new BigInteger (val); 297 } 298 if (BigDecimal .class.equals(type)) { 299 return new BigDecimal (val); 300 } 301 if (Byte.TYPE == type || Byte .class.equals(type)) { 302 return Byte.valueOf(val); 303 } 304 if (Short.TYPE == type || Short .class.equals(type)) { 305 return Short.valueOf(val); 306 } 307 if (Float.TYPE == type || Float .class.equals(type)) { 308 return Float.valueOf(val); 309 } 310 311 throw new IllegalArgumentException (MessageFactory.get("error.convert", 312 val, String .class, type)); 313 } 314 315 319 public final static String coerceToString(final Object obj) { 320 if (obj == null) { 321 return ""; 322 } else if (obj instanceof String ) { 323 return (String ) obj; 324 } else if (obj instanceof Enum ) { 325 return ((Enum ) obj).name(); 326 } else { 327 return obj.toString(); 328 } 329 } 330 331 public final static Object coerceToType(final Object obj, final Class type) 332 throws IllegalArgumentException { 333 if (type == null || Object .class.equals(type)) { 334 return obj; 335 } 336 if (String .class.equals(type)) { 337 return coerceToString(obj); 338 } 339 if (ELArithmetic.isNumberType(type)) { 340 return coerceToNumber(obj, type); 341 } 342 if (Character .class.equals(type) || Character.TYPE == type) { 343 return coerceToCharacter(obj); 344 } 345 if (Boolean .class.equals(type) || Boolean.TYPE == type) { 346 return coerceToBoolean(obj); 347 } 348 if (type.isEnum()) { 349 return coerceToEnum(obj, type); 350 } 351 if (obj != null && type.isAssignableFrom(obj.getClass())) { 352 return obj; 353 } 354 355 if (obj == null) 357 return null; 358 if (obj instanceof String ) { 359 if ("".equals(obj)) 360 return null; 361 PropertyEditor editor = PropertyEditorManager.findEditor(type); 362 if (editor != null) { 363 editor.setAsText((String ) obj); 364 return editor.getValue(); 365 } 366 } 367 throw new IllegalArgumentException (MessageFactory.get("error.convert", 368 obj, obj.getClass(), type)); 369 } 370 371 375 public final static boolean containsNulls(final Object [] obj) { 376 for (int i = 0; i < obj.length; i++) { 377 if (obj[0] == null) { 378 return true; 379 } 380 } 381 return false; 382 } 383 384 public final static boolean isBigDecimalOp(final Object obj0, 385 final Object obj1) { 386 return (obj0 instanceof BigDecimal || obj1 instanceof BigDecimal ); 387 } 388 389 public final static boolean isBigIntegerOp(final Object obj0, 390 final Object obj1) { 391 return (obj0 instanceof BigInteger || obj1 instanceof BigInteger ); 392 } 393 394 public final static boolean isDoubleOp(final Object obj0, final Object obj1) { 395 return (obj0 instanceof Double 396 || obj1 instanceof Double 397 || obj0 instanceof Float 398 || obj1 instanceof Float 399 || (obj0 != null && (Double.TYPE == obj0.getClass() || Float.TYPE == obj0 400 .getClass())) || (obj1 != null && (Double.TYPE == obj1 401 .getClass() || Float.TYPE == obj1.getClass()))); 402 } 403 404 public final static boolean isDoubleStringOp(final Object obj0, 405 final Object obj1) { 406 return (isDoubleOp(obj0, obj1) 407 || (obj0 instanceof String && isStringFloat((String ) obj0)) || (obj1 instanceof String && isStringFloat((String ) obj1))); 408 } 409 410 public final static boolean isLongOp(final Object obj0, final Object obj1) { 411 return (obj0 instanceof Long 412 || obj1 instanceof Long 413 || obj0 instanceof Integer 414 || obj1 instanceof Integer 415 || obj0 instanceof Character 416 || obj1 instanceof Character 417 || obj0 instanceof Short 418 || obj1 instanceof Short 419 || obj0 instanceof Byte 420 || obj1 instanceof Byte 421 || (obj0 != null && (Long.TYPE == obj0.getClass() 422 || Integer.TYPE == obj0.getClass() 423 || Character.TYPE == obj0.getClass() 424 || Short.TYPE == obj0.getClass() || Byte.TYPE == obj0 425 .getClass())) || (obj0 != null && (Long.TYPE == obj0 426 .getClass() 427 || Integer.TYPE == obj0.getClass() 428 || Character.TYPE == obj0.getClass() 429 || Short.TYPE == obj0.getClass() || Byte.TYPE == obj0 430 .getClass()))); 431 } 432 433 public final static boolean isStringFloat(final String str) { 434 int len = str.length(); 435 if (len > 1) { 436 char c = 0; 437 for (int i = 0; i < len; i++) { 438 switch (c = str.charAt(i)) { 439 case 'E': 440 return true; 441 case 'e': 442 return true; 443 case '.': 444 return true; 445 } 446 } 447 } 448 return false; 449 } 450 451 public final static Number toFloat(final String value) { 452 try { 453 if (Double.parseDouble(value) > Double.MAX_VALUE) { 454 return new BigDecimal (value); 455 } else { 456 return new Double (value); 457 } 458 } catch (NumberFormatException e0) { 459 return new BigDecimal (value); 460 } 461 } 462 463 public final static Number toNumber(final String value) { 464 try { 465 return new Integer (Integer.parseInt(value)); 466 } catch (NumberFormatException e0) { 467 try { 468 return new Long (Long.parseLong(value)); 469 } catch (NumberFormatException e1) { 470 return new BigInteger (value); 471 } 472 } 473 } 474 475 478 public ELSupport() { 479 super(); 480 } 481 482 } 483 | Popular Tags |