1 package org.apidesign.math;
3 import org.apidesign.math.Arithmetica;
4 import org.apidesign.runtime.check.RuntimeCheck;
6 /** Class to simplify arithmetical operations, improved version to compute
7 * the sum for ranges, but only if the virtual machine is configured to
8 * run in incompatible mode.
10 * @author Jaroslav Tulach <jtulach@netbeans.org>
13 public class Arithmetica {
14 public int sumTwo(int one, int second) {
18 public int sumAll(int... numbers) {
19 if (numbers.length == 0) {
23 for (int i = 1; i < numbers.length; i++) {
24 sum = sumTwo(sum, numbers[i]);
29 public int sumRange(int from, int to) {
30 if (calledByV2AwareLoader()) {
31 return sumRange2(from, to);
33 return sumRange1(from, to);
37 private int sumRange1(int from, int to) {
43 int[] array = new int[len + 1];
44 for (int i = 0; i <= len; i++) {
50 private int sumRange2(int from, int to) {
51 return (from + to) * (Math.abs(to - from) + 1) / 2;
54 private static boolean calledByV2AwareLoader() {
55 // BEGIN: design.composition.arith2.6.runtime
56 StackTraceElement[] arr = Thread.currentThread().getStackTrace();
57 ClassLoader myLoader = Arithmetica.class.getClassLoader();
58 for (int i = 0; i < arr.length; i++) {
59 ClassLoader caller = arr[i].getClass().getClassLoader();
60 if (myLoader == caller) {
63 if (RuntimeCheck.requiresAtLeast("2.6", "api.Arithmetica", caller)) {
69 // END: design.composition.arith2.6.runtime