Add DurationUtils.of(FailableConsumer|FailableRunnbale)

This commit is contained in:
Gary Gregory 2022-05-28 09:02:02 -04:00
parent eb2bc4d3b7
commit 16de452a37
3 changed files with 54 additions and 0 deletions

View File

@ -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>

View File

@ -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.
*

View File

@ -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);