samples/sidemeanings/src/org/apidesign/sidemeanings/math/Arithmetica.java
changeset 325 4553c2885ce6
child 376 bb34a70d36ba
     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