Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement StopWatch lap #709

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion src/main/java/org/apache/commons/lang3/time/StopWatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ public static StopWatch createStarted() {
*/
private long stopTimeNanos;

/**
* The lap time in nanoseconds.
*/
private long lapStartTimeNanos;

/**
* <p>
* Constructor.
Expand Down Expand Up @@ -483,7 +488,9 @@ public void resume() {
if (this.runningState != State.SUSPENDED) {
throw new IllegalStateException("Stopwatch must be suspended to resume. ");
}
this.startTimeNanos += System.nanoTime() - this.stopTimeNanos;
long offset = System.nanoTime() - this.stopTimeNanos;
this.startTimeNanos += offset;
this.lapStartTimeNanos += offset;
this.runningState = State.RUNNING;
}

Expand All @@ -508,6 +515,47 @@ public void split() {
this.splitState = SplitState.SPLIT;
}


/**
* <p>
* Gets the time of current lap in the specified TimeUnit. Closes current lap and opens new one.
* </p>
*
* <p>
* First lap is opened on start.
* </p>
*
* <p>
* The resulting time will be expressed in the desired TimeUnit with any remainder rounded down.
* </p>
*
* @param timeUnit the unit of time, not null
* @return the time in the specified TimeUnit, rounded down
* @since 3.12
*/
public long lap(final TimeUnit timeUnit) {
if (this.runningState != State.RUNNING) {
throw new IllegalStateException("Stopwatch is not running. ");
}
long now = System.nanoTime();
long lap = now - this.lapStartTimeNanos;
this.lapStartTimeNanos = now;
return timeUnit.convert(lap, TimeUnit.NANOSECONDS);
}

/**
* <p>
* Shortcut for lap(TimeUnit.MILLISECONDS)
* </p>
*
* @see StopWatch#lap(TimeUnit)
* @return the lap time in milliseconds
* @since 3.12
*/
public long lap() {
return lap(TimeUnit.MILLISECONDS);
}

/**
* <p>
* Starts the stopwatch.
Expand All @@ -528,6 +576,7 @@ public void start() {
throw new IllegalStateException("Stopwatch already started. ");
}
this.startTimeNanos = System.nanoTime();
this.lapStartTimeNanos = startTimeNanos;
this.startTimeMillis = System.currentTimeMillis();
this.runningState = State.RUNNING;
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,18 @@ public void testToStringWithMessage() throws InterruptedException {
final String splitStr = watch.toString();
assertEquals(splitStr.length(), 12 + MESSAGE.length() + 1, "Formatted split string not the correct length");
}

@Test
public void testStopWatchLap() throws InterruptedException {
final StopWatch watch = StopWatch.createStarted();
sleepQuietly(550);
final long lap550 = watch.lap();
sleepQuietly(250);
final long lap250 = watch.lap();

assertTrue(lap550 >= 500);
assertTrue(lap550 < 700);
assertTrue(lap250 >= 200);
assertTrue(lap250 < 400);
}
}