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).

// get() works as expected (calculating the element on demand).
// containsKey(), entrySet(), size() etc. only refer to the elements
// already made.
// Also allows put().
// Not synchronized
sclass AutoMap<A, B> extends AbstractMap<A, B> {
  Map<A, B> map = new HashMap;
  IF1<A, B> f;
  
  *() {}
  *(IF1<A, B> *f) {}
  *(IF0<B> f) { this.f = if0ToIF1(f); }
  *(Class<B> c) { this.f = a -> nu(c); }
  
  public B get(O a) {
    if (map.containsKey(a))
      ret map.get(a);
    cast a to A;
    B b = f.get(a);
    map.put(a, b);
    ret b;
  }
  
  // unclear semantics
  public bool containsKey(O a) {
    ret map.containsKey(a);
  }
  
  public Set<Map.Entry<A, B>> entrySet() {
    ret map.entrySet();
  }
  
  public int size() { ret map.size(); }
  
  public B put(A a, B b) {
    ret map.put(a, b);
  }
}

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: 639 / 2165
Version history: 17 change(s)
Referenced in: [show references]