Skip to content

Commit b356226

Browse files
committed
replace Map with ConcurrentMap for lazy loading
Signed-off-by: Samir Romdhani <samir.romdhani_externe@rte-france.com> test Signed-off-by: Samir Romdhani <samir.romdhani_externe@rte-france.com>
1 parent a2cfa27 commit b356226

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

iidm/iidm-serde/src/main/java/com/powsybl/iidm/serde/NetworkSerDe.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
import java.time.ZonedDateTime;
5757
import java.time.format.DateTimeFormatter;
5858
import java.util.*;
59+
import java.util.concurrent.ConcurrentHashMap;
60+
import java.util.concurrent.ConcurrentMap;
5961
import java.util.concurrent.ExecutorService;
6062
import java.util.concurrent.ForkJoinPool;
6163
import java.util.function.Supplier;
@@ -89,7 +91,7 @@ public final class NetworkSerDe {
8991
static final byte[] BIIDM_MAGIC_NUMBER = {0x42, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x20, 0x49, 0x49, 0x44, 0x4d};
9092

9193
private static final Supplier<Schema> DEFAULT_SCHEMA_SUPPLIER = Suppliers.memoize(() -> NetworkSerDe.createSchema(DefaultExtensionsSupplier.getInstance()));
92-
private static final Supplier<Map<IidmVersion, Schema>> DEFAULT_SCHEMAS_SUPPLIER = Suppliers.memoize(() -> NetworkSerDe.createDefaultSchemas(DefaultExtensionsSupplier.getInstance()));
94+
private static final Supplier<ConcurrentMap<IidmVersion, Schema>> DEFAULT_SCHEMAS_SUPPLIER = Suppliers.memoize(ConcurrentHashMap::new);
9395
private static final int MAX_NAMESPACE_PREFIX_NUM = 100;
9496
private static final String XSD_RESOURCE_DIR = "/xsd/";
9597

@@ -177,10 +179,7 @@ private static void validate(InputStream is, IidmVersion version, ExtensionsSupp
177179
// XSD validation
178180
Schema schema;
179181
if (extensionsSupplier == DefaultExtensionsSupplier.getInstance()) {
180-
schema = DEFAULT_SCHEMAS_SUPPLIER.get().get(version);
181-
if (schema == null) {
182-
throw new PowsyblException("Schema not found: version=" + version);
183-
}
182+
schema = DEFAULT_SCHEMAS_SUPPLIER.get().computeIfAbsent(version, v -> createSchema(DefaultExtensionsSupplier.getInstance(), v));
184183
} else {
185184
schema = createSchema(extensionsSupplier, version);
186185
}
@@ -193,15 +192,6 @@ private static void validate(InputStream is, IidmVersion version, ExtensionsSupp
193192
}
194193
}
195194

196-
private static Map<IidmVersion, Schema> createDefaultSchemas(ExtensionsSupplier extensionsSupplier) {
197-
Map<IidmVersion, Schema> schemasByIIdmVersion = new EnumMap<>(IidmVersion.class);
198-
for (IidmVersion version : IidmVersion.values()) {
199-
Schema schema = createSchema(extensionsSupplier, version);
200-
schemasByIIdmVersion.put(version, schema);
201-
}
202-
return schemasByIIdmVersion;
203-
}
204-
205195
private static Schema createSchema(ExtensionsSupplier extensionsSupplier, IidmVersion version) {
206196
Objects.requireNonNull(extensionsSupplier);
207197
Objects.requireNonNull(version);

iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/NetworkSerDeTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.nio.file.Files;
3333
import java.nio.file.Path;
3434
import java.time.ZonedDateTime;
35+
import java.util.concurrent.*;
3536

3637
import static com.powsybl.commons.test.ComparisonUtils.assertTxtEquals;
3738
import static com.powsybl.iidm.serde.IidmSerDeConstants.CURRENT_IIDM_VERSION;
@@ -419,6 +420,7 @@ void testValidateByVersionWhenMismatchedNetworkVersion() throws IOException {
419420
@Test
420421
void testValidateByVersionWhenInvalidNetwork() throws IOException {
421422
try (InputStream is = getClass().getResourceAsStream("/V1_16/shuntOldTagName.xml")) {
423+
assertNotNull(is);
422424
assertThatThrownBy(() -> NetworkSerDe.validate(is, IidmVersion.V_1_16))
423425
.isInstanceOf(com.powsybl.commons.exceptions.UncheckedSaxException.class)
424426
.hasMessageContaining("Invalid content was found starting with element '{\"http://www.powsybl.org/schema/iidm/1_16\":shunt}'");
@@ -429,13 +431,15 @@ void testValidateByVersionWhenInvalidNetwork() throws IOException {
429431
void testValidateByVersionWhenNetworkContainSlackTerminalExtension() throws IOException {
430432
// Given extension: slack_terminal, version 1.5 that require iidm version 1.8 when validate should succeed
431433
try (InputStream is = getClass().getResourceAsStream("/slackTerminal.xml")) {
434+
assertNotNull(is);
432435
assertDoesNotThrow(() -> NetworkSerDe.validate(is, IidmVersion.V_1_16));
433436
}
434437
}
435438

436439
@Test
437440
void testValidateByVersionWhenNetworkContainTerminalMockExtension() throws IOException {
438441
try (InputStream is = getClass().getResourceAsStream("/V1_16/eurostag-tutorial-example1-with-terminalMock-ext.xml")) {
442+
assertNotNull(is);
439443
assertDoesNotThrow(() -> NetworkSerDe.validate(is, IidmVersion.V_1_16));
440444
}
441445
}
@@ -448,8 +452,6 @@ void testValidateByVersionWhenInValidExtension() throws IOException {
448452
.isInstanceOf(com.powsybl.commons.exceptions.UncheckedSaxException.class)
449453
.hasMessageContaining("Value 'TEST' is not facet-valid with respect to enumeration " +
450454
"'[IN_OPERATION, PLANNED_OUTAGE, FORCED_OUTAGE]'. It must be a value from the enumeration.");
451-
452455
}
453456
}
454-
455457
}

0 commit comments

Comments
 (0)