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

31
LINES

< > BotCompany Repo | #1013935 // getOrCreate - map lookup or create from class

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

Libraryless. Click here for Pure Java version (3004L/17K).

// allows null keys but not null values

static <A, B> B getOrCreate(Map<A, B> map, A key, Class<? extends B> c) ctex {
  B b = map.get(key);
  if (b == null)
    map.put(key, b = c.newInstance());
  ret b;
}

// f : func -> B
static <A, B> B getOrCreate(Map<A, B> map, A key, O f) ctex {
  B b = map.get(key);
  if (b == null)
    map.put(key, b = (B) callF(f));
  ret b;
}

static <A, B> B getOrCreate(IF0<B> f, Map<A, B> map, A key) {
  ret getOrCreate(map, key, f);
}

static <A, B> B getOrCreate(Map<A, B> map, A key, IF0<B> f) {
  B b = map.get(key);
  if (b == null)
    map.put(key, b = f!);
  ret b;
}

static <A, B> B nuLike getOrCreate(Class<? extends B> c, Map<A, B> map, A key) {
  ret getOrCreate(map, key, c);
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1013935
Snippet name: getOrCreate - map lookup or create from class
Eternal ID of this version: #1013935/12
Text MD5: c8e1aa1765798e0d379e4436ee7d2d1e
Transpilation MD5: ab872c665638ad04b8f3adce12cfa3e4
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-07-23 19:36:39
Source code size: 756 bytes / 31 lines
Pitched / IR pitched: No / No
Views / Downloads: 494 / 583
Version history: 11 change(s)
Referenced in: [show references]