-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathStableValueExample.java
More file actions
100 lines (83 loc) · 3.11 KB
/
StableValueExample.java
File metadata and controls
100 lines (83 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import java.util.logging.Logger;
import static java.lang.IO.println;
/**
* To run: `java --source 25 --enable-preview StableValueExample.java`
*/
public class StableValueExample {
static final StableValue<ElementClass> ELEMENT = StableValue.of();
static final List<ElementClass> ELEMENTS_LIST = StableValue.list(5, index -> {
System.out.println("Initializing ElementClass at index " + index);
return new ElementClass(index);
});
static final Map<String, ElementClass> ELEMENTS_MAP = StableValue.map(Set.of("foo", "bar"), key -> {
System.out.println("Initializing ElementClass for key " + key);
return new ElementClass(key);
});
public static void main(String[] args) {
basicApiExample();
lazyInitializationExample();
stableListExample();
stableMapExample();
}
static ElementClass getElement() {
return ELEMENT.orElseSet(() -> {
System.err.println("orElseSet called, initializing ElementClass");
return new ElementClass("Lazy Initialization");
});
}
static void basicApiExample() {
println("=== Basic API Example ===");
// example of using a stable value
var stableValue = StableValue.of();
try {
println(stableValue.orElseThrow());
} catch (NoSuchElementException e) {
println("No value present");
}
var updated = stableValue.trySet("Value trySet");
println("Was the value updated? " + updated);
println("Value: " + stableValue.orElseThrow());
// Attempting to set a new value again
var secondUpdate = stableValue.trySet("Second Value trySet");
println("Was the value updated again? " + secondUpdate);
}
static void lazyInitializationExample() {
println("\n=== Lazy Initialization Example ===");
println("First call to getElement() will initialize ElementClass lazily");
println("First call: " + getElement());
println("Subsequent calls will return the already initialized value without re-initialization");
println("Second call: " + getElement());
}
static void stableListExample() {
println("\n=== Stable List Example ===");
println("Accessing ELEMENTS_LIST will initialize ElementClass lazily for each index");
for (int i = 0; i < ELEMENTS_LIST.size(); i++) {
println("\tElement at index " + i + ": " + ELEMENTS_LIST.get(i));
}
println("Accessing an already initialized index 2: " + ELEMENTS_LIST.get(2));
}
static void stableMapExample() {
println("\n=== Stable Map Example ===");
println("Accessing ELEMENTS_MAP will initialize ElementClass lazily for each key");
for (String key : ELEMENTS_MAP.keySet()) {
println("\tElement for key '" + key + "': " + ELEMENTS_MAP.get(key));
}
println("Accessing an already initialized key foo: " + ELEMENTS_MAP.get("foo"));
try {
println("Accessing a non-existent key: " + ELEMENTS_MAP.get("non-existent"));
} catch (NoSuchElementException e) {
println("Caught exception for non-existent key");
}
}
}
class ElementClass {
ElementClass(Object arg) {
// expensive initialization logic
println("\tElementClass constructor called with arg: " + arg);
}
}