You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/05/28 13:02:11 UTC

[commons-lang] branch master updated: Add DurationUtils.of(FailableConsumer|FailableRunnbale)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 16de452a3 Add DurationUtils.of(FailableConsumer|FailableRunnbale)
16de452a3 is described below

commit 16de452a3754bfb2b36d8f6cd4a0b55432803455
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat May 28 09:02:02 2022 -0400

    Add DurationUtils.of(FailableConsumer|FailableRunnbale)
---
 src/changes/changes.xml                            |  1 +
 .../apache/commons/lang3/time/DurationUtils.java   | 34 ++++++++++++++++++++++
 .../commons/lang3/time/DurationUtilsTest.java      | 19 ++++++++++++
 3 files changed, 54 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ada808fcb..2cae78c4c 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -143,6 +143,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add github/codeql-action.</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add coverage.yml.</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add DurationUtils.since(Temporal).</action>
+    <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add DurationUtils.of(FailableConsumer|FailableRunnbale).</action>
     <!-- UPDATE -->
     <action                   type="update" dev="ggregory" due-to="Dependabot, XenoAmess, Gary Gregory">Bump actions/cache from 2.1.4 to 3.0.2 #742, #752, #764, #833, #867.</action>
     <action                   type="update" dev="ggregory" due-to="Dependabot">Bump actions/checkout from 2 to 3 #819, #825, #859.</action>
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;
 
 /**
@@ -85,6 +87,38 @@ public class DurationUtils {
         return !duration.isNegative() && !duration.isZero();
     }
 
+    /**
+     * 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.
      *
diff --git a/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java b/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java
index 514e71aed..dfe0b94b5 100644
--- a/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java
@@ -20,7 +20,9 @@ package org.apache.commons.lang3.time;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
+import java.io.IOException;
 import java.time.Duration;
 import java.time.Instant;
 import java.util.concurrent.TimeUnit;
@@ -72,6 +74,23 @@ public class DurationUtilsTest {
         assertEquals(Short.MAX_VALUE, DurationUtils.LONG_TO_INT_RANGE.fit((long) Short.MAX_VALUE));
     }
 
+    @Test
+    public void testOfRunnble() {
+        assertTrue(DurationUtils.of(() -> testSince()).compareTo(Duration.ZERO) >= 0);
+    }
+
+    @Test
+    public void testOfConsumer() {
+        assertTrue(DurationUtils.of(start -> assertTrue(start.compareTo(Instant.now()) >= 0)).compareTo(Duration.ZERO) >= 0);
+    }
+
+    @Test
+    public void testOfRunnbleThrowing() {
+        assertThrows(IOException.class, () -> DurationUtils.of(() -> {
+            throw new IOException();
+        }));
+    }
+
     @Test
     public void testSince() {
         assertTrue(DurationUtils.since(Instant.EPOCH).compareTo(Duration.ZERO) >= 0);