19
19
import java .util .concurrent .TimeUnit ;
20
20
21
21
/**
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.
25
30
*/
26
31
@ ThreadSafe
27
32
public abstract class BlazeClock {
28
33
29
- private BlazeClock () {
30
- }
34
+ private BlazeClock () {}
31
35
32
36
private static volatile Clock instance = new JavaClock ();
33
37
34
- /**
35
- * Returns singleton instance of the clock
36
- */
38
+ /** Returns singleton instance of the clock */
37
39
public static Clock instance () {
38
40
return instance ;
39
41
}
40
42
41
- /**
42
- * Overrides default clock instance.
43
- */
43
+ /** Overrides default clock instance. */
44
44
public static synchronized void setClock (Clock clock ) {
45
45
instance = clock ;
46
46
}
@@ -49,34 +49,51 @@ public static long nanoTime() {
49
49
return instance ().nanoTime ();
50
50
}
51
51
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. */
57
53
public interface NanosToMillisSinceEpochConverter {
54
+
58
55
/** Converts from nanos to millis since the epoch. */
59
56
long toEpochMillis (long timeNanos );
60
57
}
61
58
62
59
/**
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.
66
62
*/
67
63
public static NanosToMillisSinceEpochConverter createNanosToMillisSinceEpochConverter () {
68
64
return createNanosToMillisSinceEpochConverter (instance );
69
65
}
70
66
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
+
71
83
/**
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 .
74
86
*/
87
+ public static MillisSinceEpochToNanosConverter createMillisSinceEpochToNanosConverter () {
88
+ return createMillisSinceEpochToNanosConverter (instance );
89
+ }
90
+
91
+ /** Creates a {@link MillisSinceEpochToNanosConverter} from the given {@link Clock}. */
75
92
@ VisibleForTesting
76
- public static NanosToMillisSinceEpochConverter createNanosToMillisSinceEpochConverter (
93
+ public static MillisSinceEpochToNanosConverter createMillisSinceEpochToNanosConverter (
77
94
Clock clock ) {
78
95
long nowInMillis = clock .currentTimeMillis ();
79
96
long nowInNanos = clock .nanoTime ();
80
- return (timeNanos ) -> nowInMillis - TimeUnit .NANOSECONDS . toMillis (( nowInNanos - timeNanos ) );
97
+ return (timeMillis ) -> nowInNanos - TimeUnit .MILLISECONDS . toNanos ( nowInMillis - timeMillis );
81
98
}
82
99
}
0 commit comments