Skip to content

Commit

Permalink
Refactor with Java 17 new features (#280)
Browse files Browse the repository at this point in the history
* Use Java 14 switch
* Use Java 14 pattern matching instanceof
* Use Java 14 records
* Replace collect(Collectors.toList()) with Java 16 toList()

Signed-off-by: lisrte <laurent.issertial@rte-france.com>
  • Loading branch information
Lisrte authored Sep 26, 2023
1 parent a0e215b commit a2cfde7
Show file tree
Hide file tree
Showing 27 changed files with 100 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private static void mergeLoadsInVoltageLevel(VoltageLevel vl) {
List<LoadsToMerge> loadsToMergeList = vl.getBusBreakerView().getBusStream()
.filter(bus -> bus.getLoadStream().count() > 1)
.flatMap(LoadsMerger::getLoadsToMergeStream)
.collect(Collectors.toList());
.toList();

loadsToMergeList.forEach(LoadsToMerge::merge);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,8 @@

/**
* @author Florian Dupuy <florian.dupuy at rte-france.com>
* @author Laurent Issertial <laurent.issertial at rte-france.com>
*/
public class TimelineEntry {
public record TimelineEntry(double time, String modelName, String message) {

private final double time;
private final String modelName;
private final String message;

public TimelineEntry(double time, String modelName, String message) {
this.time = time;
this.modelName = modelName;
this.message = message;
}

public double getTime() {
return time;
}

public String getModelName() {
return modelName;
}

public String getMessage() {
return message;
}
}
29 changes: 1 addition & 28 deletions commons/src/test/java/com/powsybl/dynawo/commons/LoadState.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,6 @@
/**
* @author Laurent Isertial <laurent.issertial at rte-france.com>
*/
public final class LoadState {
public record LoadState(double p, double q, double p0, double q0) {

private final double p;
private final double q;
private final double p0;
private final double q0;

public LoadState(double p, double q, double p0, double q0) {
this.p = p;
this.q = q;
this.p0 = p0;
this.q0 = q0;
}

public double getP0() {
return p0;
}

public double getQ0() {
return q0;
}

public double getP() {
return p;
}

public double getQ() {
return q;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ static Network createMultiLoadsBusesNetwork(List<LoadState> loadStates) {
for (int i = 0; i < loadStates.size(); i++) {
LoadState loadState = loadStates.get(i);
vl1.getNodeBreakerView().newDisconnector().setNode1(0).setNode2(i + 1).setId("d" + (i + 1)).add();
vl1.newLoad().setId("load" + (i + 1)).setNode(i + 1).setP0(loadState.getP0()).setQ0(loadState.getQ0()).add()
.getTerminal().setP(loadState.getP()).setQ(loadState.getQ());
vl1.newLoad().setId("load" + (i + 1)).setNode(i + 1).setP0(loadState.p0()).setQ0(loadState.q0()).add()
.getTerminal().setP(loadState.p()).setQ(loadState.q());
}
vl1.getNodeBreakerView().newDisconnector().setNode1(0).setNode2(loadStates.size() + 1).setId("d" + (loadStates.size() + 1)).add();

Expand All @@ -93,8 +93,8 @@ static Network createMultiLoadsBusesNetwork(List<LoadState> loadStates) {

for (int i = 0; i < loadStates.size(); i++) {
LoadState loadState = loadStates.get(i);
vl2.newLoad().setId("load" + (loadStates.size() + i + 1)).setBus(b2.getId()).setP0(loadState.getP0()).setQ0(loadState.getQ0()).add()
.getTerminal().setP(loadState.getP()).setQ(loadState.getQ());
vl2.newLoad().setId("load" + (loadStates.size() + i + 1)).setBus(b2.getId()).setP0(loadState.p0()).setQ0(loadState.q0()).add()
.getTerminal().setP(loadState.p()).setQ(loadState.q());
}

// Line between the two voltage levels
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ void testTimeline(String fileName) throws URISyntaxException {
List<TimelineEntry> timeline = new CsvTimeLineParser().parse(path);

assertEquals(5, timeline.size());
assertEquals("PMIN : activation", timeline.get(0).getMessage());
assertEquals("GEN____8_SM", timeline.get(0).getModelName());
assertEquals(0., timeline.get(0).getTime(), 1e-9);
assertEquals("PMIN : activation", timeline.get(1).getMessage());
assertEquals("GEN____3_SM", timeline.get(1).getModelName());
assertEquals(0.0306911, timeline.get(1).getTime(), 1e-9);
assertEquals("PMIN : deactivation", timeline.get(2).getMessage());
assertEquals("GEN____8_SM", timeline.get(2).getModelName());
assertEquals("PMIN : deactivation", timeline.get(3).getMessage());
assertEquals("GEN____3_SM", timeline.get(3).getModelName());
assertEquals("PMIN : activation", timeline.get(4).getMessage());
assertEquals("GEN____8_SM", timeline.get(4).getModelName());
assertEquals("PMIN : activation", timeline.get(0).message());
assertEquals("GEN____8_SM", timeline.get(0).modelName());
assertEquals(0., timeline.get(0).time(), 1e-9);
assertEquals("PMIN : activation", timeline.get(1).message());
assertEquals("GEN____3_SM", timeline.get(1).modelName());
assertEquals(0.0306911, timeline.get(1).time(), 1e-9);
assertEquals("PMIN : deactivation", timeline.get(2).message());
assertEquals("GEN____8_SM", timeline.get(2).modelName());
assertEquals("PMIN : deactivation", timeline.get(3).message());
assertEquals("GEN____3_SM", timeline.get(3).modelName());
assertEquals("PMIN : activation", timeline.get(4).message());
assertEquals("GEN____8_SM", timeline.get(4).modelName());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ void test() throws XMLStreamException {
List<TimelineEntry> timeline = XmlTimeLineParser.parse(xml);

assertEquals(5, timeline.size());
assertEquals("PMIN : activation", timeline.get(0).getMessage());
assertEquals("GEN____8_SM", timeline.get(0).getModelName());
assertEquals(0., timeline.get(0).getTime(), 1e-9);
assertEquals("PMIN : activation", timeline.get(1).getMessage());
assertEquals("GEN____3_SM", timeline.get(1).getModelName());
assertEquals(0.030691068513160655, timeline.get(1).getTime(), 1e-9);
assertEquals("PMIN : deactivation", timeline.get(2).getMessage());
assertEquals("GEN____8_SM", timeline.get(2).getModelName());
assertEquals("PMIN : deactivation", timeline.get(3).getMessage());
assertEquals("GEN____3_SM", timeline.get(3).getModelName());
assertEquals("PMIN : activation", timeline.get(4).getMessage());
assertEquals("GEN____8_SM", timeline.get(4).getModelName());
assertEquals("PMIN : activation", timeline.get(0).message());
assertEquals("GEN____8_SM", timeline.get(0).modelName());
assertEquals(0., timeline.get(0).time(), 1e-9);
assertEquals("PMIN : activation", timeline.get(1).message());
assertEquals("GEN____3_SM", timeline.get(1).modelName());
assertEquals(0.030691068513160655, timeline.get(1).time(), 1e-9);
assertEquals("PMIN : deactivation", timeline.get(2).message());
assertEquals("GEN____8_SM", timeline.get(2).modelName());
assertEquals("PMIN : deactivation", timeline.get(3).message());
assertEquals("GEN____3_SM", timeline.get(3).modelName());
assertEquals("PMIN : activation", timeline.get(4).message());
assertEquals("GEN____8_SM", timeline.get(4).modelName());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static com.powsybl.dynaflow.DynaFlowConstants.CONFIG_FILENAME;
import static com.powsybl.dynaflow.DynaFlowConstants.IIDM_FILENAME;
Expand Down Expand Up @@ -170,7 +169,7 @@ public SecurityAnalysisReport after(Path workingDir, ExecutionReport report) thr
Path constraintsDir = workingDir.resolve(DYNAWO_CONSTRAINTS_FOLDER);
List<PostContingencyResult> contingenciesResults = contingencies.stream()
.map(c -> getPostContingencyResult(network, violationFilter, constraintsDir, c))
.collect(Collectors.toList());
.toList();

// Report the timeline events from the timeline files written by dynawo
Path timelineDir = workingDir.resolve(DYNAWO_TIMELINE_FOLDER);
Expand Down
6 changes: 3 additions & 3 deletions dynaflow/src/main/java/com/powsybl/dynaflow/Reports.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public static void reportTimelineEvent(Reporter reporter, TimelineEntry timeline
reporter.report(Report.builder()
.withKey("DynawoTimelineEvent")
.withDefaultMessage("[t=${time}] ${message} on equipment '${identifiableId}'")
.withTypedValue("time", timelineEntry.getTime(), TIME_MS)
.withTypedValue("identifiableId", timelineEntry.getModelName(), ID)
.withValue("message", timelineEntry.getMessage())
.withTypedValue("time", timelineEntry.time(), TIME_MS)
.withTypedValue("identifiableId", timelineEntry.modelName(), ID)
.withValue("message", timelineEntry.message())
.withSeverity(TypedValue.TRACE_SEVERITY)
.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ private static Optional<Identifiable<?>> getLimitViolationIdentifiable(Network n
if (identifiable == null) {
LOGGER.warn("Unknown equipment/bus {} for limit violation in result constraints file", name);
}
if (identifiable instanceof Bus) {
identifiable = ((Bus) identifiable).getVoltageLevel(); // Limit violation on buses are identified by their voltage level id
if (identifiable instanceof Bus bus) {
identifiable = bus.getVoltageLevel(); // Limit violation on buses are identified by their voltage level id
}
return Optional.ofNullable(identifiable);
}
Expand All @@ -155,18 +155,12 @@ private static Branch.Side toBranchSide(Integer side) {
}

private static LimitViolationType toLimitViolationType(String kind) {
switch (kind) {
case "UInfUmin":
return LimitViolationType.LOW_VOLTAGE;
case "USupUmax":
return LimitViolationType.HIGH_VOLTAGE;
case "OverloadOpen":
case "OverloadUp":
case "PATL":
return LimitViolationType.CURRENT;
default:
throw new PowsyblException("Unexpect violation type " + kind);
}
return switch (kind) {
case "UInfUmin" -> LimitViolationType.LOW_VOLTAGE;
case "USupUmax" -> LimitViolationType.HIGH_VOLTAGE;
case "OverloadOpen", "OverloadUp", "PATL" -> LimitViolationType.CURRENT;
default -> throw new PowsyblException("Unexpect violation type " + kind);
};
}

private ConstraintsReader() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;

import static com.powsybl.commons.test.ComparisonUtils.compareTxt;
import static com.powsybl.commons.test.ComparisonUtils.compareXml;
Expand Down Expand Up @@ -115,7 +114,7 @@ void test() throws IOException {
Contingency contingency1 = Contingency.builder("NHV1_NHV2_2_contingency").addBranch("NHV1_NHV2_2").build();
Contingency contingency2 = Contingency.builder("NB_NGEN_contingency").addBranch("NB_NGEN").build();
List<Contingency> contingencies = List.of(contingency1, contingency2);
List<String> contingencyIds = contingencies.stream().map(Contingency::getId).collect(Collectors.toList());
List<String> contingencyIds = contingencies.stream().map(Contingency::getId).toList();

LocalCommandExecutor commandExecutor = new LocalCommandExecutorMock("/dynawo_version.out",
"/SecurityAnalysis/input.xiidm", "/SecurityAnalysis/contingencies.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import com.powsybl.iidm.network.Load
import com.powsybl.iidm.network.Network
import com.powsybl.iidm.network.TwoWindingsTransformer

import java.util.stream.Collectors

/**
* @author Laurent Issertial <laurent.issertial at rte-france.com>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public DynaWaltzContext(Network network, String workingVariantId, List<BlackBoxM
this.dynaWaltzParameters = Objects.requireNonNull(dynaWaltzParameters);
this.frequencySynchronizer = setupFrequencySynchronizer(dynamicModels.stream().anyMatch(InfiniteBus.class::isInstance) ? SetPoint::new : OmegaRef::new);

for (BlackBoxModel bbm : getBlackBoxDynamicModelStream().collect(Collectors.toList())) {
for (BlackBoxModel bbm : getBlackBoxDynamicModelStream().toList()) {
macroStaticReferences.computeIfAbsent(bbm.getName(), k -> new MacroStaticReference(k, bbm.getVarsMapping()));
bbm.createMacroConnections(this);
}
Expand All @@ -76,7 +76,7 @@ private FrequencySynchronizerModel setupFrequencySynchronizer(Function<List<Freq
return fsConstructor.apply(dynamicModels.stream()
.filter(FrequencySynchronizedModel.class::isInstance)
.map(FrequencySynchronizedModel.class::cast)
.collect(Collectors.toList()));
.toList());
}

public Network getNetwork() {
Expand Down Expand Up @@ -213,7 +213,7 @@ public Stream<BlackBoxModel> getBlackBoxDynamicModelStream() {
}

public List<BlackBoxModel> getBlackBoxDynamicModels() {
return getBlackBoxDynamicModelStream().collect(Collectors.toList());
return getBlackBoxDynamicModelStream().toList();
}

public List<BlackBoxModel> getBlackBoxModels() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public void write(XMLStreamWriter writer) throws XMLStreamException {
writer.writeEmptyElement(DYN_URI, "macroConnect");
writer.writeAttribute("connector", id);
for (MacroConnectAttribute attribute : attributesFrom) {
writer.writeAttribute(attribute.getName(), attribute.getValue());
writer.writeAttribute(attribute.name(), attribute.value());
}
for (MacroConnectAttribute attribute : attributesTo) {
writer.writeAttribute(attribute.getName(), attribute.getValue());
writer.writeAttribute(attribute.name(), attribute.value());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@
/**
* @author Laurent Issertial <laurent.issertial at rte-france.com>
*/
public class MacroConnectAttribute {
private final String name;
private final String value;

public MacroConnectAttribute(String name, String value) {
this.name = name;
this.value = value;
}
public record MacroConnectAttribute(String name, String value) {

public static MacroConnectAttribute of(String name, String value) {
return new MacroConnectAttribute(name, value);
Expand All @@ -26,12 +19,4 @@ public static MacroConnectAttribute of(String name, String value) {
public static MacroConnectAttribute ofIndex1(int index) {
return new MacroConnectAttribute("index1", String.valueOf(index));
}

public String getName() {
return name;
}

public String getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public void write(XMLStreamWriter writer) throws XMLStreamException {
writer.writeAttribute("id", id);
for (VarConnection varConnection : this.varConnections) {
writer.writeEmptyElement(DYN_URI, "connect");
writer.writeAttribute("var1", varConnection.getVar1());
writer.writeAttribute("var2", varConnection.getVar2());
writer.writeAttribute("var1", varConnection.var1());
writer.writeAttribute("var2", varConnection.var2());
}
writer.writeEndElement();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,7 @@

/**
* @author Florian Dupuy <florian.dupuy at rte-france.com>
* @author Laurent Issertial <laurent.issertial at rte-france.com>
*/
public class VarConnection {
private final String var1;
private final String var2;

public VarConnection(String var1, String var2) {
this.var1 = var1;
this.var2 = var2;
}

public String getVar1() {
return var1;
}

public String getVar2() {
return var2;
}
public record VarConnection(String var1, String var2) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,7 @@

/**
* @author Florian Dupuy <florian.dupuy at rte-france.com>
* @author Laurent Issertial <laurent.issertial at rte-france.com>
*/
public class VarMapping {
private final String dynamicVar;
private final String staticVar;

public VarMapping(String dynamicVar, String staticVar) {
this.dynamicVar = dynamicVar;
this.staticVar = staticVar;
}

public String getStaticVar() {
return staticVar;
}

public String getDynamicVar() {
return dynamicVar;
}
public record VarMapping(String dynamicVar, String staticVar) {
}
Loading

0 comments on commit a2cfde7

Please sign in to comment.