You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringPatternsPreview1.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import java.io.File;
  2. import java.net.InetAddress;
  3. import java.net.UnknownHostException;
  4. import java.time.LocalDate;
  5. import java.time.LocalDateTime;
  6. import java.time.LocalTime;
  7. import java.time.format.DateTimeFormatter;
  8. import static java.lang.StringTemplate.RAW;
  9. import static java.util.FormatProcessor.FMT;
  10. /**
  11. * Examples taken from <a href="https://openjdk.org/jeps/430">JEP 430</a>
  12. */
  13. public class StringPatternsPreview1 {
  14. public static void main(String[] args) {
  15. // Embedded expressions can be strings
  16. String firstName = "Bill", lastName = "Duck";
  17. System.out.println(STR."\{firstName} \{lastName}");
  18. // Embedded expressions can perform arithmetic
  19. int x = 10, y = 20;
  20. System.out.println(STR."\{x} + \{y} = \{x + y}");
  21. // Embedded expressions can invoke methods and access fields
  22. System.out.println(STR."You have a \{getOfferType()} waiting for you!");
  23. Request req = new Request();
  24. System.out.println(STR."Access at \{req.date} \{req.time} from \{req.ipAddress}");
  25. // Embedded expressions can use double quotes without escaping them
  26. String filePath = "_dummy.dat";
  27. File file = new File(filePath);
  28. System.out.println(STR."The file \{filePath} \{file.exists() ? "does" : "does not"} exist");
  29. // Embedded expressions can span multiple lines
  30. System.out.println(
  31. STR."The time is \{
  32. DateTimeFormatter
  33. .ofPattern("HH:mm:ss")
  34. .format(LocalTime.of(11, 11, 11))
  35. } or roughly eleven after eleven"
  36. );
  37. // Embedded expressions can be nested
  38. String[] fruit = { "apples", "oranges", "peaches" };
  39. System.out.println(STR."\{fruit[0]}, \{STR."\{fruit[1]}, \{fruit[2]}"}\n");
  40. // Embedded expressions can be used in multi-line strings
  41. String title = "My Web Page";
  42. String text = "Hello, world";
  43. String html = STR."""
  44. <html>
  45. <head>
  46. <title>\{title}</title>
  47. </head>
  48. <body>
  49. <p>\{text}</p>
  50. </body>
  51. </html>
  52. """;
  53. System.out.println(html);
  54. // The FMT template processor interprets format specifiers which appear to the left of embedded expressions.
  55. // The format specifiers are the same as those defined in java.util.Formatter.
  56. Rectangle[] zone = new Rectangle[] {
  57. new Rectangle("Alfa", 17.8, 31.4),
  58. new Rectangle("Bravo", 9.6, 12.4),
  59. new Rectangle("Charlie", 7.1, 11.23),
  60. };
  61. String table = FMT."""
  62. Description Width Height Area
  63. %-12s\{zone[0].name} %7.2f\{zone[0].width} %7.2f\{zone[0].height} %7.2f\{zone[0].area()}
  64. %-12s\{zone[1].name} %7.2f\{zone[1].width} %7.2f\{zone[1].height} %7.2f\{zone[1].area()}
  65. %-12s\{zone[2].name} %7.2f\{zone[2].width} %7.2f\{zone[2].height} %7.2f\{zone[2].area()}
  66. \{" ".repeat(28)} Total %7.2f\{zone[0].area() + zone[1].area() + zone[2].area()}
  67. """;
  68. System.out.println(table);
  69. // Built-in security: Each template expression needs to pass through a processor.
  70. String name = "Joan";
  71. StringTemplate stringTemplate = RAW."My name is \{name}";
  72. String processedTemplate = STR.process(stringTemplate);
  73. System.out.println(processedTemplate);
  74. }
  75. static Object getOfferType() {
  76. return "special New Year's sale discount";
  77. }
  78. static class Request {
  79. LocalDate date;
  80. LocalTime time;
  81. InetAddress ipAddress;
  82. Request() {
  83. LocalDateTime dateTime = LocalDateTime.of(2011, 11, 11, 11, 11, 11);
  84. date = dateTime.toLocalDate();
  85. time = dateTime.toLocalTime();
  86. try {
  87. // localhost/127.0.0.1
  88. ipAddress = InetAddress.getByAddress("localhost", new byte[] { 127, 0, 0, 1 });
  89. } catch (UnknownHostException e) {
  90. throw new RuntimeException(e);
  91. }
  92. }
  93. }
  94. record Rectangle(String name, double width, double height) {
  95. double area() {
  96. return width * height;
  97. }
  98. }
  99. }