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

27
LINES

< > BotCompany Repo | #1031861 // BigLittleMapPolicy - uses two different map classes for small and big maps respectively

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

Libraryless. Click here for Pure Java version (3167L/18K).

1  
// threshold must be at least 2
2  
// not synchronized
3  
// untested
4  
srecord BigLittleMapPolicy(Class<? extends Map> smallClass, Class<? extends Map> bigClass, int threshold) implements IMapImplementationPolicy {
5  
  bool nullOnEmpty = false;
6  
  
7  
  public <A, B> Map<A, B> makeSingletonMap(A key, B value) {
8  
    ret put(null, key, value);
9  
  }
10  
  
11  
  public <A, B> Map<A, B> put(Map<A, B> map, A key, B value) {
12  
    if (map == null) map = newInstance(smallClass);
13  
    if (l(map) == threshold-1 && !map.containsKey(key))
14  
      map = putAll(newInstance(bigClass), map);
15  
    map.put(key, value);
16  
    ret map;
17  
  }
18  
  
19  
  public <A, B> Map<A, B> remove(Map<A, B> map, O key) {
20  
    if (map == null || !map.containsKey(key)) ret map;
21  
    map.remove(key);
22  
    int n = l(map);
23  
    if (n == threshold-1)
24  
      map = putAll(newInstance(smallClass), map);
25  
    ret nullOnEmpty && n == 0 ? null : map;
26  
  }
27  
}

Author comment

Began life as a copy of #1031860

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1031861
Snippet name: BigLittleMapPolicy - uses two different map classes for small and big maps respectively
Eternal ID of this version: #1031861/6
Text MD5: 30961b7883a186ca8bca57c435a31b32
Transpilation MD5: dcf698d285a16ad354b3a5be4c5766d3
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-07-16 07:08:24
Source code size: 906 bytes / 27 lines
Pitched / IR pitched: No / No
Views / Downloads: 97 / 220
Version history: 5 change(s)
Referenced in: [show references]