Moving code to org.apidesign.math package
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Feb 2009 17:30:06 +0100
changeset 32106bf3a32eaa0
parent 320 9ad0cc253aa9
child 322 c12a563d36a4
Moving code to org.apidesign.math package
samples/composition/src-api1.0/api/Arithmetica.java
samples/composition/src-api1.0/org/apidesign/math/Arithmetica.java
samples/composition/src-api2.0-compat/api/Arithmetica.java
samples/composition/src-api2.0-compat/org/apidesign/math/Arithmetica.java
samples/composition/src-api2.0-enum/api/Arithmetica.java
samples/composition/src-api2.0-enum/org/apidesign/math/Arithmetica.java
samples/composition/src-api2.0-property/api/Arithmetica.java
samples/composition/src-api2.0-property/org/apidesign/math/Arithmetica.java
samples/composition/src-api2.0-runtime/api/Arithmetica.java
samples/composition/src-api2.0-runtime/org/apidesign/math/Arithmetica.java
samples/composition/src-api2.0-runtime/org/apidesign/runtime/check/RuntimeCheck.java
samples/composition/src-api2.0-runtime/org/apidesign/runtime/check/VersionAwareClassLoader.java
samples/composition/src-api2.0/api/Arithmetica.java
samples/composition/src-api2.0/org/apidesign/math/Arithmetica.java
samples/composition/src-test/api/ArithmeticaCompatibilityTest.java
samples/composition/src-test/api/ArithmeticaTest.java
samples/composition/src-test/api/Factorial.java
samples/composition/src-test/api/FactorialTest.java
samples/composition/src-test/org/apidesign/math/test/ArithmeticaCompatibilityTest.java
samples/composition/src-test/org/apidesign/math/test/ArithmeticaTest.java
samples/composition/src-test/org/apidesign/math/test/Factorial.java
samples/composition/src-test/org/apidesign/math/test/FactorialTest.java
     1.1 --- a/samples/composition/src-api1.0/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,38 +0,0 @@
     1.4 -package api;
     1.5 -
     1.6 -/** Class to simplify arithmetical operations.
     1.7 - *
     1.8 - * @author Jaroslav Tulach <jtulach@netbeans.org>
     1.9 - * @version 1.0
    1.10 - */
    1.11 -// BEGIN: design.composition.arith1.0
    1.12 -public class Arithmetica {
    1.13 -    public int sumTwo(int one, int second) {
    1.14 -        return one + second;
    1.15 -    }
    1.16 -    
    1.17 -    public int sumAll(int... numbers) {
    1.18 -        if (numbers.length == 0) {
    1.19 -            return 0;
    1.20 -        }
    1.21 -        int sum = numbers[0];
    1.22 -        for (int i = 1; i < numbers.length; i++) {
    1.23 -            sum = sumTwo(sum, numbers[i]);
    1.24 -        }
    1.25 -        return sum;
    1.26 -    }
    1.27 -    
    1.28 -    public int sumRange(int from, int to) {
    1.29 -        int len = to - from;
    1.30 -        if (len < 0) {
    1.31 -            len = -len;
    1.32 -            from = to;
    1.33 -        }
    1.34 -        int[] array = new int[len + 1];
    1.35 -        for (int i = 0; i <= len; i++) {
    1.36 -            array[i] = from + i;
    1.37 -        }
    1.38 -        return sumAll(array);
    1.39 -    }
    1.40 -}
    1.41 -// END: design.composition.arith1.0
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/samples/composition/src-api1.0/org/apidesign/math/Arithmetica.java	Sat Feb 14 17:30:06 2009 +0100
     2.3 @@ -0,0 +1,38 @@
     2.4 +package org.apidesign.math;
     2.5 +
     2.6 +/** Class to simplify arithmetical operations.
     2.7 + *
     2.8 + * @author Jaroslav Tulach <jtulach@netbeans.org>
     2.9 + * @version 1.0
    2.10 + */
    2.11 +// BEGIN: design.composition.arith1.0
    2.12 +public class Arithmetica {
    2.13 +    public int sumTwo(int one, int second) {
    2.14 +        return one + second;
    2.15 +    }
    2.16 +    
    2.17 +    public int sumAll(int... numbers) {
    2.18 +        if (numbers.length == 0) {
    2.19 +            return 0;
    2.20 +        }
    2.21 +        int sum = numbers[0];
    2.22 +        for (int i = 1; i < numbers.length; i++) {
    2.23 +            sum = sumTwo(sum, numbers[i]);
    2.24 +        }
    2.25 +        return sum;
    2.26 +    }
    2.27 +    
    2.28 +    public int sumRange(int from, int to) {
    2.29 +        int len = to - from;
    2.30 +        if (len < 0) {
    2.31 +            len = -len;
    2.32 +            from = to;
    2.33 +        }
    2.34 +        int[] array = new int[len + 1];
    2.35 +        for (int i = 0; i <= len; i++) {
    2.36 +            array[i] = from + i;
    2.37 +        }
    2.38 +        return sumAll(array);
    2.39 +    }
    2.40 +}
    2.41 +// END: design.composition.arith1.0
     3.1 --- a/samples/composition/src-api2.0-compat/api/Arithmetica.java	Thu Feb 12 11:00:41 2009 +0100
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,61 +0,0 @@
     3.4 -package api;
     3.5 -
     3.6 -/** Class to simplify arithmetical operations, improved version to compute
     3.7 - * the sum for ranges, but only if one uses the new constructor to indicate
     3.8 - * need for new version.
     3.9 - *
    3.10 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.11 - * @version 2.0
    3.12 - */
    3.13 -// BEGIN: design.composition.arith2.0.compat
    3.14 -public class Arithmetica {
    3.15 -    private final int version;
    3.16 -    
    3.17 -    public Arithmetica() {
    3.18 -        this(1);
    3.19 -    }
    3.20 -    public Arithmetica(int version) {
    3.21 -        this.version = version;
    3.22 -    }
    3.23 -    
    3.24 -    public int sumTwo(int one, int second) {
    3.25 -        return one + second;
    3.26 -    }
    3.27 -    
    3.28 -    public int sumAll(int... numbers) {
    3.29 -        if (numbers.length == 0) {
    3.30 -            return 0;
    3.31 -        }
    3.32 -        int sum = numbers[0];
    3.33 -        for (int i = 1; i < numbers.length; i++) {
    3.34 -            sum = sumTwo(sum, numbers[i]);
    3.35 -        }
    3.36 -        return sum;
    3.37 -    }
    3.38 -    
    3.39 -    public int sumRange(int from, int to) {
    3.40 -        switch (version) {
    3.41 -            case 1: return sumRange1(from, to);
    3.42 -            case 2: return sumRange2(from, to);
    3.43 -            default: throw new IllegalStateException();
    3.44 -        }
    3.45 -    }
    3.46 -
    3.47 -    private int sumRange1(int from, int to) {
    3.48 -        int len = to - from;
    3.49 -        if (len < 0) {
    3.50 -            len = -len;
    3.51 -            from = to;
    3.52 -        }
    3.53 -        int[] array = new int[len + 1];
    3.54 -        for (int i = 0; i <= len; i++) {
    3.55 -            array[i] = from + i;
    3.56 -        }
    3.57 -        return sumAll(array);
    3.58 -    }
    3.59 -    
    3.60 -    private int sumRange2(int from, int to) {
    3.61 -        return (from + to) * (Math.abs(to - from) + 1) / 2;
    3.62 -    }
    3.63 -}
    3.64 -// END: design.composition.arith2.0.compat
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/samples/composition/src-api2.0-compat/org/apidesign/math/Arithmetica.java	Sat Feb 14 17:30:06 2009 +0100
     4.3 @@ -0,0 +1,61 @@
     4.4 +package org.apidesign.math;
     4.5 +
     4.6 +/** Class to simplify arithmetical operations, improved version to compute
     4.7 + * the sum for ranges, but only if one uses the new constructor to indicate
     4.8 + * need for new version.
     4.9 + *
    4.10 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.11 + * @version 2.0
    4.12 + */
    4.13 +// BEGIN: design.composition.arith2.0.compat
    4.14 +public class Arithmetica {
    4.15 +    private final int version;
    4.16 +    
    4.17 +    public Arithmetica() {
    4.18 +        this(1);
    4.19 +    }
    4.20 +    public Arithmetica(int version) {
    4.21 +        this.version = version;
    4.22 +    }
    4.23 +    
    4.24 +    public int sumTwo(int one, int second) {
    4.25 +        return one + second;
    4.26 +    }
    4.27 +    
    4.28 +    public int sumAll(int... numbers) {
    4.29 +        if (numbers.length == 0) {
    4.30 +            return 0;
    4.31 +        }
    4.32 +        int sum = numbers[0];
    4.33 +        for (int i = 1; i < numbers.length; i++) {
    4.34 +            sum = sumTwo(sum, numbers[i]);
    4.35 +        }
    4.36 +        return sum;
    4.37 +    }
    4.38 +    
    4.39 +    public int sumRange(int from, int to) {
    4.40 +        switch (version) {
    4.41 +            case 1: return sumRange1(from, to);
    4.42 +            case 2: return sumRange2(from, to);
    4.43 +            default: throw new IllegalStateException();
    4.44 +        }
    4.45 +    }
    4.46 +
    4.47 +    private int sumRange1(int from, int to) {
    4.48 +        int len = to - from;
    4.49 +        if (len < 0) {
    4.50 +            len = -len;
    4.51 +            from = to;
    4.52 +        }
    4.53 +        int[] array = new int[len + 1];
    4.54 +        for (int i = 0; i <= len; i++) {
    4.55 +            array[i] = from + i;
    4.56 +        }
    4.57 +        return sumAll(array);
    4.58 +    }
    4.59 +    
    4.60 +    private int sumRange2(int from, int to) {
    4.61 +        return (from + to) * (Math.abs(to - from) + 1) / 2;
    4.62 +    }
    4.63 +}
    4.64 +// END: design.composition.arith2.0.compat
     5.1 --- a/samples/composition/src-api2.0-enum/api/Arithmetica.java	Thu Feb 12 11:00:41 2009 +0100
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,66 +0,0 @@
     5.4 -package api;
     5.5 -
     5.6 -/** Class to simplify arithmetical operations, improved version to compute
     5.7 - * the sum for ranges, but only if one uses the new constructor to indicate
     5.8 - * need for new version.
     5.9 - *
    5.10 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    5.11 - * @version 2.0
    5.12 - */
    5.13 -// BEGIN: design.composition.arith2.0.enum
    5.14 -public class Arithmetica {
    5.15 -    private final Version version;
    5.16 -    public enum Version { VERSION_1_0, VERSION_2_0 }
    5.17 -    
    5.18 -    public Arithmetica() {
    5.19 -        this(Version.VERSION_1_0);
    5.20 -    }
    5.21 -    public Arithmetica(Version version) {
    5.22 -        this.version = version;
    5.23 -    }
    5.24 -
    5.25 -    public int sumRange(int from, int to) {
    5.26 -        switch (version) {
    5.27 -            case VERSION_1_0:
    5.28 -                return sumRange1(from, to);
    5.29 -            case VERSION_2_0:
    5.30 -                return sumRange2(from, to);
    5.31 -            default:
    5.32 -                throw new IllegalStateException();
    5.33 -        }
    5.34 -    }
    5.35 -// FINISH: design.composition.arith2.0.enum
    5.36 -    
    5.37 -    public int sumTwo(int one, int second) {
    5.38 -        return one + second;
    5.39 -    }
    5.40 -    
    5.41 -    public int sumAll(int... numbers) {
    5.42 -        if (numbers.length == 0) {
    5.43 -            return 0;
    5.44 -        }
    5.45 -        int sum = numbers[0];
    5.46 -        for (int i = 1; i < numbers.length; i++) {
    5.47 -            sum = sumTwo(sum, numbers[i]);
    5.48 -        }
    5.49 -        return sum;
    5.50 -    }
    5.51 -    
    5.52 -
    5.53 -    private int sumRange1(int from, int to) {
    5.54 -        int len = to - from;
    5.55 -        if (len < 0) {
    5.56 -            len = -len;
    5.57 -            from = to;
    5.58 -        }
    5.59 -        int[] array = new int[len + 1];
    5.60 -        for (int i = 0; i <= len; i++) {
    5.61 -            array[i] = from + i;
    5.62 -        }
    5.63 -        return sumAll(array);
    5.64 -    }
    5.65 -    
    5.66 -    private int sumRange2(int from, int to) {
    5.67 -        return (from + to) * (Math.abs(to - from) + 1) / 2;
    5.68 -    }
    5.69 -}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/samples/composition/src-api2.0-enum/org/apidesign/math/Arithmetica.java	Sat Feb 14 17:30:06 2009 +0100
     6.3 @@ -0,0 +1,66 @@
     6.4 +package org.apidesign.math;
     6.5 +
     6.6 +/** Class to simplify arithmetical operations, improved version to compute
     6.7 + * the sum for ranges, but only if one uses the new constructor to indicate
     6.8 + * need for new version.
     6.9 + *
    6.10 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    6.11 + * @version 2.0
    6.12 + */
    6.13 +// BEGIN: design.composition.arith2.0.enum
    6.14 +public class Arithmetica {
    6.15 +    private final Version version;
    6.16 +    public enum Version { VERSION_1_0, VERSION_2_0 }
    6.17 +    
    6.18 +    public Arithmetica() {
    6.19 +        this(Version.VERSION_1_0);
    6.20 +    }
    6.21 +    public Arithmetica(Version version) {
    6.22 +        this.version = version;
    6.23 +    }
    6.24 +
    6.25 +    public int sumRange(int from, int to) {
    6.26 +        switch (version) {
    6.27 +            case VERSION_1_0:
    6.28 +                return sumRange1(from, to);
    6.29 +            case VERSION_2_0:
    6.30 +                return sumRange2(from, to);
    6.31 +            default:
    6.32 +                throw new IllegalStateException();
    6.33 +        }
    6.34 +    }
    6.35 +// FINISH: design.composition.arith2.0.enum
    6.36 +    
    6.37 +    public int sumTwo(int one, int second) {
    6.38 +        return one + second;
    6.39 +    }
    6.40 +    
    6.41 +    public int sumAll(int... numbers) {
    6.42 +        if (numbers.length == 0) {
    6.43 +            return 0;
    6.44 +        }
    6.45 +        int sum = numbers[0];
    6.46 +        for (int i = 1; i < numbers.length; i++) {
    6.47 +            sum = sumTwo(sum, numbers[i]);
    6.48 +        }
    6.49 +        return sum;
    6.50 +    }
    6.51 +    
    6.52 +
    6.53 +    private int sumRange1(int from, int to) {
    6.54 +        int len = to - from;
    6.55 +        if (len < 0) {
    6.56 +            len = -len;
    6.57 +            from = to;
    6.58 +        }
    6.59 +        int[] array = new int[len + 1];
    6.60 +        for (int i = 0; i <= len; i++) {
    6.61 +            array[i] = from + i;
    6.62 +        }
    6.63 +        return sumAll(array);
    6.64 +    }
    6.65 +    
    6.66 +    private int sumRange2(int from, int to) {
    6.67 +        return (from + to) * (Math.abs(to - from) + 1) / 2;
    6.68 +    }
    6.69 +}
     7.1 --- a/samples/composition/src-api2.0-property/api/Arithmetica.java	Thu Feb 12 11:00:41 2009 +0100
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,54 +0,0 @@
     7.4 -package api;
     7.5 -
     7.6 -/** Class to simplify arithmetical operations, improved version to compute
     7.7 - * the sum for ranges, but only if the virtual machine is configured to
     7.8 - * run in incompatible mode.
     7.9 - *
    7.10 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    7.11 - * @version 2.0
    7.12 - */
    7.13 -// BEGIN: design.composition.arith2.0.property
    7.14 -public class Arithmetica {
    7.15 -    public int sumTwo(int one, int second) {
    7.16 -        return one + second;
    7.17 -    }
    7.18 -    
    7.19 -    public int sumAll(int... numbers) {
    7.20 -        if (numbers.length == 0) {
    7.21 -            return 0;
    7.22 -        }
    7.23 -        int sum = numbers[0];
    7.24 -        for (int i = 1; i < numbers.length; i++) {
    7.25 -            sum = sumTwo(sum, numbers[i]);
    7.26 -        }
    7.27 -        return sum;
    7.28 -    }
    7.29 -    
    7.30 -    public int sumRange(int from, int to) {
    7.31 -        // BEGIN: design.composition.arith2.0.property.if
    7.32 -        if (Boolean.getBoolean("arithmetica.v2")) {
    7.33 -            return sumRange2(from, to);
    7.34 -        } else {
    7.35 -            return sumRange1(from, to);
    7.36 -        }
    7.37 -        // END: design.composition.arith2.0.property.if
    7.38 -    }
    7.39 -
    7.40 -    private int sumRange1(int from, int to) {
    7.41 -        int len = to - from;
    7.42 -        if (len < 0) {
    7.43 -            len = -len;
    7.44 -            from = to;
    7.45 -        }
    7.46 -        int[] array = new int[len + 1];
    7.47 -        for (int i = 0; i <= len; i++) {
    7.48 -            array[i] = from + i;
    7.49 -        }
    7.50 -        return sumAll(array);
    7.51 -    }
    7.52 -    
    7.53 -    private int sumRange2(int from, int to) {
    7.54 -        return (from + to) * (Math.abs(to - from) + 1) / 2;
    7.55 -    }
    7.56 -}
    7.57 -// END: design.composition.arith2.0.property
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/samples/composition/src-api2.0-property/org/apidesign/math/Arithmetica.java	Sat Feb 14 17:30:06 2009 +0100
     8.3 @@ -0,0 +1,54 @@
     8.4 +package org.apidesign.math;
     8.5 +
     8.6 +/** Class to simplify arithmetical operations, improved version to compute
     8.7 + * the sum for ranges, but only if the virtual machine is configured to
     8.8 + * run in incompatible mode.
     8.9 + *
    8.10 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    8.11 + * @version 2.0
    8.12 + */
    8.13 +// BEGIN: design.composition.arith2.0.property
    8.14 +public class Arithmetica {
    8.15 +    public int sumTwo(int one, int second) {
    8.16 +        return one + second;
    8.17 +    }
    8.18 +    
    8.19 +    public int sumAll(int... numbers) {
    8.20 +        if (numbers.length == 0) {
    8.21 +            return 0;
    8.22 +        }
    8.23 +        int sum = numbers[0];
    8.24 +        for (int i = 1; i < numbers.length; i++) {
    8.25 +            sum = sumTwo(sum, numbers[i]);
    8.26 +        }
    8.27 +        return sum;
    8.28 +    }
    8.29 +    
    8.30 +    public int sumRange(int from, int to) {
    8.31 +        // BEGIN: design.composition.arith2.0.property.if
    8.32 +        if (Boolean.getBoolean("arithmetica.v2")) {
    8.33 +            return sumRange2(from, to);
    8.34 +        } else {
    8.35 +            return sumRange1(from, to);
    8.36 +        }
    8.37 +        // END: design.composition.arith2.0.property.if
    8.38 +    }
    8.39 +
    8.40 +    private int sumRange1(int from, int to) {
    8.41 +        int len = to - from;
    8.42 +        if (len < 0) {
    8.43 +            len = -len;
    8.44 +            from = to;
    8.45 +        }
    8.46 +        int[] array = new int[len + 1];
    8.47 +        for (int i = 0; i <= len; i++) {
    8.48 +            array[i] = from + i;
    8.49 +        }
    8.50 +        return sumAll(array);
    8.51 +    }
    8.52 +    
    8.53 +    private int sumRange2(int from, int to) {
    8.54 +        return (from + to) * (Math.abs(to - from) + 1) / 2;
    8.55 +    }
    8.56 +}
    8.57 +// END: design.composition.arith2.0.property
     9.1 --- a/samples/composition/src-api2.0-runtime/api/Arithmetica.java	Thu Feb 12 11:00:41 2009 +0100
     9.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.3 @@ -1,71 +0,0 @@
     9.4 -package api;
     9.5 -
     9.6 -import org.apidesign.runtime.check.RuntimeCheck;
     9.7 -
     9.8 -/** Class to simplify arithmetical operations, improved version to compute
     9.9 - * the sum for ranges, but only if the virtual machine is configured to
    9.10 - * run in incompatible mode.
    9.11 - *
    9.12 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    9.13 - * @version 2.0
    9.14 - */
    9.15 -public class Arithmetica {
    9.16 -    public int sumTwo(int one, int second) {
    9.17 -        return one + second;
    9.18 -    }
    9.19 -    
    9.20 -    public int sumAll(int... numbers) {
    9.21 -        if (numbers.length == 0) {
    9.22 -            return 0;
    9.23 -        }
    9.24 -        int sum = numbers[0];
    9.25 -        for (int i = 1; i < numbers.length; i++) {
    9.26 -            sum = sumTwo(sum, numbers[i]);
    9.27 -        }
    9.28 -        return sum;
    9.29 -    }
    9.30 -    
    9.31 -    public int sumRange(int from, int to) {
    9.32 -        if (calledByV2AwareLoader()) {
    9.33 -            return sumRange2(from, to);
    9.34 -        } else {
    9.35 -            return sumRange1(from, to);
    9.36 -        }
    9.37 -    }
    9.38 -
    9.39 -    private int sumRange1(int from, int to) {
    9.40 -        int len = to - from;
    9.41 -        if (len < 0) {
    9.42 -            len = -len;
    9.43 -            from = to;
    9.44 -        }
    9.45 -        int[] array = new int[len + 1];
    9.46 -        for (int i = 0; i <= len; i++) {
    9.47 -            array[i] = from + i;
    9.48 -        }
    9.49 -        return sumAll(array);
    9.50 -    }
    9.51 -    
    9.52 -    private int sumRange2(int from, int to) {
    9.53 -        return (from + to) * (Math.abs(to - from) + 1) / 2;
    9.54 -    }
    9.55 -
    9.56 -    private static boolean calledByV2AwareLoader() {
    9.57 -        // BEGIN: design.composition.arith2.6.runtime
    9.58 -        StackTraceElement[] arr = Thread.currentThread().getStackTrace();
    9.59 -        ClassLoader myLoader = Arithmetica.class.getClassLoader();
    9.60 -        for (int i = 0; i < arr.length; i++) {
    9.61 -            ClassLoader caller = arr[i].getClass().getClassLoader();
    9.62 -            if (myLoader == caller) {
    9.63 -                continue;
    9.64 -            }
    9.65 -            if (RuntimeCheck.requiresAtLeast("2.6", "api.Arithmetica", caller)) {
    9.66 -                return true;
    9.67 -            }
    9.68 -            return false;
    9.69 -        }
    9.70 -        return true;
    9.71 -        // END: design.composition.arith2.6.runtime
    9.72 -    }
    9.73 -    
    9.74 -}
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/samples/composition/src-api2.0-runtime/org/apidesign/math/Arithmetica.java	Sat Feb 14 17:30:06 2009 +0100
    10.3 @@ -0,0 +1,72 @@
    10.4 +package org.apidesign.math;
    10.5 +
    10.6 +import org.apidesign.math.Arithmetica;
    10.7 +import org.apidesign.runtime.check.RuntimeCheck;
    10.8 +
    10.9 +/** Class to simplify arithmetical operations, improved version to compute
   10.10 + * the sum for ranges, but only if the virtual machine is configured to
   10.11 + * run in incompatible mode.
   10.12 + *
   10.13 + * @author Jaroslav Tulach <jtulach@netbeans.org>
   10.14 + * @version 2.0
   10.15 + */
   10.16 +public class Arithmetica {
   10.17 +    public int sumTwo(int one, int second) {
   10.18 +        return one + second;
   10.19 +    }
   10.20 +    
   10.21 +    public int sumAll(int... numbers) {
   10.22 +        if (numbers.length == 0) {
   10.23 +            return 0;
   10.24 +        }
   10.25 +        int sum = numbers[0];
   10.26 +        for (int i = 1; i < numbers.length; i++) {
   10.27 +            sum = sumTwo(sum, numbers[i]);
   10.28 +        }
   10.29 +        return sum;
   10.30 +    }
   10.31 +    
   10.32 +    public int sumRange(int from, int to) {
   10.33 +        if (calledByV2AwareLoader()) {
   10.34 +            return sumRange2(from, to);
   10.35 +        } else {
   10.36 +            return sumRange1(from, to);
   10.37 +        }
   10.38 +    }
   10.39 +
   10.40 +    private int sumRange1(int from, int to) {
   10.41 +        int len = to - from;
   10.42 +        if (len < 0) {
   10.43 +            len = -len;
   10.44 +            from = to;
   10.45 +        }
   10.46 +        int[] array = new int[len + 1];
   10.47 +        for (int i = 0; i <= len; i++) {
   10.48 +            array[i] = from + i;
   10.49 +        }
   10.50 +        return sumAll(array);
   10.51 +    }
   10.52 +    
   10.53 +    private int sumRange2(int from, int to) {
   10.54 +        return (from + to) * (Math.abs(to - from) + 1) / 2;
   10.55 +    }
   10.56 +
   10.57 +    private static boolean calledByV2AwareLoader() {
   10.58 +        // BEGIN: design.composition.arith2.6.runtime
   10.59 +        StackTraceElement[] arr = Thread.currentThread().getStackTrace();
   10.60 +        ClassLoader myLoader = Arithmetica.class.getClassLoader();
   10.61 +        for (int i = 0; i < arr.length; i++) {
   10.62 +            ClassLoader caller = arr[i].getClass().getClassLoader();
   10.63 +            if (myLoader == caller) {
   10.64 +                continue;
   10.65 +            }
   10.66 +            if (RuntimeCheck.requiresAtLeast("2.6", "api.Arithmetica", caller)) {
   10.67 +                return true;
   10.68 +            }
   10.69 +            return false;
   10.70 +        }
   10.71 +        return true;
   10.72 +        // END: design.composition.arith2.6.runtime
   10.73 +    }
   10.74 +    
   10.75 +}
    11.1 --- a/samples/composition/src-api2.0-runtime/org/apidesign/runtime/check/RuntimeCheck.java	Thu Feb 12 11:00:41 2009 +0100
    11.2 +++ b/samples/composition/src-api2.0-runtime/org/apidesign/runtime/check/RuntimeCheck.java	Sat Feb 14 17:30:06 2009 +0100
    11.3 @@ -1,7 +1,6 @@
    11.4  package org.apidesign.runtime.check;
    11.5  
    11.6  import java.util.Map;
    11.7 -import java.util.Set;
    11.8  
    11.9  public final class RuntimeCheck {
   11.10      private RuntimeCheck() {
    12.1 --- a/samples/composition/src-api2.0-runtime/org/apidesign/runtime/check/VersionAwareClassLoader.java	Thu Feb 12 11:00:41 2009 +0100
    12.2 +++ b/samples/composition/src-api2.0-runtime/org/apidesign/runtime/check/VersionAwareClassLoader.java	Sat Feb 14 17:30:06 2009 +0100
    12.3 @@ -3,9 +3,6 @@
    12.4  import java.io.IOException;
    12.5  import java.io.InputStream;
    12.6  import java.util.Map;
    12.7 -import java.util.Set;
    12.8 -import java.util.logging.Level;
    12.9 -import java.util.logging.Logger;
   12.10  
   12.11  final class VersionAwareClassLoader extends ClassLoader 
   12.12  implements RuntimeCheck.AwareLoader {
    13.1 --- a/samples/composition/src-api2.0/api/Arithmetica.java	Thu Feb 12 11:00:41 2009 +0100
    13.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.3 @@ -1,30 +0,0 @@
    13.4 -package api;
    13.5 -
    13.6 -/** Class to simplify arithmetical operations, improved version to compute
    13.7 - * the sum for ranges.
    13.8 - *
    13.9 - * @author Jaroslav Tulach <jtulach@netbeans.org>
   13.10 - * @version 2.0
   13.11 - */
   13.12 -public class Arithmetica {
   13.13 -    public int sumTwo(int one, int second) {
   13.14 -        return one + second;
   13.15 -    }
   13.16 -    
   13.17 -    public int sumAll(int... numbers) {
   13.18 -        if (numbers.length == 0) {
   13.19 -            return 0;
   13.20 -        }
   13.21 -        int sum = numbers[0];
   13.22 -        for (int i = 1; i < numbers.length; i++) {
   13.23 -            sum = sumTwo(sum, numbers[i]);
   13.24 -        }
   13.25 -        return sum;
   13.26 -    }
   13.27 -    
   13.28 -// BEGIN: design.composition.arith2.0
   13.29 -    public int sumRange(int from, int to) {
   13.30 -        return (from + to) * (Math.abs(to - from) + 1) / 2;
   13.31 -    }
   13.32 -// END: design.composition.arith2.0
   13.33 -}
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/samples/composition/src-api2.0/org/apidesign/math/Arithmetica.java	Sat Feb 14 17:30:06 2009 +0100
    14.3 @@ -0,0 +1,30 @@
    14.4 +package org.apidesign.math;
    14.5 +
    14.6 +/** Class to simplify arithmetical operations, improved version to compute
    14.7 + * the sum for ranges.
    14.8 + *
    14.9 + * @author Jaroslav Tulach <jtulach@netbeans.org>
   14.10 + * @version 2.0
   14.11 + */
   14.12 +public class Arithmetica {
   14.13 +    public int sumTwo(int one, int second) {
   14.14 +        return one + second;
   14.15 +    }
   14.16 +    
   14.17 +    public int sumAll(int... numbers) {
   14.18 +        if (numbers.length == 0) {
   14.19 +            return 0;
   14.20 +        }
   14.21 +        int sum = numbers[0];
   14.22 +        for (int i = 1; i < numbers.length; i++) {
   14.23 +            sum = sumTwo(sum, numbers[i]);
   14.24 +        }
   14.25 +        return sum;
   14.26 +    }
   14.27 +    
   14.28 +// BEGIN: design.composition.arith2.0
   14.29 +    public int sumRange(int from, int to) {
   14.30 +        return (from + to) * (Math.abs(to - from) + 1) / 2;
   14.31 +    }
   14.32 +// END: design.composition.arith2.0
   14.33 +}
    15.1 --- a/samples/composition/src-test/api/ArithmeticaCompatibilityTest.java	Thu Feb 12 11:00:41 2009 +0100
    15.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.3 @@ -1,206 +0,0 @@
    15.4 -package api;
    15.5 -
    15.6 -import junit.framework.*;
    15.7 -
    15.8 -public class ArithmeticaCompatibilityTest extends TestCase {
    15.9 -    public ArithmeticaCompatibilityTest(String name) {
   15.10 -        super(name);
   15.11 -    }
   15.12 -
   15.13 -    private static final class CountingSubclass extends Arithmetica {
   15.14 -        int countSumTwo;
   15.15 -        int countSumAll;
   15.16 -        int countSumRange;
   15.17 -
   15.18 -        @Override
   15.19 -        public int sumAll(int... numbers) {
   15.20 -            countSumAll++;
   15.21 -            return super.sumAll(numbers);
   15.22 -        }
   15.23 -
   15.24 -        @Override
   15.25 -        public int sumRange(int from, int to) {
   15.26 -            countSumRange++;
   15.27 -            return super.sumRange(from, to);
   15.28 -        }
   15.29 -
   15.30 -        @Override
   15.31 -        public int sumTwo(int one, int second) {
   15.32 -            countSumTwo++;
   15.33 -            return super.sumTwo(one, second);
   15.34 -        }
   15.35 -    } // end of CountingSubclass
   15.36 -    
   15.37 -    private static final class CountingOldSubclass extends OldArithmetica1 {
   15.38 -        int countSumTwo;
   15.39 -        int countSumAll;
   15.40 -        int countSumRange;
   15.41 -
   15.42 -        @Override
   15.43 -        public int sumAll(int... numbers) {
   15.44 -            countSumAll++;
   15.45 -            return super.sumAll(numbers);
   15.46 -        }
   15.47 -
   15.48 -        @Override
   15.49 -        public int sumRange(int from, int to) {
   15.50 -            countSumRange++;
   15.51 -            return super.sumRange(from, to);
   15.52 -        }
   15.53 -
   15.54 -        @Override
   15.55 -        public int sumTwo(int one, int second) {
   15.56 -            countSumTwo++;
   15.57 -            return super.sumTwo(one, second);
   15.58 -        }
   15.59 -    } // end of CountingSubclass
   15.60 -    
   15.61 -    // BEGIN: total.rewrite.tests
   15.62 -    public void testRandomCheck () throws Exception {
   15.63 -        if (Boolean.getBoolean("no.failures")) return;
   15.64 -        long seed = System.currentTimeMillis();
   15.65 -        try {
   15.66 -            CountingSubclass now = new CountingSubclass();
   15.67 -            CountingOldSubclass old = new CountingOldSubclass();
   15.68 -            
   15.69 -            compare(now, old, seed);
   15.70 -        
   15.71 -            assertEquals(
   15.72 -                "Verify amount calls to of sumRange is the same", 
   15.73 -                now.countSumRange, old.countSumRange
   15.74 -            );
   15.75 -            assertEquals(
   15.76 -                "Verify amount calls to of sumAll is the same", 
   15.77 -                now.countSumAll, old.countSumAll
   15.78 -            );
   15.79 -            assertEquals(
   15.80 -                "Verify amount calls to of sumTwo is the same", 
   15.81 -                now.countSumTwo, old.countSumTwo
   15.82 -            );
   15.83 -        } catch (AssertionFailedError ex) {
   15.84 -            IllegalStateException n = new IllegalStateException (
   15.85 -                "Seed: " + seed + "\n" + ex.getMessage ()
   15.86 -            );
   15.87 -            n.initCause(ex);
   15.88 -            throw n;
   15.89 -        } catch (Exception ex) {
   15.90 -            IllegalStateException n = new IllegalStateException (
   15.91 -                "Seed: " + seed + "\n" + ex.getMessage ()
   15.92 -            );
   15.93 -            n.initCause(ex);
   15.94 -            throw n;
   15.95 -        }
   15.96 -    }
   15.97 -    
   15.98 -    public void testSimulateOKRunOn1208120436947() throws Exception {
   15.99 -        CountingSubclass now = new CountingSubclass();
  15.100 -        CountingOldSubclass old = new CountingOldSubclass();
  15.101 -
  15.102 -        compare(now, old, 1208120436947L);
  15.103 -
  15.104 -        assertEquals(
  15.105 -            "Verify amount of calls to sumRange is the same", 
  15.106 -            now.countSumRange, old.countSumRange
  15.107 -        );
  15.108 -        assertEquals(
  15.109 -            "Verify amount of calls to sumAll is the same", 
  15.110 -            now.countSumAll, old.countSumAll
  15.111 -        );
  15.112 -        assertEquals(
  15.113 -            "Verify amount of calls to sumTwo is the same", 
  15.114 -            now.countSumTwo, old.countSumTwo
  15.115 -        );
  15.116 -    }
  15.117 -
  15.118 -    public void testSimulateFailureOn1208120628821() throws Exception {
  15.119 -        if (Boolean.getBoolean("no.failures")) return;
  15.120 -        CountingSubclass now = new CountingSubclass();
  15.121 -        CountingOldSubclass old = new CountingOldSubclass();
  15.122 -
  15.123 -        compare(now, old, 1208120628821L);
  15.124 -
  15.125 -        assertEquals(
  15.126 -            "Verify amount of calls to sumRange is the same", 
  15.127 -            now.countSumRange, old.countSumRange
  15.128 -        );
  15.129 -        assertEquals(
  15.130 -            "Verify amount of calls to sumAll is the same", 
  15.131 -            now.countSumAll, old.countSumAll
  15.132 -        );
  15.133 -        assertEquals(
  15.134 -            "Verify amount of calls to sumTwo is the same", 
  15.135 -            now.countSumTwo, old.countSumTwo
  15.136 -        );
  15.137 -    }
  15.138 -    // END: total.rewrite.tests
  15.139 -    
  15.140 -    // BEGIN: total.rewrite.compare
  15.141 -    private void compare (Arithmetica now, OldArithmetica1 old, long seed) 
  15.142 -    throws Exception {
  15.143 -        java.util.Random r = new java.util.Random (seed);
  15.144 -        
  15.145 -        for (int loop = 0; loop < r.nextInt(5); loop++) {
  15.146 -            int operation = r.nextInt(3);
  15.147 -            switch (operation) {
  15.148 -                case 0: { // sumTwo
  15.149 -                    int a1 = r.nextInt(100);
  15.150 -                    int a2 = r.nextInt(100);
  15.151 -                    int resNow = now.sumTwo(a1, a2);
  15.152 -                    int resOld = old.sumTwo(a1, a2);
  15.153 -                    assertEquals("sumTwo results are equal", resNow, resOld);
  15.154 -                    break;
  15.155 -                }
  15.156 -                case 1: { // sumArray
  15.157 -                    int[] arr = new int[r.nextInt(100)];
  15.158 -                    for (int i = 0; i < arr.length; i++) {
  15.159 -                        arr[i] = r.nextInt(100);
  15.160 -                    }
  15.161 -                    int resNow = now.sumAll(arr);
  15.162 -                    int resOld = old.sumAll(arr);
  15.163 -                    assertEquals("sumArray results are equal", resNow, resOld);
  15.164 -                    break;
  15.165 -                }
  15.166 -                case 2: { // sumRange
  15.167 -                    int a1 = r.nextInt(100);
  15.168 -                    int a2 = r.nextInt(100);
  15.169 -                    int resNow = now.sumRange(a1, a1 + a2);
  15.170 -                    int resOld = old.sumRange(a1, a1 + a2);
  15.171 -                    assertEquals("sumRange results are equal", resNow, resOld);
  15.172 -                    break;
  15.173 -                }
  15.174 -            }
  15.175 -        }
  15.176 -    }
  15.177 -    // END: total.rewrite.compare
  15.178 -
  15.179 -    
  15.180 -    // BEGIN: total.rewrite.oldimpl
  15.181 -    /** This is a copy of the implementation of Arithmetica from version 1.0 */
  15.182 -    static class OldArithmetica1 {
  15.183 -        public int sumTwo(int one, int second) {
  15.184 -            return one + second;
  15.185 -        }
  15.186 -
  15.187 -        public int sumAll(int... numbers) {
  15.188 -            if (numbers.length == 0) {
  15.189 -                return 0;
  15.190 -            }
  15.191 -            int sum = numbers[0];
  15.192 -            for (int i = 1; i < numbers.length; i++) {
  15.193 -                sum = sumTwo(sum, numbers[i]);
  15.194 -            }
  15.195 -            return sum;
  15.196 -        }
  15.197 -
  15.198 -        public int sumRange(int from, int to) {
  15.199 -            int len = to - from;
  15.200 -            int[] array = new int[len + 1];
  15.201 -            for (int i = 0; i <= len; i++) {
  15.202 -                array[i] = from + i;
  15.203 -            }
  15.204 -            return sumAll(array);
  15.205 -        }
  15.206 -    } 
  15.207 -    // END: total.rewrite.oldimpl
  15.208 -    
  15.209 -}
    16.1 --- a/samples/composition/src-test/api/ArithmeticaTest.java	Thu Feb 12 11:00:41 2009 +0100
    16.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.3 @@ -1,45 +0,0 @@
    16.4 -package api;
    16.5 -
    16.6 -import junit.framework.TestCase;
    16.7 -
    16.8 -/**
    16.9 - *
   16.10 - * @author Jaroslav Tulach <jtulach@netbeans.org>
   16.11 - */
   16.12 -public class ArithmeticaTest extends TestCase {
   16.13 -    
   16.14 -    public ArithmeticaTest(String testName) {
   16.15 -        super(testName);
   16.16 -    }            
   16.17 -
   16.18 -    @Override
   16.19 -    protected void setUp() throws Exception {
   16.20 -        super.setUp();
   16.21 -    }
   16.22 -
   16.23 -    @Override
   16.24 -    protected void tearDown() throws Exception {
   16.25 -        super.tearDown();
   16.26 -    }
   16.27 -
   16.28 -    // BEGIN: design.composition.arith.test
   16.29 -    public void testSumTwo() {
   16.30 -        Arithmetica instance = new Arithmetica();
   16.31 -        assertEquals("+", 5, instance.sumTwo(3, 2));
   16.32 -    }
   16.33 -
   16.34 -    public void testSumAll() {
   16.35 -        Arithmetica instance = new Arithmetica();
   16.36 -        assertEquals("+", 6, instance.sumAll(3, 2, 1));
   16.37 -    }
   16.38 -
   16.39 -    public void testSumRange() {
   16.40 -        Arithmetica instance = new Arithmetica();
   16.41 -        assertEquals("1+2+3=6", 6, instance.sumRange(1, 3));
   16.42 -        assertEquals("sum(1,10)=55", 55, instance.sumRange(1, 10));
   16.43 -        assertEquals("sum(1,1)=1", 1, instance.sumRange(1, 1));
   16.44 -        assertEquals("sum(10,1)=55", 55, instance.sumRange(10, 1));
   16.45 -    }
   16.46 -    // END: design.composition.arith.test
   16.47 -
   16.48 -}
    17.1 --- a/samples/composition/src-test/api/Factorial.java	Thu Feb 12 11:00:41 2009 +0100
    17.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.3 @@ -1,18 +0,0 @@
    17.4 -package api;
    17.5 -
    17.6 -/** Class showing inventive, non-expected use of 
    17.7 - * Arithmetica methods to do multiplication instead of
    17.8 - * addition.
    17.9 - */
   17.10 -// BEGIN: design.composition.arith.factorial
   17.11 -public final class Factorial extends Arithmetica {
   17.12 -    public static int factorial(int n) {
   17.13 -        return new Factorial().sumRange(1, n);
   17.14 -    }
   17.15 -    @Override
   17.16 -    public int sumTwo(int one, int second) {
   17.17 -        return one * second;
   17.18 -    }
   17.19 -}
   17.20 -// END: design.composition.arith.factorial
   17.21 -
    18.1 --- a/samples/composition/src-test/api/FactorialTest.java	Thu Feb 12 11:00:41 2009 +0100
    18.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.3 @@ -1,44 +0,0 @@
    18.4 -/*
    18.5 - * Žluťoučký kůň je naše hříbátko.
    18.6 - * and open the template in the editor.
    18.7 - */
    18.8 -
    18.9 -package api;
   18.10 -
   18.11 -import junit.framework.TestCase;
   18.12 -
   18.13 -/**
   18.14 - *
   18.15 - * @author Jaroslav Tulach <jtulach@netbeans.org>
   18.16 - */
   18.17 -public class FactorialTest extends TestCase {
   18.18 -    
   18.19 -    public FactorialTest(String testName) {
   18.20 -        super(testName);
   18.21 -    }            
   18.22 -
   18.23 -    @Override
   18.24 -    protected void setUp() throws Exception {
   18.25 -        super.setUp();
   18.26 -    }
   18.27 -
   18.28 -    @Override
   18.29 -    protected void tearDown() throws Exception {
   18.30 -        super.tearDown();
   18.31 -    }
   18.32 -    
   18.33 -    public void testFactorial3() {
   18.34 -        if (Boolean.getBoolean("no.failures")) return;
   18.35 -        assertEquals(6, Factorial.factorial(3));
   18.36 -    }
   18.37 -    
   18.38 -    public void testFactorial4() {
   18.39 -        if (Boolean.getBoolean("no.failures")) return;
   18.40 -        assertEquals(24, Factorial.factorial(4));
   18.41 -    }
   18.42 -    
   18.43 -    public void testFactorial5() {
   18.44 -        if (Boolean.getBoolean("no.failures")) return;
   18.45 -        assertEquals(120, Factorial.factorial(5));
   18.46 -    }
   18.47 -}
    19.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.2 +++ b/samples/composition/src-test/org/apidesign/math/test/ArithmeticaCompatibilityTest.java	Sat Feb 14 17:30:06 2009 +0100
    19.3 @@ -0,0 +1,208 @@
    19.4 +package org.apidesign.math.test;
    19.5 +
    19.6 +import junit.framework.AssertionFailedError;
    19.7 +import junit.framework.TestCase;
    19.8 +import org.apidesign.math.Arithmetica;
    19.9 +
   19.10 +public class ArithmeticaCompatibilityTest extends TestCase {
   19.11 +    public ArithmeticaCompatibilityTest(String name) {
   19.12 +        super(name);
   19.13 +    }
   19.14 +
   19.15 +    private static final class CountingSubclass extends Arithmetica {
   19.16 +        int countSumTwo;
   19.17 +        int countSumAll;
   19.18 +        int countSumRange;
   19.19 +
   19.20 +        @Override
   19.21 +        public int sumAll(int... numbers) {
   19.22 +            countSumAll++;
   19.23 +            return super.sumAll(numbers);
   19.24 +        }
   19.25 +
   19.26 +        @Override
   19.27 +        public int sumRange(int from, int to) {
   19.28 +            countSumRange++;
   19.29 +            return super.sumRange(from, to);
   19.30 +        }
   19.31 +
   19.32 +        @Override
   19.33 +        public int sumTwo(int one, int second) {
   19.34 +            countSumTwo++;
   19.35 +            return super.sumTwo(one, second);
   19.36 +        }
   19.37 +    } // end of CountingSubclass
   19.38 +    
   19.39 +    private static final class CountingOldSubclass extends OldArithmetica1 {
   19.40 +        int countSumTwo;
   19.41 +        int countSumAll;
   19.42 +        int countSumRange;
   19.43 +
   19.44 +        @Override
   19.45 +        public int sumAll(int... numbers) {
   19.46 +            countSumAll++;
   19.47 +            return super.sumAll(numbers);
   19.48 +        }
   19.49 +
   19.50 +        @Override
   19.51 +        public int sumRange(int from, int to) {
   19.52 +            countSumRange++;
   19.53 +            return super.sumRange(from, to);
   19.54 +        }
   19.55 +
   19.56 +        @Override
   19.57 +        public int sumTwo(int one, int second) {
   19.58 +            countSumTwo++;
   19.59 +            return super.sumTwo(one, second);
   19.60 +        }
   19.61 +    } // end of CountingSubclass
   19.62 +    
   19.63 +    // BEGIN: total.rewrite.tests
   19.64 +    public void testRandomCheck () throws Exception {
   19.65 +        if (Boolean.getBoolean("no.failures")) return;
   19.66 +        long seed = System.currentTimeMillis();
   19.67 +        try {
   19.68 +            CountingSubclass now = new CountingSubclass();
   19.69 +            CountingOldSubclass old = new CountingOldSubclass();
   19.70 +            
   19.71 +            compare(now, old, seed);
   19.72 +        
   19.73 +            assertEquals(
   19.74 +                "Verify amount calls to of sumRange is the same", 
   19.75 +                now.countSumRange, old.countSumRange
   19.76 +            );
   19.77 +            assertEquals(
   19.78 +                "Verify amount calls to of sumAll is the same", 
   19.79 +                now.countSumAll, old.countSumAll
   19.80 +            );
   19.81 +            assertEquals(
   19.82 +                "Verify amount calls to of sumTwo is the same", 
   19.83 +                now.countSumTwo, old.countSumTwo
   19.84 +            );
   19.85 +        } catch (AssertionFailedError ex) {
   19.86 +            IllegalStateException n = new IllegalStateException (
   19.87 +                "Seed: " + seed + "\n" + ex.getMessage ()
   19.88 +            );
   19.89 +            n.initCause(ex);
   19.90 +            throw n;
   19.91 +        } catch (Exception ex) {
   19.92 +            IllegalStateException n = new IllegalStateException (
   19.93 +                "Seed: " + seed + "\n" + ex.getMessage ()
   19.94 +            );
   19.95 +            n.initCause(ex);
   19.96 +            throw n;
   19.97 +        }
   19.98 +    }
   19.99 +    
  19.100 +    public void testSimulateOKRunOn1208120436947() throws Exception {
  19.101 +        CountingSubclass now = new CountingSubclass();
  19.102 +        CountingOldSubclass old = new CountingOldSubclass();
  19.103 +
  19.104 +        compare(now, old, 1208120436947L);
  19.105 +
  19.106 +        assertEquals(
  19.107 +            "Verify amount of calls to sumRange is the same", 
  19.108 +            now.countSumRange, old.countSumRange
  19.109 +        );
  19.110 +        assertEquals(
  19.111 +            "Verify amount of calls to sumAll is the same", 
  19.112 +            now.countSumAll, old.countSumAll
  19.113 +        );
  19.114 +        assertEquals(
  19.115 +            "Verify amount of calls to sumTwo is the same", 
  19.116 +            now.countSumTwo, old.countSumTwo
  19.117 +        );
  19.118 +    }
  19.119 +
  19.120 +    public void testSimulateFailureOn1208120628821() throws Exception {
  19.121 +        if (Boolean.getBoolean("no.failures")) return;
  19.122 +        CountingSubclass now = new CountingSubclass();
  19.123 +        CountingOldSubclass old = new CountingOldSubclass();
  19.124 +
  19.125 +        compare(now, old, 1208120628821L);
  19.126 +
  19.127 +        assertEquals(
  19.128 +            "Verify amount of calls to sumRange is the same", 
  19.129 +            now.countSumRange, old.countSumRange
  19.130 +        );
  19.131 +        assertEquals(
  19.132 +            "Verify amount of calls to sumAll is the same", 
  19.133 +            now.countSumAll, old.countSumAll
  19.134 +        );
  19.135 +        assertEquals(
  19.136 +            "Verify amount of calls to sumTwo is the same", 
  19.137 +            now.countSumTwo, old.countSumTwo
  19.138 +        );
  19.139 +    }
  19.140 +    // END: total.rewrite.tests
  19.141 +    
  19.142 +    // BEGIN: total.rewrite.compare
  19.143 +    private void compare (Arithmetica now, OldArithmetica1 old, long seed) 
  19.144 +    throws Exception {
  19.145 +        java.util.Random r = new java.util.Random (seed);
  19.146 +        
  19.147 +        for (int loop = 0; loop < r.nextInt(5); loop++) {
  19.148 +            int operation = r.nextInt(3);
  19.149 +            switch (operation) {
  19.150 +                case 0: { // sumTwo
  19.151 +                    int a1 = r.nextInt(100);
  19.152 +                    int a2 = r.nextInt(100);
  19.153 +                    int resNow = now.sumTwo(a1, a2);
  19.154 +                    int resOld = old.sumTwo(a1, a2);
  19.155 +                    assertEquals("sumTwo results are equal", resNow, resOld);
  19.156 +                    break;
  19.157 +                }
  19.158 +                case 1: { // sumArray
  19.159 +                    int[] arr = new int[r.nextInt(100)];
  19.160 +                    for (int i = 0; i < arr.length; i++) {
  19.161 +                        arr[i] = r.nextInt(100);
  19.162 +                    }
  19.163 +                    int resNow = now.sumAll(arr);
  19.164 +                    int resOld = old.sumAll(arr);
  19.165 +                    assertEquals("sumArray results are equal", resNow, resOld);
  19.166 +                    break;
  19.167 +                }
  19.168 +                case 2: { // sumRange
  19.169 +                    int a1 = r.nextInt(100);
  19.170 +                    int a2 = r.nextInt(100);
  19.171 +                    int resNow = now.sumRange(a1, a1 + a2);
  19.172 +                    int resOld = old.sumRange(a1, a1 + a2);
  19.173 +                    assertEquals("sumRange results are equal", resNow, resOld);
  19.174 +                    break;
  19.175 +                }
  19.176 +            }
  19.177 +        }
  19.178 +    }
  19.179 +    // END: total.rewrite.compare
  19.180 +
  19.181 +    
  19.182 +    // BEGIN: total.rewrite.oldimpl
  19.183 +    /** This is a copy of the implementation of Arithmetica from version 1.0 */
  19.184 +    static class OldArithmetica1 {
  19.185 +        public int sumTwo(int one, int second) {
  19.186 +            return one + second;
  19.187 +        }
  19.188 +
  19.189 +        public int sumAll(int... numbers) {
  19.190 +            if (numbers.length == 0) {
  19.191 +                return 0;
  19.192 +            }
  19.193 +            int sum = numbers[0];
  19.194 +            for (int i = 1; i < numbers.length; i++) {
  19.195 +                sum = sumTwo(sum, numbers[i]);
  19.196 +            }
  19.197 +            return sum;
  19.198 +        }
  19.199 +
  19.200 +        public int sumRange(int from, int to) {
  19.201 +            int len = to - from;
  19.202 +            int[] array = new int[len + 1];
  19.203 +            for (int i = 0; i <= len; i++) {
  19.204 +                array[i] = from + i;
  19.205 +            }
  19.206 +            return sumAll(array);
  19.207 +        }
  19.208 +    } 
  19.209 +    // END: total.rewrite.oldimpl
  19.210 +    
  19.211 +}
    20.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    20.2 +++ b/samples/composition/src-test/org/apidesign/math/test/ArithmeticaTest.java	Sat Feb 14 17:30:06 2009 +0100
    20.3 @@ -0,0 +1,46 @@
    20.4 +package org.apidesign.math.test;
    20.5 +
    20.6 +import org.apidesign.math.Arithmetica;
    20.7 +import junit.framework.TestCase;
    20.8 +
    20.9 +/**
   20.10 + *
   20.11 + * @author Jaroslav Tulach <jtulach@netbeans.org>
   20.12 + */
   20.13 +public class ArithmeticaTest extends TestCase {
   20.14 +    
   20.15 +    public ArithmeticaTest(String testName) {
   20.16 +        super(testName);
   20.17 +    }            
   20.18 +
   20.19 +    @Override
   20.20 +    protected void setUp() throws Exception {
   20.21 +        super.setUp();
   20.22 +    }
   20.23 +
   20.24 +    @Override
   20.25 +    protected void tearDown() throws Exception {
   20.26 +        super.tearDown();
   20.27 +    }
   20.28 +
   20.29 +    // BEGIN: design.composition.arith.test
   20.30 +    public void testSumTwo() {
   20.31 +        Arithmetica instance = new Arithmetica();
   20.32 +        assertEquals("+", 5, instance.sumTwo(3, 2));
   20.33 +    }
   20.34 +
   20.35 +    public void testSumAll() {
   20.36 +        Arithmetica instance = new Arithmetica();
   20.37 +        assertEquals("+", 6, instance.sumAll(3, 2, 1));
   20.38 +    }
   20.39 +
   20.40 +    public void testSumRange() {
   20.41 +        Arithmetica instance = new Arithmetica();
   20.42 +        assertEquals("1+2+3=6", 6, instance.sumRange(1, 3));
   20.43 +        assertEquals("sum(1,10)=55", 55, instance.sumRange(1, 10));
   20.44 +        assertEquals("sum(1,1)=1", 1, instance.sumRange(1, 1));
   20.45 +        assertEquals("sum(10,1)=55", 55, instance.sumRange(10, 1));
   20.46 +    }
   20.47 +    // END: design.composition.arith.test
   20.48 +
   20.49 +}
    21.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    21.2 +++ b/samples/composition/src-test/org/apidesign/math/test/Factorial.java	Sat Feb 14 17:30:06 2009 +0100
    21.3 @@ -0,0 +1,20 @@
    21.4 +package org.apidesign.math.test;
    21.5 +
    21.6 +import org.apidesign.math.Arithmetica;
    21.7 +
    21.8 +/** Class showing inventive, non-expected use of 
    21.9 + * Arithmetica methods to do multiplication instead of
   21.10 + * addition.
   21.11 + */
   21.12 +// BEGIN: design.composition.arith.factorial
   21.13 +public final class Factorial extends Arithmetica {
   21.14 +    public static int factorial(int n) {
   21.15 +        return new Factorial().sumRange(1, n);
   21.16 +    }
   21.17 +    @Override
   21.18 +    public int sumTwo(int one, int second) {
   21.19 +        return one * second;
   21.20 +    }
   21.21 +}
   21.22 +// END: design.composition.arith.factorial
   21.23 +
    22.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    22.2 +++ b/samples/composition/src-test/org/apidesign/math/test/FactorialTest.java	Sat Feb 14 17:30:06 2009 +0100
    22.3 @@ -0,0 +1,44 @@
    22.4 +/*
    22.5 + * Žluťoučký kůň je naše hříbátko.
    22.6 + * and open the template in the editor.
    22.7 + */
    22.8 +
    22.9 +package org.apidesign.math.test;
   22.10 +
   22.11 +import junit.framework.TestCase;
   22.12 +
   22.13 +/**
   22.14 + *
   22.15 + * @author Jaroslav Tulach <jtulach@netbeans.org>
   22.16 + */
   22.17 +public class FactorialTest extends TestCase {
   22.18 +    
   22.19 +    public FactorialTest(String testName) {
   22.20 +        super(testName);
   22.21 +    }            
   22.22 +
   22.23 +    @Override
   22.24 +    protected void setUp() throws Exception {
   22.25 +        super.setUp();
   22.26 +    }
   22.27 +
   22.28 +    @Override
   22.29 +    protected void tearDown() throws Exception {
   22.30 +        super.tearDown();
   22.31 +    }
   22.32 +    
   22.33 +    public void testFactorial3() {
   22.34 +        if (Boolean.getBoolean("no.failures")) return;
   22.35 +        assertEquals(6, Factorial.factorial(3));
   22.36 +    }
   22.37 +    
   22.38 +    public void testFactorial4() {
   22.39 +        if (Boolean.getBoolean("no.failures")) return;
   22.40 +        assertEquals(24, Factorial.factorial(4));
   22.41 +    }
   22.42 +    
   22.43 +    public void testFactorial5() {
   22.44 +        if (Boolean.getBoolean("no.failures")) return;
   22.45 +        assertEquals(120, Factorial.factorial(5));
   22.46 +    }
   22.47 +}