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

67
LINES

< > BotCompany Repo | #1002872 // PersistentCIMap

JavaX fragment (include)

// a persistent map using a clever combination of persisting and logging
// and with case-insensitive keys
// (well, only logging as of now)
// Note: don't put in static initializer (programID not set yet)
static class PersistentCIMap<B> extends AbstractMap<S, B> {
  new Map<S, B> m;
  File file;
  
  PersistentCIMap(S fileName) {
    this(getProgramFile(fileName));
  }
  
  PersistentCIMap(S progID, S fileName) {
    this(getProgramFile(progID, fileName));
  }
  
  *(File *file) {
    for (S s : scanLog(file)) pcall {
      L l = cast unstructure(s);
      if (eq(l.get(0), "add"))
        m.put(toLowerCase((S) l.get(1)), (B) l.get(2));
      else if (eq(l.get(0), "remove"))
        m.remove(toLowerCase((S) l.get(1)));
      else
        print("Unknown command in log: " + l.get(0));
    }
  }
  
  // just delegates
  
  public int size() {
    ret m.size();
  }
  
  public B get(O a) {
    ret m.get(toLowerCase((S) a));
  }
  
  public boolean containsKey(O a) {
    ret m.containsKey(toLowerCase((S) a));
  }
  
  // TODO: calling remove() in the iterator will have unpersisted
  // effects.
  public Set<Map.Entry<S, B>> entrySet() {
    ret m.entrySet();
  }
  
  // delegates with logging

  public B put(S a, B b) {
    a = toLowerCase(a);
    B c = m.get(a);
    if (neq(b, c)) {
      m.put(a, b);
      logQuoted(file, structure(litlist("add", a, b)));
    }
    ret c;
  }
  
  public B remove(O a) {
    a = toLowerCase((S) a);
    B result = m.remove(a);
    logQuoted(file, structure(litlist("remove", a)));
    ret result;
  }
}

Author comment

Began life as a copy of #1002063

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002872
Snippet name: PersistentCIMap
Eternal ID of this version: #1002872/1
Text MD5: bca8a24cba7d14bd7b34b87cacec29f4
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-03-10 19:01:43
Source code size: 1621 bytes / 67 lines
Pitched / IR pitched: No / No
Views / Downloads: 563 / 1543
Referenced in: #1003872 - Integrating #759 in One Program
#1003874 - Backup of #759 Before Integration
#1004091 - 759 with new loadClasses (spike)
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)