samples/composition/src-api2.0-runtime/api/Arithmetica.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 10:04:53 +0200
changeset 210 acf2c31e22d4
parent 209 1c999569643b
permissions -rw-r--r--
Merge: Geertjan's changes to the end of the chapter
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@187
    18
        if (numbers.length == 0) {
jtulach@187
    19
            return 0;
jtulach@187
    20
        }
jtulach@155
    21
        int sum = numbers[0];
jtulach@155
    22
        for (int i = 1; i < numbers.length; i++) {
jtulach@155
    23
            sum = sumTwo(sum, numbers[i]);
jtulach@155
    24
        }
jtulach@155
    25
        return sum;
jtulach@155
    26
    }
jtulach@155
    27
    
jtulach@155
    28
    public int sumRange(int from, int to) {
jtulach@155
    29
        if (calledByV2AwareLoader()) {
jtulach@155
    30
            return sumRange2(from, to);
jtulach@155
    31
        } else {
jtulach@155
    32
            return sumRange1(from, to);
jtulach@155
    33
        }
jtulach@155
    34
    }
jtulach@155
    35
jtulach@155
    36
    private int sumRange1(int from, int to) {
jtulach@155
    37
        int len = to - from;
jtulach@210
    38
        if (len < 0) {
jtulach@210
    39
            len = -len;
jtulach@210
    40
            from = to;
jtulach@210
    41
        }
jtulach@155
    42
        int[] array = new int[len + 1];
jtulach@155
    43
        for (int i = 0; i <= len; i++) {
jtulach@155
    44
            array[i] = from + i;
jtulach@155
    45
        }
jtulach@155
    46
        return sumAll(array);
jtulach@155
    47
    }
jtulach@155
    48
    
jtulach@155
    49
    private int sumRange2(int from, int to) {
jtulach@210
    50
        return (from + to) * (Math.abs(to - from) + 1) / 2;
jtulach@155
    51
    }
jtulach@155
    52
jtulach@155
    53
    private static boolean calledByV2AwareLoader() {
jtulach@155
    54
        // BEGIN: design.composition.arith2.6.runtime
jtulach@155
    55
        StackTraceElement[] arr = Thread.currentThread().getStackTrace();
jtulach@155
    56
        ClassLoader myLoader = Arithmetica.class.getClassLoader();
jtulach@155
    57
        for (int i = 0; i < arr.length; i++) {
jtulach@155
    58
            ClassLoader caller = arr[i].getClass().getClassLoader();
jtulach@155
    59
            if (myLoader == caller) {
jtulach@155
    60
                continue;
jtulach@155
    61
            }
jtulach@155
    62
            if (RuntimeCheck.requiresAtLeast("2.6", "api.Arithmetica", caller)) {
jtulach@155
    63
                return true;
jtulach@155
    64
            }
jtulach@155
    65
            return false;
jtulach@155
    66
        }
jtulach@155
    67
        return true;
jtulach@155
    68
        // END: design.composition.arith2.6.runtime
jtulach@155
    69
    }
jtulach@155
    70
    
jtulach@155
    71
}