Package de.xima.fc.common.bean_models
Class TimeDuration
- java.lang.Object
-
- de.xima.fc.common.bean_models.TimeDuration
-
- All Implemented Interfaces:
Serializable
,Comparable<TimeDuration>
public final class TimeDuration extends Object implements Comparable<TimeDuration>, Serializable
Model class with bean validation for a duration. Consists of a value and a unit of time.- Since:
- 8.1.0
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
TimeDuration.duration
Custom label for thegetDuration()
property.static interface
TimeDuration.unit
Custom label for thegetUnit()
property.
-
Constructor Summary
Constructors Constructor Description TimeDuration()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareTo(TimeDuration that)
boolean
equals(Object obj)
Long
getDuration()
Gets the duration, interpreted according to the configured unit of time.ETimeUnit
getUnit()
Gets the unit of time for the duration.int
hashCode()
static TimeDuration
of(Long duration, ETimeUnit unit)
Creates a new instance ofTimeDuration
with the given duration and unit of time.static TimeDuration
ofDays(long duration)
Creates a new instance ofTimeDuration
with the given duration in days.static TimeDuration
ofEmpty(ETimeUnit unit)
Creates a new instance ofTimeDuration
with no duration and the given unit of time.static TimeDuration
ofHours(long duration)
Creates a new instance ofTimeDuration
with the given duration in hours.static TimeDuration
ofMicroseconds(long duration)
Creates a new instance ofTimeDuration
with the given duration in microseconds.static TimeDuration
ofMilliseconds(long duration)
Creates a new instance ofTimeDuration
with the given duration in milliseconds.static TimeDuration
ofMinutes(long duration)
Creates a new instance ofTimeDuration
with the given duration in minutes.static TimeDuration
ofNanoseconds(long duration)
Creates a new instance ofTimeDuration
with the given duration in nanoseconds.static TimeDuration
ofSeconds(long duration)
Creates a new instance ofTimeDuration
with the given duration in seconds.static TimeDuration
ofWeeks(long duration)
Creates a new instance ofTimeDuration
with the given duration in weeks.void
setDuration(Long duration)
Sets the duration, interpreted according to the configured unit of time.void
setUnit(ETimeUnit unit)
Sets the unit of time for the duration.long
to(ETimeUnit targetUnit)
Converts the duration to the given unit of time.long
toDays()
Gets the duration in days.Duration
toDuration()
Converts this duration to aDuration
.long
toHours()
Gets the duration in hours.long
toMicroseconds()
Gets the duration in microseconds.long
toMilliseconds()
Gets the duration in milliseconds.long
toMinutes()
Gets the duration in minutes.long
toNanoseconds()
Gets the duration in nanoseconds.long
toSeconds()
Gets the duration in seconds.long
toWeeks()
Gets the duration in weeks.
-
-
-
Method Detail
-
compareTo
public int compareTo(TimeDuration that)
- Specified by:
compareTo
in interfaceComparable<TimeDuration>
-
getDuration
@Nullable public Long getDuration()
Gets the duration, interpreted according to the configured unit of time. A value of0
means no restriction (unlimited), a value of-1
means the default system timeout.May be
null
if the duration is not set.- Returns:
- The duration.
-
setDuration
public void setDuration(@Nullable Long duration)
Sets the duration, interpreted according to the configured unit of time. A value of0
means no restriction (unlimited), a value of-1
means the default system timeout.May be
null
if the duration is not set.- Parameters:
duration
- The duration.
-
getUnit
public ETimeUnit getUnit()
Gets the unit of time for the duration.- Returns:
- The unit of time for the duration.
-
setUnit
public void setUnit(ETimeUnit unit)
Sets the unit of time for the duration.- Parameters:
unit
- The unit of time for the duration.
-
to
public long to(ETimeUnit targetUnit)
Converts the duration to the given unit of time.- Parameters:
targetUnit
- The target unit of time.- Returns:
- The duration in the target unit of time.
-
toDays
public long toDays()
Gets the duration in days.- Returns:
- The duration in days.
-
toDuration
public Duration toDuration()
Converts this duration to aDuration
.- Returns:
- A
Duration
object representing the same duration.
-
toHours
public long toHours()
Gets the duration in hours.- Returns:
- The duration in hours.
-
toMicroseconds
public long toMicroseconds()
Gets the duration in microseconds.- Returns:
- The duration in microseconds.
-
toMilliseconds
public long toMilliseconds()
Gets the duration in milliseconds.- Returns:
- The duration in milliseconds.
-
toMinutes
public long toMinutes()
Gets the duration in minutes.- Returns:
- The duration in minutes.
-
toNanoseconds
public long toNanoseconds()
Gets the duration in nanoseconds.- Returns:
- The duration in nanoseconds.
-
toSeconds
public long toSeconds()
Gets the duration in seconds.- Returns:
- The duration in seconds.
-
toWeeks
public long toWeeks()
Gets the duration in weeks.- Returns:
- The duration in weeks.
-
of
public static TimeDuration of(Long duration, ETimeUnit unit)
Creates a new instance ofTimeDuration
with the given duration and unit of time.- Parameters:
duration
- The duration, interpreted according to the given unit of time.unit
- The unit of time for the duration.- Returns:
- A new instance of
TimeDuration
with the given duration and unit of time.
-
ofDays
public static TimeDuration ofDays(long duration)
Creates a new instance ofTimeDuration
with the given duration in days.- Parameters:
duration
- The duration in days.- Returns:
- A new instance of
TimeDuration
with the given duration in days.
-
ofEmpty
public static TimeDuration ofEmpty(ETimeUnit unit)
Creates a new instance ofTimeDuration
with no duration and the given unit of time.- Parameters:
unit
- The unit of time for the duration.- Returns:
- A new instance of
TimeDuration
with no duration and the given unit of time.
-
ofHours
public static TimeDuration ofHours(long duration)
Creates a new instance ofTimeDuration
with the given duration in hours.- Parameters:
duration
- The duration in hours.- Returns:
- A new instance of
TimeDuration
with the given duration in hours.
-
ofMicroseconds
public static TimeDuration ofMicroseconds(long duration)
Creates a new instance ofTimeDuration
with the given duration in microseconds.- Parameters:
duration
- The duration in microseconds.- Returns:
- A new instance of
TimeDuration
with the given duration in microseconds.
-
ofMilliseconds
public static TimeDuration ofMilliseconds(long duration)
Creates a new instance ofTimeDuration
with the given duration in milliseconds.- Parameters:
duration
- The duration in milliseconds.- Returns:
- A new instance of
TimeDuration
with the given duration in milliseconds.
-
ofMinutes
public static TimeDuration ofMinutes(long duration)
Creates a new instance ofTimeDuration
with the given duration in minutes.- Parameters:
duration
- The duration in minutes.- Returns:
- A new instance of
TimeDuration
with the given duration in minutes.
-
ofNanoseconds
public static TimeDuration ofNanoseconds(long duration)
Creates a new instance ofTimeDuration
with the given duration in nanoseconds.- Parameters:
duration
- The duration in nanoseconds.- Returns:
- A new instance of
TimeDuration
with the given duration in nanoseconds.
-
ofSeconds
public static TimeDuration ofSeconds(long duration)
Creates a new instance ofTimeDuration
with the given duration in seconds.- Parameters:
duration
- The duration in seconds.- Returns:
- A new instance of
TimeDuration
with the given duration in seconds.
-
ofWeeks
public static TimeDuration ofWeeks(long duration)
Creates a new instance ofTimeDuration
with the given duration in weeks.- Parameters:
duration
- The duration in weeks.- Returns:
- A new instance of
TimeDuration
with the given duration in weeks.
-
-