Eujj, add custom separator support :))

This commit is contained in:
SiboVG 2023-08-25 00:00:14 +02:00
parent ccffffa186
commit e72870c306
2 changed files with 42 additions and 18 deletions

View File

@ -26,8 +26,9 @@ import java.util.regex.Pattern;
* General substitutor for motor configurations. This currently includes substitutions for * General substitutor for motor configurations. This currently includes substitutions for
* - {motors} - the motor designation (e.g. "M1350-0") * - {motors} - the motor designation (e.g. "M1350-0")
* - {manufacturers} - the motor manufacturer (e.g. "AeroTech") * - {manufacturers} - the motor manufacturer (e.g. "AeroTech")
* - a combination of motors and manufacturers, e.g. {motors manufacturers} -> "M1350-0 AeroTech" * - {cases} - the motor case (e.g. "SU 18.0x70.0")
* You can choose which comes first is. E.g. {manufacturers motors} -> "AeroTech M1350-0". * - a combination of motors and manufacturers, e.g. {motors | manufacturers} -> "M1350-0 | AeroTech"
* You can choose which comes first and what the separator is. E.g. {manufacturers, motors} -> "AeroTech, M1350-0".
* *
* <p> * <p>
* This substitutor is added through injection. All substitutors with the "@Plugin" tag in the formatting package will * This substitutor is added through injection. All substitutors with the "@Plugin" tag in the formatting package will
@ -75,10 +76,27 @@ public class MotorConfigurationSubstitutor implements RocketSubstitutor {
while (matcher.find()) { while (matcher.find()) {
String tagContent = matcher.group(1).trim(); String tagContent = matcher.group(1).trim();
String[] keys = tagContent.split("\\s");
// Step 1: Find the keys
List<String> foundKeys = new ArrayList<>();
Matcher keyMatcher = Pattern.compile("\\b(" + String.join("|", SUBSTITUTIONS.keySet()) + ")\\b").matcher(tagContent);
while (keyMatcher.find()) {
foundKeys.add(keyMatcher.group());
}
// Step 2: Extracting the separators
List<String> separators = new ArrayList<>();
int lastEnd = 0;
for (int i = 0; i < foundKeys.size() - 1; i++) {
int startOfNextKey = tagContent.indexOf(foundKeys.get(i + 1), lastEnd);
String separator = tagContent.substring(lastEnd + foundKeys.get(i).length(), startOfNextKey);
separators.add(separator);
lastEnd = startOfNextKey + foundKeys.get(i + 1).length();
}
// Continue with the original function
List<Map<AxialStage, List<String>>> stageSubstitutes = new ArrayList<>(); List<Map<AxialStage, List<String>>> stageSubstitutes = new ArrayList<>();
for (String key : keys) { for (String key : foundKeys) {
if (SUBSTITUTIONS.containsKey(key)) { if (SUBSTITUTIONS.containsKey(key)) {
Map<AxialStage, List<String>> sub = SUBSTITUTIONS.get(key).substitute(rocket, configId); Map<AxialStage, List<String>> sub = SUBSTITUTIONS.get(key).substitute(rocket, configId);
stageSubstitutes.add(sub); stageSubstitutes.add(sub);
@ -86,7 +104,8 @@ public class MotorConfigurationSubstitutor implements RocketSubstitutor {
} }
FlightConfiguration config = rocket.getFlightConfiguration(configId); FlightConfiguration config = rocket.getFlightConfiguration(configId);
List<String> combinations = combineSubstitutesForStages(rocket, config, stageSubstitutes, " "); // Use the extracted separators instead of a single space
List<String> combinations = combineSubstitutesForStages(rocket, config, stageSubstitutes, separators);
String combined = String.join("; ", combinations); String combined = String.join("; ", combinations);
matcher.appendReplacement(resultBuffer, Matcher.quoteReplacement(combined)); matcher.appendReplacement(resultBuffer, Matcher.quoteReplacement(combined));
@ -98,7 +117,7 @@ public class MotorConfigurationSubstitutor implements RocketSubstitutor {
} }
private List<String> combineSubstitutesForStages(Rocket rocket, FlightConfiguration config, private List<String> combineSubstitutesForStages(Rocket rocket, FlightConfiguration config,
List<Map<AxialStage, List<String>>> stageSubstitutes, String separator) { List<Map<AxialStage, List<String>>> stageSubstitutes, List<String> separators) {
List<String> combinations = new ArrayList<>(); List<String> combinations = new ArrayList<>();
// Parse through all the stages to get the final configuration string // Parse through all the stages to get the final configuration string
@ -110,6 +129,7 @@ public class MotorConfigurationSubstitutor implements RocketSubstitutor {
StringBuilder sbStageSub = new StringBuilder(); StringBuilder sbStageSub = new StringBuilder();
// Parse through all the substitutes (motors, manufacturers, etc.) for each stage to build a combined stage substitution // Parse through all the substitutes (motors, manufacturers, etc.) for each stage to build a combined stage substitution
int idx = 0;
for (Map<AxialStage, List<String>> substituteMap : stageSubstitutes) { for (Map<AxialStage, List<String>> substituteMap : stageSubstitutes) {
List<String> substitutes = substituteMap.get(stage); List<String> substitutes = substituteMap.get(stage);
if (substitutes == null || substitutes.isEmpty()) { if (substitutes == null || substitutes.isEmpty()) {
@ -117,13 +137,15 @@ public class MotorConfigurationSubstitutor implements RocketSubstitutor {
} }
// If this is not the first substitute, add a separator between the different substitutes (motor, manufacturer, etc.) // If this is not the first substitute, add a separator between the different substitutes (motor, manufacturer, etc.)
if (!sbStageSub.isEmpty()) { if (!sbStageSub.isEmpty() && idx > 0) {
sbStageSub.append(separator); sbStageSub.append(separators.get(idx - 1));
} }
// Create a final substitute for this sub tag from the list of substitutes // Create a final substitute for this sub tag from the list of substitutes
String finalSubstitute = getFinalSubstitute(substitutes, sbStageSub); String finalSubstitute = getFinalSubstitute(substitutes, sbStageSub);
sbStageSub.append(finalSubstitute); sbStageSub.append(finalSubstitute);
idx++;
} }
if (sbStageSub.isEmpty()) { if (sbStageSub.isEmpty()) {

View File

@ -651,36 +651,38 @@ public class FlightConfigurationTest extends BaseTestCase {
assertEquals("{}", selected.getName()); assertEquals("{}", selected.getName());
// Test invalid tags (1) // Test invalid tags (1)
selected.setName("{motorsm}"); selected.setName("{motorms}");
selected.setAllStages(); selected.setAllStages();
assertEquals("{motorsm}", selected.getName()); assertEquals("{motorms}", selected.getName());
selected.setOnlyStage(0); selected.setOnlyStage(0);
assertEquals("{motorsm}", selected.getName()); assertEquals("{motorms}", selected.getName());
selected.setOnlyStage(1); selected.setOnlyStage(1);
assertEquals("{motorsm}", selected.getName()); assertEquals("{motorms}", selected.getName());
selected.setName("{motor}");
selected.setAllStages(); selected.setAllStages();
selected._setStageActive(0, false); selected._setStageActive(0, false);
assertEquals("{motorsm}", selected.getName()); assertEquals("{motor}", selected.getName());
// Test invalid tags (2) // Test invalid tags (2)
selected.setName("{motors manufacturers '}"); selected.setName("{mot'ors manuf'acturers '}");
selected.setAllStages(); selected.setAllStages();
assertEquals("{motors manufacturers '}", selected.getName()); assertEquals("{mot'ors manuf'acturers '}", selected.getName());
selected.setOnlyStage(0); selected.setOnlyStage(0);
assertEquals("{motors manufacturers '}", selected.getName()); assertEquals("{mot'ors manuf'acturers '}", selected.getName());
selected.setOnlyStage(1); selected.setOnlyStage(1);
assertEquals("{motors manufacturers '}", selected.getName()); assertEquals("{mot'ors manuf'acturers '}", selected.getName());
selected.setAllStages(); selected.setAllStages();
selected._setStageActive(0, false); selected._setStageActive(0, false);
assertEquals("{motors manufacturers '}", selected.getName()); assertEquals("{mot'ors manuf'acturers '}", selected.getName());
} }
@Test @Test