Class DurationUtils


  • public final class DurationUtils
    extends java.lang.Object
    Helper utilities for Duration.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.time.Duration ensurePositive​(java.time.Duration duration, java.lang.String name)
      Ensures the duration is positive, excluding zero.
      static boolean isInfinite​(java.time.Duration duration, java.time.Duration maxDuration)
      Checks if the duration is considered "infinite".
      static boolean isPositive​(java.time.Duration duration)
      Checks if the duration is positive, excluding zero.
      static long toNanos​(java.time.Duration duration)
      Converts a Duration to nanoseconds or if the resulting value would overflow a 64-bit signed integer then either Long.MIN_VALUE or Long.MAX_VALUE as appropriate.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • isPositive

        public static boolean isPositive​(java.time.Duration duration)
        Checks if the duration is positive, excluding zero.
        Parameters:
        duration - the Duration to validate
        Returns:
        true if the passed duration is greater than Duration.ZERO, false otherwise
      • ensurePositive

        public static java.time.Duration ensurePositive​(java.time.Duration duration,
                                                        java.lang.String name)
        Ensures the duration is positive, excluding zero.
        Parameters:
        duration - the Duration to validate
        name - name of the Duration variable
        Returns:
        the passed duration if all checks pass
        Throws:
        java.lang.NullPointerException - if the passed duration is null
        java.lang.IllegalArgumentException - if the passed duration is not greater than Duration.ZERO
      • isInfinite

        public static boolean isInfinite​(@Nullable
                                         java.time.Duration duration,
                                         java.time.Duration maxDuration)
        Checks if the duration is considered "infinite".
        Parameters:
        duration - the Duration to validate
        maxDuration - the max Duration limit
        Returns:
        true if the passed duration is null or exceeds the maxDuration, false otherwise
      • toNanos

        public static long toNanos​(java.time.Duration duration)
        Converts a Duration to nanoseconds or if the resulting value would overflow a 64-bit signed integer then either Long.MIN_VALUE or Long.MAX_VALUE as appropriate.
        Parameters:
        duration - the duration to convert
        Returns:
        the converted nanoseconds value