aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/apache/commons/lang3/time
diff options
context:
space:
mode:
authorGary Gregory <garydgregory@gmail.com>2022-05-28 09:02:02 -0400
committerGary Gregory <garydgregory@gmail.com>2022-05-28 09:02:02 -0400
commit16de452a3754bfb2b36d8f6cd4a0b55432803455 (patch)
treea3d83a6ad0a29acfd00822a35ebd9d1bf1dc06ee /src/main/java/org/apache/commons/lang3/time
parenteb2bc4d3b7c9c74085d52286a96321bd23ee48ab (diff)
downloadapache-commons-lang-16de452a3754bfb2b36d8f6cd4a0b55432803455.tar.gz
Add DurationUtils.of(FailableConsumer|FailableRunnbale)
Diffstat (limited to 'src/main/java/org/apache/commons/lang3/time')
-rw-r--r--src/main/java/org/apache/commons/lang3/time/DurationUtils.java34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/main/java/org/apache/commons/lang3/time/DurationUtils.java b/src/main/java/org/apache/commons/lang3/time/DurationUtils.java
index becd89316..d349a7473 100644
--- a/src/main/java/org/apache/commons/lang3/time/DurationUtils.java
+++ b/src/main/java/org/apache/commons/lang3/time/DurationUtils.java
@@ -27,6 +27,8 @@ import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.Range;
import org.apache.commons.lang3.function.FailableBiConsumer;
+import org.apache.commons.lang3.function.FailableConsumer;
+import org.apache.commons.lang3.function.FailableRunnable;
import org.apache.commons.lang3.math.NumberUtils;
/**
@@ -86,6 +88,38 @@ public class DurationUtils {
}
/**
+ * Runs the lambda and returns the duration of its execution.
+ *
+ * @param <E> The type of exception throw by the lambda.
+ * @param consumer What to execute.
+ * @return The Duration of execution.
+ * @throws E thrown by the lambda.
+ * @since 3.13.0
+ */
+ public static <E extends Throwable> Duration of(final FailableConsumer<Instant, E> consumer) throws E {
+ return since(now(consumer::accept));
+ }
+
+ /**
+ * Runs the lambda and returns the duration of its execution.
+ *
+ * @param <E> The type of exception throw by the lambda.
+ * @param runnable What to execute.
+ * @return The Duration of execution.
+ * @throws E thrown by the lambda.
+ * @since 3.13.0
+ */
+ public static <E extends Throwable> Duration of(final FailableRunnable<E> runnable) throws E {
+ return of(start -> runnable.run());
+ }
+
+ private static <E extends Throwable> Instant now(final FailableConsumer<Instant, E> nowConsumer) throws E {
+ final Instant start = Instant.now();
+ nowConsumer.accept(start);
+ return start;
+ }
+
+ /**
* Computes the Duration between a start instant and now.
*
* @param startInclusive the start instant, inclusive, not null.