Skip to content

Commit c448bb3

Browse files
committed
JEP 430: StringTemplates
1 parent 6b34419 commit c448bb3

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ This repository contains Java examples that are designed to track and document t
55

66
## Specifications & Practices
77

8+
* [Java 21](java-21) (September, 2023)
9+
* [JEP 430](https://openjdk.org/jeps/430): String Templates
10+
811
* [Java 16](java-16/) (March, 2021)
912
* [JEP 395](https://openjdk.java.net/jeps/395): Records
1013

java-21/pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.ibrahimatay</groupId>
8+
<artifactId>Java-Features</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>java-21</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>18</maven.compiler.source>
16+
<maven.compiler.target>18</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<configuration>
25+
<source>21</source>
26+
<target>21</target>
27+
<compilerArgs>--enable-preview</compilerArgs>
28+
</configuration>
29+
</plugin>
30+
</plugins>
31+
</build>
32+
33+
</project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.ibrahimatay;
2+
3+
4+
import static java.lang.StringTemplate.RAW;
5+
import static java.lang.StringTemplate.STR;
6+
import static java.util.FormatProcessor.FMT;
7+
8+
/*
9+
* JEP 430: String Templates (Preview)
10+
* https://openjdk.org/jeps/430
11+
* */
12+
public class JEP430StringTemplates {
13+
14+
public static void main(String[] args) {
15+
System.out.println(interpolationUsingSTRProcessor(getFeelsLike(), getTemperature(), getUnit()));
16+
System.out.println(interpolationOfJSONBlock(getFeelsLike(), getTemperature(), getUnit()));
17+
System.out.println(interpolationWithExpressions());
18+
System.out.println(interpolationWithTemplates());
19+
System.out.println(interpolationOfJSONBlockWithFMT(getFeelsLike(), 25, getUnit()));
20+
}
21+
22+
static String interpolationUsingSTRProcessor(String feelsLike, String temperature, String unit) {
23+
return STR
24+
. "Today's weather is \{ feelsLike }, with a temperature of \{ temperature } degrees \{ unit }" ;
25+
}
26+
27+
static String interpolationOfJSONBlock(String feelsLike, String temperature, String unit) {
28+
return STR
29+
. """
30+
{
31+
"feelsLike": "\{ feelsLike }",
32+
"temperature": "\{ temperature }",
33+
"unit": "\{ unit }"
34+
}
35+
""" ;
36+
}
37+
38+
static String interpolationWithExpressions() {
39+
return STR
40+
. "Today's weather is \{ getFeelsLike() }, with a temperature of \{ getTemperature() } degrees \{ getUnit() }" ;
41+
}
42+
43+
static String interpolationWithTemplates() {
44+
StringTemplate str = RAW
45+
. "Today's weather is \{ getFeelsLike() }, with a temperature of \{ getTemperature() } degrees \{ getUnit() }" ;
46+
return STR.process(str);
47+
}
48+
49+
static String interpolationOfJSONBlockWithFMT(String feelsLike, float temperature, String unit) {
50+
return FMT
51+
. """
52+
{
53+
"feelsLike": "%1s\{ feelsLike }",
54+
"temperature": "%2.2f\{ temperature }",
55+
"unit": "%1s\{ unit }"
56+
}
57+
""" ;
58+
}
59+
60+
private static String getFeelsLike() {
61+
return "pleasant";
62+
}
63+
64+
private static String getTemperature() {
65+
return "25";
66+
}
67+
68+
private static String getUnit() {
69+
return "Celsius";
70+
}
71+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<module>java-7</module>
1818
<module>java-6</module>
1919
<module>java-16</module>
20+
<module>java-21</module>
2021
</modules>
2122

2223
<properties>

0 commit comments

Comments
 (0)