WIP - [STATISTICS-14] - BigDecimalStatistics - Continued.
- Fixed PMD issues. Only one left:
PMD Failure: ....BigDecimalSummaryStatistics:25 Rule:DataClass Priority:3
The class 'BigDecimalSummaryStatistics' is suspected to be a Data Class (WOC=25.000%, NOPA=0, NOAM=4, WMC=27).
diff --git a/commons-statistics-bigdecimal/src/main/java/org/apache/commons/statistics/bigdecimal/descriptive/BigDecimalSummaryStatistics.java b/commons-statistics-bigdecimal/src/main/java/org/apache/commons/statistics/bigdecimal/descriptive/BigDecimalSummaryStatistics.java
index ad5f05a..8eae837 100644
--- a/commons-statistics-bigdecimal/src/main/java/org/apache/commons/statistics/bigdecimal/descriptive/BigDecimalSummaryStatistics.java
+++ b/commons-statistics-bigdecimal/src/main/java/org/apache/commons/statistics/bigdecimal/descriptive/BigDecimalSummaryStatistics.java
@@ -25,6 +25,10 @@
public class BigDecimalSummaryStatistics implements Consumer<BigDecimal> {
/**
+ * This is used to assign min/max a useful value which is not {@code null}.
+ */
+ private static final BigDecimal UNKNOWN = BigDecimal.ZERO;
+ /**
* The count value for zero.
*/
private static final long ZERO_COUNT = 0L;
@@ -46,14 +50,21 @@
private BigDecimal max;
/**
+ * This keeps the information if min/max have been assigned a correct value or not.
+ */
+ private boolean minMaxAssigned;
+
+ /**
* Create an instance of BigDecimalSummaryStatistics. {@code count = 0} and sum = {@link
* BigDecimal#ZERO}
*/
public BigDecimalSummaryStatistics() {
this.count = ZERO_COUNT;
this.sum = BigDecimal.ZERO;
- this.max = null;
- this.min = null;
+
+ this.minMaxAssigned = false;
+ this.max = UNKNOWN;
+ this.min = UNKNOWN;
}
/**
@@ -102,6 +113,7 @@
this.min = min;
this.max = max;
+ this.minMaxAssigned = true;
}
}
@@ -121,12 +133,13 @@
count++;
sum = sum.add(value);
- if (min == null) {
- min = value;
- max = value;
- } else {
+ if (minMaxAssigned) {
min = min.min(value);
max = max.max(value);
+ } else {
+ min = value;
+ max = value;
+ minMaxAssigned = true;
}
}
@@ -144,12 +157,13 @@
count += other.count;
sum = sum.add(other.sum);
- if (min == null) {
- min = other.min;
- max = other.max;
- } else {
+ if (minMaxAssigned) {
min = min.min(other.min);
max = max.max(other.max);
+ } else {
+ min = other.min;
+ max = other.max;
+ minMaxAssigned = true;
}
}
@@ -209,7 +223,7 @@
* @return The arithmetic mean of values, or zero if none
*/
public final BigDecimal getAverage() {
- if (this.count > 0) {
+ if (this.count > ZERO_COUNT) {
return this.sum.divide(BigDecimal.valueOf(this.count));
} else {
return BigDecimal.ZERO;