samples/composition/src-api2.0-runtime/org/apidesign/math/Arithmetica.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 21:30:10 +0100
changeset 409 40cabcdcd2be
parent 321 06bf3a32eaa0
permissions -rw-r--r--
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
jtulach@321
     1
package org.apidesign.math;
jtulach@155
     2
jtulach@155
     3
import org.apidesign.runtime.check.RuntimeCheck;
jtulach@155
     4
jtulach@155
     5
/** Class to simplify arithmetical operations, improved version to compute
jtulach@155
     6
 * the sum for ranges, but only if the virtual machine is configured to
jtulach@155
     7
 * run in incompatible mode.
jtulach@155
     8
 *
jtulach@155
     9
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@155
    10
 * @version 2.0
jtulach@155
    11
 */
jtulach@323
    12
// BEGIN: design.composition.arith.runtime
jtulach@155
    13
public class Arithmetica {
jtulach@155
    14
    public int sumTwo(int one, int second) {
jtulach@155
    15
        return one + second;
jtulach@155
    16
    }
jtulach@155
    17
    
jtulach@155
    18
    public int sumAll(int... numbers) {
jtulach@187
    19
        if (numbers.length == 0) {
jtulach@187
    20
            return 0;
jtulach@187
    21
        }
jtulach@155
    22
        int sum = numbers[0];
jtulach@155
    23
        for (int i = 1; i < numbers.length; i++) {
jtulach@155
    24
            sum = sumTwo(sum, numbers[i]);
jtulach@155
    25
        }
jtulach@155
    26
        return sum;
jtulach@155
    27
    }
jtulach@155
    28
    
jtulach@155
    29
    public int sumRange(int from, int to) {
jtulach@155
    30
        if (calledByV2AwareLoader()) {
jtulach@155
    31
            return sumRange2(from, to);
jtulach@155
    32
        } else {
jtulach@155
    33
            return sumRange1(from, to);
jtulach@155
    34
        }
jtulach@155
    35
    }
jtulach@155
    36
jtulach@155
    37
    private int sumRange1(int from, int to) {
jtulach@155
    38
        int len = to - from;
jtulach@210
    39
        if (len < 0) {
jtulach@210
    40
            len = -len;
jtulach@210
    41
            from = to;
jtulach@210
    42
        }
jtulach@155
    43
        int[] array = new int[len + 1];
jtulach@155
    44
        for (int i = 0; i <= len; i++) {
jtulach@155
    45
            array[i] = from + i;
jtulach@155
    46
        }
jtulach@155
    47
        return sumAll(array);
jtulach@155
    48
    }
jtulach@155
    49
    
jtulach@155
    50
    private int sumRange2(int from, int to) {
jtulach@210
    51
        return (from + to) * (Math.abs(to - from) + 1) / 2;
jtulach@155
    52
    }
jtulach@155
    53
jtulach@155
    54
    private static boolean calledByV2AwareLoader() {
jtulach@155
    55
        // BEGIN: design.composition.arith2.6.runtime
jtulach@155
    56
        StackTraceElement[] arr = Thread.currentThread().getStackTrace();
jtulach@155
    57
        ClassLoader myLoader = Arithmetica.class.getClassLoader();
jtulach@155
    58
        for (int i = 0; i < arr.length; i++) {
jtulach@155
    59
            ClassLoader caller = arr[i].getClass().getClassLoader();
jtulach@155
    60
            if (myLoader == caller) {
jtulach@155
    61
                continue;
jtulach@155
    62
            }
jtulach@323
    63
            if (RuntimeCheck.requiresAtLeast(
jtulach@323
    64
                "2.0", "org.apidesign.math", caller
jtulach@323
    65
            )) {
jtulach@155
    66
                return true;
jtulach@155
    67
            }
jtulach@155
    68
            return false;
jtulach@155
    69
        }
jtulach@155
    70
        return true;
jtulach@155
    71
        // END: design.composition.arith2.6.runtime
jtulach@155
    72
    }
jtulach@155
    73
    
jtulach@155
    74
}
jtulach@323
    75
// END: design.composition.arith.runtime