Class Durations
- java.lang.Object
-
- com.google.protobuf.util.Durations
-
public final class Durations extends java.lang.Object
Utilities to help create/manipulateprotobuf/duration.proto
. All operations throw anIllegalArgumentException
if the input(s) are not valid.
-
-
Field Summary
Fields Modifier and Type Field Description static Duration
MAX_VALUE
A constant holding the maximum validDuration
, approximately+10,000
years.static Duration
MIN_VALUE
A constant holding the minimum validDuration
, approximately-10,000
years.static Duration
ZERO
A constant holding the duration of zero.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Duration
add(Duration d1, Duration d2)
Add two durations.static Duration
checkNotNegative(Duration duration)
Ensures that the givenDuration
is not negative.static Duration
checkPositive(Duration duration)
Ensures that the givenDuration
is positive.static Duration
checkValid(Duration duration)
Throws anIllegalArgumentException
if the givenDuration
is not valid.static Duration
checkValid(Duration.Builder durationBuilder)
Builds the given builder and throws anIllegalArgumentException
if it is not valid.static java.util.Comparator<Duration>
comparator()
Returns aComparator
forDuration
s which sorts in increasing chronological order.static int
compare(Duration x, Duration y)
Compares two durations.static Duration
fromDays(long days)
Create a Duration from the number of days.static Duration
fromHours(long hours)
Create a Duration from the number of hours.static Duration
fromMicros(long microseconds)
Create a Duration from the number of microseconds.static Duration
fromMillis(long milliseconds)
Create a Duration from the number of milliseconds.static Duration
fromMinutes(long minutes)
Create a Duration from the number of minutes.static Duration
fromNanos(long nanoseconds)
Create a Duration from the number of nanoseconds.static Duration
fromSeconds(long seconds)
Create a Duration from the number of seconds.static boolean
isNegative(Duration duration)
Returns whether the givenDuration
is negative or not.static boolean
isPositive(Duration duration)
Returns whether the givenDuration
is positive or not.static boolean
isValid(long seconds, int nanos)
Returns true if the given number of seconds and nanos is a validDuration
.static boolean
isValid(Duration duration)
Returns true if the givenDuration
is valid.static Duration
parse(java.lang.String value)
Parse a string to produce a duration.static Duration
parseUnchecked(java.lang.String value)
Parses a string in RFC 3339 format into aDuration
.static Duration
subtract(Duration d1, Duration d2)
Subtract a duration from another.static long
toDays(Duration duration)
Convert a Duration to the number of days.static long
toHours(Duration duration)
Convert a Duration to the number of hours.static long
toMicros(Duration duration)
Convert a Duration to the number of microseconds.static long
toMillis(Duration duration)
Convert a Duration to the number of milliseconds.static long
toMinutes(Duration duration)
Convert a Duration to the number of minutes.static long
toNanos(Duration duration)
Convert a Duration to the number of nanoseconds.static long
toSeconds(Duration duration)
Convert a Duration to the number of seconds.static double
toSecondsAsDouble(Duration duration)
Returns the number of seconds of the given duration as adouble
.static java.lang.String
toString(Duration duration)
Convert Duration to string format.
-
-
-
Field Detail
-
MIN_VALUE
public static final Duration MIN_VALUE
A constant holding the minimum validDuration
, approximately-10,000
years.
-
MAX_VALUE
public static final Duration MAX_VALUE
A constant holding the maximum validDuration
, approximately+10,000
years.
-
ZERO
public static final Duration ZERO
A constant holding the duration of zero.
-
-
Method Detail
-
comparator
public static java.util.Comparator<Duration> comparator()
Returns aComparator
forDuration
s which sorts in increasing chronological order. Nulls and invalidDuration
s are not allowed (seeisValid(com.google.protobuf.Duration)
). The returned comparator is serializable.
-
compare
public static int compare(Duration x, 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
ifx == y
; a value less than0
ifx < y
; and a value greater than0
ifx > y
-
isValid
public static boolean isValid(Duration duration)
Returns true if the givenDuration
is valid. Theseconds
value must be in the range [-315,576,000,000, +315,576,000,000]. Thenanos
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 negativenanos
field. For durations of one second or more, a non-zero value for thenanos
field must be of the same sign as theseconds
field.
-
isValid
public static boolean isValid(long seconds, int nanos)
Returns true if the given number of seconds and nanos is a validDuration
. Theseconds
value must be in the range [-315,576,000,000, +315,576,000,000]. Thenanos
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 negativenanos
field. For durations of one second or more, a non-zero value for thenanos
field must be of the same sign as theseconds
field.
-
isNegative
public static boolean isNegative(Duration duration)
Returns whether the givenDuration
is negative or not.
-
isPositive
public static boolean isPositive(Duration duration)
Returns whether the givenDuration
is positive or not.
-
checkNotNegative
@CanIgnoreReturnValue public static Duration checkNotNegative(Duration duration)
Ensures that the givenDuration
is not negative.- Throws:
java.lang.IllegalArgumentException
- ifduration
is negative or invalidjava.lang.NullPointerException
- ifduration
isnull
-
checkPositive
@CanIgnoreReturnValue public static Duration checkPositive(Duration duration)
Ensures that the givenDuration
is positive.- Throws:
java.lang.IllegalArgumentException
- ifduration
is negative,ZERO
, or invalidjava.lang.NullPointerException
- ifduration
isnull
-
checkValid
@CanIgnoreReturnValue public static Duration checkValid(Duration duration)
Throws anIllegalArgumentException
if the givenDuration
is not valid.
-
checkValid
public static Duration checkValid(Duration.Builder durationBuilder)
Builds the given builder and throws anIllegalArgumentException
if it is not valid. SeecheckValid(Duration)
.- Returns:
- A valid, built
Duration
.
-
toString
public static java.lang.String toString(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 Duration parse(java.lang.String value) throws java.text.ParseException
Parse a string to produce a duration.- Returns:
- a Duration parsed from the string
- Throws:
java.text.ParseException
- if the string is not in the duration format
-
parseUnchecked
public static Duration parseUnchecked(@CompileTimeConstant java.lang.String value)
Parses a string in RFC 3339 format into aDuration
.Identical to
parse(String)
, but throws anIllegalArgumentException
instead of aParseException
if parsing fails.- Returns:
- a
Duration
parsed from the string - Throws:
java.lang.IllegalArgumentException
- if parsing fails
-
fromDays
public static Duration fromDays(long days)
Create a Duration from the number of days.
-
fromHours
public static Duration fromHours(long hours)
Create a Duration from the number of hours.
-
fromMinutes
public static Duration fromMinutes(long minutes)
Create a Duration from the number of minutes.
-
fromSeconds
public static Duration fromSeconds(long seconds)
Create a Duration from the number of seconds.
-
fromMillis
public static Duration fromMillis(long milliseconds)
Create a Duration from the number of milliseconds.
-
fromMicros
public static Duration fromMicros(long microseconds)
Create a Duration from the number of microseconds.
-
fromNanos
public static Duration fromNanos(long nanoseconds)
Create a Duration from the number of nanoseconds.
-
toDays
public static long toDays(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(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(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(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(Duration duration)
Returns the number of seconds of the given duration as adouble
. This method should be used to accommodate APIs that only accept durations asdouble
values.This conversion may lose precision.
If you need the number of seconds in this duration as a
long
(not adouble
), simply useduration.getSeconds()
ortoSeconds(com.google.protobuf.Duration)
(which includes validation).
-
toMillis
public static long toMillis(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(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(Duration duration)
Convert a Duration to the number of nanoseconds.
-
-