You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "obfischer (via GitHub)" <gi...@apache.org> on 2024/08/01 21:28:59 UTC
Re: [PR] LANG-1747: Add support for actions to simplify the measurement of times [commons-lang]
obfischer commented on code in PR #1254:
URL: https://github.com/apache/commons-lang/pull/1254#discussion_r1700889127
##########
src/main/java/org/apache/commons/lang3/time/StopWatch.java:
##########
@@ -671,4 +680,368 @@ public void unsplit() {
splitState = SplitState.UNSPLIT;
}
+ /**
+ * Take the time of the exectution of a given {@linkplain FailableSupplier}.
+ *
+ * <p>
+ * <b>Take the time of the execution of given {@linkplain FailableSupplier}</b>
+ * <pre>{@code
+ * final StopWatch watch = StopWatch.create();
+ *
+ * String result = watch.get(() -> "A");
+ * }</pre>
+ * </p>
+ * <p>
+ *
+ * @param supplier the {@linkplain FailableSupplier} those execution should be measured
+ *
+ * @return the result of the {@linkplain FailableSupplier#get()} operation
+ *
+ * @throws E if the supplier fails
+ *
+ * @param <T> the type of return value of the supplier
+ * @param <E> the kind of thrown exception or error
+ *
+ * @since 3.16
+ */
+ public <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 Supplier}.
+ *
+ * <p>
+ * <b>Take the time of the execution of given {@linkplain Supplier}</b>
+ * <pre>{@code
+ * final StopWatch watch = StopWatch.create();
+ *
+ * String result = watch.get(() -> "A");
+ * }</pre>
+ * </p>
+ * <p>
+ *
+ * @param supplier the {@linkplain Supplier} those execution should be measured
+ *
+ * @return the result of the {@linkplain Supplier#get()} operation
+ *
+ * @throws IllegalStateException if the StopWatch is not stopped or suspended
+ *
+ * @param <T> the type of the input to the function
+ *
+ * @since 3.16
+ */
+ public <T> T get(Supplier<T> supplier) {
+ 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>
+ * </p>
+ * <p>
+ * <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>
+ * </p>
+ *
+ * @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) {
+ return arg -> {
+ if (isStopped()) {
+ start();
+ } else if (isSuspended()) {
+ resume();
+ }
+ try {
+ return function.apply(arg);
+ } finally {
+ suspend();
+ }
+ };
+ }
+
+
+ /**
+ * Take the time of the execution of a given {@linkplain BiFunction}.
+ *
+ * <p>
+ * <b>Take the time of given {@linkplain BiFunction}</b>
+ * <pre>{@code
+ * final StopWatch watch = StopWatch.create();
+ *
+ * String result = watch.apply((first, second) -> first + second)).apply("A", "B");
+ * }</pre>
+ * </p>
+ * <p>
+ *
+ * @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 first argument to the function
+ * @param <U> the type of the second argument to the function
+ * @param <R> the type of the result of the function
+ *
+ * @since 3.16
+ */
+ public <T, U, R> BiFunction<T, U, R> apply(BiFunction<T, U, R> function) {
+ return (firstArg, secondArg) -> {
+ if (isStopped()) {
+ start();
+ } else if (isSuspended()) {
+ resume();
+ }
+ try {
+ return function.apply(firstArg, secondArg);
+ } finally {
+ suspend();
+ }
+ };
+ }
+
+ /**
+ * Take the time of the execution of a given {@linkplain TriFunction}.
+ *
+ * <p>
+ * <b>Take the time of given {@linkplain BiFunction}</b>
+ * <pre>{@code
+ * final StopWatch watch = StopWatch.create();
+ *
+ * String result = watch.apply((first, second, third) -> first + second)).apply("A", "B", "C");
+ * }</pre>
+ * </p>
+ * <p>
+ *
+ * @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 first argument to the function
+ * @param <U> the type of the second argument to the function
+ * @param <V> the type of the thrird argument to the function
+ * @param <R> the type of the result of the function
+ *
+ * @since 3.16
+ */
+ public <T, U, V, R> TriFunction<T, U, V, R> apply(TriFunction<T, U, V, R> function) {
+ return (firstArg, secondArg, thirdArg) -> {
+ if (isStopped()) {
+ start();
+ } else if (isSuspended()) {
+ resume();
+ }
+ try {
+ return function.apply(firstArg, secondArg, thirdArg);
+ } finally {
+ suspend();
+ }
+ };
+ }
+
+
+ /**
+ * Take the time of the execution of a given {@linkplain Consumer}.
+ *
+ * <p>
+ * <b>Take the time of given {@linkplain Consumer}</b>
+ * <pre>{@code
+ * final StopWatch watch = StopWatch.create();
+ *
+ * watch.accept((argument) -> process(argument)).accept("A");
+ * }</pre>
+ * </p>
+ * <p>
+ *
+ * @param consumer the consumer those application should be measured
+ *
+ * @return the given consumer prepared to take time if applied
+ *
+ * @throws IllegalStateException if the StopWatch is not stopped or suspended
+ *
+ * @param <T> the type of the argument to the consumer
+ *
+ * @since 3.16
+ */
+ public <T> Consumer<T> accept(Consumer<T> consumer) {
+ return (arg) -> {
+ if (isStopped()) {
+ start();
+ } else if (isSuspended()) {
+ resume();
+ }
+ try {
+ consumer.accept(arg);
+ } finally {
+ suspend();
+ }
+ };
+ }
+
+ /**
+ * Take the time of the execution of a given {@linkplain BiConsumer}.
+ *
+ * <p>
+ * <b>Take the time of given {@linkplain BiConsumer}</b>
+ * <pre>{@code
+ * final StopWatch watch = StopWatch.create();
+ *
+ * watch.accept((first, second) -> process(first, second)).accept("A", "B");
+ * }</pre>
+ * </p>
+ * <p>
+ *
+ * @param consumer the consumer those application should be measured
+ *
+ * @return the given consumer prepared to take time if applied
+ *
+ * @throws IllegalStateException if the StopWatch is not stopped or suspended
+ *
+ * @param <T> the type of the first argument to the consumer
+ * @param <U> the type of the second argument to the consumer
+ *
+ * @since 3.16
+ */
+ public <T, U> BiConsumer<T, U> accept(BiConsumer<T, U> consumer) {
+ return (first, second) -> {
+ if (isStopped()) {
+ start();
+ } else if (isSuspended()) {
+ resume();
+ }
+ try {
+ consumer.accept(first, second);
+ } finally {
+ suspend();
+ }
+ };
+ }
+
+ /**
+ * Take the time of the execution of a given {@linkplain TriConsumer}.
+ *
+ * <p>
+ * <b>Take the time of given {@linkplain TriConsumer}</b>
+ * <pre>{@code
+ * final StopWatch watch = StopWatch.create();
+ *
+ * watch.accept((first, second, third) -> process(first, second, third)).accept("A", "B", "C");
+ * }</pre>
+ * </p>
+ * <p>
+ *
+ * @param consumer the consumer those application should be measured
+ *
+ * @return the given consumer prepared to take time if applied
+ *
+ * @throws IllegalStateException if the StopWatch is not stopped or suspended
+ *
+ * @param <T> the type of the first argument to the consumer
+ * @param <U> the type of the second argument to the consumer
+ *
+ * @since 3.16
+ */
+ public <T, U, V> TriConsumer<T, U, V> accept(TriConsumer<T, U, V> consumer) {
+ return (firstArg, secondArg, thirdArg) -> {
+ if (isStopped()) {
+ start();
+ } else if (isSuspended()) {
+ resume();
+ }
+ try {
+ consumer.accept(firstArg, secondArg, thirdArg);
+ } finally {
+ suspend();
+ }
+ };
+ }
+
+
+ /**
+ * Take the time of the execution of a given {@linkplain Predicate}.
Review Comment:
s/linkplain/link/g
--
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-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org