@@ -385,12 +385,11 @@ private static Map<String, Set<String>> getNeighbours(List<Substation> substatio
385
385
for (Substation s : substations ) {
386
386
for (VoltageLevel vl : s .getVoltageLevels ()) {
387
387
for (Line line : vl .getConnectables (Line .class )) {
388
- Substation s1 = line .getTerminal1 ().getVoltageLevel ().getSubstation ().orElseThrow (); // TODO
389
- Substation s2 = line .getTerminal2 ().getVoltageLevel ().getSubstation ().orElseThrow (); // TODO
390
- if (s1 != s ) {
391
- neighbours .get (s .getId ()).add (s1 .getId ());
392
- } else if (s2 != s ) {
393
- neighbours .get (s .getId ()).add (s2 .getId ());
388
+ Substation s1 = line .getTerminal1 ().getVoltageLevel ().getSubstation ().orElse (null );
389
+ Substation s2 = line .getTerminal2 ().getVoltageLevel ().getSubstation ().orElse (null );
390
+ Substation otherSide = s1 == s ? s2 : s1 ;
391
+ if (otherSide != null ) {
392
+ neighbours .get (s .getId ()).add (otherSide .getId ());
394
393
}
395
394
}
396
395
}
@@ -445,7 +444,8 @@ boolean emptyOrEquals(String emptyable, String s) {
445
444
* <p>
446
445
* returns null when the substations at the end of the line are missing.
447
446
*/
448
- private LineGeoData getLineGeoDataWithEndSubstations (Map <String , LineGeoData > linesGeoDataDb , Map <String , SubstationGeoData > substationGeoDataDb , String lineId , Substation substation1 , Substation substation2 ) {
447
+ private LineGeoData getLineGeoDataWithEndSubstations (Map <String , LineGeoData > linesGeoDataDb ,
448
+ Map <String , SubstationGeoData > substationGeoDataDb , String lineId , Substation substation1 , Substation substation2 ) {
449
449
LineGeoData geoData = linesGeoDataDb .get (lineId );
450
450
SubstationGeoData substation1GeoData = substationGeoDataDb .get (substation1 .getId ());
451
451
SubstationGeoData substation2GeoData = substationGeoDataDb .get (substation2 .getId ());
@@ -515,17 +515,25 @@ List<LineGeoData> getLinesByCountries(Network network, Set<Country> countries) {
515
515
516
516
// we also want the destination substation (so we add the neighbouring country)
517
517
Set <Country > countryAndNextTo = mapSubstationsByLine .entrySet ().stream ().flatMap (entry ->
518
- Stream .of (entry .getValue ().getLeft (), entry .getValue ().getRight ()).map (Substation ::getNullableCountry ).filter (Objects ::nonNull )).collect (Collectors .toSet ());
518
+ Stream .of (entry .getValue ().getLeft (), entry .getValue ().getRight ())
519
+ .filter (Objects ::nonNull )
520
+ .map (Substation ::getNullableCountry )
521
+ .filter (Objects ::nonNull )).collect (Collectors .toSet ());
519
522
520
523
Map <String , SubstationGeoData > substationGeoDataDb = getSubstationMapByCountries (network , countryAndNextTo );
521
524
List <LineGeoData > geoData = new ArrayList <>();
522
525
523
- mapSubstationsByLine .forEach ((key , value ) -> {
524
- LineGeoData geo = getLineGeoDataWithEndSubstations (linesGeoDataDb , substationGeoDataDb , key , value .getLeft (), value .getRight ());
525
- if (geo != null ) {
526
- geoData .add (geo );
527
- }
528
- });
526
+ mapSubstationsByLine .entrySet ().stream ()
527
+ .filter (entry -> entry .getValue () != null )
528
+ .filter (entry -> entry .getValue ().getLeft () != null )
529
+ .filter (entry -> entry .getValue ().getRight () != null )
530
+ .forEach (entry -> {
531
+ LineGeoData geo = getLineGeoDataWithEndSubstations (linesGeoDataDb , substationGeoDataDb , entry .getKey (),
532
+ entry .getValue ().getLeft (), entry .getValue ().getRight ());
533
+ if (geo != null ) {
534
+ geoData .add (geo );
535
+ }
536
+ });
529
537
530
538
LOGGER .info ("{} lines read from DB in {} ms" , linesGeoDataDb .size (), stopWatch .getTime (TimeUnit .MILLISECONDS ));
531
539
@@ -534,8 +542,8 @@ List<LineGeoData> getLinesByCountries(Network network, Set<Country> countries) {
534
542
535
543
private Pair <Substation , Substation > getSubstations (Identifiable <?> identifiable ) {
536
544
return switch (identifiable .getType ()) {
537
- case LINE -> Pair .of (((Line ) identifiable ).getTerminal1 ().getVoltageLevel ().getSubstation ().orElseThrow ( ),
538
- ((Line ) identifiable ).getTerminal2 ().getVoltageLevel ().getSubstation ().orElseThrow ( ));
545
+ case LINE -> Pair .of (((Line ) identifiable ).getTerminal1 ().getVoltageLevel ().getSubstation ().orElse ( null ),
546
+ ((Line ) identifiable ).getTerminal2 ().getVoltageLevel ().getSubstation ().orElse ( null ));
539
547
case TIE_LINE ->
540
548
Pair .of (((TieLine ) identifiable ).getDanglingLine1 ().getTerminal ().getVoltageLevel ().getSubstation ().orElseThrow (),
541
549
((TieLine ) identifiable ).getDanglingLine2 ().getTerminal ().getVoltageLevel ().getSubstation ().orElseThrow ());
0 commit comments