Package com.complexible.common.base
Class Durations
java.lang.Object
com.complexible.common.base.Durations
- Since:
- 2.0
- Version:
- 2.0
- Author:
- Evren Sirin
-
Method Summary
Modifier and TypeMethodDescriptionstatic longParses the given readable representation of the duration into a milliseconds duration.static longParses the given readable representation of the duration into a duration value expressed in the requested time unit.static Stringreadable(long theMilliseconds) Returns a readable representation of the given milliseconds duration.static StringReturns a readable representation of the given duration.
-
Method Details
-
readable
Returns a readable representation of the given milliseconds duration. This function is equivalent toreadable(theMilliseconds, TimeUnit.MILLISECONDS).- See Also:
-
readable
Returns a readable representation of the given duration. The readable duration is a positive integer followed by either letter 'h' (for hours), letter 'm' (for minutes), letter 's' (for seconds), or letters 'ms' (for milliseconds). The function chooses the most appropriate unit to use in the output without losing precision. It is the case that for every integerNandTimeUnitT, we will haveparse(readable(N, T)) == T.toMillis(N).Examples:
readable(1, TimeUnit.SECONDS)returns"1s"readable(60, TimeUnit.SECONDS)returns"1m"readable(80, TimeUnit.SECONDS)returns"80s"readable(12000, TimeUnit.SECONDS)returns"12m"
- Parameters:
theDuration- duration valuetheUnit- time unit for the input duration value- Returns:
- readable representation of the given duration
-
parse
Parses the given readable representation of the duration into a milliseconds duration. This function is equivalent toparse(theDuration, TimeUnit.MILLISECONDS).- Throws:
IllegalArgumentException- if the string representation is not a valid duration
-
parse
Parses the given readable representation of the duration into a duration value expressed in the requested time unit. The valid string representations are the ones that are generated by thereadable(long, TimeUnit)function. If the requested time unit is coarser than the duration represented by the string the result will lose precisions. For example,parse("50s", TimeUnit.MINUTES)would return0.- Parameters:
theDuration- readable representation of a duration valuetheOutputUnit- the unit of the output value- Returns:
- a duration value expressed in the requested time unit that is represented
- Throws:
IllegalArgumentException- if the string representation is not a valid duration
-