Skip to content

Commit

Permalink
Temporal data (#1228)
Browse files Browse the repository at this point in the history
* temporal data

Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com>

* sonar

Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com>

---------

Signed-off-by: Thomas Bouquet <thomas.bouquet@rte-france.com>
  • Loading branch information
bqth29 authored Dec 9, 2024
1 parent 625d646 commit 7786ffa
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 <thomas.bouquet at rte-france.com>}
*/
public interface TemporalData<T> {
Map<OffsetDateTime, T> getDataPerTimestamp();

default Optional<T> getData(OffsetDateTime timestamp) {
return Optional.ofNullable(getDataPerTimestamp().get(timestamp));
}

default List<OffsetDateTime> getTimestamps() {
return getDataPerTimestamp().keySet().stream().sorted().toList();
}

void add(OffsetDateTime timestamp, T data);
}
Original file line number Diff line number Diff line change
@@ -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 <thomas.bouquet at rte-france.com>}
*/
public class TemporalDataImpl<T> implements TemporalData<T> {
private final Map<OffsetDateTime, T> dataPerTimestamp;

public TemporalDataImpl() {
this(new HashMap<>());
}

public TemporalDataImpl(Map<OffsetDateTime, T> dataPerTimestamp) {
this.dataPerTimestamp = new HashMap<>(dataPerTimestamp);
}

public Map<OffsetDateTime, T> getDataPerTimestamp() {
return new HashMap<>(dataPerTimestamp);
}

public void add(OffsetDateTime timestamp, T data) {
dataPerTimestamp.put(timestamp, data);
}
}
Original file line number Diff line number Diff line change
@@ -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 <thomas.bouquet at rte-france.com>}
*/
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<OffsetDateTime, String> stringPerTimestamp = Map.of(timestamp1, "Hello world!", timestamp2, "OpenRAO");
TemporalData<String> 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<String> 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));
}
}

0 comments on commit 7786ffa

Please sign in to comment.