1 25 package com.sun.el.lang; 26 27 import java.math.BigDecimal ; 28 import java.math.BigInteger ; 29 30 import com.sun.el.util.MessageFactory; 31 32 37 public abstract class ELArithmetic { 38 39 public final static class BigDecimalDelegate extends ELArithmetic { 40 41 protected Number add(Number num0, Number num1) { 42 return ((BigDecimal ) num0).add((BigDecimal ) num1); 43 } 44 45 protected Number coerce(Number num) { 46 if (num instanceof BigDecimal ) 47 return num; 48 if (num instanceof BigInteger ) 49 return new BigDecimal ((BigInteger ) num); 50 return new BigDecimal (num.doubleValue()); 51 } 52 53 protected Number coerce(String str) { 54 return new BigDecimal (str); 55 } 56 57 protected Number divide(Number num0, Number num1) { 58 return ((BigDecimal ) num0).divide((BigDecimal ) num1, 59 BigDecimal.ROUND_HALF_UP); 60 } 61 62 protected Number subtract(Number num0, Number num1) { 63 return ((BigDecimal ) num0).subtract((BigDecimal ) num1); 64 } 65 66 protected Number mod(Number num0, Number num1) { 67 return new Double (num0.doubleValue() % num1.doubleValue()); 68 } 69 70 protected Number multiply(Number num0, Number num1) { 71 return ((BigDecimal ) num0).multiply((BigDecimal ) num1); 72 } 73 74 public boolean matches(Object obj0, Object obj1) { 75 return (obj0 instanceof BigDecimal || obj1 instanceof BigDecimal ); 76 } 77 } 78 79 public final static class BigIntegerDelegate extends ELArithmetic { 80 81 protected Number add(Number num0, Number num1) { 82 return ((BigInteger ) num0).add((BigInteger ) num1); 83 } 84 85 protected Number coerce(Number num) { 86 if (num instanceof BigInteger ) 87 return num; 88 return new BigInteger (num.toString()); 89 } 90 91 protected Number coerce(String str) { 92 return new BigInteger (str); 93 } 94 95 protected Number divide(Number num0, Number num1) { 96 return (new BigDecimal ((BigInteger ) num0)).divide(new BigDecimal ((BigInteger ) num1), BigDecimal.ROUND_HALF_UP); 97 } 98 99 protected Number multiply(Number num0, Number num1) { 100 return ((BigInteger ) num0).multiply((BigInteger ) num1); 101 } 102 103 protected Number mod(Number num0, Number num1) { 104 return ((BigInteger ) num0).mod((BigInteger ) num1); 105 } 106 107 protected Number subtract(Number num0, Number num1) { 108 return ((BigInteger ) num0).subtract((BigInteger ) num1); 109 } 110 111 public boolean matches(Object obj0, Object obj1) { 112 return (obj0 instanceof BigInteger || obj1 instanceof BigInteger ); 113 } 114 } 115 116 public final static class DoubleDelegate extends ELArithmetic { 117 118 protected Number add(Number num0, Number num1) { 119 if (num0 instanceof BigDecimal ) { 121 return ((BigDecimal ) num0).add(new BigDecimal (num1.doubleValue())); 122 } else if (num1 instanceof BigDecimal ) { 123 return ((new BigDecimal (num0.doubleValue()).add((BigDecimal ) num1))); 124 } 125 return new Double (num0.doubleValue() + num1.doubleValue()); 126 } 127 128 protected Number coerce(Number num) { 129 if (num instanceof Double ) 130 return num; 131 if (num instanceof BigInteger ) 132 return new BigDecimal ((BigInteger ) num); 133 return new Double (num.doubleValue()); 134 } 135 136 protected Number coerce(String str) { 137 return new Double (str); 138 } 139 140 protected Number divide(Number num0, Number num1) { 141 return new Double (num0.doubleValue() / num1.doubleValue()); 142 } 143 144 protected Number mod(Number num0, Number num1) { 145 return new Double (num0.doubleValue() % num1.doubleValue()); 146 } 147 148 protected Number subtract(Number num0, Number num1) { 149 if (num0 instanceof BigDecimal ) { 151 return ((BigDecimal ) num0).subtract(new BigDecimal (num1.doubleValue())); 152 } else if (num1 instanceof BigDecimal ) { 153 return ((new BigDecimal (num0.doubleValue()).subtract((BigDecimal ) num1))); 154 } 155 return new Double (num0.doubleValue() - num1.doubleValue()); 156 } 157 158 protected Number multiply(Number num0, Number num1) { 159 if (num0 instanceof BigDecimal ) { 161 return ((BigDecimal ) num0).multiply(new BigDecimal (num1.doubleValue())); 162 } else if (num1 instanceof BigDecimal ) { 163 return ((new BigDecimal (num0.doubleValue()).multiply((BigDecimal ) num1))); 164 } 165 return new Double (num0.doubleValue() * num1.doubleValue()); 166 } 167 168 public boolean matches(Object obj0, Object obj1) { 169 return (obj0 instanceof Double 170 || obj1 instanceof Double 171 || obj0 instanceof Float 172 || obj1 instanceof Float 173 || (obj0 != null && (Double.TYPE == obj0.getClass() || Float.TYPE == obj0.getClass())) 174 || (obj1 != null && (Double.TYPE == obj1.getClass() || Float.TYPE == obj1.getClass())) 175 || (obj0 instanceof String && ELSupport 176 .isStringFloat((String ) obj0)) || (obj1 instanceof String && ELSupport 177 .isStringFloat((String ) obj1))); 178 } 179 } 180 181 public final static class LongDelegate extends ELArithmetic { 182 183 protected Number add(Number num0, Number num1) { 184 return new Long (num0.longValue() + num1.longValue()); 185 } 186 187 protected Number coerce(Number num) { 188 if (num instanceof Long ) 189 return num; 190 return new Long (num.longValue()); 191 } 192 193 protected Number coerce(String str) { 194 return new Long (str); 195 } 196 197 protected Number divide(Number num0, Number num1) { 198 return new Long (num0.longValue() / num1.longValue()); 199 } 200 201 protected Number mod(Number num0, Number num1) { 202 return new Long (num0.longValue() % num1.longValue()); 203 } 204 205 protected Number subtract(Number num0, Number num1) { 206 return new Long (num0.longValue() - num1.longValue()); 207 } 208 209 protected Number multiply(Number num0, Number num1) { 210 return new Long (num0.longValue() * num1.longValue()); 211 } 212 213 public boolean matches(Object obj0, Object obj1) { 214 return (obj0 instanceof Long || obj1 instanceof Long ); 215 } 216 } 217 218 public final static BigDecimalDelegate BIGDECIMAL = new BigDecimalDelegate(); 219 220 public final static BigIntegerDelegate BIGINTEGER = new BigIntegerDelegate(); 221 222 public final static DoubleDelegate DOUBLE = new DoubleDelegate(); 223 224 public final static LongDelegate LONG = new LongDelegate(); 225 226 private final static Long ZERO = new Long (0); 227 228 public final static Number add(final Object obj0, final Object obj1) { 229 if (obj0 == null && obj1 == null) { 230 return new Long (0); 231 } 232 233 final ELArithmetic delegate; 234 if (BIGDECIMAL.matches(obj0, obj1)) 235 delegate = BIGDECIMAL; 236 else if (DOUBLE.matches(obj0, obj1)) 237 delegate = DOUBLE; 238 else if (BIGINTEGER.matches(obj0, obj1)) 239 delegate = BIGINTEGER; 240 else 241 delegate = LONG; 242 243 Number num0 = delegate.coerce(obj0); 244 Number num1 = delegate.coerce(obj1); 245 246 return delegate.add(num0, num1); 247 } 248 249 public final static Number mod(final Object obj0, final Object obj1) { 250 if (obj0 == null && obj1 == null) { 251 return new Long (0); 252 } 253 254 final ELArithmetic delegate; 255 if (BIGDECIMAL.matches(obj0, obj1)) 256 delegate = BIGDECIMAL; 257 else if (DOUBLE.matches(obj0, obj1)) 258 delegate = DOUBLE; 259 else if (BIGINTEGER.matches(obj0, obj1)) 260 delegate = BIGINTEGER; 261 else 262 delegate = LONG; 263 264 Number num0 = delegate.coerce(obj0); 265 Number num1 = delegate.coerce(obj1); 266 267 return delegate.mod(num0, num1); 268 } 269 270 public final static Number subtract(final Object obj0, final Object obj1) { 271 if (obj0 == null && obj1 == null) { 272 return new Long (0); 273 } 274 275 final ELArithmetic delegate; 276 if (BIGDECIMAL.matches(obj0, obj1)) 277 delegate = BIGDECIMAL; 278 else if (DOUBLE.matches(obj0, obj1)) 279 delegate = DOUBLE; 280 else if (BIGINTEGER.matches(obj0, obj1)) 281 delegate = BIGINTEGER; 282 else 283 delegate = LONG; 284 285 Number num0 = delegate.coerce(obj0); 286 Number num1 = delegate.coerce(obj1); 287 288 return delegate.subtract(num0, num1); 289 } 290 291 public final static Number divide(final Object obj0, final Object obj1) { 292 if (obj0 == null && obj1 == null) { 293 return ZERO; 294 } 295 296 final ELArithmetic delegate; 297 if (BIGDECIMAL.matches(obj0, obj1)) 298 delegate = BIGDECIMAL; 299 else if (BIGINTEGER.matches(obj0, obj1)) 300 delegate = BIGDECIMAL; 301 else 302 delegate = DOUBLE; 303 304 Number num0 = delegate.coerce(obj0); 305 Number num1 = delegate.coerce(obj1); 306 307 return delegate.divide(num0, num1); 308 } 309 310 public final static Number multiply(final Object obj0, final Object obj1) { 311 if (obj0 == null && obj1 == null) { 312 return new Long (0); 313 } 314 315 final ELArithmetic delegate; 316 if (BIGDECIMAL.matches(obj0, obj1)) 317 delegate = BIGDECIMAL; 318 else if (DOUBLE.matches(obj0, obj1)) 319 delegate = DOUBLE; 320 else if (BIGINTEGER.matches(obj0, obj1)) 321 delegate = BIGINTEGER; 322 else 323 delegate = LONG; 324 325 Number num0 = delegate.coerce(obj0); 326 Number num1 = delegate.coerce(obj1); 327 328 return delegate.multiply(num0, num1); 329 } 330 331 public final static boolean isNumber(final Object obj) { 332 return (obj != null && isNumberType(obj.getClass())); 333 } 334 335 public final static boolean isNumberType(final Class type) { 336 return type == (java.lang.Long .class) || type == Long.TYPE || type == (java.lang.Double .class) || type == Double.TYPE || type == (java.lang.Byte .class) || type == Byte.TYPE || type == (java.lang.Short .class) || type == Short.TYPE || type == (java.lang.Integer .class) || type == Integer.TYPE || type == (java.lang.Float .class) || type == Float.TYPE || type == (java.math.BigInteger .class) || type == (java.math.BigDecimal .class); 337 } 338 339 342 protected ELArithmetic() { 343 super(); 344 } 345 346 protected abstract Number add(final Number num0, final Number num1); 347 348 protected abstract Number multiply(final Number num0, final Number num1); 349 350 protected abstract Number subtract(final Number num0, final Number num1); 351 352 protected abstract Number mod(final Number num0, final Number num1); 353 354 protected abstract Number coerce(final Number num); 355 356 protected final Number coerce(final Object obj) { 357 358 if (isNumber(obj)) { 359 return coerce((Number ) obj); 360 } 361 if (obj instanceof String ) { 362 return coerce((String ) obj); 363 } 364 if (obj == null || "".equals(obj)) { 365 return coerce(ZERO); 366 } 367 368 Class objType = obj.getClass(); 369 if (Character .class.equals(objType) || Character.TYPE == objType) { 370 return coerce(new Short ((short) ((Character ) obj).charValue())); 371 } 372 373 throw new IllegalArgumentException (MessageFactory.get("el.convert", obj, 374 objType)); 375 } 376 377 protected abstract Number coerce(final String str); 378 379 protected abstract Number divide(final Number num0, final Number num1); 380 381 protected abstract boolean matches(final Object obj0, final Object obj1); 382 383 } 384 | Popular Tags |