Skip to content

Commit 7cd2669

Browse files
committed
JDK 24: added example of Module Import and Scoped Value
1 parent 2a21835 commit 7cd2669

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import module java.base;
2+
import module java.sql;
3+
import module java.desktop;
4+
5+
import java.util.*; // priori List
6+
import java.util.Date;
7+
8+
/**
9+
* Run: `java --source 24 --enable-preview ModuleImportDeclarationsTest.java`
10+
*/
11+
public class ModuleImportDeclarationsTest {
12+
public static void main(String[] args) {
13+
Date date = new Date();
14+
15+
List l = new ArrayList();
16+
}
17+
}

java-24/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ To run each example use: `java --enable-preview --source 24 <FileName.java>`
113113
* **Scoped Values**
114114
* re-preview with API changes
115115
* removed methods `callWhere` and `runWhere` to make API more fluent
116-
* `ScopedValue.where(SCOPE, "my-value").run(() -> {});` instead of `ScopedValue.callWhere(SCOPE, "my-value", () -> {})`
116+
* `ScopedValue.where(SCOPE, "my-value").run(() -> {});` instead of `ScopedValue.runWhere(SCOPE, "my-value", () -> {})`
117+
* `ScopedValue.where(SCOPE, "my-value").call(() -> {});` instead of `ScopedValue.callWhere(SCOPE, "my-value", () -> {})`
117118
* we can bind multiples values:
118119
* `ScopedValue.where(SCOPE_A, "my-value").where(SCOPE_B, "other-value").run(() -> {})`
119120
* **Primitive Types in Patterns, instanceof, and switch**
@@ -151,6 +152,8 @@ To run each example use: `java --enable-preview --source 24 <FileName.java>`
151152
* **Simple Source Files and Instance Main Methods**
152153
* re-preview with no change
153154
* introduced new terminology "simple source file" to indicate a Java file with a implicitly declared class
155+
* **Structured Concurrency**
156+
*
154157
155158
## Links
156159

java-24/ScopedValueUsageExample.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
import java.util.concurrent.*;
3+
4+
/**
5+
* Run: `java --source 24 --enable-preview ScopedValueUsageExample.java`
6+
*/
7+
public class ScopedValueUsageExample {
8+
final static ScopedValue<String> MAIN_SCOPE = ScopedValue.newInstance();
9+
10+
public static void main(String[] args) {
11+
System.out.println("Starting scoped value");
12+
13+
// we can share a value from here
14+
ScopedValue.where(MAIN_SCOPE, "message from main")
15+
.run(() -> {
16+
var worker = new Worker();
17+
worker.execute();
18+
});
19+
System.out.println("main ending");
20+
}
21+
}
22+
23+
class Worker {
24+
final static ScopedValue<String> WORKER_SCOPE = ScopedValue.newInstance();
25+
26+
public void execute() {
27+
// accessing the value from the scope
28+
System.out.println("shared value from main: " + ScopedValueUsageExample.MAIN_SCOPE.get());
29+
30+
// === Nested Scope ===
31+
// we can create a nested scope
32+
ScopedValue.where(WORKER_SCOPE, "message from worker")
33+
.run(() -> {
34+
// the outmost scope will still works
35+
var messageFromMain = ScopedValueUsageExample.MAIN_SCOPE.get();
36+
var messageFromWorker = WORKER_SCOPE.get();
37+
System.out.println("shared value to inner scope from main: " + messageFromMain);
38+
System.out.println("shared value to inner scope from worker: " + messageFromWorker);
39+
});
40+
41+
// we cannot access it from outside its scope (NoSuchElementException)
42+
// var invalidAccess = WORKER_SCOPE.get();
43+
44+
// === Rebinded Scope Value ===
45+
// we can create a new scope that rebinds a new value to the created scope (when using the same variable)
46+
ScopedValue.where(ScopedValueUsageExample.MAIN_SCOPE, "message from worker over main")
47+
.run(() -> {
48+
// the outmost scope will still works
49+
var rebindedMessage = ScopedValueUsageExample.MAIN_SCOPE.get();
50+
System.out.println("shared value from shadowed scope: " + rebindedMessage);
51+
});
52+
53+
// but the original scope from main will still have its initial value (immutable)
54+
System.out.println("shared value from main after all scopes: " + ScopedValueUsageExample.MAIN_SCOPE.get());
55+
56+
// === Failure Handle ===
57+
try {
58+
// with `where` we get checked exception type inference (requiring try-catch block)
59+
var result = ScopedValue.where(WORKER_SCOPE, "message for a risk move")
60+
.call(() -> {
61+
var message = WORKER_SCOPE.get();
62+
63+
if (message.length() > 15) {
64+
throw new MessageTooLongException();
65+
}
66+
return 42;
67+
});
68+
System.out.println("Result from risk move: " + result);
69+
} catch (MessageTooLongException ex) {
70+
System.err.println("Error during risk move: " + ex.getMessage());
71+
}
72+
}
73+
}
74+
75+
class MessageTooLongException extends Exception {
76+
MessageTooLongException() {
77+
super("Message provided in the scope was too long");
78+
}
79+
}

0 commit comments

Comments
 (0)