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

100
LINES

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

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

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

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;
  bool useCountdown = true;
  new RestartableCountdown countdown;

  *() {}
  *(long *standardExpiryTime) {}
  *(long *standardExpiryTime, O *onChange) {}
  *(double standardExpirySeconds) { standardExpiryTime = toMS(standardExpirySeconds); }
  
  synchronized bool clean() {
    bool changes;
    Pair<Long, A> p;
    long time = sysTime();
    while ((p = queue.peek()) != null && time >= p.a) {
      if (useCountdown) countdown.stop();
      
      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);
        set changes;
        change();
      }
    }
    if (useCountdown) 
      countdown.setTargetTime(p != null ? p.a : 0, r clean);
    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 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  old transpilations   

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

No comments. add comment

Snippet ID: #1016561
Snippet name: ExpiringMap2 - now synchronized
Eternal ID of this version: #1016561/40
Text MD5: 90af88bccf36d4a1fef32af34289547a
Transpilation MD5: c0f26d4b963f896008e16074476a6514
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-14 22:48:59
Source code size: 2768 bytes / 100 lines
Pitched / IR pitched: No / No
Views / Downloads: 399 / 1114
Version history: 39 change(s)
Referenced in: [show references]