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.9KB

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