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

32
LINES

< > BotCompany Repo | #1015867 // WaitForStableValue

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

Libraryless. Click here for Pure Java version (2178L/14K).

1  
sclass WaitForStableValue<A> {
2  
  long period;
3  
  A value;
4  
  long valueSince;
5  
  
6  
  *() {}
7  
  *(long *period) {}
8  
  *(double seconds) { period = toMS(seconds); }
9  
  
10  
  synchronized A set(A a) {
11  
    if (neq(a, value)) {
12  
      value = a;
13  
      valueSince = sysNow();
14  
    }
15  
    ret get();
16  
  }
17  
  
18  
  synchronized A get() {
19  
    ret valueSince != 0 && sysNow() >= valueSince+period ? value : null;
20  
  }
21  
  
22  
  // clears only if result != null
23  
  synchronized A setAndClear(A _a) {
24  
    A a = set(_a);
25  
    if (a != null) clear();
26  
    ret a;
27  
  }
28  
  
29  
  public bool has() { ret get() != null; }
30  
  
31  
  void clear() { set(null); }
32  
}

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: #1015867
Snippet name: WaitForStableValue
Eternal ID of this version: #1015867/10
Text MD5: 4a2f2833a3082543bb64d1c8470f5d83
Transpilation MD5: 09ef5f028d4bc0ea2d298b033edc7915
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-09-01 19:06:50
Source code size: 637 bytes / 32 lines
Pitched / IR pitched: No / No
Views / Downloads: 289 / 917
Version history: 9 change(s)
Referenced in: [show references]