-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathStringTemplateExample.java
85 lines (70 loc) · 2.34 KB
/
StringTemplateExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.time.*;
import java.time.format.DateTimeFormatter;
/**
* To run: `java --enable-preview --source 21 StringTemplateExample.java`
*/
public class StringTemplateExample {
static Type type = Type.HACKER;
static String firstName = "Thomas";
static String lastName = "Anderson";
static String hackerName = "Neo";
public static void main(String[] args) {
rawTemplateProcessor();
simpleTemplate();
allowedExpressionsInsideEmbeddedTemplate();
}
static void rawTemplateProcessor() {
StringTemplate st = StringTemplate.RAW."String template built from RAW template processor: \{firstName}";
String message = StringTemplate.STR.process(st);
// same as
message = STR."String template built from RAW template processor: \{firstName}";
System.out.println(message);
}
static void simpleTemplate() {
// STR template processor returns a string
String str = STR."Simple string template without embedded expression";
// we can use template embedded with string literal
System.out.println(STR."Wake up \{hackerName}...");
// we can also use template expression with template string
System.out.println(STR."""
Mr. \{lastName}! Welcome back, we missed you!
""");
// we can multiline the embedded expression
System.out.println(STR."The time is \{
DateTimeFormatter
.ofPattern("HH:mm:ss")
.format(LocalTime.now())
} right now");
// we don't need to escape "
System.out.println(STR."Follow the white \{"rabbit"}...");
}
// we can use any Java expression
static void allowedExpressionsInsideEmbeddedTemplate() {
boolean isCaptured = true;
// arithmetic expression
var counter = 0;
System.out.println(STR."Counting... \{++counter}... \{++counter}... \{++counter}...");
System.out.println(STR."How long has he been arrested? \{4 + 2} hours");
System.out.println(STR."How long is his name? \{firstName.length() + lastName.length()}");
// call method
System.out.println(STR."What was his first program? \{greetings()}");
// ternary expression
System.out.println(STR."As you can see Mr. \{
isCaptured ? lastName : hackerName
}, we've been tracking you...");
// switch expression
System.out.println(STR."What is he? \{
switch (type) {
case HACKER -> "hackerman";
case PERSON -> "bot";
}
}");
}
static String greetings() {
return "Hello matrix";
}
}
enum Type {
HACKER,
PERSON
}