Repository: commons-lang Updated Branches: refs/heads/master 5d2728f65 -> 2244ed9d6
LANG-1223: Add StopWatch#getTime(TimeUnit) (closes #152) Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/fd59e545 Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/fd59e545 Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/fd59e545 Branch: refs/heads/master Commit: fd59e545f4f2d697ea7e197f002c5e8eeca27004 Parents: 5d2728f Author: Nick Manley <nickmanle...@outlook.com> Authored: Sat May 21 13:53:49 2016 -0500 Committer: pascalschumacher <pascalschumac...@gmx.net> Committed: Fri May 27 17:08:42 2016 +0200 ---------------------------------------------------------------------- .../apache/commons/lang3/time/StopWatch.java | 22 ++++++++ .../commons/lang3/time/StopWatchTest.java | 54 ++++++++++++++++++-- 2 files changed, 73 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-lang/blob/fd59e545/src/main/java/org/apache/commons/lang3/time/StopWatch.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/lang3/time/StopWatch.java b/src/main/java/org/apache/commons/lang3/time/StopWatch.java index f345ba9..7225ae2 100644 --- a/src/main/java/org/apache/commons/lang3/time/StopWatch.java +++ b/src/main/java/org/apache/commons/lang3/time/StopWatch.java @@ -17,6 +17,8 @@ package org.apache.commons.lang3.time; +import java.util.concurrent.TimeUnit; + /** * <p> * <code>StopWatch</code> provides a convenient API for timings. @@ -334,6 +336,26 @@ public class StopWatch { public long getTime() { return getNanoTime() / NANO_2_MILLIS; } + + /** + * <p> + * Get the time on the stopwatch in the specified TimeUnit. + * </p> + * + * <p> + * This is either the time between the start and the moment this method is called, or the amount of time between + * start and stop. The resulting time will be expressed in the desired TimeUnit with any remainder rounded down. + * For example, if the specified unit is {@code TimeUnit.HOURS} and the stopwatch time is 59 minutes, then the + * result returned will be {@code 0}. + * </p> + * + * @param timeUnit the unit of time, not null + * @return the time in the specified TimeUnit, rounded down + */ + public long getTime(final TimeUnit timeUnit) { + return timeUnit.convert(getNanoTime(), TimeUnit.NANOSECONDS); + } + /** * <p> * Get the time on the stopwatch in nanoseconds. http://git-wip-us.apache.org/repos/asf/commons-lang/blob/fd59e545/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java index 80a4242..4ae69c7 100644 --- a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java +++ b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java @@ -17,11 +17,14 @@ package org.apache.commons.lang3.time; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import org.junit.Assert; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.lang3.reflect.FieldUtils; +import org.junit.Assert; import org.junit.Test; /** @@ -62,7 +65,22 @@ public class StopWatchTest { try {Thread.sleep(500);} catch (final InterruptedException ex) {} assertTrue(watch.getTime() < 2000); } - + + @Test + public void testStopWatchGetWithTimeUnit() { + // Create a mock StopWatch with a time of 2:59:01.999 + final StopWatch watch = createMockStopWatch( + TimeUnit.HOURS.toNanos(2) + + TimeUnit.MINUTES.toNanos(59) + + TimeUnit.SECONDS.toNanos(1) + + TimeUnit.MILLISECONDS.toNanos(999)); + + assertEquals(2L, watch.getTime(TimeUnit.HOURS)); + assertEquals(179L, watch.getTime(TimeUnit.MINUTES)); + assertEquals(10741L, watch.getTime(TimeUnit.SECONDS)); + assertEquals(10741999L, watch.getTime(TimeUnit.MILLISECONDS)); + } + @Test public void testStopWatchSplit(){ final StopWatch watch = new StopWatch(); @@ -252,4 +270,34 @@ public class StopWatchTest { assertTrue(watch.isStopped()); } + /** + * <p> + * Creates a suspended StopWatch object which appears to have elapsed + * for the requested amount of time in nanoseconds. + * <p> + * + * <pre> + * // Create a mock StopWatch with a time of 2:59:01.999 + * final long nanos = TimeUnit.HOURS.toNanos(2) + * + TimeUnit.MINUTES.toNanos(59) + * + TimeUnit.SECONDS.toNanos(1) + * + TimeUnit.MILLISECONDS.toNanos(999); + * final StopWatch watch = createMockStopWatch(nanos); + * </pre> + * + * @param nanos Time in nanoseconds to have elapsed on the stop watch + * @return StopWatch in a suspended state with the elapsed time + */ + private StopWatch createMockStopWatch(long nanos) { + final StopWatch watch = StopWatch.createStarted(); + watch.suspend(); + try { + final long currentNanos = System.nanoTime(); + FieldUtils.writeField(watch, "startTime", currentNanos - nanos, true); + FieldUtils.writeField(watch, "stopTime", currentNanos, true); + } catch (IllegalAccessException e) { + return null; + } + return watch; + } }