Skip to content

Commit 50addac

Browse files
samirromdhanibqth29
authored andcommitted
refactor(AmplNetworkReader::read): use Consumer instead of Function (powsybl#3747)
Signed-off-by: Samir Romdhani <samir.romdhani_externe@rte-france.com> Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com>
1 parent 1e844eb commit 50addac

File tree

1 file changed

+16
-45
lines changed

1 file changed

+16
-45
lines changed

ampl-converter/src/main/java/com/powsybl/ampl/converter/AmplNetworkReader.java

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323
import java.util.Map;
2424
import java.util.Objects;
25+
import java.util.function.Consumer;
2526
import java.util.function.Function;
2627
import java.util.stream.Collectors;
2728

@@ -90,7 +91,7 @@ protected static List<String> parseExceptIfBetweenQuotes(String str) {
9091
return tokens;
9192
}
9293

93-
private void read(String suffix, int expectedTokenCount, Function<String[], Void> handler) throws IOException {
94+
private void read(String suffix, int expectedTokenCount, Consumer<String[]> handler) throws IOException {
9495
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
9596
dataSource.newInputStream(suffix, format.getFileExtension()), format.getFileEncoding()))) {
9697
String line;
@@ -109,7 +110,7 @@ private void read(String suffix, int expectedTokenCount, Function<String[], Void
109110

110111
//check if it is the right network
111112
if (variantIndex == Integer.parseInt(tokens[0])) {
112-
handler.apply(tokens);
113+
handler.accept(tokens);
113114
}
114115
}
115116
}
@@ -127,7 +128,7 @@ public AmplNetworkReader readGenerators() throws IOException {
127128
return this;
128129
}
129130

