blob: 951f56bd1490438d662da0f7b799aaa1c0a20ddd (
plain)
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
|
package org.shedlang.compiler.util;
import java.util.Optional;
import java.util.function.Supplier;
public class Lazy<T> {
public static <T> Lazy<T> of(Supplier<T> supplier) {
return new Lazy<>(supplier);
}
private final Supplier<T> supplier;
private Optional<T> value;
private Lazy(Supplier<T> supplier) {
this.supplier = supplier;
this.value = Optional.empty();
}
public T get() {
if (this.value.isEmpty()) {
this.value = Optional.of(this.supplier.get());
}
return this.value.get();
}
}
|