Skip to content

Commit

Permalink
Allow adding new post processors on an existing ImportConfig (powsybl…
Browse files Browse the repository at this point in the history
…#3151)

Signed-off-by: Geoffroy Jamgotchian <[email protected]>
  • Loading branch information
geofjamg authored Sep 23, 2024
1 parent 0382d34 commit 1e1884a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import com.google.common.base.Suppliers;
import com.powsybl.commons.config.PlatformConfig;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.function.Supplier;

/**
Expand All @@ -27,7 +24,7 @@ public class ImportConfig {

public static final Supplier<ImportConfig> CACHE = Suppliers.memoize(ImportConfig::load);

private static final List<String> DEFAULT_POST_PROCESSORS = Collections.emptyList();
private static final List<String> DEFAULT_POST_PROCESSORS = new ArrayList<>();

private final List<String> postProcessors;

Expand All @@ -44,23 +41,27 @@ public static ImportConfig load(PlatformConfig platformConfig) {
}

public ImportConfig() {
this(Collections.emptyList());
this(new ArrayList<>());
}

public ImportConfig(String... postProcessors) {
this(Arrays.asList(postProcessors));
this(new ArrayList<>(Arrays.asList(postProcessors)));
}

public ImportConfig(List<String> postProcessors) {
this.postProcessors = Objects.requireNonNull(postProcessors);
}

/**
* The list of enabled {@link ImportPostProcessor}, defined by their name.
* @return the list of enabled {@link ImportPostProcessor}, defined by their name.
* The immutable list of enabled {@link ImportPostProcessor}, defined by their name.
* @return the immutable list of enabled {@link ImportPostProcessor}, defined by their name.
*/
public List<String> getPostProcessors() {
return postProcessors;
return Collections.unmodifiableList(postProcessors);
}

public void addPostProcessors(List<String> postProcessors) {
this.postProcessors.addAll(Objects.requireNonNull(postProcessors));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.iidm.network;

import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.powsybl.commons.config.InMemoryPlatformConfig;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
class ImportConfigTest {

@Test
void test() throws IOException {
var importConfig = new ImportConfig();
assertTrue(importConfig.getPostProcessors().isEmpty());
importConfig = new ImportConfig("p1", "p2");
var postProcessors = importConfig.getPostProcessors();
assertEquals(List.of("p1", "p2"), postProcessors);
// assert the returned list is immutable
assertThrows(UnsupportedOperationException.class, () -> postProcessors.add("p3"));
importConfig.addPostProcessors(List.of("p3"));
assertEquals(List.of("p1", "p2", "p3"), postProcessors);
try (var fs = Jimfs.newFileSystem(Configuration.unix())) {
InMemoryPlatformConfig platformConfig = new InMemoryPlatformConfig(fs);
importConfig = ImportConfig.load(platformConfig);
assertTrue(importConfig.getPostProcessors().isEmpty());
var module = platformConfig.createModuleConfig("import");
module.setStringListProperty("postProcessors", List.of("p4"));
importConfig = ImportConfig.load(platformConfig);
assertEquals(List.of("p4"), importConfig.getPostProcessors());
}
}
}

0 comments on commit 1e1884a

Please sign in to comment.