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

49
LINES

< > BotCompany Repo | #1033319 // Schedule

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

Libraryless. Click here for Pure Java version (5749L/32K).

1  
sclass Schedule<A extends Runnable> extends Meta {
2  
  sclass Entry {}
3  
  srecord Action<A>(A action) extends Entry {}
4  
  srecord Jump (Seconds dest) extends Entry {}
5  
6  
  new TreeMultiMap<Seconds, Entry> entries;
7  
  
8  
  Timestamp start;
9  
  TreeMultiMap<Seconds, Entry> currentEntries;
10  
11  
  void add(double seconds, A a) {
12  
    entries.put(Seconds(seconds), new Action<A>(a));
13  
  }
14  
  
15  
  void jumpFromTo(double from, double to) {
16  
    entries.put(Seconds(from), new Jump(Seconds(to)));
17  
  }
18  
  
19  
  void speedUp(double factor) {
20  
    entries = (TreeMultiMap) mapMultiMapKeys(entries, seconds -> seconds.div(factor));
21  
  }
22  
  
23  
  run {
24  
    start = tsNow();
25  
    reschedule(Seconds(0));
26  
    while ping (nempty(currentEntries))
27  
      fire(first(currentEntries));
28  
  }
29  
  
30  
  void reschedule(Seconds startInSchedule) {
31  
    currentEntries = (TreeMultiMap) cloneTailMultiMap(entries, startInSchedule, true);
32  
    /*if (scaffoldActive(this))
33  
      printStruct(this);*/
34  
  }
35  
  
36  
  scaffolded void fire(Pair<Seconds, Entry> p) {
37  
    if (p == null) ret;
38  
    currentEntries.removePair(p);
39  
    sleepUntil(start.plus(p.a));
40  
    var e = p.b;
41  
    if (e cast Action<A>)
42  
      pcallF(e.action);
43  
    else if (e cast Jump) {
44  
      start = start.plus(p.a.minus(e.dest));
45  
      reschedule(e.dest);
46  
    } else
47  
      fail(e);
48  
  }
49  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033319
Snippet name: Schedule
Eternal ID of this version: #1033319/29
Text MD5: 012d7cc2107be36c040fb9a98afa6419
Transpilation MD5: 599ea699e2b2bf779b2e7cf585c7477f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-23 07:38:56
Source code size: 1326 bytes / 49 lines
Pitched / IR pitched: No / No
Views / Downloads: 219 / 417
Version history: 28 change(s)
Referenced in: [show references]