Class DurationUtils

java.lang.Object
io.servicetalk.utils.internal.DurationUtils

public final class DurationUtils extends Object
Helper utilities for Duration.
  • Method Details

    • isPositive

      public static boolean isPositive(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 Duration ensurePositive(Duration duration, 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:
      NullPointerException - if the passed duration is null
      IllegalArgumentException - if the passed duration is not greater than Duration.ZERO
    • ensureNonNegative

      public static Duration ensureNonNegative(Duration duration, String name)
      Ensures the duration is non-negative.
      Parameters:
      duration - the Duration to validate
      name - name of the Duration variable
      Returns:
      the passed duration if all checks pass
      Throws:
      NullPointerException - if the passed duration is null
      IllegalArgumentException - if the passed duration is not greater or equal to Duration.ZERO
    • isInfinite

      public static boolean isInfinite(@Nullable Duration duration, 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(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