Class B2Clock

  • Direct Known Subclasses:
    B2ClockImpl, B2ClockSim

    public abstract class B2Clock
    extends java.lang.Object
    This class provides access to two different time measurements. It also provides access to the global clock object. It's an object instead of static methods so we can use a different implementation in tests. It is an abstract class instead of an interface so that it can hold the global instance. (If we had a dependency injection system we would probably use that instead of a global, but we don't have one.) I also keep thinking that maybe there should be two different types of clock objects -- one for monotonic time and one for wall clock time. So far, I'm keeping them together because it helps to advance them both in tests and every call to get a value says what type of time it is.
    • Constructor Detail

      • B2Clock

        public B2Clock()
    • Method Detail

      • useSimulator

        public static B2ClockSim useSimulator​(java.time.LocalDateTime desiredNow)
        If theClock is null, this will create a B2ClockSim with the desiredNow. If theClock is a B2ClockSim, this will set it to the specified time. If theclock is not a B2ClockSim, this will throw.
        Parameters:
        desiredNow - is the wall clock time for the simulator's "now".
        Returns:
        theClock
      • get

        public static B2Clock get()
        Returns:
        theClock to use.
      • monotonicMillis

        public abstract long monotonicMillis()
        Returns:
        a monotonically increasing number of milliseconds. note that it has no relationship to wall clock time and will differ between different runs of the JVM. It is monotonically increasing! AND it won't wrap during any single run of a JVM. (unless the JVM runs for hundreds of of years! (2^63 nanos) / (10^9 nanos/sec) / (86400 secs/day) / (365 days/year) ~= 292 years)
      • wallClockMillis

        public abstract long wallClockMillis()
        Returns:
        the number of milliseconds since the unix epoch. may be negative if now is before the epoch. the values returned by this represent wall clock time and might not be monotonic if the system clock is changed. time might sometimes flow at a variable speed or even backwards if the clock is being adjusted.