130-
private Void readGenerator(String[] tokens) {
131+
private void readGenerator(String[] tokens) {
131132
int num = Integer.parseInt(tokens[1]);
132133
int busNum = Integer.parseInt(tokens[2]);
133134
boolean vregul = Boolean.parseBoolean(tokens[3]);
@@ -141,10 +142,7 @@ private Void readGenerator(String[] tokens) {
141142
if (g == null) {
142143
throw new AmplException("Invalid generator id '" + id + "'");
143144
}
144-
145145
networkUpdater.updateNetworkGenerators(g, busNum, vregul, targetV, targetP, targetQ, p, q);
146-
147-
return null;
148146
}
149147

150148
public AmplNetworkReader readBatteries() throws IOException {
@@ -153,7 +151,7 @@ public AmplNetworkReader readBatteries() throws IOException {
153151
return this;
154152
}
155153

156-
private Void readBattery(String[] tokens) {
154+
private void readBattery(String[] tokens) {
157155
int num = Integer.parseInt(tokens[1]);
158156
int busNum = Integer.parseInt(tokens[2]);
159157
double targetP = readDouble(tokens[3]);
@@ -167,8 +165,6 @@ private Void readBattery(String[] tokens) {
167165
throw new AmplException("Invalid battery id '" + id + "'");
168166
}
169167
networkUpdater.updateNetworkBattery(b, busNum, targetP, targetQ, p, q);
170-
171-
return null;
172168
}
173169

174170
public AmplNetworkReader readLoads() throws IOException {
@@ -177,7 +173,7 @@ public AmplNetworkReader readLoads() throws IOException {
177173
return this;
178174
}
179175

180-
private Void readLoad(String[] tokens) {
176+
private void readLoad(String[] tokens) {
181177
int num = Integer.parseInt(tokens[1]);
182178
int busNum = Integer.parseInt(tokens[2]);
183179
double p = readDouble(tokens[3]);
@@ -187,8 +183,6 @@ private Void readLoad(String[] tokens) {
187183
String id = mapper.getId(AmplSubset.LOAD, num);
188184
Load l = network.getLoad(id);
189185
networkUpdater.updateNetworkLoad(l, network, id, busNum, p, q, p0, q0);
190-
191-
return null;
192186
}
193187

194188
public AmplNetworkReader readRatioTapChangers() throws IOException {
@@ -197,13 +191,11 @@ public AmplNetworkReader readRatioTapChangers() throws IOException {
197191
return this;
198192
}
199193

200-
private Void readRatioTapChanger(String[] tokens) {
194+
private void readRatioTapChanger(String[] tokens) {
201195
int num = Integer.parseInt(tokens[1]);
202196
int tap = Integer.parseInt(tokens[2]);
203197
String id = mapper.getId(AmplSubset.RATIO_TAP_CHANGER, num);
204198
networkUpdater.updateNetworkRatioTapChanger(network, id, tap);
205-
206-
return null;
207199
}
208200

209201
public AmplNetworkReader readPhaseTapChangers() throws IOException {
@@ -212,13 +204,11 @@ public AmplNetworkReader readPhaseTapChangers() throws IOException {
212204
return this;
213205
}
214206

215-
private Void readPhaseTapChanger(String[] tokens) {
207+
private void readPhaseTapChanger(String[] tokens) {
216208
int num = Integer.parseInt(tokens[1]);
217209
int tap = Integer.parseInt(tokens[2]);
218210
String id = mapper.getId(AmplSubset.PHASE_TAP_CHANGER, num);
219211
networkUpdater.updateNetworkPhaseTapChanger(network, id, tap);
220-
221-
return null;
222212
}
223213

224214
public AmplNetworkReader readShunts() throws IOException {
@@ -227,7 +217,7 @@ public AmplNetworkReader readShunts() throws IOException {
227217
return this;
228218
}
229219

230-
private Void readShunt(String[] tokens) {
220+
private void readShunt(String[] tokens) {
231221
int num = Integer.parseInt(tokens[1]);
232222
int busNum = Integer.parseInt(tokens[2]);
233223
double b = readDouble(tokens[3]);
@@ -239,10 +229,7 @@ private Void readShunt(String[] tokens) {
239229
if (sc == null) {
240230
throw new AmplException("Invalid shunt compensator id '" + id + "'");
241231
}
242-
243232
networkUpdater.updateNetworkShunt(sc, busNum, q, b, sections);
244-
245-
return null;
246233
}
247234

248235
public AmplNetworkReader readBuses() throws IOException {
@@ -251,7 +238,7 @@ public AmplNetworkReader readBuses() throws IOException {
251238
return this;
252239
}
253240

254-
private Void readBus(String[] tokens) {
241+
private void readBus(String[] tokens) {
255242
int num = Integer.parseInt(tokens[1]);
256243
double v = readDouble(tokens[2]);
257244
double theta = readDouble(tokens[3]);
@@ -261,10 +248,7 @@ private Void readBus(String[] tokens) {
261248
if (bus == null) {
262249
throw new AmplException("Invalid bus id '" + id + "'");
263250
}
264-
265251
networkUpdater.updateNetworkBus(bus, v, theta);
266-
267-
return null;
268252
}
269253

270254
public AmplNetworkReader readBranches() throws IOException {
@@ -273,7 +257,7 @@ public AmplNetworkReader readBranches() throws IOException {
273257
return this;
274258
}
275259

276-
private Void readBranch(String[] tokens) {
260+
private void readBranch(String[] tokens) {
277261
int num = Integer.parseInt(tokens[1]);
278262
int busNum = Integer.parseInt(tokens[2]);
279263
int busNum2 = Integer.parseInt(tokens[3]);
@@ -283,11 +267,8 @@ private Void readBranch(String[] tokens) {
283267
double q2 = readDouble(tokens[7]);
284268

285269
String id = mapper.getId(AmplSubset.BRANCH, num);
286-
287-
Branch br = network.getBranch(id);
270+
Branch<?> br = network.getBranch(id);
288271
networkUpdater.updateNetworkBranch(br, network, id, busNum, busNum2, p1, p2, q1, q2);
289-
290-
return null;
291272
}
292273

293274
public AmplNetworkReader readHvdcLines() throws IOException {
@@ -296,7 +277,7 @@ public AmplNetworkReader readHvdcLines() throws IOException {
296277
return this;
297278
}
298279

299-
private Void readHvdcLine(String[] tokens) {
280+
private void readHvdcLine(String[] tokens) {
300281
int num = Integer.parseInt(tokens[1]);
301282
String converterMode = tokens[2].replace("\"", "");
302283
double targetP = readDouble(tokens[3]);
@@ -307,8 +288,6 @@ private Void readHvdcLine(String[] tokens) {
307288
throw new AmplException("Invalid HvdcLine id '" + id + "'");
308289
}
309290
networkUpdater.updateNetworkHvdcLine(hl, converterMode, targetP);
310-
311-
return null;
312291
}
313292

314293
public AmplNetworkReader readStaticVarcompensator() throws IOException {
@@ -317,7 +296,7 @@ public AmplNetworkReader readStaticVarcompensator() throws IOException {
317296
return this;
318297
}
319298

320-
private Void readSvc(String[] tokens) {
299+
private void readSvc(String[] tokens) {
321300
int num = Integer.parseInt(tokens[1]);
322301
int busNum = Integer.parseInt(tokens[2]);
323302
boolean vregul = Boolean.parseBoolean(tokens[3]);
@@ -329,10 +308,7 @@ private Void readSvc(String[] tokens) {
329308
if (svc == null) {
330309
throw new AmplException("Invalid StaticVarCompensator id '" + id + "'");
331310
}
332-
333311
networkUpdater.updateNetworkSvc(svc, busNum, vregul, targetV, q);
334-
335-
return null;
336312
}
337313

338314
public AmplNetworkReader readLccConverterStations() throws IOException {
@@ -341,7 +317,7 @@ public AmplNetworkReader readLccConverterStations() throws IOException {
341317
return this;
342318
}
343319

344-
private Void readLcc(String[] tokens) {
320+
private void readLcc(String[] tokens) {
345321
int num = Integer.parseInt(tokens[1]);
346322
int busNum = Integer.parseInt(tokens[2]);
347323
double p = readDouble(tokens[3]);
@@ -352,10 +328,7 @@ private Void readLcc(String[] tokens) {
352328
if (lcc == null) {
353329
throw new AmplException("Invalid bus id '" + id + "'");
354330
}
355-
356331
networkUpdater.updateNetworkLcc(lcc, busNum, p, q);
357-
358-
return null;
359332
}
360333

361334
public AmplNetworkReader readVscConverterStations() throws IOException {
@@ -364,7 +337,7 @@ public AmplNetworkReader readVscConverterStations() throws IOException {
364337
return this;
365338
}
366339

367-
private Void readVsc(String[] tokens) {
340+
private void readVsc(String[] tokens) {
368341
int num = Integer.parseInt(tokens[1]);
369342
int busNum = Integer.parseInt(tokens[2]);
370343
boolean vregul = Boolean.parseBoolean(tokens[3]);
@@ -376,8 +349,6 @@ private Void readVsc(String[] tokens) {
376349
String id = mapper.getId(AmplSubset.VSC_CONVERTER_STATION, num);
377350
VscConverterStation vsc = network.getVscConverterStation(id);
378351
networkUpdater.updateNetworkVsc(vsc, busNum, vregul, targetV, targetQ, p, q);
379-
380-
return null;
381352
}
382353

383354
public AmplNetworkReader readMetrics(Map<String, String> metrics) throws IOException {

0 commit comments

Comments
 (0)