Arithmetica example with fuzzy modifiers eliminated
authorJaroslav Tulach <jtulach@netbeans.org>
Fri, 27 Mar 2009 20:30:39 +0100
changeset 3254553c2885ce6
parent 324 a9ad091ed069
child 326 1657c68af062
Arithmetica example with fuzzy modifiers eliminated
samples/sidemeanings/src/org/apidesign/sidemeanings/math/Arithmetica.java
samples/sidemeanings/test/org/apidesign/sidemeanings/math/ArithmeticaTest.java
samples/sidemeanings/test/org/apidesign/sidemeanings/math/Factorial.java
samples/sidemeanings/test/org/apidesign/sidemeanings/math/FactorialNotPossibleInNotOpenedModeTest.java
samples/sidemeanings/test/org/apidesign/sidemeanings/math/FactorialTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/sidemeanings/src/org/apidesign/sidemeanings/math/Arithmetica.java	Fri Mar 27 20:30:39 2009 +0100
     1.3 @@ -0,0 +1,94 @@
     1.4 +package org.apidesign.sidemeanings.math;
     1.5 +
     1.6 +/** Rewrite of original
     1.7 + * <a href="http://source.apidesign.org/hg/apidesign/diff/4e59b6b0e907/samples/composition/src-api1.0/org/apidesign/math/Arithmetica.java">
     1.8 + * Arithmetica</a>
     1.9 + * class from the composition project
    1.10 + * to follow the <q>eliminate fuzzy modifiers</q> rule.
    1.11 + *
    1.12 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.13 + * @version 5.0
    1.14 + */
    1.15 +// BEGIN: design.sidemeanings.arith
    1.16 +public abstract class Arithmetica {
    1.17 +    // BEGIN: design.sidemeanings.arith.sumTwo
    1.18 +    public final int sumTwo(int one, int second) {
    1.19 +        return overridableSumTwo(one, second);
    1.20 +    }
    1.21 +    protected abstract int overridableSumTwo(int one, int second);
    1.22 +    protected final int defaultSumTwo(int one, int second) {
    1.23 +        return one + second;
    1.24 +    }
    1.25 +    // END: design.sidemeanings.arith.sumTwo
    1.26 +    
    1.27 +    public final int sumAll(int... numbers) {
    1.28 +        return overridableSumAll(numbers);
    1.29 +    }
    1.30 +    protected abstract int overridableSumAll(int... numbers);
    1.31 +    protected final int defaultSumAll(int... numbers) {
    1.32 +        if (numbers.length == 0) {
    1.33 +            return 0;
    1.34 +        }
    1.35 +        int sum = numbers[0];
    1.36 +        for (int i = 1; i < numbers.length; i++) {
    1.37 +            // do I want to call sumTwo or defaultSumTwo? That is a question!
    1.38 +            sum = openUpToSubclasses() ?
    1.39 +                sumTwo(sum, numbers[i]) :
    1.40 +                defaultSumTwo(sum, numbers[i]);
    1.41 +        }
    1.42 +        return sum;
    1.43 +    }
    1.44 +    
    1.45 +    public final int sumRange(int from, int to) {
    1.46 +        return overridableSumRange(from, to);
    1.47 +    }
    1.48 +    protected abstract int overridableSumRange(int from, int to);
    1.49 +    protected final int defaultSumRange(int from, int to) {
    1.50 +        int len = to - from;
    1.51 +        if (len < 0) {
    1.52 +            len = -len;
    1.53 +            from = to;
    1.54 +        }
    1.55 +        int[] array = new int[len + 1];
    1.56 +        for (int i = 0; i <= len; i++) {
    1.57 +            array[i] = from + i;
    1.58 +        }
    1.59 +        // BEGIN: design.sidemeanings.arith.the.question
    1.60 +        // Again, an API author has to ask what sumAll one wants to call?
    1.61 +        // Am I about to open up to subclasses or do I want default impl?
    1.62 +        return openUpToSubclasses() ?
    1.63 +            sumAll(array) :
    1.64 +            defaultSumAll(array);
    1.65 +        // END: design.sidemeanings.arith.the.question
    1.66 +    }
    1.67 +
    1.68 +    boolean openUpToSubclasses = true;
    1.69 +    boolean openUpToSubclasses() {
    1.70 +        return openUpToSubclasses;
    1.71 +    }
    1.72 +
    1.73 +
    1.74 +    // BEGIN: design.sidemeanings.arith.factory
    1.75 +    public static Arithmetica create() {
    1.76 +        return new Arithmetica.Impl();
    1.77 +    }
    1.78 +    private static class Impl extends Arithmetica {
    1.79 +        @Override
    1.80 +        protected int overridableSumTwo(int one, int second) {
    1.81 +            return defaultSumTwo(one, second);
    1.82 +        }
    1.83 +
    1.84 +        @Override
    1.85 +        protected int overridableSumAll(int... numbers) {
    1.86 +            return defaultSumAll(numbers);
    1.87 +        }
    1.88 +
    1.89 +        @Override
    1.90 +        protected int overridableSumRange(int from, int to) {
    1.91 +            return defaultSumRange(from, to);
    1.92 +        }
    1.93 +    }
    1.94 +    // END: design.sidemeanings.arith.factory
    1.95 +
    1.96 +}
    1.97 +// END: design.sidemeanings.arith
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/samples/sidemeanings/test/org/apidesign/sidemeanings/math/ArithmeticaTest.java	Fri Mar 27 20:30:39 2009 +0100
     2.3 @@ -0,0 +1,44 @@
     2.4 +/*
     2.5 + * To change this template, choose Tools | Templates
     2.6 + * and open the template in the editor.
     2.7 + */
     2.8 +
     2.9 +package org.apidesign.sidemeanings.math;
    2.10 +
    2.11 +import org.junit.Before;
    2.12 +import org.junit.Test;
    2.13 +import static org.junit.Assert.*;
    2.14 +
    2.15 +/**
    2.16 + *
    2.17 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.18 + */
    2.19 +public class ArithmeticaTest {
    2.20 +    Arithmetica instance;
    2.21 +
    2.22 +    public ArithmeticaTest() {
    2.23 +    }
    2.24 +
    2.25 +    @Before
    2.26 +    public void setUp() {
    2.27 +        instance = Arithmetica.create();
    2.28 +    }
    2.29 +
    2.30 +    @Test
    2.31 +    public void testSumTwo() {
    2.32 +        assertEquals("+", 5, instance.sumTwo(3, 2));
    2.33 +    }
    2.34 +
    2.35 +    @Test
    2.36 +    public void testSumAll() {
    2.37 +        assertEquals("+", 6, instance.sumAll(3, 2, 1));
    2.38 +    }
    2.39 +
    2.40 +    @Test
    2.41 +    public void testSumRange() {
    2.42 +        assertEquals("1+2+3=6", 6, instance.sumRange(1, 3));
    2.43 +        assertEquals("sum(1,10)=55", 55, instance.sumRange(1, 10));
    2.44 +        assertEquals("sum(1,1)=1", 1, instance.sumRange(1, 1));
    2.45 +        assertEquals("sum(10,1)=55", 55, instance.sumRange(10, 1));
    2.46 +    }
    2.47 +}
    2.48 \ No newline at end of file
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/samples/sidemeanings/test/org/apidesign/sidemeanings/math/Factorial.java	Fri Mar 27 20:30:39 2009 +0100
     3.3 @@ -0,0 +1,27 @@
     3.4 +package org.apidesign.sidemeanings.math;
     3.5 +
     3.6 +/** 
     3.7 + */
     3.8 +// BEGIN: design.sidemeanings.arith.factorial
     3.9 +public final class Factorial extends Arithmetica {
    3.10 +    public int factorial(int n) {
    3.11 +        return sumRange(1, n);
    3.12 +    }
    3.13 +
    3.14 +    @Override
    3.15 +    protected int overridableSumTwo(int one, int second) {
    3.16 +        return one * second;
    3.17 +    }
    3.18 +
    3.19 +    @Override
    3.20 +    protected int overridableSumAll(int... numbers) {
    3.21 +        return defaultSumAll(numbers);
    3.22 +    }
    3.23 +
    3.24 +    @Override
    3.25 +    protected int overridableSumRange(int from, int to) {
    3.26 +        return defaultSumRange(from, to);
    3.27 +    }
    3.28 +}
    3.29 +// END: design.sidemeanings.arith.factorial
    3.30 +
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/samples/sidemeanings/test/org/apidesign/sidemeanings/math/FactorialNotPossibleInNotOpenedModeTest.java	Fri Mar 27 20:30:39 2009 +0100
     4.3 @@ -0,0 +1,29 @@
     4.4 +package org.apidesign.sidemeanings.math;
     4.5 +
     4.6 +import org.junit.Before;
     4.7 +import org.junit.Test;
     4.8 +
     4.9 +/**
    4.10 + *
    4.11 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.12 + */
    4.13 +public class FactorialNotPossibleInNotOpenedModeTest extends FactorialTest {
    4.14 +    @Before @Override
    4.15 +    public void setUp() {
    4.16 +        instance = new Factorial();
    4.17 +        instance.openUpToSubclasses = false;
    4.18 +    }
    4.19 +
    4.20 +    @Test @Override
    4.21 +    public void testFactorial4() {
    4.22 +        if (Boolean.getBoolean("no.failures")) return;
    4.23 +        super.testFactorial4();
    4.24 +    }
    4.25 +
    4.26 +    @Test @Override
    4.27 +    public void testFactorial5() {
    4.28 +        if (Boolean.getBoolean("no.failures")) return;
    4.29 +        super.testFactorial5();
    4.30 +    }
    4.31 +
    4.32 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/samples/sidemeanings/test/org/apidesign/sidemeanings/math/FactorialTest.java	Fri Mar 27 20:30:39 2009 +0100
     5.3 @@ -0,0 +1,38 @@
     5.4 +/*
     5.5 + * Žluťoučký kůň je naše hříbátko.
     5.6 + * and open the template in the editor.
     5.7 + */
     5.8 +
     5.9 +package org.apidesign.sidemeanings.math;
    5.10 +
    5.11 +import org.junit.Assert;
    5.12 +import org.junit.Before;
    5.13 +import org.junit.Test;
    5.14 +
    5.15 +/**
    5.16 + *
    5.17 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    5.18 + */
    5.19 +public class FactorialTest {
    5.20 +    Factorial instance;
    5.21 +
    5.22 +    @Before
    5.23 +    public void setUp() {
    5.24 +        instance = new Factorial();
    5.25 +    }
    5.26 +
    5.27 +    @Test
    5.28 +    public void testFactorial3() {
    5.29 +        Assert.assertEquals(6, instance.factorial(3));
    5.30 +    }
    5.31 +    
    5.32 +    @Test
    5.33 +    public void testFactorial4() {
    5.34 +        Assert.assertEquals(24, instance.factorial(4));
    5.35 +    }
    5.36 +    
    5.37 +    @Test
    5.38 +    public void testFactorial5() {
    5.39 +        Assert.assertEquals(120, instance.factorial(5));
    5.40 +    }
    5.41 +}