11package net .laprun .sustainability .power ;
22
33import java .util .ArrayList ;
4- import java .util .Arrays ;
54import java .util .BitSet ;
65import java .util .Collections ;
76import java .util .Comparator ;
@@ -22,9 +21,6 @@ public class SensorMetadata {
2221 private final Map <String , ComponentMetadata > components ;
2322 @ JsonProperty ("documentation" )
2423 private final String documentation ;
25- // todo: remove
26- @ JsonProperty ("totalComponents" )
27- private final int [] totalComponents ;
2824
2925 /**
3026 * Initializes sensor metadata information
@@ -36,24 +32,23 @@ public class SensorMetadata {
3632 */
3733 public SensorMetadata (List <ComponentMetadata > components , String documentation ) {
3834 Objects .requireNonNull (components , "Must provide components" );
35+ if (components .isEmpty ()) {
36+ throw new IllegalArgumentException ("Must provide at least one component" );
37+ }
38+
3939 final var cardinality = components .size ();
4040 this .components = new HashMap <>(cardinality );
4141 this .documentation = documentation ;
4242 final var errors = new Errors ();
4343 final var indices = new BitSet (cardinality );
44- final var totalIndices = new BitSet (cardinality );
45- final var baseUnit = new SensorUnit [1 ];
4644 components .forEach (component -> {
4745 // check that index is valid
4846 final var index = component .index ;
49- boolean indexValid = true ;
5047 if (index < 0 || index >= cardinality ) {
5148 errors .addError (index + " is not a valid index: must be between 0 and " + (cardinality - 1 ));
52- indexValid = false ;
5349 } else if (indices .get (index )) {
5450 errors .addError ("Multiple components are using index " + index + ": "
5551 + components .stream ().filter (cm -> index == cm .index ).toList ());
56- indexValid = false ;
5752 } else {
5853 // record index as known
5954 indices .set (index );
@@ -78,35 +73,29 @@ public SensorMetadata(List<ComponentMetadata> components, String documentation)
7873 if (errors .hasErrors ()) {
7974 throw new IllegalArgumentException (errors .formatErrors ());
8075 }
81-
82- this .totalComponents = totalIndices .stream ().toArray ();
8376 }
8477
8578 @ JsonCreator
86- SensorMetadata (Map <String , ComponentMetadata > components , String documentation , int [] totalComponents ) {
79+ SensorMetadata (Map <String , ComponentMetadata > components , String documentation ) {
8780 this .components = components ;
8881 this .documentation = documentation ;
89- this .totalComponents = totalComponents ;
9082 }
9183
9284 public static SensorMetadata .Builder withNewComponent (String name , String description , boolean isAttributed ,
93- String unitSymbol ,
94- boolean participatesInTotal ) {
95- return new SensorMetadata .Builder ().withNewComponent (name , description , isAttributed , unitSymbol , participatesInTotal );
85+ String unitSymbol ) {
86+ return new SensorMetadata .Builder ().withNewComponent (name , description , isAttributed , unitSymbol );
9687 }
9788
9889 public static SensorMetadata .Builder withNewComponent (String name , String description , boolean isAttributed ,
99- SensorUnit unit ,
100- boolean participatesInTotal ) {
101- return new SensorMetadata .Builder ().withNewComponent (name , description , isAttributed , unit , participatesInTotal );
90+ SensorUnit unit ) {
91+ return new SensorMetadata .Builder ().withNewComponent (name , description , isAttributed , unit );
10292 }
10393
10494 public static SensorMetadata .Builder from (SensorMetadata sensorMetadata ) {
10595 final var builder = new Builder ();
10696 sensorMetadata .components .values ().stream ().sorted (Comparator .comparing (ComponentMetadata ::index ))
10797 .forEach (component -> builder .withNewComponent (component .name , component .description , component .isAttributed ,
108- component .unit ,
109- component .isIncludedInTotal ));
98+ component .unit ));
11099 return builder ;
111100 }
112101
@@ -117,8 +106,7 @@ public String toString() {
117106 .forEach (cm -> sb .append ("- " ).append (cm ).append ("\n " ));
118107 return "components:\n "
119108 + sb
120- + "documentation: " + documentation + "\n "
121- + "totalComponents: " + Arrays .toString (totalComponents );
109+ + "documentation: " + documentation ;
122110 }
123111
124112 /**
@@ -174,15 +162,6 @@ public String documentation() {
174162 return documentation ;
175163 }
176164
177- /**
178- * Retrieves the indices of the components that can be used to compute a total
179- *
180- * @return the indices of the components that can be used to compute a total
181- */
182- public int [] totalComponents () {
183- return totalComponents ;
184- }
185-
186165 /**
187166 * Retrieves the metadata associated with the specified component index if it exists.
188167 *
@@ -203,16 +182,14 @@ public static class Builder {
203182 private int currentIndex = 0 ;
204183 private String documentation ;
205184
206- public Builder withNewComponent (String name , String description , boolean isAttributed , String unitSymbol ,
207- boolean isIncludedInTotal ) {
185+ public Builder withNewComponent (String name , String description , boolean isAttributed , String unitSymbol ) {
208186 components
209- .add (new ComponentMetadata (name , currentIndex ++, description , isAttributed , unitSymbol , isIncludedInTotal ));
187+ .add (new ComponentMetadata (name , currentIndex ++, description , isAttributed , unitSymbol ));
210188 return this ;
211189 }
212190
213- public Builder withNewComponent (String name , String description , boolean isAttributed , SensorUnit unit ,
214- boolean isIncludedInTotal ) {
215- components .add (new ComponentMetadata (name , currentIndex ++, description , isAttributed , unit , isIncludedInTotal ));
191+ public Builder withNewComponent (String name , String description , boolean isAttributed , SensorUnit unit ) {
192+ components .add (new ComponentMetadata (name , currentIndex ++, description , isAttributed , unit ));
216193 return this ;
217194 }
218195
@@ -239,12 +216,8 @@ public SensorMetadata build() {
239216 * attributed share for each process needs to be performed. This is needed because some sensors only provide
240217 * system-wide measures instead of on a per-process basis.
241218 * @param unit a textual representation of the unit used for measures associated with this component (e.g. mW)
242- * @param isIncludedInTotal whether or not this component takes part in the computation to get a total power consumption
243- * metric for that sensor. Components that take part of the total computation must use a unit commensurable with
244- * {@link SensorUnit#W}
245219 */
246- public record ComponentMetadata (String name , int index , String description , boolean isAttributed , SensorUnit unit ,
247- boolean isIncludedInTotal ) {
220+ public record ComponentMetadata (String name , int index , String description , boolean isAttributed , SensorUnit unit ) {
248221
249222 public ComponentMetadata {
250223 if (name == null ) {
@@ -255,9 +228,8 @@ public record ComponentMetadata(String name, int index, String description, bool
255228 }
256229 }
257230
258- public ComponentMetadata (String name , int index , String description , boolean isAttributed , String unitSymbol ,
259- boolean isIncludedInTotal ) {
260- this (name , index , description , isAttributed , SensorUnit .of (unitSymbol ), isIncludedInTotal );
231+ public ComponentMetadata (String name , int index , String description , boolean isAttributed , String unitSymbol ) {
232+ this (name , index , description , isAttributed , SensorUnit .of (unitSymbol ));
261233 }
262234 }
263235}
0 commit comments