Skip to content

Commit 9f7cfa8

Browse files
committed
feat: add ability to reset the measure
1 parent d143619 commit 9f7cfa8

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
public class OngoingPowerMeasure extends ProcessorAware implements PowerMeasure {
1616
private static final int DEFAULT_SIZE = 32;
1717
private final SensorMetadata metadata;
18-
private final long startedAt;
1918
private final BitSet nonZeroComponents;
20-
private final double[][] measures;
2119
private final List<RegisteredSyntheticComponent> syntheticComponents;
20+
private final long samplePeriod;
21+
private double[][] measures;
22+
private long startedAt;
2223
private int samples;
2324
private long[] timestamps;
24-
private long samplePeriod;
2525

2626
public OngoingPowerMeasure(SensorMetadata metadata, SyntheticComponent... syntheticComponents) {
2727
this(metadata, -1, syntheticComponents);
@@ -30,11 +30,9 @@ public OngoingPowerMeasure(SensorMetadata metadata, SyntheticComponent... synthe
3030
public OngoingPowerMeasure(SensorMetadata metadata, long samplePeriod, SyntheticComponent... syntheticComponents) {
3131
super(Processors.empty);
3232

33-
startedAt = System.currentTimeMillis();
34-
final var numComponents = metadata.componentCardinality();
35-
measures = new double[numComponents][DEFAULT_SIZE];
36-
nonZeroComponents = new BitSet(numComponents);
37-
timestamps = new long[DEFAULT_SIZE];
33+
final int componentCardinality = metadata.componentCardinality();
34+
nonZeroComponents = new BitSet(componentCardinality);
35+
reset(componentCardinality);
3836
this.samplePeriod = samplePeriod;
3937

4038
if (syntheticComponents != null) {
@@ -52,6 +50,18 @@ public OngoingPowerMeasure(SensorMetadata metadata, long samplePeriod, Synthetic
5250
}
5351
}
5452

53+
public void reset() {
54+
reset(metadata.componentCardinality());
55+
}
56+
57+
private synchronized void reset(int componentCardinality) {
58+
startedAt = System.currentTimeMillis();
59+
nonZeroComponents.clear();
60+
measures = new double[componentCardinality][DEFAULT_SIZE];
61+
timestamps = new long[DEFAULT_SIZE];
62+
samples = 0;
63+
}
64+
5565
@Override
5666
public synchronized int numberOfSamples() {
5767
return samples;

0 commit comments

Comments
 (0)