Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
1 package org.apidesign.math;
3 /** Class to simplify arithmetical operations, improved version to compute
4 * the sum for ranges, but only if one uses the new constructor to indicate
5 * need for new version.
7 * @author Jaroslav Tulach <jtulach@netbeans.org>
10 // BEGIN: design.composition.arith2.0.enum
11 public class Arithmetica {
12 private final Version version;
13 public enum Version { VERSION_1_0, VERSION_2_0 }
15 public Arithmetica() {
16 this(Version.VERSION_1_0);
18 public Arithmetica(Version version) {
19 this.version = version;
22 public int sumRange(int from, int to) {
25 return sumRange1(from, to);
27 return sumRange2(from, to);
29 throw new IllegalStateException();
32 // FINISH: design.composition.arith2.0.enum
34 public int sumTwo(int one, int second) {
38 public int sumAll(int... numbers) {
39 if (numbers.length == 0) {
43 for (int i = 1; i < numbers.length; i++) {
44 sum = sumTwo(sum, numbers[i]);
50 private int sumRange1(int from, int to) {
56 int[] array = new int[len + 1];
57 for (int i = 0; i <= len; i++) {
63 private int sumRange2(int from, int to) {
64 return (from + to) * (Math.abs(to - from) + 1) / 2;