Alternative behaviour with constructor described on Arithmetica example
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:57:54 +0200
changeset 147e81ff4f391b8
parent 146 afad3bdb7bce
child 148 e762b177d4b0
Alternative behaviour with constructor described on Arithmetica example
samples/composition/build.xml
samples/composition/nbproject/project.xml
samples/composition/src-api2.0-compat/api/Arithmetica.java
     1.1 --- a/samples/composition/build.xml	Sat Jun 14 09:57:18 2008 +0200
     1.2 +++ b/samples/composition/build.xml	Sat Jun 14 09:57:54 2008 +0200
     1.3 @@ -12,6 +12,9 @@
     1.4          <antcall target="-build-one">
     1.5              <param name="version" value="api2.0"/>
     1.6          </antcall>
     1.7 +        <antcall target="-build-one">
     1.8 +            <param name="version" value="api2.0-compat"/>
     1.9 +        </antcall>
    1.10          
    1.11          <antcall target="-build-one">
    1.12              <param name="version" value="test"/>
    1.13 @@ -28,6 +31,10 @@
    1.14          <antcall target="-run-one">
    1.15              <param name="version" value="api2.0"/>
    1.16          </antcall>
    1.17 +        <echo level="info" message="Running the Implementation against Version 2.0 with compatible extension of the API. This should succeed."/>
    1.18 +        <antcall target="-run-one">
    1.19 +            <param name="version" value="api2.0-compat"/>
    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:18 2008 +0200
     2.2 +++ b/samples/composition/nbproject/project.xml	Sat Jun 14 09:57:54 2008 +0200
     2.3 @@ -23,6 +23,12 @@
     2.4                      <encoding>UTF-8</encoding>
     2.5                  </source-folder>
     2.6                  <source-folder>
     2.7 +                    <label>src-api2.0-compat</label>
     2.8 +                    <type>java</type>
     2.9 +                    <location>src-api2.0-compat</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 @@ -39,6 +45,9 @@
    2.17                  <action name="test">
    2.18                      <target>test</target>
    2.19                  </action>
    2.20 +                <action name="run">
    2.21 +                    <target>test</target>
    2.22 +                </action>
    2.23                  <action name="rebuild">
    2.24                      <target>clean</target>
    2.25                      <target>build</target>
    2.26 @@ -55,6 +64,10 @@
    2.27                          <location>src-api2.0</location>
    2.28                      </source-folder>
    2.29                      <source-folder style="packages">
    2.30 +                        <label>API Version 2.0, in compatible style</label>
    2.31 +                        <location>src-api2.0-compat</location>
    2.32 +                    </source-folder>
    2.33 +                    <source-folder style="packages">
    2.34                          <label>Usage of the API</label>
    2.35                          <location>src-test</location>
    2.36                      </source-folder>
    2.37 @@ -65,6 +78,7 @@
    2.38                  <context-menu>
    2.39                      <ide-action name="build"/>
    2.40                      <ide-action name="rebuild"/>
    2.41 +                    <ide-action name="run"/>
    2.42                      <ide-action name="clean"/>
    2.43                      <ide-action name="test"/>
    2.44                  </context-menu>
    2.45 @@ -80,6 +94,10 @@
    2.46                  <source-level>1.5</source-level>
    2.47              </compilation-unit>
    2.48              <compilation-unit>
    2.49 +                <package-root>src-api2.0-compat</package-root>
    2.50 +                <source-level>1.5</source-level>
    2.51 +            </compilation-unit>
    2.52 +            <compilation-unit>
    2.53                  <package-root>src-test</package-root>
    2.54                  <classpath mode="compile">src-api1.0:../libs/dist/junit-4.4.jar</classpath>
    2.55                  <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-compat/api/Arithmetica.java	Sat Jun 14 09:57:54 2008 +0200
     3.3 @@ -0,0 +1,54 @@
     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 +        int sum = numbers[0];
    3.30 +        for (int i = 1; i < numbers.length; i++) {
    3.31 +            sum = sumTwo(sum, numbers[i]);
    3.32 +        }
    3.33 +        return sum;
    3.34 +    }
    3.35 +    
    3.36 +    public int sumRange(int from, int to) {
    3.37 +        switch (version) {
    3.38 +            case 1: return sumRange1(from, to);
    3.39 +            case 2: return sumRange2(from, to);
    3.40 +            default: throw new IllegalStateException();
    3.41 +        }
    3.42 +    }
    3.43 +
    3.44 +    private int sumRange1(int from, int to) {
    3.45 +        int len = to - from;
    3.46 +        int[] array = new int[len + 1];
    3.47 +        for (int i = 0; i <= len; i++) {
    3.48 +            array[i] = from + i;
    3.49 +        }
    3.50 +        return sumAll(array);
    3.51 +    }
    3.52 +    
    3.53 +    private int sumRange2(int from, int to) {
    3.54 +        return (from + to) * (to - from + 1) / 2;
    3.55 +    }
    3.56 +// END: design.composition.arith2.0.compat
    3.57 +}