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

57
LINES

< > BotCompany Repo | #1015851 // FileBasedStringMap (OK)

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

Libraryless. Click here for Pure Java version (4136L/24K).

1  
sclass FileBasedStringMap extends AbstractMap<S> {
2  
  File dir;
3  
  bool compress; // use .gz
4  
  
5  
  *() {}
6  
  *(File *dir) {}
7  
  
8  
  public S get(O a) {
9  
    File f = fileForKey(a);
10  
    ret dropFirstLine(compress ? loadGZTextFile(f) : loadTextFile(f));
11  
  }
12  
  
13  
  public synchronized Set<Map.Entry<S,S>> entrySet() {
14  
    Set<Map.Entry<S,S>> set = new HashSet;
15  
    for (File f : listFiles(dir)) {
16  
      S md5 = md5FromUniqueFile(f);
17  
      if (md5 == null) continue;
18  
      fS key = unquote(compress ? firstLineFromGZTextFile(f) : firstLineFromFile(f));
19  
      set.add(new Map.Entry<S,S>() {
20  
        public S getKey() { ret key; }
21  
        public S getValue() { ret get(key); }
22  
        public S setValue(S s) { S old = get(key); put(key, s); ret old; }
23  
        public bool equals(O o) {
24  
          if (!(o instanceof Map.Entry)) false;
25  
          Map.Entry e = (Map.Entry) o;
26  
          ret eq(getKey(), e.getKey()) && eq(getValue(), e.getValue());
27  
        }
28  
        public int hashCode() {
29  
          S s = getValue();
30  
          ret (getKey()==null ? 0 : getKey().hashCode())
31  
            ^ (s == null ? 0 : s.hashCode());
32  
        }
33  
      });
34  
    }
35  
    ret set;
36  
  }
37  
  
38  
  public S put(S a, S b) {
39  
    S old = get(a);
40  
    S text = b == null ? null : quote(a) + "\n" + b;
41  
    if (compress)
42  
      saveGZTextFile(fileForKey(a), text);
43  
    else
44  
      saveTextFile(fileForKey(a), text);
45  
    ret old;
46  
  }
47  
  
48  
  public S remove(O a) {
49  
    if (!isString(a)) null;
50  
    ret put((S) a, null);
51  
  }
52  
53  
  File fileForKey(O key) {
54  
    if (!isString(key)) null;
55  
    ret new File(assertNotNull(dir), uniqueFileNameUsingMD5_80((S) key) + (compress ? ".gz" : ".txt"));
56  
  }
57  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1015851
Snippet name: FileBasedStringMap (OK)
Eternal ID of this version: #1015851/19
Text MD5: 3de2c02edade5345fd8c2591a2858d02
Transpilation MD5: 7ab041e089a5b3d75cbdaa326b1e8563
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-08-27 03:46:18
Source code size: 1687 bytes / 57 lines
Pitched / IR pitched: No / No
Views / Downloads: 399 / 1023
Version history: 18 change(s)
Referenced in: [show references]