Class DurationUtils


  • public class DurationUtils
    extends java.lang.Object
    Utilities for Duration.
    Since:
    3.12.0
    • Constructor Summary

      Constructors 
      Constructor Description
      DurationUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T extends java.lang.Throwable>
      void
      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).
      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.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DurationUtils

        public DurationUtils()
    • 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
      • getNanosOfMiili

        public static int getNanosOfMiili​(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) and Thread.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.
      • isPositive

        public static boolean isPositive​(java.time.Duration duration)
        Tests whether the given Duration is positive (>0).
        Parameters:
        duration - the value to test
        Returns:
        whether the given Duration is positive (>0).
      • 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 negative
        timeUnit - 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 return Integer.MAX_VALUE.
        • If the duration milliseconds are lesser than Integer.MIN_VALUE, then return Integer.MIN_VALUE.
        Parameters:
        duration - The duration to convert, not null.
        Returns:
        int milliseconds.
      • zeroIfNull

        public static java.time.Duration zeroIfNull​(java.time.Duration duration)
        Returns the given non-null value or Duration.ZERO if null.
        Parameters:
        duration - The duration to test.
        Returns:
        The given duration or Duration.ZERO.