Class Durations


  • public final class Durations
    extends java.lang.Object
    Utilities to help create/manipulate protobuf/duration.proto. All operations throw an IllegalArgumentException if the input(s) are not valid.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static com.google.protobuf.Duration MAX_VALUE
      A constant holding the maximum valid Duration, approximately +10,000 years.
      static com.google.protobuf.Duration MIN_VALUE
      A constant holding the minimum valid Duration, approximately -10,000 years.
      static com.google.protobuf.Duration ZERO
      A constant holding the duration of zero.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static com.google.protobuf.Duration add​(com.google.protobuf.Duration d1, com.google.protobuf.Duration d2)
      Add two durations.
      static com.google.protobuf.Duration checkNotNegative​(com.google.protobuf.Duration duration)
      Ensures that the given Duration is not negative.
      static com.google.protobuf.Duration checkPositive​(com.google.protobuf.Duration duration)
      Ensures that the given Duration is positive.
      static com.google.protobuf.Duration checkValid​(com.google.protobuf.Duration duration)
      Throws an IllegalArgumentException if the given Duration is not valid.
      static com.google.protobuf.Duration checkValid​(com.google.protobuf.Duration.Builder durationBuilder)
      Builds the given builder and throws an IllegalArgumentException if it is not valid.
      static java.util.Comparator<com.google.protobuf.Duration> comparator()
      Returns a Comparator for Durations which sorts in increasing chronological order.
      static int compare​(com.google.protobuf.Duration x, com.google.protobuf.Duration y)
      Compares two durations.
      static com.google.protobuf.Duration fromDays​(long days)
      Create a Duration from the number of days.
      static com.google.protobuf.Duration fromHours​(long hours)
      Create a Duration from the number of hours.
      static com.google.protobuf.Duration fromMicros​(long microseconds)
      Create a Duration from the number of microseconds.
      static com.google.protobuf.Duration fromMillis​(long milliseconds)
      Create a Duration from the number of milliseconds.
      static com.google.protobuf.Duration fromMinutes​(long minutes)
      Create a Duration from the number of minutes.
      static com.google.protobuf.Duration fromNanos​(long nanoseconds)
      Create a Duration from the number of nanoseconds.
      static com.google.protobuf.Duration fromSeconds​(long seconds)
      Create a Duration from the number of seconds.
      static boolean isNegative​(com.google.protobuf.Duration duration)
      Returns whether the given Duration is negative or not.
      static boolean isValid​(long seconds, int nanos)
      Returns true if the given number of seconds and nanos is a valid Duration.
      static boolean isValid​(com.google.protobuf.Duration duration)
      Returns true if the given Duration is valid.
      static com.google.protobuf.Duration parse​(java.lang.String value)
      Parse from a string to produce a duration.
      static com.google.protobuf.Duration subtract​(com.google.protobuf.Duration d1, com.google.protobuf.Duration d2)
      Subtract a duration from another.
      static long toDays​(com.google.protobuf.Duration duration)
      Convert a Duration to the number of days.
      static long toHours​(com.google.protobuf.Duration duration)
      Convert a Duration to the number of hours.
      static long toMicros​(com.google.protobuf.Duration duration)
      Convert a Duration to the number of microseconds.
      static long toMillis​(com.google.protobuf.Duration duration)
      Convert a Duration to the number of milliseconds.
      static long toMinutes​(com.google.protobuf.Duration duration)
      Convert a Duration to the number of minutes.
      static long toNanos​(com.google.protobuf.Duration duration)
      Convert a Duration to the number of nanoseconds.
      static long toSeconds​(com.google.protobuf.Duration duration)
      Convert a Duration to the number of seconds.
      static double toSecondsAsDouble​(com.google.protobuf.Duration duration)
      Returns the number of seconds of the given duration as a double.
      static java.lang.String toString​(com.google.protobuf.Duration duration)
      Convert Duration to string format.
      • Methods inherited from class java.lang.Object

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

      • MIN_VALUE

        public static final com.google.protobuf.Duration MIN_VALUE
        A constant holding the minimum valid Duration, approximately -10,000 years.
      • MAX_VALUE

        public static final com.google.protobuf.Duration MAX_VALUE
        A constant holding the maximum valid Duration, approximately +10,000 years.
      • ZERO

        public static final com.google.protobuf.Duration ZERO
        A constant holding the duration of zero.
    • Method Detail

      • comparator

        public static java.util.Comparator<com.google.protobuf.Duration> comparator()
        Returns a Comparator for Durations which sorts in increasing chronological order. Nulls and invalid Durations are not allowed (see isValid(com.google.protobuf.Duration)).
      • compare

        public static int compare​(com.google.protobuf.Duration x,
                                  com.google.protobuf.Duration y)
        Compares two durations. The value returned is identical to what would be returned by: Durations.comparator().compare(x, y).
        Returns:
        the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y
      • isValid

        public static boolean isValid​(com.google.protobuf.Duration duration)
        Returns true if the given Duration is valid. The seconds value must be in the range [-315,576,000,000, +315,576,000,000]. The nanos value must be in the range [-999,999,999, +999,999,999].

        Note: Durations less than one second are represented with a 0 seconds field and a positive or negative nanos field. For durations of one second or more, a non-zero value for the nanos field must be of the same sign as the seconds field.

      • isValid

        public static boolean isValid​(long seconds,
                                      int nanos)
        Returns true if the given number of seconds and nanos is a valid Duration. The seconds value must be in the range [-315,576,000,000, +315,576,000,000]. The nanos value must be in the range [-999,999,999, +999,999,999].

        Note: Durations less than one second are represented with a 0 seconds field and a positive or negative nanos field. For durations of one second or more, a non-zero value for the nanos field must be of the same sign as the seconds field.

      • isNegative

        public static boolean isNegative​(com.google.protobuf.Duration duration)
        Returns whether the given Duration is negative or not.
      • checkNotNegative

        @CanIgnoreReturnValue
        public static com.google.protobuf.Duration checkNotNegative​(com.google.protobuf.Duration duration)
        Ensures that the given Duration is not negative.
        Throws:
        java.lang.IllegalArgumentException - if duration is negative or invalid
        java.lang.NullPointerException - if duration is null
      • checkPositive

        @CanIgnoreReturnValue
        public static com.google.protobuf.Duration checkPositive​(com.google.protobuf.Duration duration)
        Ensures that the given Duration is positive.
        Throws:
        java.lang.IllegalArgumentException - if duration is negative, ZERO, or invalid
        java.lang.NullPointerException - if duration is null
      • checkValid

        @CanIgnoreReturnValue
        public static com.google.protobuf.Duration checkValid​(com.google.protobuf.Duration duration)
        Throws an IllegalArgumentException if the given Duration is not valid.
      • checkValid

        public static com.google.protobuf.Duration checkValid​(com.google.protobuf.Duration.Builder durationBuilder)
        Builds the given builder and throws an IllegalArgumentException if it is not valid. See checkValid(Duration).
        Returns:
        A valid, built Duration.
      • toString

        public static java.lang.String toString​(com.google.protobuf.Duration duration)
        Convert Duration to string format. The string format will contains 3, 6, or 9 fractional digits depending on the precision required to represent the exact Duration value. For example: "1s", "1.010s", "1.000000100s", "-3.100s" The range that can be represented by Duration is from -315,576,000,000 to +315,576,000,000 inclusive (in seconds).
        Returns:
        The string representation of the given duration.
        Throws:
        java.lang.IllegalArgumentException - if the given duration is not in the valid range.
      • parse

        public static com.google.protobuf.Duration parse​(java.lang.String value)
                                                  throws java.text.ParseException
        Parse from a string to produce a duration.
        Returns:
        A Duration parsed from the string.
        Throws:
        java.text.ParseException - if parsing fails.
      • fromDays

        public static com.google.protobuf.Duration fromDays​(long days)
        Create a Duration from the number of days.
      • fromHours

        public static com.google.protobuf.Duration fromHours​(long hours)
        Create a Duration from the number of hours.
      • fromMinutes

        public static com.google.protobuf.Duration fromMinutes​(long minutes)
        Create a Duration from the number of minutes.
      • fromSeconds

        public static com.google.protobuf.Duration fromSeconds​(long seconds)
        Create a Duration from the number of seconds.
      • fromMillis

        public static com.google.protobuf.Duration fromMillis​(long milliseconds)
        Create a Duration from the number of milliseconds.
      • fromMicros

        public static com.google.protobuf.Duration fromMicros​(long microseconds)
        Create a Duration from the number of microseconds.
      • fromNanos

        public static com.google.protobuf.Duration fromNanos​(long nanoseconds)
        Create a Duration from the number of nanoseconds.
      • toDays

        public static long toDays​(com.google.protobuf.Duration duration)
        Convert a Duration to the number of days. The result will be rounded towards 0 to the nearest day.
      • toHours

        public static long toHours​(com.google.protobuf.Duration duration)
        Convert a Duration to the number of hours. The result will be rounded towards 0 to the nearest hour.
      • toMinutes

        public static long toMinutes​(com.google.protobuf.Duration duration)
        Convert a Duration to the number of minutes. The result will be rounded towards 0 to the nearest minute.
      • toSeconds

        public static long toSeconds​(com.google.protobuf.Duration duration)
        Convert a Duration to the number of seconds. The result will be rounded towards 0 to the nearest second. E.g., if the duration represents -1 nanosecond, it will be rounded to 0.
      • toSecondsAsDouble

        public static double toSecondsAsDouble​(com.google.protobuf.Duration duration)
        Returns the number of seconds of the given duration as a double. This method should be used to accommodate APIs that only accept durations as double values.

        This conversion may lose precision.

        If you need the number of seconds in this duration as a long (not a double), simply use duration.getSeconds() or toSeconds(com.google.protobuf.Duration) (which includes validation).

      • toMillis

        public static long toMillis​(com.google.protobuf.Duration duration)
        Convert a Duration to the number of milliseconds. The result will be rounded towards 0 to the nearest millisecond. E.g., if the duration represents -1 nanosecond, it will be rounded to 0.
      • toMicros

        public static long toMicros​(com.google.protobuf.Duration duration)
        Convert a Duration to the number of microseconds. The result will be rounded towards 0 to the nearest microseconds. E.g., if the duration represents -1 nanosecond, it will be rounded to 0.
      • toNanos

        public static long toNanos​(com.google.protobuf.Duration duration)
        Convert a Duration to the number of nanoseconds.
      • add

        public static com.google.protobuf.Duration add​(com.google.protobuf.Duration d1,
                                                       com.google.protobuf.Duration d2)
        Add two durations.
      • subtract

        public static com.google.protobuf.Duration subtract​(com.google.protobuf.Duration d1,
                                                            com.google.protobuf.Duration d2)
        Subtract a duration from another.