Permalink
Browse files

Merge pull request #117 from chids/feature/make-clock-provide-time

Allow Clock to provide time in addition to ticks
  • Loading branch information...
2 parents edccd0b + 881b62c commit b41f46cbfe0976a80dbebb5259ffc59a8456a0d9 @codahale codahale committed Nov 18, 2011
Showing with 16 additions and 6 deletions.
  1. +16 −6 metrics-core/src/main/java/com/yammer/metrics/core/Clock.java
@@ -1,19 +1,29 @@
package com.yammer.metrics.core;
-import java.lang.management.ThreadMXBean;
import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadMXBean;
/**
* An abstraction for how time passes. It is passed to {@link TimerMetric} to
* track timing.
*/
-public interface Clock {
+public abstract class Clock {
+
/**
* Gets the current time tick
- *
+ *
* @return time tick in nanoseconds
*/
- long tick();
+ public abstract long tick();
+
+ /**
+ * Gets the current time in milliseconds
+ *
+ * @return time in milliseconds
+ */
+ public long time() {
+ return System.currentTimeMillis();
+ }
/**
* The default clock to use.
@@ -23,7 +33,7 @@
/**
* Default implementation, uses {@link System#nanoTime()}.
*/
- public static class UserTime implements Clock {
+ public static class UserTime extends Clock {
@Override
public long tick() {
return System.nanoTime();
@@ -33,7 +43,7 @@ public long tick() {
/**
* Another implementation, uses {@link ThreadMXBean#getCurrentThreadCpuTime()}
*/
- public static class CpuTime implements Clock {
+ public static class CpuTime extends Clock {
private static ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
@Override

0 comments on commit b41f46c

Please sign in to comment.