Skip to content

Commit

Permalink
Revert "Add possibility to subclass Timer.Context (#1226)" (#1244)
Browse files Browse the repository at this point in the history
This reverts commit 1bacf4d.
  • Loading branch information
arteam committed Jan 3, 2018
1 parent d6f97b3 commit 76af224
Showing 1 changed file with 17 additions and 28 deletions.
45 changes: 17 additions & 28 deletions metrics-core/src/main/java/com/codahale/metrics/Timer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,41 @@
* throughput statistics via {@link Meter}.
*/
public class Timer implements Metered, Sampling {

/**
* A timing context.
*
* @see Timer#time()
*/
public interface Context extends AutoCloseable {

/**
* Updates the timer with the difference between current and start time. Call to this method will
* not reset the start time. Multiple calls result in multiple updates.
*
* @return the elapsed time in nanoseconds
*/
long stop();

/**
* Equivalent to calling {@link #stop()}.
*/
@Override
default void close() {
stop();
}
}

/**
* The default timing context.
*/
private static class DefaultContext implements Context {

public static class Context implements AutoCloseable {
private final Timer timer;
private final Clock clock;
private final long startTime;

private DefaultContext(Timer timer, Clock clock) {
private Context(Timer timer, Clock clock) {
this.timer = timer;
this.clock = clock;
this.startTime = clock.getTick();
}

@Override
/**
* Updates the timer with the difference between current and start time. Call to this method will
* not reset the start time. Multiple calls result in multiple updates.
*
* @return the elapsed time in nanoseconds
*/
public long stop() {
final long elapsed = clock.getTick() - startTime;
timer.update(elapsed, TimeUnit.NANOSECONDS);
return elapsed;
}

/**
* Equivalent to calling {@link #stop()}.
*/
@Override
public void close() {
stop();
}
}

private final Meter meter;
Expand Down Expand Up @@ -158,7 +147,7 @@ public void time(Runnable event) {
* @see Context
*/
public Context time() {
return new DefaultContext(this, clock);
return new Context(this, clock);
}

@Override
Expand Down

0 comments on commit 76af224

Please sign in to comment.