1.1 --- a/samples/composition/src-api2.0-enum/api/Arithmetica.java Thu Feb 12 11:00:41 2009 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,66 +0,0 @@
1.4 -package api;
1.5 -
1.6 -/** Class to simplify arithmetical operations, improved version to compute
1.7 - * the sum for ranges, but only if one uses the new constructor to indicate
1.8 - * need for new version.
1.9 - *
1.10 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.11 - * @version 2.0
1.12 - */
1.13 -// BEGIN: design.composition.arith2.0.enum
1.14 -public class Arithmetica {
1.15 - private final Version version;
1.16 - public enum Version { VERSION_1_0, VERSION_2_0 }
1.17 -
1.18 - public Arithmetica() {
1.19 - this(Version.VERSION_1_0);
1.20 - }
1.21 - public Arithmetica(Version version) {
1.22 - this.version = version;
1.23 - }
1.24 -
1.25 - public int sumRange(int from, int to) {
1.26 - switch (version) {
1.27 - case VERSION_1_0:
1.28 - return sumRange1(from, to);
1.29 - case VERSION_2_0:
1.30 - return sumRange2(from, to);
1.31 - default:
1.32 - throw new IllegalStateException();
1.33 - }
1.34 - }
1.35 -// FINISH: design.composition.arith2.0.enum
1.36 -
1.37 - public int sumTwo(int one, int second) {
1.38 - return one + second;
1.39 - }
1.40 -
1.41 - public int sumAll(int... numbers) {
1.42 - if (numbers.length == 0) {
1.43 - return 0;
1.44 - }
1.45 - int sum = numbers[0];
1.46 - for (int i = 1; i < numbers.length; i++) {
1.47 - sum = sumTwo(sum, numbers[i]);
1.48 - }
1.49 - return sum;
1.50 - }
1.51 -
1.52 -
1.53 - private int sumRange1(int from, int to) {
1.54 - int len = to - from;
1.55 - if (len < 0) {
1.56 - len = -len;
1.57 - from = to;
1.58 - }
1.59 - int[] array = new int[len + 1];
1.60 - for (int i = 0; i <= len; i++) {
1.61 - array[i] = from + i;
1.62 - }
1.63 - return sumAll(array);
1.64 - }
1.65 -
1.66 - private int sumRange2(int from, int to) {
1.67 - return (from + to) * (Math.abs(to - from) + 1) / 2;
1.68 - }
1.69 -}