Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/javademos/init/JEPInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public record JEPData(int jep, int jdk, String name, String dscr, boolean link,
private static Map<Integer, JEPData> getAllInfos() {
var jeps = new HashMap<Integer, JEPData>();

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)) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/javademos/init/Java13DemoLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -15,5 +16,6 @@ public class Java13DemoLoader implements IDemoLoader {
public void loadDemos(Map<Integer, IDemo> demos) {
// JEP 354
demos.put(354, new SwitchExpressionsSecondPreview());
demos.put(355, new TextBlockFirstPreviewDemo());
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/javademos/init/Java14DemoLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -21,6 +22,7 @@ public void loadDemos(Map<Integer, IDemo> 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
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/javademos/init/Java15DemoLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,7 @@ public void loadDemos(Map<Integer, IDemo> 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());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.javademos.java13.jep355;

import org.javademos.commons.IDemo;

/// Demo for JDK 13 feature <strong>JEP 355 - Text Blocks (Preview)</strong>.
///
/// 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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.javademos.java14.jep368;

import org.javademos.commons.IDemo;

/// Demo for JDK 14 feature <strong>JEP 368 - Text Blocks (Second Preview)</strong>.
///
/// 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);
}

}
118 changes: 118 additions & 0 deletions src/main/java/org/javademos/java15/jep378/TextBlockDemo.java
Original file line number Diff line number Diff line change
@@ -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 \\<line-terminator> 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);

}
}
45 changes: 0 additions & 45 deletions src/main/java/org/javademos/java17/textblock/TextBlockDemo.java

This file was deleted.

8 changes: 8 additions & 0 deletions src/main/resources/JDK13Info.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
8 changes: 8 additions & 0 deletions src/main/resources/JDK14Info.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/JDK15Info.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down