Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some issues detected by the new Sonar profile #2671

Merged
merged 10 commits into from
Aug 11, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ public int hashCode() {

@Override
public boolean equals(Object obj) {
if (obj instanceof BranchAndSide) {
return ((BranchAndSide) obj).compareTo(this) == 0;
if (obj instanceof BranchAndSide branchAndSide) {
return branchAndSide.compareTo(this) == 0;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,12 @@ public boolean isActionTaken(String actionId) {
ExpressionNode conditionExpr = ((ExpressionCondition) rule.getCondition()).getNode();
return ActionExpressionEvaluator.evaluate(conditionExpr, evalContext).equals(Boolean.TRUE);
})
.collect(Collectors.toList());
.toList();
List<String> testActionIds = activedRules.stream()
.flatMap(r -> r.getActions().stream())
.distinct()
.filter(id -> !context.isTested(id))
.collect(Collectors.toList());
.toList();

if (testActionIds.isEmpty()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ private static Network prepareEmptyNetworkForExport(CgmesModel cgmes) {
Network network = NetworkFactory.findDefault().createNetwork("empty", "CGMES");
// We ensure that the fixes are exported to CGMES files with the same version of the input files
// To achieve it, we set the CIM characteristics of the empty Network created
if (cgmes instanceof CgmesModelTripleStore) {
if (cgmes instanceof CgmesModelTripleStore cgmesModelTripleStore) {
network.newExtension(CimCharacteristicsAdder.class)
.setTopologyKind(cgmes.isNodeBreaker() ? CgmesTopologyKind.NODE_BREAKER : CgmesTopologyKind.BUS_BRANCH)
.setCimVersion(((CgmesModelTripleStore) cgmes).getCimVersion())
.setCimVersion(cgmesModelTripleStore.getCimVersion())
.add();
}
return network;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public String getCgmesIdFromAlias(Identifiable<?> identifiable, String aliasType
// Tap changers of power transformers
String id;
Identifiable<?> realIdentifiable = identifiable;
if (identifiable instanceof DanglingLine) {
DanglingLine dl = (DanglingLine) identifiable;
if (identifiable instanceof DanglingLine dl) {
id = identifiable.getAliasFromType(aliasType).or(() -> dl.getTieLine().flatMap(tl -> tl.getAliasFromType(aliasType))).orElseThrow(() -> new PowsyblException("Missing alias " + aliasType + " in " + identifiable.getId()));
if (dl.isPaired()) {
realIdentifiable = dl.getTieLine().orElseThrow(IllegalStateException::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private static void addProfilesIdentifiers(Network network, String profile, Cgme
description.setIds(network.getPropertyNames().stream()
.filter(p -> p.startsWith(Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + profile + "_ID"))
.map(network::getProperty)
.collect(Collectors.toList()));
.toList());
}

private static void checkConsistency(List<String> profiles, Network network, CgmesExportContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static com.powsybl.cgmes.conversion.CgmesReports.importedCgmesNetworkReport;
import static com.powsybl.cgmes.conversion.Conversion.Config.StateProfile.SSH;
Expand Down Expand Up @@ -404,10 +403,10 @@ private int readVersion(PropertyBags propertyBags, Context context) {
}

private void addCimCharacteristics(Network network) {
if (cgmes instanceof CgmesModelTripleStore) {
if (cgmes instanceof CgmesModelTripleStore cgmesModelTripleStore) {
network.newExtension(CimCharacteristicsAdder.class)
.setTopologyKind(cgmes.isNodeBreaker() ? CgmesTopologyKind.NODE_BREAKER : CgmesTopologyKind.BUS_BRANCH)
.setCimVersion(((CgmesModelTripleStore) cgmes).getCimVersion())
.setCimVersion(cgmesModelTripleStore.getCimVersion())
.add();
}
}
Expand Down Expand Up @@ -621,11 +620,11 @@ private void convertEquipmentAtBoundaryNode(Context context, String node) {
// In some TYNDP there are three acLineSegments at the boundary node,
// one of them disconnected. The two connected acLineSegments are imported.
List<BoundaryEquipment> connectedBeqs = beqs.stream()
.filter(beq -> !beq.isAcLineSegmentDisconnected(context)).collect(Collectors.toList());
.filter(beq -> !beq.isAcLineSegmentDisconnected(context)).toList();
if (connectedBeqs.size() == 2) {
convertTwoEquipmentsAtBoundaryNode(context, node, connectedBeqs.get(0), connectedBeqs.get(1));
// There can be multiple disconnected ACLineSegment to the same X-node (for example, for planning purposes)
beqs.stream().filter(beq -> !connectedBeqs.contains(beq)).collect(Collectors.toList())
beqs.stream().filter(beq -> !connectedBeqs.contains(beq)).toList()
.forEach(beq -> {
context.fixed("convertEquipmentAtBoundaryNode",
String.format("Multiple AcLineSegments at boundary %s. Disconnected AcLineSegment %s is imported as a dangling line.", node, beq.getAcLineSegmentId()));
Expand Down Expand Up @@ -687,7 +686,7 @@ private void clearUnattachedHvdcConverterStations(Network network, Context conte
network.getHvdcConverterStationStream()
.filter(converter -> converter.getHvdcLine() == null)
.peek(converter -> context.ignored("HVDC Converter Station " + converter.getId(), "No correct linked HVDC line found."))
.collect(Collectors.toList())
.toList()
.forEach(Connectable::remove);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import com.powsybl.cgmes.model.CgmesModel;
import com.powsybl.cgmes.model.CgmesNames;
Expand Down Expand Up @@ -149,7 +148,7 @@ void report() {
long ptc2 = tcs.stream().filter(tc -> end(tc) == 2 && isPhase(tc)).count();
long rtc3 = tcs.stream().filter(tc -> end(tc) == 3 && isRatio(tc)).count();
long ptc3 = tcs.stream().filter(tc -> end(tc) == 3 && isPhase(tc)).count();
List<Integer> steps = tcs.stream().map(this::steps).collect(Collectors.toList());
List<Integer> steps = tcs.stream().map(this::steps).toList();
boolean sameSteps = steps.isEmpty()
|| steps.stream().allMatch(steps.get(0)::equals);
d.col(tcs.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public String getCgmesId(Identifiable<?> identifiable, String subObject) {
public String getCgmesIdFromAlias(Identifiable<?> identifiable, String aliasType) {
// We assume all identifiers stored in aliases came from original CGMES models
// and we do not try to fix them
if (identifiable instanceof DanglingLine) {
DanglingLine dl = (DanglingLine) identifiable;
if (identifiable instanceof DanglingLine dl) {
return identifiable.getAliasFromType(aliasType).or(() -> dl.getTieLine().flatMap(tl -> tl.getAliasFromType(aliasType))).orElseThrow(() -> new PowsyblException("Missing alias " + aliasType + " in " + identifiable.getId()));
}
return identifiable.getAliasFromType(aliasType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@

package com.powsybl.cgmes.conversion.elements;

import com.powsybl.iidm.network.*;
import com.powsybl.cgmes.conversion.Context;
import com.powsybl.iidm.network.Branch;
import com.powsybl.iidm.network.DanglingLine;
import com.powsybl.iidm.network.Identifiable;
import com.powsybl.iidm.network.Switch;
import com.powsybl.iidm.network.Terminal;
import com.powsybl.iidm.network.TwoWindingsTransformer;
import com.powsybl.iidm.network.*;
import com.powsybl.triplestore.api.PropertyBag;

import java.util.Optional;
Expand Down Expand Up @@ -61,8 +55,8 @@ public OperationalLimitConversion(PropertyBag l, Context context) {
Identifiable<?> i = context.network().getIdentifiable(equipmentId);
if (i == null) {
vl = context.network().getVoltageLevel(p.getId("EquipmentContainer")); // happens in BusBranch when the voltage limit is linked to a busbarSection
} else if (i instanceof Injection) {
vl = ((Injection) i).getTerminal().getVoltageLevel();
} else if (i instanceof Injection<?> injection) {
vl = injection.getTerminal().getVoltageLevel();
}
}
} else {
Expand Down Expand Up @@ -162,12 +156,10 @@ private void createLimitsAdder(int terminalNumber, String limitSubClass, Identif
} else {
createLimitsAdder(terminalNumber, limitSubClass, b);
}
} else if (identifiable instanceof DanglingLine) {
DanglingLine danglingLine = (DanglingLine) identifiable;
} else if (identifiable instanceof DanglingLine danglingLine) {
loadingLimitsAdder = context.loadingLimitsMapping().computeIfAbsentLoadingLimitsAdder(danglingLine.getId() + "_" + limitSubClass,
getLoadingLimitAdderSupplier(limitSubClass, danglingLine));
} else if (identifiable instanceof ThreeWindingsTransformer) {
ThreeWindingsTransformer twt = (ThreeWindingsTransformer) identifiable;
} else if (identifiable instanceof ThreeWindingsTransformer twt) {
if (terminalNumber == -1) {
context.ignored(limitSubClass, "Defined for Equipment ThreeWindingsTransformer. Should be defined for one Terminal of Three");
notAssigned(twt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import com.powsybl.cgmes.conversion.elements.hvdc.IslandEndHvdc.HvdcEnd;
import com.powsybl.cgmes.conversion.elements.hvdc.IslandEndHvdc.HvdcEndType;
Expand Down Expand Up @@ -49,7 +48,7 @@ private static Associations createAssociations(IslandEndHvdc islandEndHvdc1, Isl
Associations associations = new Associations();
islandEndHvdc1.getHvdc().forEach(hvdcEnd -> {
List<HvdcEnd> associatedEnd2 = islandEndHvdc2.getHvdc().stream()
.filter(otherHvdcEnd -> otherHvdcEnd.isAssociatedWith(hvdcEnd)).collect(Collectors.toList());
.filter(otherHvdcEnd -> otherHvdcEnd.isAssociatedWith(hvdcEnd)).toList();

associatedEnd2.forEach(otherHvdcEnd -> associations.add(hvdcEnd, otherHvdcEnd));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
*
Expand Down Expand Up @@ -81,10 +80,10 @@ private static boolean isNodeOtherEnd(NodeEquipment nodeEquipment, Set<String> v
private static void addToRightEnd(NodeEquipment nodeEquipment, List<String> nodesEnd1, List<String> nodesEnd2, List<String> nodes) {
List<String> nodesConnectedToEnd2 = nodes.stream()
.filter(n -> nodesEnd2.stream().anyMatch(n2 -> nodeEquipment.existDcLineSegmentBetweenBothNodes(n, n2)))
.collect(Collectors.toList());
.toList();
List<String> nodesConnectedToEnd1 = nodes.stream()
.filter(n -> nodesEnd1.stream().anyMatch(n1 -> nodeEquipment.existDcLineSegmentBetweenBothNodes(n, n1)))
.collect(Collectors.toList());
.toList();

if (nodesConnectedToEnd1.isEmpty() && !nodesConnectedToEnd2.isEmpty()) {
nodesEnd1.addAll(nodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,7 @@ public boolean isExportedEquipment(Identifiable<?> c) {
boolean ignored = c.isFictitious() &&
(c instanceof Load
|| c instanceof Switch && "true".equals(c.getProperty(Conversion.PROPERTY_IS_CREATED_FOR_DISCONNECTED_TERMINAL)));
if (c instanceof Switch) {
Switch ss = (Switch) c;
if (c instanceof Switch ss) {
VoltageLevel.BusBreakerView view = ss.getVoltageLevel().getBusBreakerView();
if (ss.isRetained() && view.getBus1(ss.getId()).equals(view.getBus2(ss.getId()))) {
ignored = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,14 @@ public static int getTerminalSide(Terminal t, Connectable<?> c) {
public static int getTerminalSequenceNumber(Terminal t) {
Connectable<?> c = t.getConnectable();
if (c.getTerminals().size() == 1) {
if (c instanceof DanglingLine) {
DanglingLine dl = (DanglingLine) c;
if (dl.isPaired()) {
// TODO(Luma) Export tie line components instead of a single equipment
// If this dangling line is part of a tie line we will be exporting the tie line as a single equipment
// We need to return the proper terminal of the single tie line that will be exported
// When we change the export and write the two dangling lines as separate equipment,
// then we should always return 1 and forget about special case
return dl.getTieLine().map(tl -> tl.getDanglingLine1() == dl ? 1 : 2).orElse(1);
}
if (c instanceof DanglingLine dl && dl.isPaired()) {
// TODO(Luma) Export tie line components instead of a single equipment
// If this dangling line is part of a tie line we will be exporting the tie line as a single equipment
// We need to return the proper terminal of the single tie line that will be exported
// When we change the export and write the two dangling lines as separate equipment,
// then we should always return 1 and forget about special case
return dl.getTieLine().map(tl -> tl.getDanglingLine1() == dl ? 1 : 2).orElse(1);

}
return 1;
} else {
Expand All @@ -246,16 +244,16 @@ public static int getTerminalSequenceNumber(Terminal t) {
default:
throw new IllegalStateException("Incorrect branch side " + ((Branch<?>) c).getSide(t));
}
} else if (c instanceof ThreeWindingsTransformer) {
switch (((ThreeWindingsTransformer) c).getSide(t)) {
} else if (c instanceof ThreeWindingsTransformer twt) {
switch (twt.getSide(t)) {
case ONE:
return 1;
case TWO:
return 2;
case THREE:
return 3;
default:
throw new IllegalStateException("Incorrect three-windings transformer side " + ((ThreeWindingsTransformer) c).getSide(t));
throw new IllegalStateException("Incorrect three-windings transformer side " + twt.getSide(t));
}
} else {
throw new PowsyblException("Unexpected Connectable instance: " + c.getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -994,8 +994,7 @@ private static void writeControlArea(CgmesControlArea cgmesControlArea, String e
String controlAreaCgmesId = context.getNamingStrategy().getCgmesId(cgmesControlArea.getId());
ControlAreaEq.write(controlAreaCgmesId, cgmesControlArea.getName(), cgmesControlArea.getEnergyIdentificationCodeEIC(), energyAreaId, cimNamespace, euNamespace, writer, context);
for (Terminal terminal : cgmesControlArea.getTerminals()) {
if (terminal.getConnectable() instanceof DanglingLine) {
DanglingLine dl = (DanglingLine) terminal.getConnectable();
if (terminal.getConnectable() instanceof DanglingLine dl) {
if (!dl.isPaired()) {
TieFlowEq.write(CgmesExportUtil.getUniqueId(), controlAreaCgmesId,
context.getNamingStrategy().getCgmesIdFromAlias(dl, Conversion.CGMES_PREFIX_ALIAS_PROPERTIES + TERMINAL_BOUNDARY),
Expand Down Expand Up @@ -1073,8 +1072,8 @@ private static void writeTerminal(Terminal t, Map<Terminal, String> mapTerminal2
// We need to write the proper terminal of the single tie line that will be exported
// When we change the export and write the two dangling lines as separate equipment,
// then we should always return 1 and forget about this special case
if (t.getConnectable() instanceof DanglingLine && ((DanglingLine) t.getConnectable()).isPaired()) {
equipmentId = context.getNamingStrategy().getCgmesId(((DanglingLine) t.getConnectable()).getTieLine().orElseThrow(IllegalStateException::new));
if (t.getConnectable() instanceof DanglingLine dl && dl.isPaired()) {
equipmentId = context.getNamingStrategy().getCgmesId(dl.getTieLine().orElseThrow(IllegalStateException::new));
}
writeTerminal(t, mapTerminal2Id, CgmesExportUtil.getTerminalId(t, context), equipmentId, connectivityNodeId(mapNodeKey2NodeId, t), CgmesExportUtil.getTerminalSequenceNumber(t), cimNamespace, writer, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ private static void writeStatus(Network network, String cimNamespace, XMLStreamW
}

private static void writeConnectableStatus(Connectable<?> connectable, String cimNamespace, XMLStreamWriter writer, CgmesExportContext context) {
if (connectable instanceof DanglingLine && ((DanglingLine) connectable).isPaired()) {
if (connectable instanceof DanglingLine dl && dl.isPaired()) {
// TODO(Luma) Export tie line components instead of a single equipment
// If this dangling line is part of a tie line we will be exporting the tie line as a single equipment
// We ignore dangling lines inside tie lines for now
Expand Down
Loading