samples/sidemeanings/src/org/apidesign/sidemeanings/math/Arithmetica.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 15 Oct 2011 22:11:18 +0200
changeset 376 bb34a70d36ba
parent 325 4553c2885ce6
permissions -rw-r--r--
Shortening lines to less than 75 characters
     1 package org.apidesign.sidemeanings.math;
     2 
     3 /** Rewrite of original
     4  * <a href="http://source.apidesign.org/hg/apidesign/diff/4e59b6b0e907/samples/composition/src-api1.0/org/apidesign/math/Arithmetica.java">
     5  * Arithmetica</a>
     6  * class from the composition project
     7  * to follow the <q>eliminate fuzzy modifiers</q> rule.
     8  *
     9  * @author Jaroslav Tulach <jtulach@netbeans.org>
    10  * @version 5.0
    11  */
    12 // BEGIN: design.sidemeanings.arith
    13 public abstract class Arithmetica {
    14     // BEGIN: design.sidemeanings.arith.sumTwo
    15     public final int sumTwo(int one, int second) {
    16         return overridableSumTwo(one, second);
    17     }
    18     protected abstract int overridableSumTwo(int one, int second);
    19     protected final int defaultSumTwo(int one, int second) {
    20         return one + second;
    21     }
    22     // END: design.sidemeanings.arith.sumTwo
    23     
    24     public final int sumAll(int... numbers) {
    25         return overridableSumAll(numbers);
    26     }
    27     protected abstract int overridableSumAll(int... numbers);
    28     protected final int defaultSumAll(int... numbers) {
    29         if (numbers.length == 0) {
    30             return 0;
    31         }
    32         int sum = numbers[0];
    33         for (int i = 1; i < numbers.length; i++) {
    34             // do I want to call sumTwo or defaultSumTwo? 
    35             // That is a question!
    36             sum = openUpToSubclasses() ?
    37                 sumTwo(sum, numbers[i]) :
    38                 defaultSumTwo(sum, numbers[i]);
    39         }
    40         return sum;
    41     }
    42     
    43     public final int sumRange(int from, int to) {
    44         return overridableSumRange(from, to);
    45     }
    46     protected abstract int overridableSumRange(int from, int to);
    47     protected final int defaultSumRange(int from, int to) {
    48         int len = to - from;
    49         if (len < 0) {
    50             len = -len;
    51             from = to;
    52         }
    53         int[] array = new int[len + 1];
    54         for (int i = 0; i <= len; i++) {
    55             array[i] = from + i;
    56         }
    57         // BEGIN: design.sidemeanings.arith.the.question
    58         // Again, an API author has to ask what sumAll one wants to call?
    59         // Am I about to open up to subclasses or do I want default impl?
    60         return openUpToSubclasses() ?
    61             sumAll(array) :
    62             defaultSumAll(array);
    63         // END: design.sidemeanings.arith.the.question
    64     }
    65 
    66     boolean openUpToSubclasses = true;
    67     boolean openUpToSubclasses() {
    68         return openUpToSubclasses;
    69     }
    70 
    71 
    72     // BEGIN: design.sidemeanings.arith.factory
    73     public static Arithmetica create() {
    74         return new Arithmetica.Impl();
    75     }
    76     private static class Impl extends Arithmetica {
    77         @Override
    78         protected int overridableSumTwo(int one, int second) {
    79             return defaultSumTwo(one, second);
    80         }
    81 
    82         @Override
    83         protected int overridableSumAll(int... numbers) {
    84             return defaultSumAll(numbers);
    85         }
    86 
    87         @Override
    88         protected int overridableSumRange(int from, int to) {
    89             return defaultSumRange(from, to);
    90         }
    91     }
    92     // END: design.sidemeanings.arith.factory
    93 
    94 }
    95 // END: design.sidemeanings.arith