Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

38
LINES

< > BotCompany Repo | #1008575 // AutoMap - map with automatic element creation

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (4717L/26K).

1  
// get() works as expected (calculating the element on demand).
2  
// containsKey(), entrySet(), size() etc. only refer to the elements
3  
// already made.
4  
// Also allows put().
5  
// Not synchronized
6  
sclass AutoMap<A, B> extends AbstractMap<A, B> {
7  
  Map<A, B> map = new HashMap;
8  
  IF1<A, B> f;
9  
  
10  
  *() {}
11  
  *(IF1<A, B> *f) {}
12  
  *(IF0<B> f) { this.f = if0ToIF1(f); }
13  
  *(Class<B> c) { this.f = a -> nu(c); }
14  
  
15  
  public B get(O a) {
16  
    if (map.containsKey(a))
17  
      ret map.get(a);
18  
    cast a to A;
19  
    B b = f.get(a);
20  
    map.put(a, b);
21  
    ret b;
22  
  }
23  
  
24  
  // unclear semantics
25  
  public bool containsKey(O a) {
26  
    ret map.containsKey(a);
27  
  }
28  
  
29  
  public Set<Map.Entry<A, B>> entrySet() {
30  
    ret map.entrySet();
31  
  }
32  
  
33  
  public int size() { ret map.size(); }
34  
  
35  
  public B put(A a, B b) {
36  
    ret map.put(a, b);
37  
  }
38  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1008575
Snippet name: AutoMap - map with automatic element creation
Eternal ID of this version: #1008575/18
Text MD5: 411501a882e32e3955287e27c05ee205
Transpilation MD5: ee36378d9a01f080d6df2929d71cb9fd
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-29 02:08:09
Source code size: 850 bytes / 38 lines
Pitched / IR pitched: No / No
Views / Downloads: 640 / 2166
Version history: 17 change(s)
Referenced in: [show references]