Skip to content

Commit 110ed58

Browse files
committed
feat: validate measure contains enough values
1 parent 7b73935 commit 110ed58

File tree

1 file changed

+7
-0
lines changed
  • measure/src/main/java/net/laprun/sustainability/power/analysis/total

1 file changed

+7
-0
lines changed

measure/src/main/java/net/laprun/sustainability/power/analysis/total/Totaler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Totaler {
1313
private final SensorUnit expectedResultUnit;
1414
private final Function<double[], Double> formula;
1515
private final String name;
16+
private final int[] totalComponentIndices;
1617
private Errors errors;
1718

1819
Totaler(SensorMetadata metadata, SensorUnit expectedResultUnit, int... totalComponentIndices) {
@@ -27,6 +28,7 @@ class Totaler {
2728
.map(TotalComponent::name)
2829
.collect(Collectors.joining(" + ", "total (", ")"));
2930
formula = formulaFrom(totalComponents);
31+
this.totalComponentIndices = totalComponentIndices;
3032
}
3133

3234
void validate() {
@@ -52,6 +54,11 @@ public SensorUnit expectedResultUnit() {
5254
}
5355

5456
public double computeTotalFrom(double[] measure) {
57+
if (measure.length < totalComponentIndices.length) {
58+
throw new IllegalArgumentException("Provided measure " + Arrays.toString(measure) +
59+
" doesn't countain components for required total indices: " + Arrays.toString(totalComponentIndices));
60+
}
61+
5562
checkValidated();
5663
return convertToExpectedUnit(formula.apply(measure));
5764
}

0 commit comments

Comments
 (0)