// Drives the real Guava RateLimiter with a fake clock on the traces written by
// `python3 rl_sim.py --dump-trace traces.txt` and prints 1/0 per arrival.
// Lives in Guava's package to reach the package-private SleepingStopwatch and
// SmoothBursty constructor (classpath only, not the module path).
package com.google.common.util.concurrent;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.StringJoiner;
import java.util.concurrent.TimeUnit;

public class GuavaCheck {
  static final class FakeStopwatch extends RateLimiter.SleepingStopwatch {
    long now;

    @Override
    protected long readMicros() {
      return now;
    }

    @Override
    protected void sleepMicrosUninterruptibly(long micros) {
      now += micros;
    }
  }

  public static void main(String[] args) throws Exception {
    List<String> lines = Files.readAllLines(Paths.get(args[0]));
    for (int i = 0; i < lines.size(); i++) {
      FakeStopwatch clock = new FakeStopwatch();
      RateLimiter limiter;
      if (i == 0) {
        limiter = RateLimiter.create(10.0, clock);        // public create(10.0)
      } else {
        limiter = new SmoothRateLimiter.SmoothBursty(clock, 0.9);
        limiter.setRate(10.0);
      }
      StringJoiner out = new StringJoiner(" ");
      for (String tok : lines.get(i).trim().split(" ")) {
        clock.now = Long.parseLong(tok);
        out.add(limiter.tryAcquire(1, 0, TimeUnit.MICROSECONDS) ? "1" : "0");
      }
      System.out.println(out);
    }
  }
}
