3 import org.apidesign.runtime.check.RuntimeCheck;
5 /** Class to simplify arithmetical operations, improved version to compute
6 * the sum for ranges, but only if the virtual machine is configured to
7 * run in incompatible mode.
9 * @author Jaroslav Tulach <jtulach@netbeans.org>
12 public class Arithmetica {
13 public int sumTwo(int one, int second) {
17 public int sumAll(int... numbers) {
18 if (numbers.length == 0) {
22 for (int i = 1; i < numbers.length; i++) {
23 sum = sumTwo(sum, numbers[i]);
28 public int sumRange(int from, int to) {
29 if (calledByV2AwareLoader()) {
30 return sumRange2(from, to);
32 return sumRange1(from, to);
36 private int sumRange1(int from, int to) {
38 int[] array = new int[len + 1];
39 for (int i = 0; i <= len; i++) {
45 private int sumRange2(int from, int to) {
46 return (from + to) * (to - from + 1) / 2;
49 private static boolean calledByV2AwareLoader() {
50 // BEGIN: design.composition.arith2.6.runtime
51 StackTraceElement[] arr = Thread.currentThread().getStackTrace();
52 ClassLoader myLoader = Arithmetica.class.getClassLoader();
53 for (int i = 0; i < arr.length; i++) {
54 ClassLoader caller = arr[i].getClass().getClassLoader();
55 if (myLoader == caller) {
58 if (RuntimeCheck.requiresAtLeast("2.6", "api.Arithmetica", caller)) {
64 // END: design.composition.arith2.6.runtime