[BAEL-6956] string templates (#14825)
Co-authored-by: Bhaskar <bhaskar.dastidar@freshworks.com>
This commit is contained in:
parent
b172cbf69b
commit
c689708383
@ -12,23 +12,22 @@
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<!-- <build>-->
|
||||
<!-- <plugins>-->
|
||||
<!-- <plugin>-->
|
||||
<!-- <groupId>org.apache.maven.plugins</groupId>-->
|
||||
<!-- <artifactId>maven-compiler-plugin</artifactId>-->
|
||||
<!-- <configuration>-->
|
||||
<!-- <source>{maven.compiler.source.version}</source>-->
|
||||
<!-- <target>{maven.compiler.target.version}</target>-->
|
||||
<!-- </configuration>-->
|
||||
<!-- </plugin>-->
|
||||
<!-- </plugins>-->
|
||||
<!-- </build>-->
|
||||
|
||||
<!-- <properties>-->
|
||||
<!-- <maven.compiler.source.version>21</maven.compiler.source.version>-->
|
||||
<!-- <maven.compiler.target.version>21</maven.compiler.target.version>-->
|
||||
<!-- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>-->
|
||||
<!-- </properties>-->
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>21</source>
|
||||
<target>21</target>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<maven.compiler.source.version>21</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>21</maven.compiler.target.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
</project>
|
@ -0,0 +1,37 @@
|
||||
package com.baeldung.stringtemplates;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
|
||||
public class StringCompositionTechniques {
|
||||
String composeUsingPlus(String feelsLike, String temperature, String unit) {
|
||||
return "Today's weather is " + feelsLike + ", with a temperature of " + temperature + " degrees " + unit;
|
||||
}
|
||||
|
||||
String composeUsingStringBuffer(String feelsLike, String temperature, String unit) {
|
||||
return new StringBuffer().append("Today's weather is ")
|
||||
.append(feelsLike)
|
||||
.append(", with a temperature of ")
|
||||
.append(temperature)
|
||||
.append(" degrees ")
|
||||
.append(unit)
|
||||
.toString();
|
||||
}
|
||||
|
||||
String composeUsingStringBuilder(String feelsLike, String temperature, String unit) {
|
||||
return new StringBuilder().append("Today's weather is ")
|
||||
.append(feelsLike)
|
||||
.append(", with a temperature of ")
|
||||
.append(temperature)
|
||||
.append(" degrees ")
|
||||
.append(unit)
|
||||
.toString();
|
||||
}
|
||||
|
||||
String composeUsingFormatters(String feelsLike, String temperature, String unit) {
|
||||
return String.format("Today's weather is %s, with a temperature of %s degrees %s", feelsLike, temperature, unit);
|
||||
}
|
||||
|
||||
String composeUsingMessageFormatter(String feelsLike, String temperature, String unit) {
|
||||
return MessageFormat.format("Today''s weather is {0}, with a temperature of {1} degrees {2}", feelsLike, temperature, unit);
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package com.baeldung.stringtemplates;
|
||||
|
||||
import static java.lang.StringTemplate.RAW;
|
||||
import static java.util.FormatProcessor.FMT;
|
||||
|
||||
public class StringTemplateExamples {
|
||||
String interpolationUsingSTRProcessor(String feelsLike, String temperature, String unit) {
|
||||
return STR
|
||||
. "Today's weather is \{ feelsLike }, with a temperature of \{ temperature } degrees \{ unit }" ;
|
||||
}
|
||||
|
||||
String interpolationOfJSONBlock(String feelsLike, String temperature, String unit) {
|
||||
return STR
|
||||
. """
|
||||
{
|
||||
"feelsLike": "\{ feelsLike }",
|
||||
"temperature": "\{ temperature }",
|
||||
"unit": "\{ unit }"
|
||||
}
|
||||
""" ;
|
||||
}
|
||||
|
||||
String interpolationWithExpressions() {
|
||||
return STR
|
||||
. "Today's weather is \{ getFeelsLike() }, with a temperature of \{ getTemperature() } degrees \{ getUnit() }" ;
|
||||
}
|
||||
|
||||
String interpolationWithTemplates() {
|
||||
StringTemplate str = RAW
|
||||
. "Today's weather is \{ getFeelsLike() }, with a temperature of \{ getTemperature() } degrees \{ getUnit() }" ;
|
||||
return STR.process(str);
|
||||
}
|
||||
|
||||
String interpolationOfJSONBlockWithFMT(String feelsLike, float temperature, String unit) {
|
||||
return FMT
|
||||
. """
|
||||
{
|
||||
"feelsLike": "%1s\{ feelsLike }",
|
||||
"temperature": "%2.2f\{ temperature }",
|
||||
"unit": "%1s\{ unit }"
|
||||
}
|
||||
""" ;
|
||||
}
|
||||
|
||||
private String getFeelsLike() {
|
||||
return "pleasant";
|
||||
}
|
||||
|
||||
private String getTemperature() {
|
||||
return "25";
|
||||
}
|
||||
|
||||
private String getUnit() {
|
||||
return "Celsius";
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package com.baeldung.stringtemplates;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class StringTemplatesUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenStringConcat_thenReturnComposedString() {
|
||||
StringCompositionTechniques stringCompositionTechniques = new StringCompositionTechniques();
|
||||
Assert.assertEquals("Today's weather is pleasant, with a temperature of 25 degrees Celsius", stringCompositionTechniques.composeUsingPlus("pleasant", "25", "Celsius"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringBuffer_thenReturnComposedString() {
|
||||
StringCompositionTechniques stringCompositionTechniques = new StringCompositionTechniques();
|
||||
Assert.assertEquals("Today's weather is pleasant, with a temperature of 25 degrees Celsius", stringCompositionTechniques.composeUsingStringBuffer("pleasant", "25", "Celsius"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringBuilder_thenReturnComposedString() {
|
||||
StringCompositionTechniques stringCompositionTechniques = new StringCompositionTechniques();
|
||||
Assert.assertEquals("Today's weather is pleasant, with a temperature of 25 degrees Celsius", stringCompositionTechniques.composeUsingStringBuilder("pleasant", "25", "Celsius"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringFormatter_thenReturnComposedFormattedString() {
|
||||
StringCompositionTechniques stringCompositionTechniques = new StringCompositionTechniques();
|
||||
Assert.assertEquals("Today's weather is pleasant, with a temperature of 25 degrees Celsius", stringCompositionTechniques.composeUsingFormatters("pleasant", "25", "Celsius"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMessageFormatter_thenReturnComposedFormattedString() {
|
||||
StringCompositionTechniques stringCompositionTechniques = new StringCompositionTechniques();
|
||||
Assert.assertEquals("Today's weather is pleasant, with a temperature of 25 degrees Celsius", stringCompositionTechniques.composeUsingMessageFormatter("pleasant", "25", "Celsius"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingStringTemplateSTR_thenReturnInterpolatedString() {
|
||||
StringTemplateExamples templateExamples = new StringTemplateExamples();
|
||||
Assert.assertEquals("Today's weather is pleasant, with a temperature of 25 degrees Celsius", templateExamples.interpolationUsingSTRProcessor("pleasant", "25", "Celsius"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingMultilineStringTemplateSTR_thenReturnInterpolatedString() {
|
||||
StringTemplateExamples templateExamples = new StringTemplateExamples();
|
||||
Assert.assertEquals("{\n" + " \"feelsLike\": \"pleasant\",\n" + " \"temperature\": \"25\",\n" + " \"unit\": \"Celsius\"\n" + "}\n", templateExamples.interpolationOfJSONBlock("pleasant", "25", "Celsius"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingExpressionSTR_thenReturnInterpolatedString() {
|
||||
StringTemplateExamples templateExamples = new StringTemplateExamples();
|
||||
Assert.assertEquals("Today's weather is pleasant, with a temperature of 25 degrees Celsius", templateExamples.interpolationWithExpressions());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingExpressionRAW_thenReturnInterpolatedString() {
|
||||
StringTemplateExamples templateExamples = new StringTemplateExamples();
|
||||
Assert.assertEquals("Today's weather is pleasant, with a temperature of 25 degrees Celsius", templateExamples.interpolationWithTemplates());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingExpressionFMT_thenReturnInterpolatedString() {
|
||||
StringTemplateExamples templateExamples = new StringTemplateExamples();
|
||||
Assert.assertEquals("{\n" + " \"feelsLike\": \"pleasant\",\n" + " \"temperature\": \"25.86\",\n" + " \"unit\": \"Celsius\"\n" + "}\n", templateExamples.interpolationOfJSONBlockWithFMT("pleasant", 25.8636F, "Celsius"));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user