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

45
LINES

< > BotCompany Repo | #1015716 // DeferredComputation

JavaX fragment (include)

1  
sclass DeferredComputation {
2  
  new Flag done;
3  
  O value;
4  
  Throwable error;
5  
  Runnable continuation, onError;
6  
7  
  void setContinuation(Runnable r) {
8  
    synchronized(done) {
9  
      if (!done.isUp()) ret with continuation = r;
10  
    }
11  
    if (error == null)
12  
      callF(r);
13  
    else
14  
      if (onError != null)
15  
        callF(onError);
16  
      else
17  
        printStackTrace(error);
18  
  }
19  
20  
  void setValue(O value) {
21  
    synchronized(done) {
22  
      if (done.isUp()) fail("setValue/setError called twice");
23  
      this.value = value;
24  
      done.raise();
25  
    }
26  
    callF(continuation);
27  
  }
28  
29  
  void setError(Throwable error) {
30  
    synchronized(done) {
31  
      if (done.isUp()) fail("setValue/setError called twice");
32  
      this.error = error;
33  
      done.raise();
34  
    }
35  
    if (onError != null)
36  
      callF(onError);
37  
    else
38  
      printStackTrace(error);
39  
  }
40  
41  
  O getValue() {
42  
    assertTrue(done.isUp());
43  
    ret value;
44  
  }
45  
}

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: 350 / 915
Referenced in: [show references]