Using Arithmetica for yet another example
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:57:56 +0200
changeset 148e762b177d4b0
parent 147 e81ff4f391b8
child 149 eb52f31b18f4
Using Arithmetica for yet another example
samples/composition/build.xml
samples/composition/nbproject/project.xml
samples/composition/src-api2.0-property/api/Arithmetica.java
     1.1 --- a/samples/composition/build.xml	Sat Jun 14 09:57:54 2008 +0200
     1.2 +++ b/samples/composition/build.xml	Sat Jun 14 09:57:56 2008 +0200
     1.3 @@ -15,6 +15,9 @@
     1.4          <antcall target="-build-one">
     1.5              <param name="version" value="api2.0-compat"/>
     1.6          </antcall>
     1.7 +        <antcall target="-build-one">
     1.8 +            <param name="version" value="api2.0-property"/>
     1.9 +        </antcall>
    1.10          
    1.11          <antcall target="-build-one">
    1.12              <param name="version" value="test"/>
    1.13 @@ -35,6 +38,10 @@
    1.14          <antcall target="-run-one">
    1.15              <param name="version" value="api2.0-compat"/>
    1.16          </antcall>
    1.17 +        <echo level="info" message="Running the Implementation against Version 2.0 with another compatible extension of the API. This should succeed."/>
    1.18 +        <antcall target="-run-one">
    1.19 +            <param name="version" value="api2.0-property"/>
    1.20 +        </antcall>
    1.21      </target>
    1.22      
    1.23      <!-- support methods -->
     2.1 --- a/samples/composition/nbproject/project.xml	Sat Jun 14 09:57:54 2008 +0200
     2.2 +++ b/samples/composition/nbproject/project.xml	Sat Jun 14 09:57:56 2008 +0200
     2.3 @@ -29,6 +29,12 @@
     2.4                      <encoding>UTF-8</encoding>
     2.5                  </source-folder>
     2.6                  <source-folder>
     2.7 +                    <label>src-api2.0-property</label>
     2.8 +                    <type>java</type>
     2.9 +                    <location>src-api2.0-property</location>
    2.10 +                    <encoding>UTF-8</encoding>
    2.11 +                </source-folder>
    2.12 +                <source-folder>
    2.13                      <label>test</label>
    2.14                      <type>java</type>
    2.15                      <location>src-test</location>
    2.16 @@ -68,6 +74,10 @@
    2.17                          <location>src-api2.0-compat</location>
    2.18                      </source-folder>
    2.19                      <source-folder style="packages">
    2.20 +                        <label>API Version 2.0, with property</label>
    2.21 +                        <location>src-api2.0-property</location>
    2.22 +                    </source-folder>
    2.23 +                    <source-folder style="packages">
    2.24                          <label>Usage of the API</label>
    2.25                          <location>src-test</location>
    2.26                      </source-folder>
    2.27 @@ -98,6 +108,10 @@
    2.28                  <source-level>1.5</source-level>
    2.29              </compilation-unit>
    2.30              <compilation-unit>
    2.31 +                <package-root>src-api2.0-property</package-root>
    2.32 +                <source-level>1.5</source-level>
    2.33 +            </compilation-unit>
    2.34 +            <compilation-unit>
    2.35                  <package-root>src-test</package-root>
    2.36                  <classpath mode="compile">src-api1.0:../libs/dist/junit-4.4.jar</classpath>
    2.37                  <source-level>1.5</source-level>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/samples/composition/src-api2.0-property/api/Arithmetica.java	Sat Jun 14 09:57:56 2008 +0200
     3.3 @@ -0,0 +1,45 @@
     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 the virtual machine is configured to
     3.8 + * run in incompatible mode.
     3.9 + *
    3.10 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.11 + * @version 2.0
    3.12 + */
    3.13 +// BEGIN: design.composition.arith2.0.property
    3.14 +public class Arithmetica {
    3.15 +    public int sumTwo(int one, int second) {
    3.16 +        return one + second;
    3.17 +    }
    3.18 +    
    3.19 +    public int sumAll(int... numbers) {
    3.20 +        int sum = numbers[0];
    3.21 +        for (int i = 1; i < numbers.length; i++) {
    3.22 +            sum = sumTwo(sum, numbers[i]);
    3.23 +        }
    3.24 +        return sum;
    3.25 +    }
    3.26 +    
    3.27 +    public int sumRange(int from, int to) {
    3.28 +        if (Boolean.getBoolean("arithmetica.v2")) {
    3.29 +            return sumRange2(from, to);
    3.30 +        } else {
    3.31 +            return sumRange1(from, to);
    3.32 +        }
    3.33 +    }
    3.34 +
    3.35 +    private int sumRange1(int from, int to) {
    3.36 +        int len = to - from;
    3.37 +        int[] array = new int[len + 1];
    3.38 +        for (int i = 0; i <= len; i++) {
    3.39 +            array[i] = from + i;
    3.40 +        }
    3.41 +        return sumAll(array);
    3.42 +    }
    3.43 +    
    3.44 +    private int sumRange2(int from, int to) {
    3.45 +        return (from + to) * (to - from + 1) / 2;
    3.46 +    }
    3.47 +// END: design.composition.arith2.0.property
    3.48 +}