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

34
LINES

< > BotCompany Repo | #1015658 // PastValueKeeper

JavaX fragment (include)

sclass PastValueKeeper<A> {
  long forgetAfter;
  Lock lock = lock();
  L<Pair<A, Long>> values = synchroList();
  
  *() {}
  *(double seconds) { forgetAfter = toMS(seconds); }
  
  void add(A a) {
    lock lock;
    if (nempty(values) && eq(last(values).a, a)) ret;
    clean();
    values.add(pair(a, machineTime()));
  }
  
  A valueAt_orFirst(long timeBack) {
    lock lock;
    clean();
    long time = machineTime()-timeBack;
    int i = 0;
    while (i < l(values) && values.get(i).b < time)
      ++i;
    ret pairA(get(values, max(i-1, 0)));
  }
  
  void clean {
    lock lock;
    long time = machineTime()-forgetAfter;
    int i = 0;
    while (i < l(values) && values.get(i).b < time)
      ++i;
    removeSubList(values, 0, i);
  }
}

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: #1015658
Snippet name: PastValueKeeper
Eternal ID of this version: #1015658/6
Text MD5: b4cf3e5d108720b50cf1090106376d8e
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-20 19:30:55
Source code size: 781 bytes / 34 lines
Pitched / IR pitched: No / No
Views / Downloads: 395 / 962
Version history: 5 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)