Libraryless. Click here for Pure Java version (2696L/17K).
1 | sclass ValueOnDemandMap<A, B> extends AbstractMap<A, B> { |
2 | Set<A> keys; // keys may be changed if map is changed |
3 | IF1<A, B> makeValue; |
4 | |
5 | *() {} |
6 | *(Set<A> *keys, IF1<A, B> *makeValue) {} // doesn't clone keys |
7 | |
8 | public B get(O a) { |
9 | ret !containsKey(a) ? null : makeValue.get((A) a); |
10 | } |
11 | |
12 | public bool containsKey(O a) { |
13 | ret keys.contains(a); |
14 | } |
15 | |
16 | public Set<Map.Entry<A, B>> entrySet() { |
17 | ret new EntrySet; |
18 | } |
19 | |
20 | final class EntrySet extends AbstractSet<Map.Entry<A,B>> { |
21 | public final int size() { ret keys.size(); } |
22 | public final void clear() { keys.clear(); } |
23 | public final Iterator<Map.Entry<A,B>> iterator() { |
24 | return mapI(keys.iterator(), key -> new Map.Entry<A, B>() { |
25 | public A getKey() { ret key; } |
26 | public B getValue() { ret makeValue.get(key); } |
27 | public B setValue(B newValue) { |
28 | throw unimplemented(); |
29 | } |
30 | }); |
31 | } |
32 | public final boolean contains(Object o) { |
33 | if (o cast Map.Entry) |
34 | ret keys.contains(o.getKey()); |
35 | false; |
36 | } |
37 | public final boolean remove(Object o) { |
38 | if (o cast Map.Entry) |
39 | ret keys.remove(o.getKey()); |
40 | false; |
41 | } |
42 | } |
43 | |
44 | public Set<A> keySet() { ret keys; } |
45 | |
46 | public int size() { ret keys.size(); } |
47 | } |
Began life as a copy of #1008575
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1028138 |
Snippet name: | ValueOnDemandMap - map over a key set where values are calculated on every access |
Eternal ID of this version: | #1028138/12 |
Text MD5: | 95661ad9b4011faa89cd994cd2fdbca1 |
Transpilation MD5: | 1b44fe9d830feb8f1e170c857ecf974c |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-05-23 14:34:54 |
Source code size: | 1307 bytes / 47 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 286 / 647 |
Version history: | 11 change(s) |
Referenced in: | [show references] |