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