Skip to content

Commit

Permalink
Use SupplierJsonDeserializer
Browse files Browse the repository at this point in the history
Signed-off-by: lisrte <[email protected]>
  • Loading branch information
Lisrte committed Jul 2, 2024
1 parent 753ba12 commit 0810aac
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
public class CurvesJsonDeserializer extends StdDeserializer<List<DynawoCurvesBuilder>> {

private transient final Supplier<DynawoCurvesBuilder> builderConstructor;
private final transient Supplier<DynawoCurvesBuilder> builderConstructor;

public CurvesJsonDeserializer(Supplier<DynawoCurvesBuilder> builderConstructor) {
super(List.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,15 @@
*/
package com.powsybl.dynawaltz.suppliers.curves;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.powsybl.commons.report.ReportNode;
import com.powsybl.dynamicsimulation.Curve;
import com.powsybl.dynamicsimulation.CurvesSupplier;
import com.powsybl.dynawaltz.DynaWaltzProvider;
import com.powsybl.dynawaltz.curves.DynawoCurvesBuilder;
import com.powsybl.dynawaltz.suppliers.SupplierJsonDeserializer;
import com.powsybl.iidm.network.Network;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
Expand All @@ -31,16 +25,20 @@
* Instantiates an {@link com.powsybl.dynawaltz.curves.DynawoCurve} list from JSON
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public class DynawoCurveSupplier implements CurvesSupplier {
public final class DynawoCurveSupplier implements CurvesSupplier {

private final Function<CurvesJsonDeserializer, List<DynawoCurvesBuilder>> deserializerFunction;

public DynawoCurveSupplier(InputStream is) {
deserializerFunction = d -> deserialize(is, d);
public static DynawoCurveSupplier load(InputStream is) {
return new DynawoCurveSupplier(d -> new SupplierJsonDeserializer<>(d).deserialize(is));
}

public DynawoCurveSupplier(Path path) {
deserializerFunction = d -> deserialize(path, d);
public static DynawoCurveSupplier load(Path path) {
return new DynawoCurveSupplier(d -> new SupplierJsonDeserializer<>(d).deserialize(path));
}

private DynawoCurveSupplier(Function<CurvesJsonDeserializer, List<DynawoCurvesBuilder>> deserializerFunction) {
this.deserializerFunction = deserializerFunction;
}

@Override
Expand All @@ -56,27 +54,4 @@ public List<Curve> get(Network network, ReportNode reportNode) {
public String getName() {
return DynaWaltzProvider.NAME;
}

private static List<DynawoCurvesBuilder> deserialize(Path path, CurvesJsonDeserializer deserializer) {
try {
Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
return setupObjectMapper(deserializer).readValue(reader, new TypeReference<>() {
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static List<DynawoCurvesBuilder> deserialize(InputStream is, CurvesJsonDeserializer deserializer) {
try {
return setupObjectMapper(deserializer).readValue(is, new TypeReference<>() {
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static ObjectMapper setupObjectMapper(CurvesJsonDeserializer deserializer) {
return new ObjectMapper().registerModule(new SimpleModule().addDeserializer(List.class, deserializer));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void testCurvesSupplier() throws IOException {

Network network = EurostagTutorialExample1Factory.create();
try (InputStream is = getClass().getResourceAsStream("/suppliers/curves.json")) {
DynawoCurveSupplier dynawoCurveSupplier = new DynawoCurveSupplier(is);
DynawoCurveSupplier dynawoCurveSupplier = DynawoCurveSupplier.load(is);
List<Curve> curves = dynawoCurveSupplier.get(network, ReportNode.NO_OP);
assertThat(curves).usingRecursiveFieldByFieldElementComparatorOnFields()
.containsExactlyInAnyOrderElementsOf(getExpectedCurves());
Expand Down

0 comments on commit 0810aac

Please sign in to comment.