Skip to content
Browse files

Extracted out TimerContext.

  • Loading branch information...
1 parent d09080a commit 19057110fc9694abb6ccf98e7f20aa2f92c21861 @codahale codahale committed
View
31 metrics-core/src/main/java/com/yammer/metrics/core/TimerContext.java
@@ -0,0 +1,31 @@
+package com.yammer.metrics.core;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * A timing context.
+ *
+ * @see com.yammer.metrics.core.TimerMetric#time()
+ */
+public class TimerContext {
+ private final TimerMetric timer;
+ private final long startTime;
+
+ /**
+ * Creates a new {@link TimerContext} with the current time as its starting value and with the
+ * given {@link com.yammer.metrics.core.TimerMetric}.
+ *
+ * @param timer the {@link com.yammer.metrics.core.TimerMetric} to report the elapsed time to
+ */
+ TimerContext(TimerMetric timer) {
+ this.timer = timer;
+ this.startTime = System.nanoTime();
+ }
+
+ /**
+ * Stops recording the elapsed time and updates the timer.
+ */
+ public void stop() {
+ timer.update(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
+ }
+}
View
35 metrics-core/src/main/java/com/yammer/metrics/core/TimerMetric.java
@@ -16,33 +16,6 @@
* statistics, plus throughput statistics via {@link MeterMetric}.
*/
public class TimerMetric implements Metered {
- /**
- * A timing context.
- *
- * @see TimerMetric#time()
- */
- public static class Context {
- private final TimerMetric timer;
- private final long startTime;
-
- /**
- * Creates a new {@link Context} with the current time as its starting value and with the
- * given {@link TimerMetric}.
- *
- * @param timer the {@link TimerMetric} to report the elapsed time to
- */
- protected Context(TimerMetric timer) {
- this.timer = timer;
- this.startTime = System.nanoTime();
- }
-
- /**
- * Stops recording the elapsed time and updates the timer.
- */
- public void stop() {
- timer.update(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
- }
- }
private final TimeUnit durationUnit, rateUnit;
private final MeterMetric meter;
@@ -155,12 +128,12 @@ public void update(long duration, TimeUnit unit) {
}
/**
- * Returns a timing {@link Context}, which measures an elapsed time in nanoseconds.
+ * Returns a timing {@link TimerContext}, which measures an elapsed time in nanoseconds.
*
- * @return a new {@link Context}
+ * @return a new {@link TimerContext}
*/
- public Context time() {
- return new Context(this);
+ public TimerContext time() {
+ return new TimerContext(this);
}
@Override
View
16 metrics-java7/src/main/java/com/yammer/metrics/core/Timer.java
@@ -0,0 +1,16 @@
+package com.yammer.metrics.core;
+
+public class Timer extends TimerContext implements AutoCloseable {
+ public static Timer time(TimerMetric timer) {
+ return new Timer(timer);
+ }
+
+ public Timer(TimerMetric timer) {
+ super(timer);
+ }
+
+ @Override
+ public void close() {
+ stop();
+ }
+}
View
16 metrics-java7/src/main/java/com/yammer/metrics/core/TimerContext.java
@@ -1,16 +0,0 @@
-package com.yammer.metrics.core;
-
-public class TimerContext extends TimerMetric.Context implements AutoCloseable {
- public static TimerContext time(TimerMetric timer) {
- return new TimerContext(timer);
- }
-
- public TimerContext(TimerMetric timer) {
- super(timer);
- }
-
- @Override
- public void close() {
- stop();
- }
-}
View
10 .../metrics/core/tests/TimerContextTest.java → .../yammer/metrics/core/tests/TimerTest.java
@@ -1,7 +1,7 @@
package com.yammer.metrics.core.tests;
import com.yammer.metrics.core.MetricsRegistry;
-import com.yammer.metrics.core.TimerContext;
+import com.yammer.metrics.core.Timer;
import com.yammer.metrics.core.TimerMetric;
import org.junit.After;
import org.junit.Test;
@@ -9,12 +9,12 @@
import java.util.concurrent.TimeUnit;
import static org.hamcrest.Matchers.closeTo;
-import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
-public class TimerContextTest {
+public class TimerTest {
private final MetricsRegistry registry = new MetricsRegistry();
- private final TimerMetric timer = registry.newTimer(TimerContextTest.class,
+ private final TimerMetric timer = registry.newTimer(TimerTest.class,
"example",
TimeUnit.MILLISECONDS,
TimeUnit.SECONDS);
@@ -26,7 +26,7 @@ public void tearDown() throws Exception {
@Test
public void timesAnExecutionContext() throws Exception {
- try (TimerContext context = TimerContext.time(timer)) {
+ try (@SuppressWarnings("unused") Timer t = Timer.time(timer)) {
Thread.sleep(100);
}

0 comments on commit 1905711

Please sign in to comment.
Something went wrong with that request. Please try again.