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

34
LINES

< > BotCompany Repo | #1015658 // PastValueKeeper

JavaX fragment (include)

1  
sclass PastValueKeeper<A> {
2  
  long forgetAfter;
3  
  Lock lock = lock();
4  
  L<Pair<A, Long>> values = synchroList();
5  
  
6  
  *() {}
7  
  *(double seconds) { forgetAfter = toMS(seconds); }
8  
  
9  
  void add(A a) {
10  
    lock lock;
11  
    if (nempty(values) && eq(last(values).a, a)) ret;
12  
    clean();
13  
    values.add(pair(a, machineTime()));
14  
  }
15  
  
16  
  A valueAt_orFirst(long timeBack) {
17  
    lock lock;
18  
    clean();
19  
    long time = machineTime()-timeBack;
20  
    int i = 0;
21  
    while (i < l(values) && values.get(i).b < time)
22  
      ++i;
23  
    ret pairA(get(values, max(i-1, 0)));
24  
  }
25  
  
26  
  void clean {
27  
    lock lock;
28  
    long time = machineTime()-forgetAfter;
29  
    int i = 0;
30  
    while (i < l(values) && values.get(i).b < time)
31  
      ++i;
32  
    removeSubList(values, 0, i);
33  
  }
34  
}

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: 396 / 964
Version history: 5 change(s)
Referenced in: [show references]