Skip to content

Commit b2b354e

Browse files
committed
fix: properly ensure arrays size, avoid writing timestamp several times
1 parent 14ec77c commit b2b354e

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

measure/src/main/java/net/laprun/sustainability/power/measure/OngoingPowerMeasure.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,18 @@ public SensorMetadata metadata() {
5858

5959
public void recordMeasure(double[] components) {
6060
samples++;
61+
ensureArraysSize();
62+
6163
final var timestamp = System.currentTimeMillis();
64+
timestamps[samples - 1] = timestamp;
65+
6266
for (int component = 0; component < components.length; component++) {
6367
final var componentValue = components[component];
6468
// record that the value is not zero
6569
if (componentValue != 0) {
6670
nonZeroComponents.set(component);
6771
}
68-
recordComponentValue(component, componentValue, timestamp);
72+
measures[component][samples - 1] = componentValue;
6973
}
7074

7175
final var processors = processors();
@@ -78,8 +82,8 @@ public void recordMeasure(double[] components) {
7882
}
7983
}
8084

81-
private void recordComponentValue(int component, double value, long timestamp) {
82-
final var currentSize = measures[component].length;
85+
private void ensureArraysSize() {
86+
final int currentSize = timestamps.length;
8387
if (currentSize <= samples) {
8488
final var newSize = currentSize * 2;
8589
for (int index = 0; index < measures.length; index++) {
@@ -91,8 +95,6 @@ private void recordComponentValue(int component, double value, long timestamp) {
9195
System.arraycopy(timestamps, 0, newTimestamps, 0, currentSize);
9296
timestamps = newTimestamps;
9397
}
94-
timestamps[samples - 1] = timestamp;
95-
measures[component][samples - 1] = value;
9698
}
9799

98100
public Duration duration() {

0 commit comments

Comments
 (0)