diff --git a/java-samples/src/main/java/com/jetbrains/code/jdk/preview/PatternMatchingForSwitch.java b/java-samples/src/main/java/com/jetbrains/code/jdk21/PatternMatchingForSwitch.java
similarity index 63%
rename from java-samples/src/main/java/com/jetbrains/code/jdk/preview/PatternMatchingForSwitch.java
rename to java-samples/src/main/java/com/jetbrains/code/jdk21/PatternMatchingForSwitch.java
index 87ad0cf..7ce1794 100644
--- a/java-samples/src/main/java/com/jetbrains/code/jdk/preview/PatternMatchingForSwitch.java
+++ b/java-samples/src/main/java/com/jetbrains/code/jdk21/PatternMatchingForSwitch.java
@@ -1,35 +1,39 @@
-package com.jetbrains.code.jdk.preview;
+package com.jetbrains.code.jdk21;
import java.util.List;
/**
*
* - #PreviewFeature First Preview #JDK17
+ * - #StandardFeature #JDK21
*
+ * See: JEP 441
*/
@SuppressWarnings("unused")
public class PatternMatchingForSwitch {
- static String exampleJEP406OriginalCode(Object o) {
+ // Prior to Java 21
+ static String exampleJEP411OriginalCode(Object obj) {
String formatted = "unknown";
- if (o instanceof Integer i) {
+ if (obj instanceof Integer i) {
formatted = String.format("int %d", i);
- } else if (o instanceof Long l) {
+ } else if (obj instanceof Long l) {
formatted = String.format("long %d", l);
- } else if (o instanceof Double d) {
+ } else if (obj instanceof Double d) {
formatted = String.format("double %f", d);
- } else if (o instanceof String s) {
+ } else if (obj instanceof String s) {
formatted = String.format("String %s", s);
}
return formatted;
}
- static String exampleJEP406PatternMatchingForSwitch(Object o) {
- return switch (o) {
+ // As of Java 21
+ static String formatterPatternSwitch(Object obj) {
+ return switch (obj) {
case Integer i -> String.format("int %d", i);
case Long l -> String.format("long %d", l);
case Double d -> String.format("double %f", d);
case String s -> String.format("String %s", s);
- default -> o.toString();
+ default -> obj.toString();
};
}
@@ -65,4 +69,20 @@ static void testFooBar(String s) {
default -> System.out.println("Ok");
}
}
+
+ // As of Java 21
+ static void testStringNew(String response) {
+ switch (response) {
+ case null -> {}
+ case "y", "Y" -> System.out.println("You got it");
+ case "n", "N" -> System.out.println("Shame");
+ case String s
+ when s.equalsIgnoreCase("YES") -> System.out.println("You got it");
+ case String s
+ when s.equalsIgnoreCase("NO") -> System.out.println("Shame");
+ case String s -> {
+ System.out.println("Sorry?");
+ }
+ }
+ }
}
diff --git a/java-samples/src/main/java/com/jetbrains/code/jdk21/Point.java b/java-samples/src/main/java/com/jetbrains/code/jdk21/Point.java
new file mode 100644
index 0000000..a3344aa
--- /dev/null
+++ b/java-samples/src/main/java/com/jetbrains/code/jdk21/Point.java
@@ -0,0 +1,4 @@
+package com.jetbrains.code.jdk21;
+
+record Point(int x, int y) {
+}
diff --git a/java-samples/src/main/java/com/jetbrains/code/jdk21/RecordPatterns.java b/java-samples/src/main/java/com/jetbrains/code/jdk21/RecordPatterns.java
new file mode 100644
index 0000000..c823dd9
--- /dev/null
+++ b/java-samples/src/main/java/com/jetbrains/code/jdk21/RecordPatterns.java
@@ -0,0 +1,28 @@
+package com.jetbrains.code.jdk21;
+
+/**
+ *
+ * - #StandardFeature #JDK21
+ *
+ * See: JEP 440
+ */
+public class RecordPatterns {
+
+ // Not sure why, but the inspection doesn't seem to trigger for this code
+ static int sum(Object obj) {
+ if (obj instanceof Point p) {
+ int x = p.x();
+ int y = p.y();
+ return (x + y);
+ }
+ return 0;
+ }
+
+ static int sumUpdated(Object obj) {
+ if (obj instanceof Point(int x, int y)) {
+ return (x+y);
+ }
+ return 0;
+ }
+}
+
diff --git a/java-samples/src/main/java/com/jetbrains/code/jdk21/StringTemplate.java b/java-samples/src/main/java/com/jetbrains/code/jdk21/StringTemplate.java
new file mode 100644
index 0000000..a538907
--- /dev/null
+++ b/java-samples/src/main/java/com/jetbrains/code/jdk21/StringTemplate.java
@@ -0,0 +1,9 @@
+package com.jetbrains.code.jdk21;
+
+@SuppressWarnings("unused")
+public class StringTemplate {
+ private void templateCanBeUsed() {
+ String name = "Bob";
+ String greeting = "Hello, " + name + ". You are " + 29 + " years old.";
+ }
+}
diff --git a/java-samples/src/test/com/jetbrains/code/jdk21/RecordPatternsTest.java b/java-samples/src/test/com/jetbrains/code/jdk21/RecordPatternsTest.java
new file mode 100644
index 0000000..9018b12
--- /dev/null
+++ b/java-samples/src/test/com/jetbrains/code/jdk21/RecordPatternsTest.java
@@ -0,0 +1,28 @@
+package com.jetbrains.code.jdk21;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+class RecordPatternsTest {
+ @Test
+ @DisplayName("Should sum point values")
+ void shouldSum() {
+ // when
+ int sum = RecordPatterns.sum(new Point(1, 2));
+
+ // then
+ Assertions.assertEquals(3, sum);
+ }
+
+ @Test
+ @DisplayName("Should sum points with JDK21")
+ void shouldSumPointsWithJdk21() {
+ // when
+ int sum = RecordPatterns.sumUpdated(new Point(1, 2));
+
+ // then
+ Assertions.assertEquals(3, sum);
+ }
+
+}
\ No newline at end of file