public class DurationUtils extends Object
Duration
.Constructor and Description |
---|
DurationUtils() |
Modifier and Type | Method and Description |
---|---|
static <T extends Throwable> |
accept(FailableBiConsumer<Long,Integer,T> consumer,
Duration duration)
Accepts the function with the duration as a long milliseconds and int nanoseconds.
|
static int |
getNanosOfMiili(Duration duration)
Deprecated.
|
static int |
getNanosOfMilli(Duration duration)
Gets the nanosecond part of a Duration converted to milliseconds.
|
static boolean |
isPositive(Duration duration)
Tests whether the given Duration is positive (>0).
|
static <E extends Throwable> |
of(FailableConsumer<Instant,E> consumer)
Runs the lambda and returns the duration of its execution.
|
static <E extends Throwable> |
of(FailableRunnable<E> runnable)
Runs the lambda and returns the duration of its execution.
|
static Duration |
since(Temporal startInclusive)
Computes the Duration between a start instant and now.
|
static Duration |
toDuration(long amount,
TimeUnit timeUnit)
Converts an amount and TimeUnit into a Duration.
|
static int |
toMillisInt(Duration duration)
Converts a Duration to milliseconds bound to an int (instead of a long).
|
static Duration |
zeroIfNull(Duration duration)
Returns the given non-null value or
Duration.ZERO if null. |
public DurationUtils()
public static <T extends Throwable> void accept(FailableBiConsumer<Long,Integer,T> consumer, Duration duration) throws T extends Throwable
T
- The function exception.consumer
- Accepting function.duration
- The duration to pick apart.T
- See the function signature.T extends Throwable
@Deprecated public static int getNanosOfMiili(Duration duration)
getNanosOfMilli(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 int getNanosOfMilli(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(Duration duration)
duration
- the value to testpublic static <E extends Throwable> Duration of(FailableConsumer<Instant,E> consumer) throws E extends Throwable
E
- The type of exception throw by the lambda.consumer
- What to execute.E
- thrown by the lambda.E extends Throwable
public static <E extends Throwable> Duration of(FailableRunnable<E> runnable) throws E extends Throwable
E
- The type of exception throw by the lambda.runnable
- What to execute.E
- thrown by the lambda.E extends Throwable
public static Duration since(Temporal startInclusive)
startInclusive
- the start instant, inclusive, not null.Duration
, not null.public static Duration toDuration(long amount, 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(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 Duration zeroIfNull(Duration duration)
Duration.ZERO
if null.duration
- The duration to test.Duration.ZERO
.Copyright © 2001–2023 The Apache Software Foundation. All rights reserved.