Skip to content

Commit 86855fc

Browse files
committed
refactor: extract PartialCursor record
1 parent a6a09fe commit 86855fc

File tree

2 files changed

+41
-40
lines changed

2 files changed

+41
-40
lines changed

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

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -60,44 +60,4 @@ public static PartialCursor cursorOver(long[] timestamps, long timestamp, Durati
6060

6161
return new PartialCursor(startIndex, endIndex, startRatio, endRatio);
6262
}
63-
64-
public record PartialCursor(int startIndex, int endIndex, double firstMeasureRatio, double lastMeasureRatio) {
65-
66-
public static final PartialCursor empty = new PartialCursor(-1, -1, 0.0, 0.0);
67-
68-
public double sum(double[] values) {
69-
if (values == null || values.length == 0 || this == empty || values.length < startIndex + endIndex) {
70-
return 0.0;
71-
}
72-
73-
if (startIndex == endIndex) {
74-
return values[startIndex] * firstMeasureRatio;
75-
}
76-
77-
double sum = values[startIndex] * firstMeasureRatio;
78-
for (int i = startIndex + 1; i < endIndex; i++) {
79-
sum += values[i];
80-
}
81-
sum += values[endIndex] * lastMeasureRatio;
82-
83-
return sum;
84-
}
85-
86-
public double[] viewOf(double[] values) {
87-
if (values == null || values.length == 0 || this == empty || values.length < startIndex + endIndex) {
88-
return new double[0];
89-
}
90-
91-
if (startIndex == endIndex) {
92-
return new double[] { values[startIndex] * firstMeasureRatio };
93-
}
94-
95-
final int len = endIndex - startIndex + 1;
96-
final double[] view = new double[len];
97-
view[0] = values[startIndex] * firstMeasureRatio;
98-
System.arraycopy(values, startIndex + 1, view, 1, len - 1 - 1);
99-
view[len - 1] = values[endIndex] * lastMeasureRatio;
100-
return view;
101-
}
102-
}
10363
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package net.laprun.sustainability.power.measure;
2+
3+
public record PartialCursor(int startIndex, int endIndex, double firstMeasureRatio, double lastMeasureRatio) {
4+
5+
public static final PartialCursor empty = new PartialCursor(-1, -1, 0.0, 0.0);
6+
7+
public double sum(double[] values) {
8+
if (values == null || values.length == 0 || this == empty || values.length <= endIndex) {
9+
return 0.0;
10+
}
11+
12+
if (startIndex == endIndex) {
13+
return values[startIndex] * firstMeasureRatio;
14+
}
15+
16+
double sum = values[startIndex] * firstMeasureRatio;
17+
for (int i = startIndex + 1; i < endIndex; i++) {
18+
sum += values[i];
19+
}
20+
sum += values[endIndex] * lastMeasureRatio;
21+
22+
return sum;
23+
}
24+
25+
public double[] viewOf(double[] values) {
26+
if (values == null || values.length == 0 || this == empty || values.length < startIndex + endIndex) {
27+
return new double[0];
28+
}
29+
30+
if (startIndex == endIndex) {
31+
return new double[] { values[startIndex] * firstMeasureRatio };
32+
}
33+
34+
final int len = endIndex - startIndex + 1;
35+
final double[] view = new double[len];
36+
view[0] = values[startIndex] * firstMeasureRatio;
37+
System.arraycopy(values, startIndex + 1, view, 1, len - 1 - 1);
38+
view[len - 1] = values[endIndex] * lastMeasureRatio;
39+
return view;
40+
}
41+
}

0 commit comments

Comments
 (0)