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

93
LINES

< > BotCompany Repo | #1016561 - ExpiringMap2 - now synchronized

JavaX fragment (include)

sclass ExpiringMap2<A, B> extends AbstractMap<A, B> {
  Map<A, Pair<Long, B>> byKey = new HashMap;      // key -> pair(expiry sys time, value)
  new PriorityBlockingQueue<Pair<Long, A>> queue; // queue(pair(expiry sys time, key))
  long standardExpiryTime; // ms
  bool renewOnOverwrite = true, renewOnGet;
  O onChange;
  // new RestartableCountdown countdown; // TODO
  
  *() {}
  *(long *standardExpiryTime) {}
  *(long *standardExpiryTime, O *onChange) {}
  
  synchronized bool clean() {
    bool changes = false;
    Pair<Long, A> p;
    while ((p = queue.peek()) != null && sysTime() >= p.a) {
      p = queue.poll();
      ifdef ExpiringMap2_debug
        print("ExpiringMap: retiring key " + p.b);
      endifdef
      Pair<Long, B> v = byKey.get(p.b);
      if (v != null /*&& v.a == p.a*/) {
        ifdef ExpiringMap2_debug
          print("ExpiringMap: retired key " + p.b);
        endifdef
        byKey.remove(p.b);
        changes = true;
        change();
      }
    }
    ret changes;
  }
  
  void change() { callF(onChange); }
  
  synchronized public B put(A a, B b) {
    clean();
    long timeout = sysTime()+standardExpiryTime;
    Pair<Long, B> p = byKey.get(a);
    if (p != null && renewOnOverwrite)
      queue.remove(Pair(p.a, a));
    byKey.put(a, pair(timeout, b));
    change();
    if (p == null || renewOnOverwrite)
      queue.add(Pair(timeout, a));
    ret pairB(p);
  }
  
  synchronized public B remove(O a) {
    clean();
    Pair<Long, B> p = byKey.get(a);
    if (p == null) null;
    queue.remove(Pair(p.a, a));
    byKey.remove(a);
    change();
    ret p.b;
  }
  
  synchronized public B get(O a) {
    clean();
    Pair<Long, B> p = byKey.get(a);
    if (renewOnGet && p != null) {
      queue.remove(Pair(p.a, a));
      long timeout = sysTime()+standardExpiryTime;
      byKey.put((A) a, pair(timeout, p.b));
      queue.add(Pair(timeout, a));
    }
    ret pairB(p);
  }
  
  synchronized public Set<Map.Entry<A,B>> entrySet() {
    clean();
    // TODO: mutex
    ret synchronizedSet(mapValues(f pairB, byKey).entrySet());
  }
  
  synchronized public Set<A> keySet() {
    clean();
    ret synchronizedSet(byKey.keySet());
  }
  
  synchronized public int size() {
    clean();
    ret byKey.size();
  }
  
  void setStandardExpiryTime(long ms) { standardExpiryTime = ms; }
  
  synchronized ExpiringMap2<A, B> setMap(Map innerMap) {
    byKey = innerMap;
    this;
  }
}

download  show line numbers  debug dex   

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

No comments. add comment

Snippet ID: #1016561
Snippet name: ExpiringMap2 - now synchronized
Eternal ID of this version: #1016561/32
Text MD5: 61156e29d15cedbef03c0fffc84b558a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-12-11 13:56:15
Source code size: 2517 bytes / 93 lines
Pitched / IR pitched: No / No
Views / Downloads: 232 / 667
Version history: 31 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1016561 & http://1016561.tinybrain.de