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

45
LINES

< > BotCompany Repo | #1015716 // DeferredComputation

JavaX fragment (include)

sclass DeferredComputation {
  new Flag done;
  O value;
  Throwable error;
  Runnable continuation, onError;

  void setContinuation(Runnable r) {
    synchronized(done) {
      if (!done.isUp()) ret with continuation = r;
    }
    if (error == null)
      callF(r);
    else
      if (onError != null)
        callF(onError);
      else
        printStackTrace(error);
  }

  void setValue(O value) {
    synchronized(done) {
      if (done.isUp()) fail("setValue/setError called twice");
      this.value = value;
      done.raise();
    }
    callF(continuation);
  }

  void setError(Throwable error) {
    synchronized(done) {
      if (done.isUp()) fail("setValue/setError called twice");
      this.error = error;
      done.raise();
    }
    if (onError != null)
      callF(onError);
    else
      printStackTrace(error);
  }

  O getValue() {
    assertTrue(done.isUp());
    ret value;
  }
}

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: #1015716
Snippet name: DeferredComputation
Eternal ID of this version: #1015716/1
Text MD5: 729e466ae82b8757a7408509933ceb03
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-24 17:49:27
Source code size: 906 bytes / 45 lines
Pitched / IR pitched: No / No
Views / Downloads: 420 / 988
Referenced in: [show references]