garydgregory commented on code in PR #1254: URL: https://github.com/apache/commons-lang/pull/1254#discussion_r1693715754
########## src/main/java/org/apache/commons/lang3/time/StopWatch.java: ########## @@ -671,4 +677,68 @@ public void unsplit() { splitState = SplitState.UNSPLIT; } + <T, E extends Throwable> T get(FailableSupplier<T, E> supplier) throws E { + if (isStopped()) { + start(); + } else if (isSuspended()) { + resume(); + } + + try { + return supplier.get(); + } finally { + suspend(); + } + } + + + /** + * Take the time of the execution of a given {@linkplain Function}. + * + * <p> Review Comment: Close HTML tags ########## src/main/java/org/apache/commons/lang3/time/StopWatch.java: ########## @@ -671,4 +677,68 @@ public void unsplit() { splitState = SplitState.UNSPLIT; } + <T, E extends Throwable> T get(FailableSupplier<T, E> supplier) throws E { + if (isStopped()) { + start(); + } else if (isSuspended()) { + resume(); + } + + try { + return supplier.get(); + } finally { + suspend(); + } + } + + Review Comment: Remove extra whitespace. ########## src/main/java/org/apache/commons/lang3/time/StopWatch.java: ########## @@ -671,4 +677,68 @@ public void unsplit() { splitState = SplitState.UNSPLIT; } + <T, E extends Throwable> T get(FailableSupplier<T, E> supplier) throws E { + if (isStopped()) { + start(); + } else if (isSuspended()) { + resume(); + } + Review Comment: Remove extra whitespace. ########## src/test/java/org/apache/commons/lang3/time/StopWatchTest.java: ########## @@ -448,4 +451,29 @@ public void testToStringWithMessage() throws InterruptedException { final String splitStr = watch.toString(); assertEquals(12 + MESSAGE.length() + 1, splitStr.length(), "Formatted split string not the correct length"); } + + @Test Review Comment: Test failable code paths. ########## src/main/java/org/apache/commons/lang3/time/StopWatch.java: ########## @@ -671,4 +677,68 @@ public void unsplit() { splitState = SplitState.UNSPLIT; } + <T, E extends Throwable> T get(FailableSupplier<T, E> supplier) throws E { Review Comment: Add Javadoc with a `@since` tag. ########## src/main/java/org/apache/commons/lang3/time/StopWatch.java: ########## @@ -671,4 +677,68 @@ public void unsplit() { splitState = SplitState.UNSPLIT; } + <T, E extends Throwable> T get(FailableSupplier<T, E> supplier) throws E { + if (isStopped()) { + start(); + } else if (isSuspended()) { + resume(); + } + + try { + return supplier.get(); + } finally { + suspend(); + } + } + + + /** + * Take the time of the execution of a given {@linkplain Function}. + * + * <p> + * <b>Take the time of given {@linkplain Function}</b> + * <pre>{@code + * final StopWatch watch = StopWatch.create(); + * + * String result = watch.apply(it -> it.toLowerCase(Locale.ROOT)).apply("A"); + * }</pre> + + * <b>Take the time of an applied {@linkplain Function} in a stream</b> + * + * <pre>{@code + * final StopWatch watch = StopWatch.create(); + * + * String result = Stream.of("A", "B", "C") + * .map(watch.apply(it -> it.toLowerCase(Locale.ROOT))) + * .collect(Collectors.joining()); + * }</pre> + * + * @param function the function those application should be measured + * + * @return the given function prepared to take time if applied + * + * @throws IllegalStateException if the StopWatch is not stopped or suspended + * + * @param <T> the type of the input to the function + * @param <R> the type of the result of the function + * + * @since 3.16 + */ + public <T, R> Function<T, R> apply(Function<T, R> function) { Review Comment: Why is the new method above failable and this one not? ########## src/test/java/org/apache/commons/lang3/time/StopWatchTest.java: ########## @@ -448,4 +451,29 @@ public void testToStringWithMessage() throws InterruptedException { final String splitStr = watch.toString(); assertEquals(12 + MESSAGE.length() + 1, splitStr.length(), "Formatted split string not the correct length"); } + + @Test + public void testGetForFailableSupplier() throws InterruptedException { + final StopWatch watch = StopWatch.create(); + + String result = watch.get(() -> { + StopWatchTest.this.sleep(MILLIS_200); + return "Foobar"; + }); + + assertEquals("Foobar", result, "Watch returned result other then expected"); + assertTrue(watch.isSuspended(), "Watch should be suspended"); + } + + @Test Review Comment: Add tests for edge cases. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@commons.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org