-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ReportFormatter for printing ReportNode (#3317)
* Add formatter to getMessage and print * Add unit test * Add doc Signed-off-by: Florian Dupuy <[email protected]>
- Loading branch information
Showing
6 changed files
with
134 additions
and
22 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
commons/src/main/java/com/powsybl/commons/report/ReportFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Copyright (c) 2025, 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.commons.report; | ||
|
||
/** | ||
* @author Florian Dupuy {@literal <florian.dupuy at rte-france.com>} | ||
*/ | ||
@FunctionalInterface | ||
public interface ReportFormatter { | ||
ReportFormatter DEFAULT = t -> t.getValue().toString(); | ||
|
||
String format(TypedValue value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
commons/src/test/java/com/powsybl/commons/report/ReportFormatterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Copyright (c) 2025, 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.commons.report; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Locale; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
/** | ||
* @author Florian Dupuy {@literal <florian.dupuy at rte-france.com>} | ||
*/ | ||
class ReportFormatterTest { | ||
|
||
@Test | ||
void test() { | ||
ReportNode root = ReportNode.newRootReportNode() | ||
.withMessageTemplate("formatTest", """ | ||
Formatter test message | ||
double default format: ${doubleDefaultFormat} | ||
double format based on type: ${doubleSpecificFormat} | ||
float format based on type: ${floatSpecificFormat} | ||
string default format: ${stringDefaultFormat} | ||
string format based on type: ${stringSpecificFormat}""") | ||
.withUntypedValue("doubleDefaultFormat", 4.35684975) | ||
.withTypedValue("doubleSpecificFormat", 4.4664798548, TypedValue.ACTIVE_POWER) | ||
.withTypedValue("floatSpecificFormat", 0.6f, TypedValue.IMPEDANCE) | ||
.withUntypedValue("stringDefaultFormat", "tiny") | ||
.withTypedValue("stringSpecificFormat", "This is a sentence which needs to be truncated", "LONG_SENTENCE") | ||
.build(); | ||
ReportFormatter customFormatter = typedValue -> { | ||
if (typedValue.getType().equals(TypedValue.ACTIVE_POWER) && typedValue.getValue() instanceof Double d) { | ||
return String.format(Locale.CANADA_FRENCH, "%2.4f", d); | ||
} | ||
if (typedValue.getType().equals(TypedValue.IMPEDANCE) && typedValue.getValue() instanceof Float f) { | ||
return String.format(Locale.CANADA_FRENCH, "%.2f", f); | ||
} | ||
if (typedValue.getType().equals("LONG_SENTENCE") && typedValue.getValue() instanceof String s) { | ||
return s.substring(0, 18); | ||
} | ||
return typedValue.getValue().toString(); | ||
}; | ||
assertEquals(""" | ||
Formatter test message | ||
double default format: 4.35684975 | ||
double format based on type: 4,4665 | ||
float format based on type: 0,60 | ||
string default format: tiny | ||
string format based on type: This is a sentence""", root.getMessage(customFormatter)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters