Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/main/java/org/apache/commons/lang3/time/DurationUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down Expand Up @@ -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));
Expand Down
Loading