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