samples/composition/src-api2.0-runtime/api/Arithmetica.java
changeset 321 06bf3a32eaa0
parent 320 9ad0cc253aa9
child 322 c12a563d36a4
     1.1 --- a/samples/composition/src-api2.0-runtime/api/Arithmetica.java	Thu Feb 12 11:00:41 2009 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,71 +0,0 @@
     1.4 -package api;
     1.5 -
     1.6 -import org.apidesign.runtime.check.RuntimeCheck;
     1.7 -
     1.8 -/** Class to simplify arithmetical operations, improved version to compute
     1.9 - * the sum for ranges, but only if the virtual machine is configured to
    1.10 - * run in incompatible mode.
    1.11 - *
    1.12 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.13 - * @version 2.0
    1.14 - */
    1.15 -public class Arithmetica {
    1.16 -    public int sumTwo(int one, int second) {
    1.17 -        return one + second;
    1.18 -    }
    1.19 -    
    1.20 -    public int sumAll(int... numbers) {
    1.21 -        if (numbers.length == 0) {
    1.22 -            return 0;
    1.23 -        }
    1.24 -        int sum = numbers[0];
    1.25 -        for (int i = 1; i < numbers.length; i++) {
    1.26 -            sum = sumTwo(sum, numbers[i]);
    1.27 -        }
    1.28 -        return sum;
    1.29 -    }
    1.30 -    
    1.31 -    public int sumRange(int from, int to) {
    1.32 -        if (calledByV2AwareLoader()) {
    1.33 -            return sumRange2(from, to);
    1.34 -        } else {
    1.35 -            return sumRange1(from, to);
    1.36 -        }
    1.37 -    }
    1.38 -
    1.39 -    private int sumRange1(int from, int to) {
    1.40 -        int len = to - from;
    1.41 -        if (len < 0) {
    1.42 -            len = -len;
    1.43 -            from = to;
    1.44 -        }
    1.45 -        int[] array = new int[len + 1];
    1.46 -        for (int i = 0; i <= len; i++) {
    1.47 -            array[i] = from + i;
    1.48 -        }
    1.49 -        return sumAll(array);
    1.50 -    }
    1.51 -    
    1.52 -    private int sumRange2(int from, int to) {
    1.53 -        return (from + to) * (Math.abs(to - from) + 1) / 2;
    1.54 -    }
    1.55 -
    1.56 -    private static boolean calledByV2AwareLoader() {
    1.57 -        // BEGIN: design.composition.arith2.6.runtime
    1.58 -        StackTraceElement[] arr = Thread.currentThread().getStackTrace();
    1.59 -        ClassLoader myLoader = Arithmetica.class.getClassLoader();
    1.60 -        for (int i = 0; i < arr.length; i++) {
    1.61 -            ClassLoader caller = arr[i].getClass().getClassLoader();
    1.62 -            if (myLoader == caller) {
    1.63 -                continue;
    1.64 -            }
    1.65 -            if (RuntimeCheck.requiresAtLeast("2.6", "api.Arithmetica", caller)) {
    1.66 -                return true;
    1.67 -            }
    1.68 -            return false;
    1.69 -        }
    1.70 -        return true;
    1.71 -        // END: design.composition.arith2.6.runtime
    1.72 -    }
    1.73 -    
    1.74 -}