Package org.apache.commons.lang3.time
Class DurationUtils
- java.lang.Object
-
- org.apache.commons.lang3.time.DurationUtils
-
public class DurationUtils extends java.lang.ObjectUtilities forDuration.- Since:
- 3.12.0
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static LongRangeLONG_TO_INT_RANGEAn Integer Range that accepts Longs.
-
Constructor Summary
Constructors Constructor Description DurationUtils()Deprecated.TODO Make private in 4.0.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <T extends java.lang.Throwable>
voidaccept(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 java.time.Durationget(java.lang.String key, java.time.temporal.TemporalUnit unit, long def)Gets a Duration of unit stored in the system property at the given key.private static longgetLong(java.lang.String key, long def)static java.time.DurationgetMillis(java.lang.String key, long def)Gets a Duration of milliseconds stored in the system property at the given key.static intgetNanosOfMiili(java.time.Duration duration)Deprecated.static intgetNanosOfMilli(java.time.Duration duration)Gets the nanosecond part of a Duration converted to milliseconds.static java.time.DurationgetSeconds(java.lang.String key, long def)Gets a Duration of seconds stored in the system property at the given key.static booleanisPositive(java.time.Duration duration)Tests whether the given Duration is positive (duration > 0).private static <E extends java.lang.Throwable>
java.time.Instantnow(FailableConsumer<java.time.Instant,E> nowConsumer)static <E extends java.lang.Throwable>
java.time.Durationof(FailableConsumer<java.time.Instant,E> consumer)Runs the lambda and returns the duration of its execution.static <E extends java.lang.Throwable>
java.time.Durationof(FailableRunnable<E> runnable)Runs the lambda and returns the duration of its execution.static java.time.Durationsince(java.time.temporal.Temporal startInclusive)Computes the Duration between a start instant and now.(package private) static java.time.temporal.ChronoUnittoChronoUnit(java.util.concurrent.TimeUnit timeUnit)Converts aTimeUnitto aChronoUnit.static java.time.DurationtoDuration(long amount, java.util.concurrent.TimeUnit timeUnit)Converts an amount and TimeUnit into a Duration.static inttoMillisInt(java.time.Duration duration)Converts a Duration to milliseconds bound to an int (instead of a long).static java.time.DurationzeroIfNull(java.time.Duration duration)Returns the given non-null value orDuration.ZEROif null.
-
-
-
Field Detail
-
LONG_TO_INT_RANGE
static final LongRange LONG_TO_INT_RANGE
An Integer Range that accepts Longs.
-
-
Method Detail
-
accept
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
Accepts the function with the duration as a long milliseconds and int nanoseconds.- Type Parameters:
T- The function exception.- Parameters:
consumer- Accepting function.duration- The duration to pick apart.- Throws:
T- See the function signature.T extends java.lang.Throwable- See Also:
StopWatch
-
get
public static java.time.Duration get(java.lang.String key, java.time.temporal.TemporalUnit unit, long def)Gets a Duration of unit stored in the system property at the given key.- Parameters:
key- The property name.unit- The unit that the duration is measured in, not null.def- The default value in the given unit.- Returns:
- a Duration of seconds.
- Since:
- 3.19.0
-
getLong
private static long getLong(java.lang.String key, long def)
-
getMillis
public static java.time.Duration getMillis(java.lang.String key, long def)Gets a Duration of milliseconds stored in the system property at the given key.- Parameters:
key- The property name.def- The default value in milliseconds.- Returns:
- a Duration of milliseconds.
- Since:
- 3.19.0
-
getNanosOfMiili
@Deprecated public static int getNanosOfMiili(java.time.Duration duration)
Deprecated.Gets the nanosecond part of a Duration converted to milliseconds.Handy when calling an API that takes a long of milliseconds and an int of nanoseconds. For example,
Object.wait(long, int)andThread.sleep(long, int).Note that is this different from
Duration.getNano()because a duration are seconds and nanoseconds.- Parameters:
duration- The duration to query.- Returns:
- nanoseconds between 0 and 999,999.
-
getNanosOfMilli
public static int getNanosOfMilli(java.time.Duration duration)
Gets the nanosecond part of a Duration converted to milliseconds.Handy when calling an API that takes a long of milliseconds and an int of nanoseconds. For example,
Object.wait(long, int)andThread.sleep(long, int).Note that is this different from
Duration.getNano()because a duration are seconds and nanoseconds.- Parameters:
duration- The duration to query.- Returns:
- nanoseconds between 0 and 999,999.
- Since:
- 3.13.0
-
getSeconds
public static java.time.Duration getSeconds(java.lang.String key, long def)Gets a Duration of seconds stored in the system property at the given key.- Parameters:
key- The property name.def- The default value in seconds.- Returns:
- a Duration of seconds.
- Since:
- 3.19.0
-
isPositive
public static boolean isPositive(java.time.Duration duration)
Tests whether the given Duration is positive (duration > 0).- Parameters:
duration- the value to test- Returns:
- whether the given Duration is positive (duration > 0).
-
now
private static <E extends java.lang.Throwable> java.time.Instant now(FailableConsumer<java.time.Instant,E> nowConsumer) throws E extends java.lang.Throwable
- Throws:
E extends java.lang.Throwable
-
of
public static <E extends java.lang.Throwable> java.time.Duration of(FailableConsumer<java.time.Instant,E> consumer) throws E extends java.lang.Throwable
Runs the lambda and returns the duration of its execution.- Type Parameters:
E- The type of exception throw by the lambda.- Parameters:
consumer- What to execute.- Returns:
- The Duration of execution.
- Throws:
E- thrown by the lambda.E extends java.lang.Throwable- Since:
- 3.13.0
- See Also:
StopWatch
-
of
public static <E extends java.lang.Throwable> java.time.Duration of(FailableRunnable<E> runnable) throws E extends java.lang.Throwable
Runs the lambda and returns the duration of its execution.- Type Parameters:
E- The type of exception throw by the lambda.- Parameters:
runnable- What to execute.- Returns:
- The Duration of execution.
- Throws:
E- thrown by the lambda.E extends java.lang.Throwable- Since:
- 3.13.0
- See Also:
StopWatch
-
since
public static java.time.Duration since(java.time.temporal.Temporal startInclusive)
Computes the Duration between a start instant and now.- Parameters:
startInclusive- the start instant, inclusive, not null.- Returns:
- a
Duration, not null. - Since:
- 3.13.0
-
toChronoUnit
static java.time.temporal.ChronoUnit toChronoUnit(java.util.concurrent.TimeUnit timeUnit)
Converts aTimeUnitto aChronoUnit.- Parameters:
timeUnit- A non-null TimeUnit.- Returns:
- The corresponding ChronoUnit.
-
toDuration
public static java.time.Duration toDuration(long amount, java.util.concurrent.TimeUnit timeUnit)Converts an amount and TimeUnit into a Duration.- Parameters:
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 null- Returns:
- a Duration.
-
toMillisInt
public static int toMillisInt(java.time.Duration duration)
Converts a Duration to milliseconds bound to an int (instead of a long).Handy for low-level APIs that take millisecond timeouts in ints rather than longs.
- If the duration milliseconds are greater than
Integer.MAX_VALUE, then returnInteger.MAX_VALUE. - If the duration milliseconds are lesser than
Integer.MIN_VALUE, then returnInteger.MIN_VALUE.
- Parameters:
duration- The duration to convert, not null.- Returns:
- int milliseconds.
- If the duration milliseconds are greater than
-
zeroIfNull
public static java.time.Duration zeroIfNull(java.time.Duration duration)
Returns the given non-null value orDuration.ZEROif null.- Parameters:
duration- The duration to test.- Returns:
- The given duration or
Duration.ZERO.
-
-