You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/02/20 14:12:02 UTC
[commons-lang] branch master updated (5689c91 -> 3a35c53)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git.
from 5689c91 Add DurationUtils.isPositive(Duration) and toDuration(long,TimeUnit).
new cda703c Simplify.
new 3a35c53 Add DurationUtils.zeroIfNull(Duration).
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../java/org/apache/commons/lang3/time/DurationUtils.java | 13 ++++++++++++-
.../org/apache/commons/lang3/time/DurationUtilsTest.java | 8 +++++++-
2 files changed, 19 insertions(+), 2 deletions(-)
[commons-lang] 01/02: Simplify.
Posted by gg...@apache.org.
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
commit cda703ccb891290208f28bc6afb2abfaea7a6c8d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Feb 20 08:21:19 2021 -0500
Simplify.
---
src/main/java/org/apache/commons/lang3/time/DurationUtils.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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 950de11..a3e227f 100644
--- a/src/main/java/org/apache/commons/lang3/time/DurationUtils.java
+++ b/src/main/java/org/apache/commons/lang3/time/DurationUtils.java
@@ -105,7 +105,7 @@ public class DurationUtils {
public static int toMillisInt(final Duration duration) {
Objects.requireNonNull(duration, "duration");
// intValue() does not do a narrowing conversion here
- return DurationUtils.LONG_TO_INT_RANGE.fit(Long.valueOf(duration.toMillis())).intValue();
+ return LONG_TO_INT_RANGE.fit(Long.valueOf(duration.toMillis())).intValue();
}
}
[commons-lang] 02/02: Add DurationUtils.zeroIfNull(Duration).
Posted by gg...@apache.org.
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
commit 3a35c53c6c9a3b9a1feedaf39b72e405ecaf9df8
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Feb 20 09:11:57 2021 -0500
Add DurationUtils.zeroIfNull(Duration).
---
.../java/org/apache/commons/lang3/time/DurationUtils.java | 11 +++++++++++
.../java/org/apache/commons/lang3/time/DurationUtilsTest.java | 8 +++++++-
2 files changed, 18 insertions(+), 1 deletion(-)
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 a3e227f..5ac7f47 100644
--- a/src/main/java/org/apache/commons/lang3/time/DurationUtils.java
+++ b/src/main/java/org/apache/commons/lang3/time/DurationUtils.java
@@ -22,6 +22,7 @@ import java.time.temporal.ChronoUnit;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
+import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.Range;
import org.apache.commons.lang3.math.NumberUtils;
@@ -108,4 +109,14 @@ public class DurationUtils {
return LONG_TO_INT_RANGE.fit(Long.valueOf(duration.toMillis())).intValue();
}
+ /**
+ * Returns the given non-null value or {@link Duration#ZERO} if null.
+ *
+ * @param duration The duration to test.
+ * @return The given duration or {@link Duration#ZERO}.
+ */
+ public static Duration zeroIfNull(final Duration duration) {
+ return ObjectUtils.defaultIfNull(duration, Duration.ZERO);
+ }
+
}
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 8202ec8..0a9bb84 100644
--- a/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java
@@ -70,7 +70,7 @@ public class DurationUtilsTest {
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));
@@ -86,4 +86,10 @@ public class DurationUtilsTest {
assertEquals(Integer.MIN_VALUE, DurationUtils.toMillisInt(Duration.ofNanos(Long.MIN_VALUE)));
assertEquals(Integer.MAX_VALUE, DurationUtils.toMillisInt(Duration.ofNanos(Long.MAX_VALUE)));
}
+
+ @Test
+ public void testZeroIfNull() {
+ assertEquals(Duration.ZERO, DurationUtils.zeroIfNull(null));
+ assertEquals(Duration.ofDays(1), DurationUtils.zeroIfNull(Duration.ofDays(1)));
+ }
}