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 d95e17f1c Add DurationUtils.toMillisLong(Duration) (#1679)
d95e17f1c is described below
commit d95e17f1c173a6259886e577cf5acc914ec44c2d
Author: Gary Gregory <[email protected]>
AuthorDate: Fri May 29 06:12:22 2026 -0400
Add DurationUtils.toMillisLong(Duration) (#1679)
---
.../apache/commons/lang3/time/DurationUtils.java | 34 +++++++++++++++++-----
.../commons/lang3/time/DurationUtilsTest.java | 16 ++++++++++
2 files changed, 42 insertions(+), 8 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 f13dcbc8a..fd15aa892 100644
--- a/src/main/java/org/apache/commons/lang3/time/DurationUtils.java
+++ b/src/main/java/org/apache/commons/lang3/time/DurationUtils.java
@@ -240,25 +240,43 @@ public static Duration toDuration(final long amount,
final TimeUnit timeUnit) {
* Handy for low-level APIs that take millisecond timeouts in ints rather
than longs.
* </p>
* <ul>
- * <li>If the duration milliseconds are greater than {@link
Integer#MAX_VALUE}, then return
- * {@link Integer#MAX_VALUE}.</li>
- * <li>If the duration milliseconds are lesser than {@link
Integer#MIN_VALUE}, then return
- * {@link Integer#MIN_VALUE}.</li>
+ * <li>If the duration milliseconds are greater than {@link
Integer#MAX_VALUE}, then return {@link Integer#MAX_VALUE}.</li>
+ * <li>If the duration milliseconds are lesser than {@link
Integer#MIN_VALUE}, then return {@link Integer#MIN_VALUE}.</li>
* </ul>
*
* @param duration The duration to convert, not null.
* @return int milliseconds.
+ * @see Duration#toMillis()
+ * @see Integer#MIN_VALUE
+ * @see Integer#MAX_VALUE
*/
public static int toMillisInt(final Duration duration) {
Objects.requireNonNull(duration, "duration");
// intValue() does not do a narrowing conversion here
- final long millis;
+ return
LONG_TO_INT_RANGE.fit(Long.valueOf(toMillisLong(duration))).intValue();
+ }
+
+ /**
+ * Converts a Duration to milliseconds bound to a {@code long} without
throwing {@link ArithmeticException}.
+ * <ul>
+ * <li>If the duration milliseconds are greater than {@link
Long#MAX_VALUE}, then return {@link Long#MAX_VALUE}.</li>
+ * <li>If the duration milliseconds are lesser than {@link
Long#MIN_VALUE}, then return {@link Long#MIN_VALUE}.</li>
+ * </ul>
+ *
+ * @param duration The duration to convert, not null.
+ * @return long milliseconds.
+ * @see Duration#toMillis()
+ * @see Long#MIN_VALUE
+ * @see Long#MAX_VALUE
+ * @since 3.21.0
+ */
+ public static long toMillisLong(final Duration duration) {
+ Objects.requireNonNull(duration, "duration");
try {
- millis = duration.toMillis();
+ return duration.toMillis();
} catch (final ArithmeticException e) {
- return duration.isNegative() ? Integer.MIN_VALUE :
Integer.MAX_VALUE;
+ return duration.isNegative() ? Long.MIN_VALUE : Long.MAX_VALUE;
}
- return LONG_TO_INT_RANGE.fit(Long.valueOf(millis)).intValue();
}
/**
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 016d1f02c..c54777a70 100644
--- a/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java
@@ -176,6 +176,7 @@ void testToDuration() {
void testToMillisInt() {
assertEquals(0, DurationUtils.toMillisInt(Duration.ZERO));
assertEquals(1, DurationUtils.toMillisInt(Duration.ofMillis(1)));
+ assertEquals(-1, DurationUtils.toMillisInt(Duration.ofMillis(-1)));
//
assertEquals(Integer.MIN_VALUE,
DurationUtils.toMillisInt(Duration.ofMillis(Integer.MIN_VALUE)));
assertEquals(Integer.MAX_VALUE,
DurationUtils.toMillisInt(Duration.ofMillis(Integer.MAX_VALUE)));
@@ -204,6 +205,21 @@ void testToMillisIntUnderflowToMinInteger() {
assertEquals(Integer.MIN_VALUE,
DurationUtils.toMillisInt(Duration.ofSeconds(Long.MIN_VALUE / 1000 - 1)));
}
+ @Test
+ void testToMillisLong() {
+ assertEquals(0, DurationUtils.toMillisLong(Duration.ZERO));
+ assertEquals(1, DurationUtils.toMillisLong(Duration.ofMillis(1)));
+ assertEquals(-1, DurationUtils.toMillisLong(Duration.ofMillis(-1)));
+ assertEquals(Long.MIN_VALUE,
DurationUtils.toMillisLong(Duration.ofMillis(Long.MIN_VALUE)));
+ assertEquals(Long.MAX_VALUE,
DurationUtils.toMillisLong(Duration.ofMillis(Long.MAX_VALUE)));
+ assertEquals(Long.MAX_VALUE,
DurationUtils.toMillisLong(Duration.ofSeconds(Long.MAX_VALUE)));
+ }
+
+ @Test
+ void testToMillisLongNullDuration() {
+ assertNullPointerException(() -> DurationUtils.toMillisLong(null));
+ }
+
@Test
void testZeroIfNull() {
assertEquals(Duration.ZERO, DurationUtils.zeroIfNull(null));