Named Placeholders in String Formatting (#12576)
* Named Placeholders in String Formatting * assert notEqualTo
This commit is contained in:
parent
c8bb6d7603
commit
8ce82953c5
@ -0,0 +1,31 @@
|
||||
package com.baeldung.namedformatting;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class NamedFormatter {
|
||||
private NamedFormatter() {}
|
||||
|
||||
public static String format(String template, Map<String, Object> parameters) {
|
||||
StringBuilder newTemplate = new StringBuilder(template);
|
||||
List<Object> valueList = new ArrayList<>();
|
||||
|
||||
Matcher matcher = Pattern.compile("[$][{](\\w+)}").matcher(template);
|
||||
|
||||
while (matcher.find()) {
|
||||
String key = matcher.group(1);
|
||||
|
||||
String paramName = "${" + key + "}";
|
||||
int index = newTemplate.indexOf(paramName);
|
||||
if (index != -1) {
|
||||
newTemplate.replace(index, index + paramName.length(), "%s");
|
||||
valueList.add(parameters.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
return String.format(newTemplate.toString(), valueList.toArray());
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package com.baeldung.namedformatting;
|
||||
|
||||
import org.apache.commons.text.StrSubstitutor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class NamedFormatterUnitTest {
|
||||
private static final String TEMPLATE = "Text: [${text}] Number: [${number}] Text again: [${text}]";
|
||||
|
||||
@Test
|
||||
void givenTemplateWithNamedParam_whenCallingCommonsTextStrSubstitutor_shouldGetExpectedResult() {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("text", "It's awesome!");
|
||||
params.put("number", 42);
|
||||
String result = StrSubstitutor.replace(TEMPLATE, params, "${", "}");
|
||||
assertThat(result).isEqualTo("Text: [It's awesome!] Number: [42] Text again: [It's awesome!]");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTemplateWithNamedParam_whenCallingCommonsTextStrSubstitutorWithParameterNames_shouldNotWorkAsExpected() {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("text", "'${number}' is a placeholder.");
|
||||
params.put("number", 42);
|
||||
String result = StrSubstitutor.replace(TEMPLATE, params, "${", "}");
|
||||
|
||||
assertThat(result).isNotEqualTo("Text: ['${number}' is a placeholder.] Number: [42] Text again: ['${number}' is a placeholder.]");
|
||||
|
||||
assertThat(result).isEqualTo("Text: ['42' is a placeholder.] Number: [42] Text again: ['42' is a placeholder.]");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTemplateWithNamedParam_whenCallingNamedFormatter_shouldGetExpectedResult() {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("text", "It's awesome!");
|
||||
params.put("number", 42);
|
||||
String result = NamedFormatter.format(TEMPLATE, params);
|
||||
assertThat(result).isEqualTo("Text: [It's awesome!] Number: [42] Text again: [It's awesome!]");
|
||||
|
||||
params.put("text", "'${number}' is a placeholder.");
|
||||
result = NamedFormatter.format(TEMPLATE, params);
|
||||
assertThat(result).isEqualTo("Text: ['${number}' is a placeholder.] Number: [42] Text again: ['${number}' is a placeholder.]");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user