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

36
LINES

< > BotCompany Repo | #1024132 // syncGetOrCreate2 - map lookup or create from class, return whether key is new

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

Libraryless. Click here for Pure Java version (2121L/14K).

static <A, B> Pair<B, Bool> syncGetOrCreate2(Map<A, B> map, A key, Class<? extends B> c) ctex {
  synchronized(map) {
    B b = map.get(key);
    if (b == null) {
      map.put(key, b = c.newInstance());
      ret pair(b, true);
    }
    ret pair(b, false);
  }
}

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

static <A, B> Pair<B, Bool> syncGetOrCreate2(Map<A, B> map, A key, F0<B> f) ctex {
  synchronized(map) {
    B b = map.get(key);
    if (b == null) {
      map.put(key, b = callF(f));
      ret pair(b, true);
    }
    ret pair(b, false);
  }
}

static <A, B> Pair<B, Bool> syncGetOrCreate2(Map<A, B> map, A key, IF0<B> f) ctex {
  synchronized(map) {
    B b = map.get(key);
    if (b == null) {
      map.put(key, b = f!);
      ret pair(b, true);
    }
    ret pair(b, false);
  }
}

Author comment

Began life as a copy of #1013935

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1024132
Snippet name: syncGetOrCreate2 - map lookup or create from class, return whether key is new
Eternal ID of this version: #1024132/4
Text MD5: 63323f5d15568fd073aa3e5c52f674a9
Transpilation MD5: f6f1e8d270ff17c4f6801c3d0455ae01
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-09-08 15:47:33
Source code size: 926 bytes / 36 lines
Pitched / IR pitched: No / No
Views / Downloads: 182 / 265
Version history: 3 change(s)
Referenced in: [show references]