samples/composition/src-api2.0-runtime/api/Arithmetica.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 10:01:34 +0200
changeset 186 09d10795f55c
parent 184 6b2cd8df14c0
child 187 e8db9f297016
permissions -rw-r--r--
daily life polishing through 3500
jtulach@155
     1
package api;
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@155
    12
public class Arithmetica {
jtulach@155
    13
    public int sumTwo(int one, int second) {
jtulach@155
    14
        return one + second;
jtulach@155
    15
    }
jtulach@155
    16
    
jtulach@155
    17
    public int sumAll(int... numbers) {
jtulach@155
    18
        int sum = numbers[0];
jtulach@155
    19
        for (int i = 1; i < numbers.length; i++) {
jtulach@155
    20
            sum = sumTwo(sum, numbers[i]);
jtulach@155
    21
        }
jtulach@155
    22
        return sum;
jtulach@155
    23
    }
jtulach@155
    24
    
jtulach@155
    25
    public int sumRange(int from, int to) {
jtulach@155
    26
        if (calledByV2AwareLoader()) {
jtulach@155
    27
            return sumRange2(from, to);
jtulach@155
    28
        } else {
jtulach@155
    29
            return sumRange1(from, to);
jtulach@155
    30
        }
jtulach@155
    31
    }
jtulach@155
    32
jtulach@155
    33
    private int sumRange1(int from, int to) {
jtulach@155
    34
        int len = to - from;
jtulach@155
    35
        int[] array = new int[len + 1];
jtulach@155
    36
        for (int i = 0; i <= len; i++) {
jtulach@155
    37
            array[i] = from + i;
jtulach@155
    38
        }
jtulach@155
    39
        return sumAll(array);
jtulach@155
    40
    }
jtulach@155
    41
    
jtulach@155
    42
    private int sumRange2(int from, int to) {
jtulach@155
    43
        return (from + to) * (to - from + 1) / 2;
jtulach@155
    44
    }
jtulach@155
    45
jtulach@155
    46
    private static boolean calledByV2AwareLoader() {
jtulach@155
    47
        // BEGIN: design.composition.arith2.6.runtime
jtulach@155
    48
        StackTraceElement[] arr = Thread.currentThread().getStackTrace();
jtulach@155
    49
        ClassLoader myLoader = Arithmetica.class.getClassLoader();
jtulach@155
    50
        for (int i = 0; i < arr.length; i++) {
jtulach@155
    51
            ClassLoader caller = arr[i].getClass().getClassLoader();
jtulach@155
    52
            if (myLoader == caller) {
jtulach@155
    53
                continue;
jtulach@155
    54
            }
jtulach@155
    55
            if (RuntimeCheck.requiresAtLeast("2.6", "api.Arithmetica", caller)) {
jtulach@155
    56
                return true;
jtulach@155
    57
            }
jtulach@155
    58
            return false;
jtulach@155
    59
        }
jtulach@155
    60
        return true;
jtulach@155
    61
        // END: design.composition.arith2.6.runtime
jtulach@155
    62
    }
jtulach@155
    63
    
jtulach@155
    64
}