-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathLazyConstantExample.java
More file actions
123 lines (102 loc) · 3.86 KB
/
Copy pathLazyConstantExample.java
File metadata and controls
123 lines (102 loc) · 3.86 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.util.NoSuchElementException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import java.util.logging.Logger;
import static java.lang.IO.println;
/**
* To run: `java --source 27 --enable-preview LazyConstantExample.java`
*
* https://cr.openjdk.org/~pminborg/lazy-constants-third-preview/api/java.base/java/lang/LazyConstant.html
*/
public class LazyConstantExample {
static final LazyConstant<ElementClass> ELEMENT = LazyConstant.of(() -> {
System.out.println("Initializing ELEMENT");
return new ElementClass(42);
});
static final List<ElementClass> ELEMENTS_LIST = List.ofLazy(5, index -> {
System.out.println("[List] Initializing ElementClass at index " + index);
return new ElementClass(index);
});
static final Map<String, ElementClass> ELEMENTS_MAP = Map.ofLazy(Set.of("foo", "bar"), key -> {
System.out.println("[Map] Initializing ElementClass for key `" + key + "`");
return new ElementClass(key);
});
static final Set<String> ELEMENTS_SET = Set.ofLazy(Set.of("one", "two", "three", "four", "five"), key -> {
System.out.println("[Set] Initializing ElementClass for key `" + key + "`");
// only items with predicate true will be kept
if (key.length() > 4) {
System.out.println("\tKey `" + key + "` won't be initialized");
return false;
}
return true;
});
public static void main(String[] args) {
basicApiExample();
lazyInitializationExample();
stableListExample();
stableMapExample();
stableSetExample();
}
static void basicApiExample() {
println("=== Basic API Example ===");
// removed factory method `of()` to always have a initializer
var stableValue = LazyConstant.of(() -> {
System.out.println("Initializing lazy constant");
return 42;
});
// removed `orElseThrow`
// removed `isInitialized`
// removed `trySet`
// removed `orElse`
// `get` will trigger initialization
println("Reading with initialization: " + stableValue.get());
println("Reading an already initialized: " + stableValue.get());
}
static void lazyInitializationExample() {
println("\n=== Lazy Initialization Example ===");
println("First call to getElement() will initialize ElementClass lazily");
println("First call: " + ELEMENT.get());
println("Subsequent calls will return the already initialized value without re-initialization");
println("Second call: " + ELEMENT.get());
}
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");
}
}
static void stableSetExample() {
println("\n=== Stable Set Example ===");
println("Accessing ELEMENTS_SET will initialize ElementClass lazily for each key");
for (String key : ELEMENTS_SET) {
println("\tElement key `" + key + "` is in the set");
}
println("Accessing again (it should not print key 'three')");
for (String key : ELEMENTS_SET) {
println("\tElement key `" + key + "` is in the set");
}
}
}
class ElementClass {
ElementClass(Object arg) {
// expensive initialization logic
println("\tElementClass constructor called with arg: " + arg);
}
}