diff --git a/src/main/java/org/javademos/init/JEPInfo.java b/src/main/java/org/javademos/init/JEPInfo.java index 836dd839..c1ceb6ce 100644 --- a/src/main/java/org/javademos/init/JEPInfo.java +++ b/src/main/java/org/javademos/init/JEPInfo.java @@ -20,7 +20,7 @@ public record JEPData(int jep, int jdk, String name, String dscr, boolean link, private static Map getAllInfos() { var jeps = new HashMap(); - var sources = Arrays.asList(14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25); + var sources = Arrays.asList(13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25); for (Integer src : sources) { var srcFile = MessageFormat.format("/JDK{0}Info.json", src); try (InputStream inputStream = org.javademos.init.JEPInfo.class.getResourceAsStream(srcFile)) { diff --git a/src/main/java/org/javademos/init/Java13DemoLoader.java b/src/main/java/org/javademos/init/Java13DemoLoader.java index babe0cbf..45060963 100644 --- a/src/main/java/org/javademos/init/Java13DemoLoader.java +++ b/src/main/java/org/javademos/init/Java13DemoLoader.java @@ -5,6 +5,7 @@ import org.javademos.commons.IDemo; import org.javademos.commons.IDemoLoader; import org.javademos.java13.jep354.SwitchExpressionsSecondPreview; +import org.javademos.java13.jep355.TextBlockFirstPreviewDemo; /** * Loads demos for Java 13. @@ -15,5 +16,6 @@ public class Java13DemoLoader implements IDemoLoader { public void loadDemos(Map demos) { // JEP 354 demos.put(354, new SwitchExpressionsSecondPreview()); + demos.put(355, new TextBlockFirstPreviewDemo()); } } diff --git a/src/main/java/org/javademos/init/Java14DemoLoader.java b/src/main/java/org/javademos/init/Java14DemoLoader.java index 98dd3936..29463455 100644 --- a/src/main/java/org/javademos/init/Java14DemoLoader.java +++ b/src/main/java/org/javademos/init/Java14DemoLoader.java @@ -8,6 +8,7 @@ import org.javademos.java14.jep358.NullPointerDemo; import org.javademos.java14.jep359.RecordsPreviewDemo; import org.javademos.java14.jep361.SwitchExpressionsDemo; +import org.javademos.java14.jep368.TextBlockSecondPreviewDemo; import org.javademos.java14.jep370.ForeignMemoryAccessDemo; /** @@ -21,6 +22,7 @@ public void loadDemos(Map demos) { demos.put(358, new NullPointerDemo()); // JEP 358 demos.put(359, new RecordsPreviewDemo()); // JEP 359 demos.put(361, new SwitchExpressionsDemo()); // JEP 361 + demos.put(368, new TextBlockSecondPreviewDemo()); // JEP 368 demos.put(370, new ForeignMemoryAccessDemo()); // JEP 370 } } diff --git a/src/main/java/org/javademos/init/Java15DemoLoader.java b/src/main/java/org/javademos/init/Java15DemoLoader.java index 1ff1bd02..bda8b3e3 100644 --- a/src/main/java/org/javademos/init/Java15DemoLoader.java +++ b/src/main/java/org/javademos/init/Java15DemoLoader.java @@ -9,6 +9,7 @@ import org.javademos.java15.jep373.DatagramSocketDemo; import org.javademos.java15.jep375.InstanceofPatternMatchingSecondPreview; import org.javademos.java15.jep377.ZGarbageCollectorDemo; +import org.javademos.java15.jep378.TextBlockDemo; import org.javademos.java15.jep381.SolarisSparcRemovalDemo; import org.javademos.java15.jep383.ForeignMemoryAccessDemo; import org.javademos.java15.jep384.RecordsSecondPreviewDemo; @@ -29,6 +30,7 @@ public void loadDemos(Map demos) { demos.put(373, new DatagramSocketDemo()); demos.put(375, new InstanceofPatternMatchingSecondPreview()); demos.put(377, new ZGarbageCollectorDemo()); + demos.put(378, new TextBlockDemo()); demos.put(381, new SolarisSparcRemovalDemo()); demos.put(383, new ForeignMemoryAccessDemo()); demos.put(384, new RecordsSecondPreviewDemo()); diff --git a/src/main/java/org/javademos/java13/jep355/TextBlockFirstPreviewDemo.java b/src/main/java/org/javademos/java13/jep355/TextBlockFirstPreviewDemo.java new file mode 100644 index 00000000..2e5231c9 --- /dev/null +++ b/src/main/java/org/javademos/java13/jep355/TextBlockFirstPreviewDemo.java @@ -0,0 +1,22 @@ +package org.javademos.java13.jep355; + +import org.javademos.commons.IDemo; + +/// Demo for JDK 13 feature JEP 355 - Text Blocks (Preview). +/// +/// Relates to: +/// - [JEP 326: Raw String Literals (Preview)](https://openjdk.org/jeps/326) +/// +/// This was a preview JEP. The feature is finalized in JDK 15 by JEP 378. +/// @see org.javademos.java15.jep378.TextBlockDemo +/// +/// @author SanjanaMahapatra + + +public class TextBlockFirstPreviewDemo implements IDemo { + @Override + public void demo() { + info(355); + } + +} diff --git a/src/main/java/org/javademos/java14/jep368/TextBlockSecondPreviewDemo.java b/src/main/java/org/javademos/java14/jep368/TextBlockSecondPreviewDemo.java new file mode 100644 index 00000000..52ba6c6d --- /dev/null +++ b/src/main/java/org/javademos/java14/jep368/TextBlockSecondPreviewDemo.java @@ -0,0 +1,20 @@ +package org.javademos.java14.jep368; + +import org.javademos.commons.IDemo; + +/// Demo for JDK 14 feature JEP 368 - Text Blocks (Second Preview). +/// +/// This was a preview JEP. The feature is finalized in JDK 15 by JEP 378. +/// @see org.javademos.java15.jep378.TextBlockDemo +/// +/// @author SanjanaMahapatra + + +public class TextBlockSecondPreviewDemo implements IDemo { + + @Override + public void demo() { + info(368); + } + +} diff --git a/src/main/java/org/javademos/java15/jep378/TextBlockDemo.java b/src/main/java/org/javademos/java15/jep378/TextBlockDemo.java new file mode 100644 index 00000000..eb9cc63b --- /dev/null +++ b/src/main/java/org/javademos/java15/jep378/TextBlockDemo.java @@ -0,0 +1,118 @@ +package org.javademos.java15.jep378; + +import org.javademos.commons.IDemo; + +/// Demo for JDK 15 feature JEP 378 - Text Blocks. +/// +/// +/// JEP history: +/// - JDK 15: [JEP 378 - Text Blocks](https://openjdk.org/jeps/378) +/// - JDK 14: [JEP 368 - Text Blocks (Second Preview)](https://openjdk.org/jeps/368) +/// - JDK 13: [JEP 355 - Text Blocks (Preview)](https://openjdk.org/jeps/355) +/// +/// Further Reading: +/// - [Java Text Blocks](https://www.baeldung.com/java-text-blocks) +/// - [Programmer's Guide to Text Blocks](http://docs.oracle.com/javase/jp/15/text-blocks/index.html) +/// +/// @author alois.seckar@gmail.com, SanjanaMahapatra + +public class TextBlockDemo implements IDemo{ + + @Override + public void demo() { + info(378); + + // Java < 15 + // if you want multi-line text, you have to chain it with "+" + // and include \n to split the output into lines + String oldString = "This is multiline string \n" + + "constructed in the old fashioned way \n" + + "divided to more lines with + symbols \n" + + "and with \\n to add line breaks \n"; + + System.out.println("Old string for JDK < 15 "); + System.out.println(oldString); + + // Based on the feedback received from JEP355, in the second preview JEP368, two new escape + // sequence for explicit control were introduced -> + // a) \s (A single space preventing automatic stripping of trailing whitespace. + // b) \ (A backslash at the end of a line to suppress the insertion of a new line character. + + // This change introduced in JEP368, was also taken in the final implementation of JEP378. + + // Example of New Escape + + System.out.println(""" + The character literals and traditional string literals don't + allow the embedding of newlines, the \\ escape + sequence applicable for text blocks only. + """); + + String rawLiteral = "Lorem ipsum dolor sit amet, consectetur adipiscing " + + "elit, sed do eiusmod tempor incididunt ut labore " + + "et dolore magna aliqua."; + + System.out.println(rawLiteral); + + System.out.println("The following example demonstrates having a newline, but actually does not do"); + + String lineTerminatorText = """ + Lorem ipsum dolor sit amet, consectetur adipiscing \ + elit, sed do eiusmod tempor incididunt ut labore \ + et dolore magna aliqua.\ + """; + + System.out.println(lineTerminatorText); + + // Java 15+ + // you can have multi-line text inside """ and """ marks + // new lines are automatically being taken from new lines in source + String newString = """ + This is also multiline string + but constructed in the new way + divided to more lines without + symbols + and with no need to declare \\n line breaks + """; + System.out.println(); + System.out.println("New String for JDK 15+ "); + System.out.println(newString); + + // you can refer to output to see both Strings print out equivalently + + // adding some additional examples to support the statement + + System.out.println("Text blocks can be also used as the method argument -"); + System.out.println(""" + This is the first line in statement + This is the second line in statement + This is the third line in statement + """ + ); + + System.out.println(""" + When the text block contains sequence of three or more double quotes, + escape the first double quote of every run of three double quotes. + """); + + String originalString = """ + String source = \"\"\" + String message = "Hello, World!"; + System.out.println(message); + \"\"\"; + """; + + System.out.println("original string - "); + System.out.println(originalString); + + String betterString = """ + String source = \""" + String message = "Hello, World!"; + System.out.println(message); + \"""; + """; + + System.out.println("better string using text blocks - "); + System.out.println(betterString); + + } +} diff --git a/src/main/java/org/javademos/java17/textblock/TextBlockDemo.java b/src/main/java/org/javademos/java17/textblock/TextBlockDemo.java deleted file mode 100644 index 7ea9c02a..00000000 --- a/src/main/java/org/javademos/java17/textblock/TextBlockDemo.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.javademos.java17.textblock; - -import org.javademos.commons.IDemo; - -/// Demo for JDK 15 feature JEP 378 - Text Blocks. -/// -/// JEP history: -/// - JDK 15: [JEP 378 - Text Blocks](https://openjdk.org/jeps/378) -/// - JDK 14: [JEP 368 - Text Blocks (Second Preview)](https://openjdk.org/jeps/368) -/// - JDK 13: [JEP 355 - Text Blocks (Preview)](https://openjdk.org/jeps/355) -/// -/// Further reading: -/// - [Java Text Blocks](https://www.baeldung.com/java-text-blocks) -/// -/// @author alois.seckar@gmail.com -public class TextBlockDemo implements IDemo { - - @Override - public void demo() { - info("TEXT BLOCK DEMO", "Examples for 'text blocks' feature\nintroduced in Java 13 and finalized in Java 15"); - - // Java < 15 - // if you want multi-line text, you have to chain it with "+" - // and include \n to split the output into lines - String oldString = "This is multiline string \n" - + "constructed in the old fashioned way \n" - + "divided to more lines with + symbols \n" - + "and with \\n to add line breaks \n"; - System.out.println(oldString); - - // Java 15+ - // you can have multi-line text inside """ and """ marks - // new lines are automatically being taken from new lines in source - String newString = """ - This is also multiline string - but constructed in the new way - divided to more lines without + symbols - and with no need to declare \\n line breaks - """; - System.out.println(newString); - - // you can refer to output to see both Strings print out equivalently - } - -} diff --git a/src/main/resources/JDK13Info.json b/src/main/resources/JDK13Info.json index aaf4a842..4fb0ca84 100644 --- a/src/main/resources/JDK13Info.json +++ b/src/main/resources/JDK13Info.json @@ -6,5 +6,13 @@ "dscr": "Second preview of switch expressions introducing yield statement. Finalized in JEP 361 (JDK 14).", "link": true, "code": false + }, + { + "jep": 355, + "jdk": 13, + "name": "JEP 355 - Text Blocks (Preview)", + "dscr": "Up-to-date demo moved to `org.javademos.java15.jep378.TextBlockDemo` (JEP 378)", + "link": true, + "code": false } ] diff --git a/src/main/resources/JDK14Info.json b/src/main/resources/JDK14Info.json index a553f2b8..c4c3584f 100644 --- a/src/main/resources/JDK14Info.json +++ b/src/main/resources/JDK14Info.json @@ -31,6 +31,14 @@ "link": false, "code": true }, + { + "jep": 368, + "jdk": 14, + "name": "JEP 368 - Text Blocks (Second Preview)", + "dscr": "Up-to-date demo moved to `org.javademos.java15.jep378.TextBlockDemo` (JEP 378)", + "link": true, + "code": false + }, { "jep": 370, "jdk": 14, diff --git a/src/main/resources/JDK15Info.json b/src/main/resources/JDK15Info.json index 535b22e7..225d2781 100644 --- a/src/main/resources/JDK15Info.json +++ b/src/main/resources/JDK15Info.json @@ -55,6 +55,14 @@ "link": false, "code": false }, + { + "jep": 378, + "jdk": 15, + "name": "JEP 378 - Text Blocks", + "dscr": "Demonstrates adding text blocks to the Java language; see TextBlockDemo.java", + "link": false, + "code": true + }, { "jep": 381, "jdk": 15,