1919import java .util .concurrent .TimeUnit ;
2020
2121/**
22- * Provides the clock implementation used by Blaze, which is {@link JavaClock}
23- * by default, but can be overridden at runtime. Note that if you set this
24- * clock, you also have to set the clock used by the Profiler.
22+ * Provides the clock implementation used by Blaze, which is {@link JavaClock} by default, but can
23+ * be overridden at runtime. If you set this clock, you also have to set the clock used by the
24+ * Profiler.
25+ *
26+ * <p>Note that clock readings are relative to an unspecified reference time, so returned values are
27+ * only meaningful when compared to each other. A {@link NanosToMillisSinceEpochConverter} or {@link
28+ * MillisSinceEpochToNanosConverter} may be used to convert clock readings into milliseconds since
29+ * the epoch or vice-versa.
2530 */
2631@ ThreadSafe
2732public abstract class BlazeClock {
2833
29- private BlazeClock () {
30- }
34+ private BlazeClock () {}
3135
3236 private static volatile Clock instance = new JavaClock ();
3337
34- /**
35- * Returns singleton instance of the clock
36- */
38+ /** Returns singleton instance of the clock */
3739 public static Clock instance () {
3840 return instance ;
3941 }
4042
41- /**
42- * Overrides default clock instance.
43- */
43+ /** Overrides default clock instance. */
4444 public static synchronized void setClock (Clock clock ) {
4545 instance = clock ;
4646 }
@@ -49,34 +49,51 @@ public static long nanoTime() {
4949 return instance ().nanoTime ();
5050 }
5151
52- /**
53- * Converts from nanos to millis since the epoch. In particular, note that {@link System#nanoTime}
54- * does not specify any particular time reference but only notes that returned values are only
55- * meaningful when taking in relation to each other.
56- */
52+ /** Converts from nanos to millis since the epoch. */
5753 public interface NanosToMillisSinceEpochConverter {
54+
5855 /** Converts from nanos to millis since the epoch. */
5956 long toEpochMillis (long timeNanos );
6057 }
6158
6259 /**
63- * Creates a {@link NanosToMillisSinceEpochConverter} from the current BlazeClock instance by
64- * taking the current time in millis and the current time in nanos to compute the appropriate
65- * offset.
60+ * Creates a {@link NanosToMillisSinceEpochConverter} from the current {@link BlazeClock}
61+ * instance.
6662 */
6763 public static NanosToMillisSinceEpochConverter createNanosToMillisSinceEpochConverter () {
6864 return createNanosToMillisSinceEpochConverter (instance );
6965 }
7066
67+ /** Creates a {@link NanosToMillisSinceEpochConverter} from the given {@link Clock}. */
68+ @ VisibleForTesting
69+ public static NanosToMillisSinceEpochConverter createNanosToMillisSinceEpochConverter (
70+ Clock clock ) {
71+ long nowInMillis = clock .currentTimeMillis ();
72+ long nowInNanos = clock .nanoTime ();
73+ return (timeNanos ) -> nowInMillis - TimeUnit .NANOSECONDS .toMillis (nowInNanos - timeNanos );
74+ }
75+
76+ /** Converts from millis since the epoch to nanos. */
77+ public interface MillisSinceEpochToNanosConverter {
78+
79+ /** Converts from millis since the epoch to nanos. */
80+ long toNanos (long timeMillis );
81+ }
82+
7183 /**
72- * Creates a {@link NanosToMillisSinceEpochConverter} from clock by taking the current time in
73- * millis and the current time in nanos to compute the appropriate offset .
84+ * Creates a {@link NanosToMillisSinceEpochConverter} from the current {@link BlazeClock}
85+ * instance .
7486 */
87+ public static MillisSinceEpochToNanosConverter createMillisSinceEpochToNanosConverter () {
88+ return createMillisSinceEpochToNanosConverter (instance );
89+ }
90+
91+ /** Creates a {@link MillisSinceEpochToNanosConverter} from the given {@link Clock}. */
7592 @ VisibleForTesting
76- public static NanosToMillisSinceEpochConverter createNanosToMillisSinceEpochConverter (
93+ public static MillisSinceEpochToNanosConverter createMillisSinceEpochToNanosConverter (
7794 Clock clock ) {
7895 long nowInMillis = clock .currentTimeMillis ();
7996 long nowInNanos = clock .nanoTime ();
80- return (timeNanos ) -> nowInMillis - TimeUnit .NANOSECONDS . toMillis (( nowInNanos - timeNanos ) );
97+ return (timeMillis ) -> nowInNanos - TimeUnit .MILLISECONDS . toNanos ( nowInMillis - timeMillis );
8198 }
8299}
0 commit comments