diff --git a/README.md b/README.md index 3a72dbe..a30d32b 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,12 @@ This repository contains Java examples that are designed to track and document t ## Specifications & Practices +* [Java 24](java-24/) (March, 2025) + * [JEP 488](java-24/src/main/java/com/ibrahimatay/JEP488PrimitiveTypesInPatternsInstanceofAndSwitch.java): Primitive Types in Patterns, instanceof, and switch + * [JEP 495](java-24/src/main/java/com/ibrahimatay/JEP495SimpleSourceFilesAndInstanceMainMethods.java): Simple Source Files and Instance Main Methods + * [JEP 485](java-24/src/main/java/com/ibrahimatay/JEP485StreamGatherers.java): Stream Gatherers + * [JEP 487](java-24/src/main/java/com/ibrahimatay/JEP487ScopedValues.java): Scoped Values + * [Java 23](java-23/) (September, 2024) * [JEP 455](java-23/src/main/java/com/ibrahimatay/JEP455PrimitiveTypesInPatternsInstanceofAndSwitch.java): Primitive Types in Patterns, instanceof, and switch * [JEP 467](java-23/src/main/java/com/ibrahimatay/JEP467MarkdownDocumentationComments.java): Markdown Documentation Comments diff --git a/java-24/pom.xml b/java-24/pom.xml new file mode 100644 index 0000000..8ab69ce --- /dev/null +++ b/java-24/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + com.ibrahimatay + Java-Features + 1.0-SNAPSHOT + + + java-24 + + + 24 + 24 + UTF-8 + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 24 + 24 + --enable-preview + + + + + + \ No newline at end of file diff --git a/java-24/src/main/java/JEP485StreamGatherers.java b/java-24/src/main/java/JEP485StreamGatherers.java new file mode 100644 index 0000000..9a2df31 --- /dev/null +++ b/java-24/src/main/java/JEP485StreamGatherers.java @@ -0,0 +1,34 @@ +import java.util.concurrent.ThreadLocalRandom; +import java.util.stream.Gatherers; +import java.util.stream.Stream; + +public class JEP485StreamGatherers { + public static void main(String[] args) { + // Stream Gatherers (JEP 485) + // https://openjdk.java.net/jeps/485 + + // Custom Intermediate Operation distinctBy + /* + Stream.of("foo", "bar", "baz", "quux") + .gather(Gatherers.distinctBy(String::length)) + .toList(); + */ + + // Creating Fixed-size Windows + /* + Stream.iterate(0, i -> i + 1) + .gather(Gatherers.windowFixed(3)) + .limit(2) + .toList(); + */ + + // Parallel Processing with selectOne + /* + Stream.generate(() -> ThreadLocalRandom.current().nextInt()) + .limit(1000) + .parallel() + .gather(Gatherers.selectOne(Math::max)) + .findFirst(); + */ + } +} diff --git a/java-24/src/main/java/JEP487ScopedValues.java b/java-24/src/main/java/JEP487ScopedValues.java new file mode 100644 index 0000000..cfeb8e7 --- /dev/null +++ b/java-24/src/main/java/JEP487ScopedValues.java @@ -0,0 +1,21 @@ +import static java.lang.ScopedValue.where; + +public class JEP487ScopedValues { + // Define a scoped value + private static final ScopedValue USER_NAME = ScopedValue.newInstance(); + + public static void main(String[] args) { + // JEP 487: Scoped Values (Fourth Preview) + // https://openjdk.org/jeps/487 + + where(USER_NAME, "John").run(() -> { + System.out.println("User: " + USER_NAME.get()); + subMethod(); + }); + } + + public static void subMethod() { + // Access scoped value within a sub-method + System.out.println("User in subMethod: " + USER_NAME.get()); + } +} diff --git a/java-24/src/main/java/JEP488PrimitiveTypesInPatternsInstanceofAndSwitch.java b/java-24/src/main/java/JEP488PrimitiveTypesInPatternsInstanceofAndSwitch.java new file mode 100644 index 0000000..37eb9cf --- /dev/null +++ b/java-24/src/main/java/JEP488PrimitiveTypesInPatternsInstanceofAndSwitch.java @@ -0,0 +1,61 @@ +import java.util.Map; + +public class JEP488PrimitiveTypesInPatternsInstanceofAndSwitch { + public static void main(String[] args) { + // JEP 488: Primitive Types in Patterns, instanceof, and switch (Second Preview) + // https://openjdk.org/jeps/488 + + // Primitive Types ile Pattern Matching + Object obj = 42; + if (obj instanceof int i) { + System.out.printf("Primitive int value: %1$s\n", i); + } + + // Switch for Primitive Type + System.out.printf("%1$s HTTP status code refers to a %2$s%n", 100, getHTTPCodeDesc(100)); + System.out.printf("%1$s HTTP status code refers to a %2$s%n", 200, getHTTPCodeDesc(200)); + System.out.printf("%1$s HTTP status code refers to a %2$s%n", 403, getHTTPCodeDesc(403)); + System.out.printf("%1$s HTTP status code refers to a %2$s%n", 0, getHTTPCodeDesc(0)); + + // Record Patterns with Primitive Types + JsonValue json = new JsonObject(Map.of( + "name", new JsonString("Alice"), + "age", new JsonNumber(28) + )); + + if (json instanceof JsonObject(var map) + && map.get("name") instanceof JsonString(var name) + && map.get("age") instanceof JsonNumber(var age)) { + System.out.printf("Name: %s, Age: %.0f%n", name, age); + } + } + + // Switch for Primitive Type + public static String getHTTPCodeDesc(int code){ + return switch(code) { + case 100 -> "Continue"; + case 200 -> "OK"; + case 301 -> "Moved Permanently"; + case 302 -> "Found"; + case 400 -> "Bad Request"; + case 500 -> "Internal Server Error"; + case 502 -> "Bad Gateway"; + case int i when (i > 100 && i < 200) -> "Informational"; + case int i when (i > 200 && i < 300) -> "Successful"; + case int i when (i > 302 && i < 400) -> "Redirection"; + case int i when (i > 400 && i < 500) -> "Client Error"; + case int i when (i > 502 && i < 600) -> "Server Error"; + default -> "Unknown error"; + }; + } +} + +interface Human {} +class Employee implements Human {} +class Boss implements Human {} + +sealed interface JsonValue permits JsonString, JsonNumber, JsonObject {} + +record JsonString(String value) implements JsonValue {} +record JsonNumber(double value) implements JsonValue {} +record JsonObject(Map map) implements JsonValue {} diff --git a/java-24/src/main/java/JEP495SimpleSourceFilesAndInstanceMainMethods.java b/java-24/src/main/java/JEP495SimpleSourceFilesAndInstanceMainMethods.java new file mode 100644 index 0000000..34f6b1b --- /dev/null +++ b/java-24/src/main/java/JEP495SimpleSourceFilesAndInstanceMainMethods.java @@ -0,0 +1,10 @@ +// JEP 495: Simple Source Files and Instance Main Methods (Fourth Preview) +// https://openjdk.org/jeps/495 + +public void main() { + System.out.printf("%1$s + %2$s = %3$s", 1,2, sum(1,2)); +} + +int sum(int x, int y) { + return x+y; +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 9770967..2af150c 100644 --- a/pom.xml +++ b/pom.xml @@ -9,9 +9,6 @@ pom 1.0-SNAPSHOT - java-5 - java-6 - java-7 java-8 java-9 java-10 @@ -23,6 +20,7 @@ java-12 java-23 java-14 + java-24