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

33
LINES

< > BotCompany Repo | #1019579 // OnStableValue

JavaX fragment (include)

sclass OnStableValue<A> extends WaitForStableValue<A> {
  long period;
  A value;
  long valueSince;
  O runnable;
  A lastStableValue;
  
  *() {}
  *(long *period, O *runnable) {}
  *(double seconds, O *runnable) { period = toMS(seconds); }
  
  synchronized void set(A a) {
    if (neq(a, value)) {
      value = a;
      valueSince = sysNow();
      doLater(period, r check); // TODO: slightly dicey
    }
  }
  
  synchronized void check() {
    A a = get();
    if (neq(a, lastStableValue)) {
      lastStableValue = a;
      callF(runnable, a);
    }
  }
  
  synchronized A get() {
    ret valueSince != 0 && sysNow() >= valueSince+period ? value : null;
  }
  
  public bool has() { ret get() != null; }
}

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: 258 / 792
Version history: 3 change(s)
Referenced in: [show references]