| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package net.sf.antcontrib.math; |
| 17 |
|
| 18 |
import org.apache.tools.ant.BuildException; |
| 19 |
|
| 20 |
import java.lang.reflect.InvocationTargetException; |
| 21 |
import java.lang.reflect.Method; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class Math |
| 32 |
{ |
| 33 |
public static final Number evaluate(String operation, |
| 34 |
String datatype, |
| 35 |
boolean strict, |
| 36 |
Evaluateable operands[]) |
| 37 |
{ |
| 38 |
if (datatype == null) |
| 39 |
datatype = "double"; |
| 40 |
|
| 41 |
try |
| 42 |
{ |
| 43 |
operation = operation.toLowerCase(); |
| 44 |
|
| 45 |
Method m = Math.class.getDeclaredMethod(operation, |
| 46 |
new Class[]{ |
| 47 |
String.class, |
| 48 |
Boolean.TYPE, |
| 49 |
operands.getClass() |
| 50 |
}); |
| 51 |
Number n = (Number) m.invoke(null, |
| 52 |
new Object[]{ |
| 53 |
datatype, |
| 54 |
strict ? Boolean.TRUE : Boolean.FALSE, |
| 55 |
operands |
| 56 |
}); |
| 57 |
|
| 58 |
return n; |
| 59 |
} |
| 60 |
catch (NoSuchMethodException e) |
| 61 |
{ |
| 62 |
e.printStackTrace(); |
| 63 |
} |
| 64 |
catch (IllegalAccessException e) |
| 65 |
{ |
| 66 |
e.printStackTrace(); |
| 67 |
} |
| 68 |
catch (InvocationTargetException e) |
| 69 |
{ |
| 70 |
e.getTargetException().printStackTrace(); |
| 71 |
} |
| 72 |
return null; |
| 73 |
} |
| 74 |
|
| 75 |
public static final Number add(String datatype, |
| 76 |
boolean strict, |
| 77 |
Evaluateable operands[]) |
| 78 |
{ |
| 79 |
Number result = null; |
| 80 |
|
| 81 |
Number numbers[] = new Number[operands.length]; |
| 82 |
for (int i = 0; i < operands.length; i++) |
| 83 |
numbers[i] = operands[i].evaluate(); |
| 84 |
|
| 85 |
if (datatype.equalsIgnoreCase("int")) |
| 86 |
{ |
| 87 |
int sum = 0; |
| 88 |
for (int i = 0; i < numbers.length; i++) |
| 89 |
sum += numbers[i].intValue(); |
| 90 |
result = new Integer(sum); |
| 91 |
} |
| 92 |
else if (datatype.equalsIgnoreCase("long")) |
| 93 |
{ |
| 94 |
long sum = 0; |
| 95 |
for (int i = 0; i < numbers.length; i++) |
| 96 |
sum += numbers[i].longValue(); |
| 97 |
result = new Long(sum); |
| 98 |
} |
| 99 |
else if (datatype.equalsIgnoreCase("float")) |
| 100 |
{ |
| 101 |
float sum = 0; |
| 102 |
for (int i = 0; i < numbers.length; i++) |
| 103 |
sum += numbers[i].floatValue(); |
| 104 |
result = new Float(sum); |
| 105 |
} |
| 106 |
else if (datatype.equalsIgnoreCase("double")) |
| 107 |
{ |
| 108 |
double sum = 0; |
| 109 |
for (int i = 0; i < numbers.length; i++) |
| 110 |
sum += numbers[i].doubleValue(); |
| 111 |
result = new Double(sum); |
| 112 |
} |
| 113 |
return result; |
| 114 |
} |
| 115 |
|
| 116 |
public static final Number subtract(String datatype, |
| 117 |
boolean strict, |
| 118 |
Evaluateable operands[]) |
| 119 |
{ |
| 120 |
Number result = null; |
| 121 |
|
| 122 |
Number numbers[] = new Number[operands.length]; |
| 123 |
for (int i = 0; i < operands.length; i++) |
| 124 |
numbers[i] = operands[i].evaluate(); |
| 125 |
|
| 126 |
if (datatype.equalsIgnoreCase("int")) |
| 127 |
{ |
| 128 |
int sum = numbers[0].intValue(); |
| 129 |
for (int i = 1; i < numbers.length; i++) |
| 130 |
sum -= numbers[i].intValue(); |
| 131 |
result = new Integer(sum); |
| 132 |
} |
| 133 |
else if (datatype.equalsIgnoreCase("long")) |
| 134 |
{ |
| 135 |
long sum = numbers[0].longValue(); |
| 136 |
for (int i = 1; i < numbers.length; i++) |
| 137 |
sum -= numbers[i].longValue(); |
| 138 |
result = new Long(sum); |
| 139 |
} |
| 140 |
else if (datatype.equalsIgnoreCase("float")) |
| 141 |
{ |
| 142 |
float sum = numbers[0].floatValue(); |
| 143 |
for (int i = 1; i < numbers.length; i++) |
| 144 |
sum -= numbers[i].floatValue(); |
| 145 |
result = new Float(sum); |
| 146 |
} |
| 147 |
else if (datatype.equalsIgnoreCase("double")) |
| 148 |
{ |
| 149 |
double sum = numbers[0].doubleValue(); |
| 150 |
for (int i = 1; i < numbers.length; i++) |
| 151 |
sum -= numbers[i].doubleValue(); |
| 152 |
result = new Double(sum); |
| 153 |
} |
| 154 |
return result; |
| 155 |
} |
| 156 |
|
| 157 |
public static final Number multiply(String datatype, |
| 158 |
boolean strict, |
| 159 |
Evaluateable operands[]) |
| 160 |
{ |
| 161 |
Number result = null; |
| 162 |
|
| 163 |
Number numbers[] = new Number[operands.length]; |
| 164 |
for (int i = 0; i < operands.length; i++) |
| 165 |
numbers[i] = operands[i].evaluate(); |
| 166 |
|
| 167 |
if (datatype.equalsIgnoreCase("int")) |
| 168 |
{ |
| 169 |
int sum = 1; |
| 170 |
for (int i = 0; i < numbers.length; i++) |
| 171 |
sum *= numbers[i].intValue(); |
| 172 |
result = new Integer(sum); |
| 173 |
} |
| 174 |
else if (datatype.equalsIgnoreCase("long")) |
| 175 |
{ |
| 176 |
long sum = 1; |
| 177 |
for (int i = 0; i < numbers.length; i++) |
| 178 |
sum *= numbers[i].longValue(); |
| 179 |
result = new Long(sum); |
| 180 |
} |
| 181 |
else if (datatype.equalsIgnoreCase("float")) |
| 182 |
{ |
| 183 |
float sum = 1; |
| 184 |
for (int i = 0; i < numbers.length; i++) |
| 185 |
sum *= numbers[i].floatValue(); |
| 186 |
result = new Float(sum); |
| 187 |
} |
| 188 |
else if (datatype.equalsIgnoreCase("double")) |
| 189 |
{ |
| 190 |
double sum = 1; |
| 191 |
for (int i = 0; i < numbers.length; i++) |
| 192 |
sum *= numbers[i].doubleValue(); |
| 193 |
result = new Double(sum); |
| 194 |
} |
| 195 |
return result; |
| 196 |
} |
| 197 |
|
| 198 |
public static final Number divide(String datatype, |
| 199 |
boolean strict, |
| 200 |
Evaluateable operands[]) |
| 201 |
{ |
| 202 |
Number result = null; |
| 203 |
|
| 204 |
Number numbers[] = new Number[operands.length]; |
| 205 |
for (int i = 0; i < operands.length; i++) |
| 206 |
numbers[i] = operands[i].evaluate(); |
| 207 |
|
| 208 |
if (datatype.equalsIgnoreCase("int")) |
| 209 |
{ |
| 210 |
int sum = numbers[0].intValue(); |
| 211 |
for (int i = 1; i < numbers.length; i++) |
| 212 |
sum /= numbers[i].intValue(); |
| 213 |
result = new Integer(sum); |
| 214 |
} |
| 215 |
else if (datatype.equalsIgnoreCase("long")) |
| 216 |
{ |
| 217 |
long sum = numbers[0].longValue(); |
| 218 |
for (int i = 1; i < numbers.length; i++) |
| 219 |
sum /= numbers[i].longValue(); |
| 220 |
result = new Long(sum); |
| 221 |
} |
| 222 |
else if (datatype.equalsIgnoreCase("float")) |
| 223 |
{ |
| 224 |
float sum = numbers[0].floatValue(); |
| 225 |
for (int i = 1; i < numbers.length; i++) |
| 226 |
sum /= numbers[i].floatValue(); |
| 227 |
result = new Float(sum); |
| 228 |
} |
| 229 |
else if (datatype.equalsIgnoreCase("double")) |
| 230 |
{ |
| 231 |
double sum = numbers[0].doubleValue(); |
| 232 |
for (int i = 1; i < numbers.length; i++) |
| 233 |
sum /= numbers[i].doubleValue(); |
| 234 |
result = new Double(sum); |
| 235 |
} |
| 236 |
return result; |
| 237 |
} |
| 238 |
|
| 239 |
public static final Number mod(String datatype, |
| 240 |
boolean strict, |
| 241 |
Evaluateable operands[]) |
| 242 |
{ |
| 243 |
Number result = null; |
| 244 |
|
| 245 |
Number numbers[] = new Number[operands.length]; |
| 246 |
for (int i = 0; i < operands.length; i++) |
| 247 |
numbers[i] = operands[i].evaluate(); |
| 248 |
|
| 249 |
if (datatype.equalsIgnoreCase("int")) |
| 250 |
{ |
| 251 |
int sum = numbers[0].intValue(); |
| 252 |
for (int i = 1; i < numbers.length; i++) |
| 253 |
sum %= numbers[i].intValue(); |
| 254 |
result = new Integer(sum); |
| 255 |
} |
| 256 |
else if (datatype.equalsIgnoreCase("long")) |
| 257 |
{ |
| 258 |
long sum = numbers[0].longValue(); |
| 259 |
for (int i = 1; i < numbers.length; i++) |
| 260 |
sum %= numbers[i].longValue(); |
| 261 |
result = new Long(sum); |
| 262 |
} |
| 263 |
else if (datatype.equalsIgnoreCase("float")) |
| 264 |
{ |
| 265 |
float sum = numbers[0].floatValue(); |
| 266 |
for (int i = 1; i < numbers.length; i++) |
| 267 |
sum %= numbers[i].floatValue(); |
| 268 |
result = new Float(sum); |
| 269 |
} |
| 270 |
else if (datatype.equalsIgnoreCase("double")) |
| 271 |
{ |
| 272 |
double sum = numbers[0].doubleValue(); |
| 273 |
for (int i = 1; i < numbers.length; i++) |
| 274 |
sum %= numbers[i].doubleValue(); |
| 275 |
result = new Double(sum); |
| 276 |
} |
| 277 |
return result; |
| 278 |
} |
| 279 |
|
| 280 |
public static final Number convert(Number n, String datatype) |
| 281 |
{ |
| 282 |
if (datatype == null) |
| 283 |
datatype = "double"; |
| 284 |
if (datatype.equals("int")) |
| 285 |
return new Integer(n.intValue()); |
| 286 |
if (datatype.equals("long")) |
| 287 |
return new Long(n.longValue()); |
| 288 |
if (datatype.equals("float")) |
| 289 |
return new Float(n.floatValue()); |
| 290 |
if (datatype.equals("double")) |
| 291 |
return new Double(n.doubleValue()); |
| 292 |
throw new BuildException("Invalid datatype."); |
| 293 |
} |
| 294 |
|
| 295 |
public static final Number execute(String method, |
| 296 |
String datatype, |
| 297 |
boolean strict, |
| 298 |
Class paramTypes[], |
| 299 |
Object params[]) |
| 300 |
{ |
| 301 |
try |
| 302 |
{ |
| 303 |
Class c = null; |
| 304 |
if (strict) |
| 305 |
{ |
| 306 |
c = Thread.currentThread().getContextClassLoader().loadClass("java.lang.StrictMath"); |
| 307 |
} |
| 308 |
else |
| 309 |
{ |
| 310 |
c = Thread.currentThread().getContextClassLoader().loadClass("java.lang.Math"); |
| 311 |
} |
| 312 |
|
| 313 |
Method m = c.getDeclaredMethod(method, paramTypes); |
| 314 |
Number n = (Number) m.invoke(null, params); |
| 315 |
return convert(n, datatype); |
| 316 |
} |
| 317 |
catch (ClassNotFoundException e) |
| 318 |
{ |
| 319 |
throw new BuildException(e); |
| 320 |
} |
| 321 |
catch (NoSuchMethodException e) |
| 322 |
{ |
| 323 |
throw new BuildException(e); |
| 324 |
} |
| 325 |
catch (IllegalAccessException e) |
| 326 |
{ |
| 327 |
throw new BuildException(e); |
| 328 |
} |
| 329 |
catch (InvocationTargetException e) |
| 330 |
{ |
| 331 |
throw new BuildException(e); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
public static final Number random(String datatype, |
| 336 |
boolean strict, |
| 337 |
Evaluateable operands[]) |
| 338 |
{ |
| 339 |
return execute("random", |
| 340 |
datatype, |
| 341 |
strict, |
| 342 |
new Class[0], |
| 343 |
new Object[0]); |
| 344 |
} |
| 345 |
|
| 346 |
public static Class getPrimitiveClass(String datatype) |
| 347 |
{ |
| 348 |
if (datatype == null) |
| 349 |
return Double.TYPE; |
| 350 |
if (datatype.equals("int")) |
| 351 |
return Integer.TYPE; |
| 352 |
if (datatype.equals("long")) |
| 353 |
return Long.TYPE; |
| 354 |
if (datatype.equals("float")) |
| 355 |
return Float.TYPE; |
| 356 |
if (datatype.equals("double")) |
| 357 |
return Double.TYPE; |
| 358 |
throw new BuildException("Invalid datatype."); |
| 359 |
|
| 360 |
} |
| 361 |
|
| 362 |
public static final Number abs(String datatype, |
| 363 |
boolean strict, |
| 364 |
Evaluateable operands[]) |
| 365 |
{ |
| 366 |
Object ops[] = new Object[]{convert(operands[0].evaluate(), datatype)}; |
| 367 |
Class params[] = new Class[]{getPrimitiveClass(datatype)}; |
| 368 |
|
| 369 |
return execute("abs", |
| 370 |
datatype, |
| 371 |
strict, |
| 372 |
params, |
| 373 |
ops); |
| 374 |
} |
| 375 |
|
| 376 |
private static final Number doOneDoubleArg(String operation, |
| 377 |
String datatype, |
| 378 |
boolean strict, |
| 379 |
Evaluateable operands[]) |
| 380 |
{ |
| 381 |
Object ops[] = new Object[]{convert(operands[0].evaluate(), |
| 382 |
"double")}; |
| 383 |
Class params[] = new Class[]{Double.TYPE}; |
| 384 |
|
| 385 |
return execute(operation, |
| 386 |
datatype, |
| 387 |
strict, |
| 388 |
params, |
| 389 |
ops); |
| 390 |
} |
| 391 |
|
| 392 |
public static final Number acos(String datatype, |
| 393 |
boolean strict, |
| 394 |
Evaluateable operands[]) |
| 395 |
{ |
| 396 |
return doOneDoubleArg("acos", datatype, strict, operands); |
| 397 |
} |
| 398 |
|
| 399 |
public static final Number asin(String datatype, |
| 400 |
boolean strict, |
| 401 |
Evaluateable operands[]) |
| 402 |
{ |
| 403 |
return doOneDoubleArg("asin", datatype, strict, operands); |
| 404 |
} |
| 405 |
|
| 406 |
public static final Number atan(String datatype, |
| 407 |
boolean strict, |
| 408 |
Evaluateable operands[]) |
| 409 |
{ |
| 410 |
return doOneDoubleArg("atan", datatype, strict, operands); |
| 411 |
} |
| 412 |
|
| 413 |
public static final Number atan2(String datatype, |
| 414 |
boolean strict, |
| 415 |
Evaluateable operands[]) |
| 416 |
{ |
| 417 |
Object ops[] = new Object[]{convert(operands[0].evaluate(), |
| 418 |
"double"), |
| 419 |
convert(operands[1].evaluate(), |
| 420 |
"double")}; |
| 421 |
Class params[] = new Class[]{Double.TYPE, |
| 422 |
Double.TYPE}; |
| 423 |
|
| 424 |
return execute("atan2", |
| 425 |
datatype, |
| 426 |
strict, |
| 427 |
params, |
| 428 |
ops); |
| 429 |
} |
| 430 |
|
| 431 |
public static final Number sin(String datatype, |
| 432 |
boolean strict, |
| 433 |
Evaluateable operands[]) |
| 434 |
{ |
| 435 |
return doOneDoubleArg("sin", datatype, strict, operands); |
| 436 |
} |
| 437 |
|
| 438 |
public static final Number tan(String datatype, |
| 439 |
boolean strict, |
| 440 |
Evaluateable operands[]) |
| 441 |
{ |
| 442 |
return doOneDoubleArg("sin", datatype, strict, operands); |
| 443 |
} |
| 444 |
|
| 445 |
public static final Number cos(String datatype, |
| 446 |
boolean strict, |
| 447 |
Evaluateable operands[]) |
| 448 |
{ |
| 449 |
return doOneDoubleArg("cos", datatype, strict, operands); |
| 450 |
} |
| 451 |
|
| 452 |
public static final Number ceil(String datatype, |
| 453 |
boolean strict, |
| 454 |
Evaluateable operands[]) |
| 455 |
{ |
| 456 |
return doOneDoubleArg("ceil", datatype, strict, operands); |
| 457 |
} |
| 458 |
|
| 459 |
public static final Number floor(String datatype, |
| 460 |
boolean strict, |
| 461 |
Evaluateable operands[]) |
| 462 |
{ |
| 463 |
return doOneDoubleArg("floor", datatype, strict, operands); |
| 464 |
} |
| 465 |
|
| 466 |
public static final Number exp(String datatype, |
| 467 |
boolean strict, |
| 468 |
Evaluateable operands[]) |
| 469 |
{ |
| 470 |
return doOneDoubleArg("exp", datatype, strict, operands); |
| 471 |
} |
| 472 |
|
| 473 |
public static final Number rint(String datatype, |
| 474 |
boolean strict, |
| 475 |
Evaluateable operands[]) |
| 476 |
{ |
| 477 |
return doOneDoubleArg("rint", datatype, strict, operands); |
| 478 |
} |
| 479 |
|
| 480 |
public static final Number round(String datatype, |
| 481 |
boolean strict, |
| 482 |
Evaluateable operands[]) |
| 483 |
{ |
| 484 |
Object ops[] = new Object[]{convert(operands[0].evaluate(), |
| 485 |
datatype)}; |
| 486 |
Class params[] = new Class[]{getPrimitiveClass(datatype)}; |
| 487 |
|
| 488 |
return execute("round", |
| 489 |
datatype, |
| 490 |
strict, |
| 491 |
params, |
| 492 |
ops); |
| 493 |
} |
| 494 |
|
| 495 |
public static final Number sqrt(String datatype, |
| 496 |
boolean strict, |
| 497 |
Evaluateable operands[]) |
| 498 |
{ |
| 499 |
return doOneDoubleArg("sqrt", datatype, strict, operands); |
| 500 |
} |
| 501 |
|
| 502 |
public static final Number degrees(String datatype, |
| 503 |
boolean strict, |
| 504 |
Evaluateable operands[]) |
| 505 |
{ |
| 506 |
return todegrees(datatype, strict, operands); |
| 507 |
} |
| 508 |
|
| 509 |
public static final Number todegrees(String datatype, |
| 510 |
boolean strict, |
| 511 |
Evaluateable operands[]) |
| 512 |
{ |
| 513 |
return doOneDoubleArg("toDegrees", datatype, strict, operands); |
| 514 |
} |
| 515 |
|
| 516 |
public static final Number radians(String datatype, |
| 517 |
boolean strict, |
| 518 |
Evaluateable operands[]) |
| 519 |
{ |
| 520 |
return toradians(datatype, strict, operands); |
| 521 |
} |
| 522 |
|
| 523 |
public static final Number toradians(String datatype, |
| 524 |
boolean strict, |
| 525 |
Evaluateable operands[]) |
| 526 |
{ |
| 527 |
return doOneDoubleArg("toRadians", datatype, strict, operands); |
| 528 |
} |
| 529 |
|
| 530 |
public static final Number ieeeremainder(String datatype, |
| 531 |
boolean strict, |
| 532 |
Evaluateable operands[]) |
| 533 |
{ |
| 534 |
Object ops[] = new Object[]{convert(operands[0].evaluate(), |
| 535 |
"double"), |
| 536 |
convert(operands[1].evaluate(), |
| 537 |
"double")}; |
| 538 |
Class params[] = new Class[]{Double.TYPE, |
| 539 |
Double.TYPE}; |
| 540 |
|
| 541 |
return execute("IEEERemainder", |
| 542 |
datatype, |
| 543 |
strict, |
| 544 |
params, |
| 545 |
ops); |
| 546 |
} |
| 547 |
|
| 548 |
public static final Number min(String datatype, |
| 549 |
boolean strict, |
| 550 |
Evaluateable operands[]) |
| 551 |
{ |
| 552 |
Object ops[] = new Object[]{convert(operands[0].evaluate(), |
| 553 |
datatype), |
| 554 |
convert(operands[1].evaluate(), |
| 555 |
datatype)}; |
| 556 |
Class params[] = new Class[]{getPrimitiveClass(datatype), |
| 557 |
getPrimitiveClass(datatype)}; |
| 558 |
|
| 559 |
return execute("min", |
| 560 |
datatype, |
| 561 |
strict, |
| 562 |
params, |
| 563 |
ops); |
| 564 |
} |
| 565 |
|
| 566 |
public static final Number max(String datatype, |
| 567 |
boolean strict, |
| 568 |
Evaluateable operands[]) |
| 569 |
{ |
| 570 |
Object ops[] = new Object[]{convert(operands[0].evaluate(), |
| 571 |
datatype), |
| 572 |
convert(operands[1].evaluate(), |
| 573 |
datatype)}; |
| 574 |
Class params[] = new Class[]{getPrimitiveClass(datatype), |
| 575 |
getPrimitiveClass(datatype)}; |
| 576 |
|
| 577 |
return execute("max", |
| 578 |
datatype, |
| 579 |
strict, |
| 580 |
params, |
| 581 |
ops); |
| 582 |
} |
| 583 |
|
| 584 |
} |