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

33
LINES

< > BotCompany Repo | #1019579 // OnStableValue

JavaX fragment (include)

1  
sclass OnStableValue<A> extends WaitForStableValue<A> {
2  
  long period;
3  
  A value;
4  
  long valueSince;
5  
  O runnable;
6  
  A lastStableValue;
7  
  
8  
  *() {}
9  
  *(long *period, O *runnable) {}
10  
  *(double seconds, O *runnable) { period = toMS(seconds); }
11  
  
12  
  synchronized void set(A a) {
13  
    if (neq(a, value)) {
14  
      value = a;
15  
      valueSince = sysNow();
16  
      doLater(period, r check); // TODO: slightly dicey
17  
    }
18  
  }
19  
  
20  
  synchronized void check() {
21  
    A a = get();
22  
    if (neq(a, lastStableValue)) {
23  
      lastStableValue = a;
24  
      callF(runnable, a);
25  
    }
26  
  }
27  
  
28  
  synchronized A get() {
29  
    ret valueSince != 0 && sysNow() >= valueSince+period ? value : null;
30  
  }
31  
  
32  
  public bool has() { ret get() != null; }
33  
}

Author comment

Began life as a copy of #1015867

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1019579
Snippet name: OnStableValue
Eternal ID of this version: #1019579/4
Text MD5: e5a3c294e308825ed5038fd977280cd2
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-11-12 20:12:09
Source code size: 746 bytes / 33 lines
Pitched / IR pitched: No / No
Views / Downloads: 260 / 795
Version history: 3 change(s)
Referenced in: [show references]