This is an automated email from the ASF dual-hosted git repository.
garydgregory 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 7da57b615 Preserve negative durations in DurationUtils.accept (#1882)
7da57b615 is described below
commit 7da57b615f144b194cf05e9d11e17b04216c8e85
Author: Amar Ghugare <[email protected]>
AuthorDate: Wed Nov 18 21:15:20 2026 +0530
Preserve negative durations in DurationUtils.accept (#1882)
* Preserve negative durations in DurationUtils.accept
* Update method documentation for duration consumer
---------
Co-authored-by: Gary Gregory <[email protected]>
---
.../java/org/apache/commons/lang3/time/DurationUtils.java | 10 ++++++++--
.../java/org/apache/commons/lang3/ThreadUtilsTest.java | 14 ++++++++++++++
.../org/apache/commons/lang3/time/DurationUtilsTest.java | 12 ++++++++++++
3 files changed, 34 insertions(+), 2 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 be58514a4..c64c843c2 100644
--- a/src/main/java/org/apache/commons/lang3/time/DurationUtils.java
+++ b/src/main/java/org/apache/commons/lang3/time/DurationUtils.java
@@ -46,7 +46,11 @@ public class DurationUtils {
static final LongRange LONG_TO_INT_RANGE =
LongRange.of(NumberUtils.LONG_INT_MIN_VALUE, NumberUtils.LONG_INT_MAX_VALUE);
/**
- * Accepts the function with the duration as a long milliseconds and int
nanoseconds.
+ * Accepts the consumer with the duration split into milliseconds and
nanoseconds.
+ * <p>
+ * The nanosecond component is non-negative, and the millisecond component
is rounded down so that the two components represent the original duration,
+ * including negative durations.
+ * </p>
*
* @param <T> The function exception.
* @param consumer Accepting function.
@@ -58,7 +62,9 @@ public class DurationUtils {
public static <T extends Throwable> void accept(final
FailableBiConsumer<Long, Integer, T> consumer, final Duration duration)
throws T {
if (consumer != null && duration != null) {
- consumer.accept(duration.toMillis(), getNanosOfMilli(duration));
+ final int nanos = getNanosOfMilli(duration);
+ final long millis = duration.isNegative() ?
duration.minusNanos(nanos).toMillis() : duration.toMillis();
+ consumer.accept(millis, nanos);
}
}
diff --git a/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java
b/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java
index e9bd002e7..25f6040de 100644
--- a/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java
@@ -43,12 +43,26 @@
import org.apache.commons.lang3.ThreadUtils.ThreadPredicate;
import org.apache.commons.lang3.function.Predicates;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
/**
* Tests {@link ThreadUtils}.
*/
class ThreadUtilsTest extends AbstractLangTest {
+ @ParameterizedTest
+ @ValueSource(longs = { -1, -999_999, -1_000_001 })
+ void testJoinRejectsNegativeDuration(final long nanos) {
+ assertIllegalArgumentException(() -> ThreadUtils.join(new Thread(),
Duration.ofNanos(nanos)));
+ }
+
+ @ParameterizedTest
+ @ValueSource(longs = { -1, -999_999, -1_000_001 })
+ void testSleepRejectsNegativeDuration(final long nanos) {
+ assertIllegalArgumentException(() ->
ThreadUtils.sleep(Duration.ofNanos(nanos)));
+ }
+
private static final class TestThread extends Thread {
private final CountDownLatch latch = new CountDownLatch(1);
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 c54777a70..9b3f1e2ad 100644
--- a/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java
@@ -32,6 +32,8 @@
import org.apache.commons.lang3.AbstractLangTest;
import org.apache.commons.lang3.math.NumberUtils;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
import org.junitpioneer.jupiter.SetSystemProperty;
import org.junitpioneer.jupiter.SetSystemProperty.SetSystemProperties;
@@ -40,6 +42,16 @@
*/
class DurationUtilsTest extends AbstractLangTest {
+ @ParameterizedTest
+ @ValueSource(longs = { -1_000_000_001, -1_000_001, -1_000_000, -999_999,
-1, 0, 1, 999_999, 1_000_000, 1_000_001, 1_000_000_001 })
+ void testAcceptPreservesDuration(final long nanos) {
+ final Duration duration = Duration.ofNanos(nanos);
+ DurationUtils.accept((millis, remainder) -> {
+ assertEquals(duration,
Duration.ofMillis(millis).plusNanos(remainder));
+ assertTrue(remainder >= 0 && remainder < 1_000_000);
+ }, duration);
+ }
+
@Test
@SetSystemProperties({
@SetSystemProperty(key = "Seconds1", value = "1"),