Add DurationUtils.zeroIfNull(Duration).
This commit is contained in:
parent
cda703ccb8
commit
3a35c53c6c
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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)));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue