|
| 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