Package org.forgerock.util.time
Class Duration
- java.lang.Object
-
- org.forgerock.util.time.Duration
-
- All Implemented Interfaces:
Comparable<Duration>
public class Duration extends Object implements Comparable<Duration>
Represents a duration in english. Cases is not important, plurals units are accepted. Notice that negative durations are not supported.6 days 59 minutes and 1 millisecond 1 minute and 10 seconds 42 millis unlimited none zero
-
-
Constructor Summary
Constructors Constructor Description Duration(Long number, TimeUnit unit)
Deprecated.Prefer the use ofduration(long, TimeUnit)
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareTo(Duration that)
Duration
convertTo(TimeUnit targetUnit)
Convert the current duration to a givenTimeUnit
.static Duration
duration(long number, TimeUnit unit)
Provides aDuration
, given a number and time unit.static Duration
duration(String value)
Provides aDuration
that represents the given duration expressed in english.boolean
equals(Object other)
TimeUnit
getUnit()
Returns theTimeUnit
this duration is expressed in.long
getValue()
Returns the number ofTimeUnit
this duration represents.int
hashCode()
boolean
isUnlimited()
Returns true if this Duration represents an unlimited (or indefinite) duration.boolean
isZero()
Returns true if this Duration represents a zero-length duration.long
to(TimeUnit targetUnit)
Convert the current duration to a number of givenTimeUnit
.Duration
toJavaDuration()
Returns this duration as a JavaDuration
.String
toString()
-
-
-
Constructor Detail
-
Duration
@Deprecated public Duration(Long number, TimeUnit unit)
Deprecated.Prefer the use ofduration(long, TimeUnit)
.Builds a newDuration
.- Parameters:
number
- number of time unit (cannot be null).unit
- TimeUnit to express the duration in (cannot be null).
-
-
Method Detail
-
duration
public static Duration duration(long number, TimeUnit unit)
Provides aDuration
, given a number and time unit.- Parameters:
number
- number of time unit.unit
- TimeUnit to express the duration in (cannot be null).- Returns:
Duration
instance
-
duration
public static Duration duration(String value)
Provides aDuration
that represents the given duration expressed in english.- Parameters:
value
- natural speech duration- Returns:
Duration
instance- Throws:
IllegalArgumentException
- if the input string is incorrectly formatted.
-
getValue
public long getValue()
Returns the number ofTimeUnit
this duration represents.- Returns:
- the number of
TimeUnit
this duration represents.
-
toJavaDuration
public Duration toJavaDuration()
Returns this duration as a JavaDuration
.- Returns:
- a new equivalent Java Duration
-
getUnit
public TimeUnit getUnit()
Returns theTimeUnit
this duration is expressed in.- Returns:
- the
TimeUnit
this duration is expressed in.
-
convertTo
public Duration convertTo(TimeUnit targetUnit)
Convert the current duration to a givenTimeUnit
. Conversions from finer to coarser granularities truncate, so loose precision.- Parameters:
targetUnit
- target unit of the conversion.- Returns:
- converted duration
- See Also:
TimeUnit.convert(long, TimeUnit)
-
to
public long to(TimeUnit targetUnit)
Convert the current duration to a number of givenTimeUnit
. Conversions from finer to coarser granularities truncate, so loose precision.- Parameters:
targetUnit
- target unit of the conversion.- Returns:
- converted duration value
- See Also:
TimeUnit.convert(long, TimeUnit)
-
isUnlimited
public boolean isUnlimited()
Returns true if this Duration represents an unlimited (or indefinite) duration.- Returns:
- true if this Duration represents an unlimited duration.
-
isZero
public boolean isZero()
Returns true if this Duration represents a zero-length duration.- Returns:
- true if this Duration represents a zero-length duration.
-
compareTo
public int compareTo(Duration that)
- Specified by:
compareTo
in interfaceComparable<Duration>
-
-