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

1  
// allows null keys but not null values
2  
3  
static <A, B> B getOrCreate(Map<A, B> map, A key, Class<? extends B> c) ctex {
4  
  B b = map.get(key);
5  
  if (b == null)
6  
    map.put(key, b = c.newInstance());
7  
  ret b;
8  
}
9  
10  
// f : func -> B
11  
static <A, B> B getOrCreate(Map<A, B> map, A key, O f) ctex {
12  
  B b = map.get(key);
13  
  if (b == null)
14  
    map.put(key, b = (B) callF(f));
15  
  ret b;
16  
}
17  
18  
static <A, B> B getOrCreate(IF0<B> f, Map<A, B> map, A key) {
19  
  ret getOrCreate(map, key, f);
20  
}
21  
22  
static <A, B> B getOrCreate(Map<A, B> map, A key, IF0<B> f) {
23  
  B b = map.get(key);
24  
  if (b == null)
25  
    map.put(key, b = f!);
26  
  ret b;
27  
}
28  
29  
static <A, B> B nuLike getOrCreate(Class<? extends B> c, Map<A, B> map, A key) {
30  
  ret getOrCreate(map, key, c);
31  
}

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: 587 / 700
Version history: 11 change(s)
Referenced in: [show references]