From 7786ffa00dddc57ebf73cd7eba5538c8d8a6256b Mon Sep 17 00:00:00 2001 From: Thomas Bouquet <63302082+bqth29@users.noreply.github.com> Date: Mon, 9 Dec 2024 11:42:16 +0100 Subject: [PATCH] Temporal data (#1228) * temporal data Signed-off-by: Thomas Bouquet * sonar Signed-off-by: Thomas Bouquet --------- Signed-off-by: Thomas Bouquet --- .../powsybl/openrao/commons/TemporalData.java | 30 +++++++++++ .../openrao/commons/TemporalDataImpl.java | 35 ++++++++++++ .../openrao/commons/TemporalDataImplTest.java | 54 +++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 commons/src/main/java/com/powsybl/openrao/commons/TemporalData.java create mode 100644 commons/src/main/java/com/powsybl/openrao/commons/TemporalDataImpl.java create mode 100644 commons/src/test/java/com/powsybl/openrao/commons/TemporalDataImplTest.java diff --git a/commons/src/main/java/com/powsybl/openrao/commons/TemporalData.java b/commons/src/main/java/com/powsybl/openrao/commons/TemporalData.java new file mode 100644 index 0000000000..bb1d8979d0 --- /dev/null +++ b/commons/src/main/java/com/powsybl/openrao/commons/TemporalData.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2024, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package com.powsybl.openrao.commons; + +import java.time.OffsetDateTime; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +/** + * @author Thomas Bouquet {@literal } + */ +public interface TemporalData { + Map getDataPerTimestamp(); + + default Optional getData(OffsetDateTime timestamp) { + return Optional.ofNullable(getDataPerTimestamp().get(timestamp)); + } + + default List getTimestamps() { + return getDataPerTimestamp().keySet().stream().sorted().toList(); + } + + void add(OffsetDateTime timestamp, T data); +} diff --git a/commons/src/main/java/com/powsybl/openrao/commons/TemporalDataImpl.java b/commons/src/main/java/com/powsybl/openrao/commons/TemporalDataImpl.java new file mode 100644 index 0000000000..bdf387408e --- /dev/null +++ b/commons/src/main/java/com/powsybl/openrao/commons/TemporalDataImpl.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2024, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package com.powsybl.openrao.commons; + +import java.time.OffsetDateTime; +import java.util.HashMap; +import java.util.Map; + +/** + * @author Thomas Bouquet {@literal } + */ +public class TemporalDataImpl implements TemporalData { + private final Map dataPerTimestamp; + + public TemporalDataImpl() { + this(new HashMap<>()); + } + + public TemporalDataImpl(Map dataPerTimestamp) { + this.dataPerTimestamp = new HashMap<>(dataPerTimestamp); + } + + public Map getDataPerTimestamp() { + return new HashMap<>(dataPerTimestamp); + } + + public void add(OffsetDateTime timestamp, T data) { + dataPerTimestamp.put(timestamp, data); + } +} diff --git a/commons/src/test/java/com/powsybl/openrao/commons/TemporalDataImplTest.java b/commons/src/test/java/com/powsybl/openrao/commons/TemporalDataImplTest.java new file mode 100644 index 0000000000..99377166b8 --- /dev/null +++ b/commons/src/test/java/com/powsybl/openrao/commons/TemporalDataImplTest.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2024, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package com.powsybl.openrao.commons; + +import org.junit.jupiter.api.Test; + +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import static org.ejml.UtilEjml.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author Thomas Bouquet {@literal } + */ +class TemporalDataImplTest { + final OffsetDateTime timestamp1 = OffsetDateTime.of(2024, 6, 12, 17, 41, 0, 0, ZoneOffset.UTC); + final OffsetDateTime timestamp2 = OffsetDateTime.of(2024, 6, 12, 17, 42, 0, 0, ZoneOffset.UTC); + final OffsetDateTime timestamp3 = OffsetDateTime.of(2024, 6, 12, 17, 43, 0, 0, ZoneOffset.UTC); + + @Test + void testCreateEmptyTemporalData() { + assertTrue(new TemporalDataImpl<>().getDataPerTimestamp().isEmpty()); + } + + @Test + void testCreateTemporalDataFromMap() { + Map stringPerTimestamp = Map.of(timestamp1, "Hello world!", timestamp2, "OpenRAO"); + TemporalData stringTemporalData = new TemporalDataImpl<>(stringPerTimestamp); + + assertEquals(stringPerTimestamp, stringTemporalData.getDataPerTimestamp()); + assertEquals(List.of(timestamp1, timestamp2), stringTemporalData.getTimestamps()); + assertEquals(Optional.of("Hello world!"), stringTemporalData.getData(timestamp1)); + assertEquals(Optional.of("OpenRAO"), stringTemporalData.getData(timestamp2)); + assertTrue(stringTemporalData.getData(timestamp3).isEmpty()); + } + + @Test + void testAddData() { + TemporalData stringTemporalData = new TemporalDataImpl<>(); + stringTemporalData.add(timestamp3, "ABC"); + assertEquals(Map.of(timestamp3, "ABC"), stringTemporalData.getDataPerTimestamp()); + assertEquals(List.of(timestamp3), stringTemporalData.getTimestamps()); + assertEquals(Optional.of("ABC"), stringTemporalData.getData(timestamp3)); + } +}