Libraryless. Click here for Pure Java version (5414L/32K).
1 | sclass LazyVar<A> implements IF0<A> { |
2 | *() {} |
3 | *(A *v) { if (v == null) calculating = new Var; } |
4 | *(IF0<A> *f) {} |
5 | |
6 | IF0<A> f; |
7 | |
8 | // will keep var around after the fact (we could change this by counting how many threads are waiting) |
9 | Var<OKOrError<A>> calculating; |
10 | A v; |
11 | |
12 | public A get() { |
13 | bool meCalc; |
14 | |
15 | if (v != null) ret v; |
16 | |
17 | synchronized { |
18 | meCalc = calculating == null; |
19 | if (meCalc) |
20 | calculating = new Var; |
21 | } |
22 | |
23 | if (meCalc) { |
24 | OKOrError<A> b = okOrError(() -> f!); |
25 | calculating.set(b); |
26 | synchronized { ret v = b!; } |
27 | } else { |
28 | waitUntilNotNull(calculating); |
29 | ret v; |
30 | } |
31 | } |
32 | |
33 | public synchronized bool isEvaluated() { ret f == null; } |
34 | |
35 | public synchronized bool has() { ret get() != null; } |
36 | |
37 | toString { ret isEvaluated() ? str(get()) : "LazyVar"; } |
38 | } |
Began life as a copy of #1003550
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1032743 |
Snippet name: | LazyVar - variable evaluating itself on demand |
Eternal ID of this version: | #1032743/10 |
Text MD5: | 12fdd02d55a9bf15e46aa46e264a14ae |
Transpilation MD5: | cc04de52f3b5ad61375236fdb81b3d2d |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-10-04 07:23:36 |
Source code size: | 886 bytes / 38 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 176 / 326 |
Version history: | 9 change(s) |
Referenced in: | [show references] |