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).

sclass Schedule<A extends Runnable> extends Meta {
  sclass Entry {}
  srecord Action<A>(A action) extends Entry {}
  srecord Jump (Seconds dest) extends Entry {}

  new TreeMultiMap<Seconds, Entry> entries;
  
  Timestamp start;
  TreeMultiMap<Seconds, Entry> currentEntries;

  void add(double seconds, A a) {
    entries.put(Seconds(seconds), new Action<A>(a));
  }
  
  void jumpFromTo(double from, double to) {
    entries.put(Seconds(from), new Jump(Seconds(to)));
  }
  
  void speedUp(double factor) {
    entries = (TreeMultiMap) mapMultiMapKeys(entries, seconds -> seconds.div(factor));
  }
  
  run {
    start = tsNow();
    reschedule(Seconds(0));
    while ping (nempty(currentEntries))
      fire(first(currentEntries));
  }
  
  void reschedule(Seconds startInSchedule) {
    currentEntries = (TreeMultiMap) cloneTailMultiMap(entries, startInSchedule, true);
    /*if (scaffoldActive(this))
      printStruct(this);*/
  }
  
  scaffolded void fire(Pair<Seconds, Entry> p) {
    if (p == null) ret;
    currentEntries.removePair(p);
    sleepUntil(start.plus(p.a));
    var e = p.b;
    if (e cast Action<A>)
      pcallF(e.action);
    else if (e cast Jump) {
      start = start.plus(p.a.minus(e.dest));
      reschedule(e.dest);
    } else
      fail(e);
  }
}

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: 218 / 416
Version history: 28 change(s)
Referenced in: [show references]