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

sclass FileBasedStringMap extends AbstractMap<S> {
  File dir;
  bool compress; // use .gz
  
  *() {}
  *(File *dir) {}
  
  public S get(O a) {
    File f = fileForKey(a);
    ret dropFirstLine(compress ? loadGZTextFile(f) : loadTextFile(f));
  }
  
  public synchronized Set<Map.Entry<S,S>> entrySet() {
    Set<Map.Entry<S,S>> set = new HashSet;
    for (File f : listFiles(dir)) {
      S md5 = md5FromUniqueFile(f);
      if (md5 == null) continue;
      fS key = unquote(compress ? firstLineFromGZTextFile(f) : firstLineFromFile(f));
      set.add(new Map.Entry<S,S>() {
        public S getKey() { ret key; }
        public S getValue() { ret get(key); }
        public S setValue(S s) { S old = get(key); put(key, s); ret old; }
        public bool equals(O o) {
          if (!(o instanceof Map.Entry)) false;
          Map.Entry e = (Map.Entry) o;
          ret eq(getKey(), e.getKey()) && eq(getValue(), e.getValue());
        }
        public int hashCode() {
          S s = getValue();
          ret (getKey()==null ? 0 : getKey().hashCode())
            ^ (s == null ? 0 : s.hashCode());
        }
      });
    }
    ret set;
  }
  
  public S put(S a, S b) {
    S old = get(a);
    S text = b == null ? null : quote(a) + "\n" + b;
    if (compress)
      saveGZTextFile(fileForKey(a), text);
    else
      saveTextFile(fileForKey(a), text);
    ret old;
  }
  
  public S remove(O a) {
    if (!isString(a)) null;
    ret put((S) a, null);
  }

  File fileForKey(O key) {
    if (!isString(key)) null;
    ret new File(assertNotNull(dir), uniqueFileNameUsingMD5_80((S) key) + (compress ? ".gz" : ".txt"));
  }
}

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: 400 / 1023
Version history: 18 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)