public class DurationUtils
extends java.lang.Object
Duration
.Modifier and Type | Field and Description |
---|---|
(package private) static Range<java.lang.Long> |
LONG_TO_INT_RANGE
An Integer Range that accepts Longs.
|
Constructor and Description |
---|
DurationUtils() |
Modifier and Type | Method and Description |
---|---|
static <T extends java.lang.Throwable> |
accept(FailableBiConsumer<java.lang.Long,java.lang.Integer,T> consumer,
java.time.Duration duration)
Accepts the function with the duration as a long milliseconds and int nanoseconds.
|
static int |
getNanosOfMiili(java.time.Duration duration)
Gets the nanosecond part of a Duration converted to milliseconds.
|
static boolean |
isPositive(java.time.Duration duration)
Tests whether the given Duration is positive (>0).
|
(package private) static java.time.temporal.ChronoUnit |
toChronoUnit(java.util.concurrent.TimeUnit timeUnit)
Converts a
TimeUnit to a ChronoUnit . |
static java.time.Duration |
toDuration(long amount,
java.util.concurrent.TimeUnit timeUnit)
Converts an amount and TimeUnit into a Duration.
|
static int |
toMillisInt(java.time.Duration duration)
Converts a Duration to milliseconds bound to an int (instead of a long).
|
static java.time.Duration |
zeroIfNull(java.time.Duration duration)
Returns the given non-null value or
Duration.ZERO if null. |
static final Range<java.lang.Long> LONG_TO_INT_RANGE
public static <T extends java.lang.Throwable> void accept(FailableBiConsumer<java.lang.Long,java.lang.Integer,T> consumer, java.time.Duration duration) throws T extends java.lang.Throwable
T
- The function exception.consumer
- Accepting function.duration
- The duration to pick apart.T
- See the function signature.T extends java.lang.Throwable
public static int getNanosOfMiili(java.time.Duration duration)
Handy when calling an API that takes a long of milliseconds and an int of nanoseconds. For example,
Object.wait(long, int)
and Thread.sleep(long, int)
.
Note that is this different from Duration.getNano()
because a duration are seconds and nanoseconds.
duration
- The duration to query.public static boolean isPositive(java.time.Duration duration)
duration
- the value to teststatic java.time.temporal.ChronoUnit toChronoUnit(java.util.concurrent.TimeUnit timeUnit)
TimeUnit
to a ChronoUnit
.timeUnit
- A non-null TimeUnit.public static java.time.Duration toDuration(long amount, java.util.concurrent.TimeUnit timeUnit)
amount
- the amount of the duration, measured in terms of the unit, positive or negativetimeUnit
- the unit that the duration is measured in, must have an exact duration, not nullpublic static int toMillisInt(java.time.Duration duration)
Handy for low-level APIs that take millisecond timeouts in ints rather than longs.
Integer.MAX_VALUE
, then return
Integer.MAX_VALUE
.Integer.MIN_VALUE
, then return
Integer.MIN_VALUE
.duration
- The duration to convert, not null.public static java.time.Duration zeroIfNull(java.time.Duration duration)
Duration.ZERO
if null.duration
- The duration to test.Duration.ZERO
.