Package com.complexible.common.base
Class Durations
- java.lang.Object
-
- com.complexible.common.base.Durations
-
public final class Durations extends java.lang.Object
- Since:
- 2.0
- Version:
- 2.0
- Author:
- Evren Sirin
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static long
parse(java.lang.String theDuration)
Parses the given readable representation of the duration into a milliseconds duration.static long
parse(java.lang.String theDuration, java.util.concurrent.TimeUnit theOutputUnit)
Parses the given readable representation of the duration into a duration value expressed in the requested time unit.static java.lang.String
readable(long theMilliseconds)
Returns a readable representation of the given milliseconds duration.static java.lang.String
readable(long theDuration, java.util.concurrent.TimeUnit theUnit)
Returns a readable representation of the given duration.
-
-
-
Method Detail
-
readable
public static java.lang.String readable(long theMilliseconds)
Returns a readable representation of the given milliseconds duration. This function is equivalent toreadable(theMilliseconds, TimeUnit.MILLISECONDS)
.- See Also:
readable(long, TimeUnit)
-
readable
public static java.lang.String readable(long theDuration, java.util.concurrent.TimeUnit theUnit)
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 integerN
andTimeUnit
T
, 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
public static long parse(java.lang.String theDuration) throws java.lang.IllegalArgumentException
Parses the given readable representation of the duration into a milliseconds duration. This function is equivalent toparse(theDuration, TimeUnit.MILLISECONDS)
.- Throws:
java.lang.IllegalArgumentException
- if the string representation is not a valid duration
-
parse
public static long parse(java.lang.String theDuration, java.util.concurrent.TimeUnit theOutputUnit)
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:
java.lang.IllegalArgumentException
- if the string representation is not a valid duration
-
-