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 5689c91 Add DurationUtils.isPositive(Duration) and
toDuration(long,TimeUnit).
5689c91 is described below
commit 5689c91cf2a3a27f0a3a8a362857f2cf0919d4f6
Author: Gary Gregory <[email protected]>
AuthorDate: Tue Feb 16 09:23:38 2021 -0500
Add DurationUtils.isPositive(Duration) and toDuration(long,TimeUnit).
---
.../apache/commons/lang3/time/DurationUtils.java | 57 ++++++++++++++++++++--
.../commons/lang3/time/DurationUtilsTest.java | 24 +++++++++
2 files changed, 78 insertions(+), 3 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 1d87e12..950de11 100644
--- a/src/main/java/org/apache/commons/lang3/time/DurationUtils.java
+++ b/src/main/java/org/apache/commons/lang3/time/DurationUtils.java
@@ -18,7 +18,9 @@
package org.apache.commons.lang3.time;
import java.time.Duration;
+import java.time.temporal.ChronoUnit;
import java.util.Objects;
+import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.Range;
import org.apache.commons.lang3.math.NumberUtils;
@@ -33,9 +35,57 @@ public class DurationUtils {
/**
* An Integer Range that accepts Longs.
*/
- static final Range<Long> LONG_TO_INT_RANGE = Range.between(
- NumberUtils.LONG_INT_MIN_VALUE,
- NumberUtils.LONG_INT_MAX_VALUE);
+ static final Range<Long> LONG_TO_INT_RANGE =
Range.between(NumberUtils.LONG_INT_MIN_VALUE,
+ NumberUtils.LONG_INT_MAX_VALUE);
+
+ /**
+ * Tests whether the given Duration is positive (>0).
+ *
+ * @param duration the value to test
+ * @return whether the given Duration is positive (>0).
+ */
+ public static boolean isPositive(final Duration duration) {
+ return !duration.isNegative() && !duration.isZero();
+ }
+
+ /**
+ * Converts a {@link TimeUnit} to a {@link ChronoUnit}.
+ *
+ * @param timeUnit A non-null TimeUnit.
+ * @return The corresponding ChronoUnit.
+ */
+ static ChronoUnit toChronoUnit(final TimeUnit timeUnit) {
+ // TODO when using Java >= 9: Use TimeUnit.toChronoUnit().
+ switch (Objects.requireNonNull(timeUnit)) {
+ case NANOSECONDS:
+ return ChronoUnit.NANOS;
+ case MICROSECONDS:
+ return ChronoUnit.MICROS;
+ case MILLISECONDS:
+ return ChronoUnit.MILLIS;
+ case SECONDS:
+ return ChronoUnit.SECONDS;
+ case MINUTES:
+ return ChronoUnit.MINUTES;
+ case HOURS:
+ return ChronoUnit.HOURS;
+ case DAYS:
+ return ChronoUnit.DAYS;
+ default:
+ throw new IllegalArgumentException(timeUnit.toString());
+ }
+ }
+
+ /**
+ * Converts an amount and TimeUnit into a Duration.
+ *
+ * @param amount the amount of the duration, measured in terms of the
unit, positive or negative
+ * @param timeUnit the unit that the duration is measured in, must have an
exact duration, not null
+ * @return a Duration.
+ */
+ public static Duration toDuration(final long amount, final TimeUnit
timeUnit) {
+ return Duration.of(amount, toChronoUnit(timeUnit));
+ }
/**
* Converts a Duration to milliseconds bound to an int (instead of a long).
@@ -57,4 +107,5 @@ public class DurationUtils {
// intValue() does not do a narrowing conversion here
return
DurationUtils.LONG_TO_INT_RANGE.fit(Long.valueOf(duration.toMillis())).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 2ef2d62..8202ec8 100644
--- a/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java
@@ -18,8 +18,11 @@
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 java.time.Duration;
+import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.math.NumberUtils;
import org.junit.jupiter.api.Test;
@@ -30,6 +33,13 @@ import org.junit.jupiter.api.Test;
public class DurationUtilsTest {
@Test
+ public void testIsPositive() {
+ assertFalse(DurationUtils.isPositive(Duration.ZERO));
+ assertFalse(DurationUtils.isPositive(Duration.ofMillis(-1)));
+ assertTrue(DurationUtils.isPositive(Duration.ofMillis(1)));
+ }
+
+ @Test
public void testLongToIntRangeFit() {
assertEquals(0, DurationUtils.LONG_TO_INT_RANGE.fit(0L));
//
@@ -48,6 +58,20 @@ public class DurationUtilsTest {
}
@Test
+ public void testToDuration() {
+ assertEquals(Duration.ofDays(1), DurationUtils.toDuration(1,
TimeUnit.DAYS));
+ assertEquals(Duration.ofHours(1), DurationUtils.toDuration(1,
TimeUnit.HOURS));
+ assertEquals(Duration.ofMillis(1), DurationUtils.toDuration(1_000,
TimeUnit.MICROSECONDS));
+ assertEquals(Duration.ofMillis(1), DurationUtils.toDuration(1,
TimeUnit.MILLISECONDS));
+ assertEquals(Duration.ofMinutes(1), DurationUtils.toDuration(1,
TimeUnit.MINUTES));
+ assertEquals(Duration.ofNanos(1), DurationUtils.toDuration(1,
TimeUnit.NANOSECONDS));
+ assertEquals(Duration.ofSeconds(1), DurationUtils.toDuration(1,
TimeUnit.SECONDS));
+ assertEquals(1, DurationUtils.toDuration(1,
TimeUnit.MILLISECONDS).toMillis());
+ assertEquals(-1, DurationUtils.toDuration(-1,
TimeUnit.MILLISECONDS).toMillis());
+ assertEquals(0, DurationUtils.toDuration(0,
TimeUnit.SECONDS).toMillis());
+ }
+
+ @Test
public void testToMillisInt() {
assertEquals(0, DurationUtils.toMillisInt(Duration.ZERO));
assertEquals(1, DurationUtils.toMillisInt(Duration.ofMillis(1)));