Class MachineTime<U>
- Type Parameters:
U
- eitherTimeUnit
orSI
- All Implemented Interfaces:
Serializable
,Comparable<MachineTime<U>>
,TimeSpan<U>
Represents a duration for machine times in decimal seconds with nanosecond precision.
Note: Other time units are NOT contained but can be used in construction of a machine time. Example:
MachineTime<TimeUnit> mt = MachineTime.of(1, TimeUnit.HOURS); System.out.println(mt.contains(TimeUnit.HOURS)); // false System.out.println(mt.getSeconds); // 3600L
- Since:
- 5.0
- Author:
- Meno Hochschild
- See Also:
TimeUnit.SECONDS
,TimeUnit.NANOSECONDS
,SI.SECONDS
,SI.NANOSECONDS
, Serialized Form
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Non-localized and user-defined format for machine-time-durations based on a pattern containing some standard symbols and literals.Nested classes/interfaces inherited from interface net.time4j.engine.TimeSpan
TimeSpan.Item<U>
-
Field Summary
Modifier and TypeFieldDescriptionstatic TimeMetric<TimeUnit,MachineTime<TimeUnit>>
Reversible metric on the POSIX scale (without leap seconds).static TimeMetric<TimeUnit,MachineTime<SI>>
Reversible metric on the UTC scale (inclusive leap seconds). -
Method Summary
Modifier and TypeMethodDescriptionabs()
Converts this machine duration to its absolute amount.addTo(T time)
Adds this time span to given time point.int
compareTo(MachineTime<U> other)
Method of theComparable
-interface.boolean
Queries if given time unit is part of this time span.dividedBy(long divisor, RoundingMode roundingMode)
Divides this duration by given divisor using given rounding mode.boolean
static MachineTime<TimeUnit>
Converts given JSR-310-duration to a machine time.int
Yields the normalized nanosecond fraction of this duration.long
getPartialAmount(Object unit)
Yields the partial amount associated with given time unit.getScale()
Yields the related time scale.long
Yields the normalized seconds of this duration.Yields all containted time span items with amount and unit in the order from largest to smallest time units.int
hashCode()
inverse()
Creates a copy with inversed sign.boolean
isEmpty()
Queries if this time span is empty.boolean
isLongerThan(MachineTime<U> other)
Compares the absolute lengths and is equivalent toabs().compareTo(other.abs()) > 0
.boolean
Queries if this time span is negative.boolean
Queries if this time span is positive.boolean
isShorterThan(MachineTime<U> other)
Compares the absolute lengths and is equivalent toabs().compareTo(other.abs()) < 0
.Subtracts given temporal amount from this machine time.minus(MachineTime<U> duration)
Subtracts given temporal amount from this machine time.multipliedBy(double factor)
Multiplies this duration with given decimal factor.multipliedBy(long factor)
Multiplies this duration with given factor.static MachineTime<TimeUnit>
Creates a machine time duration on the POSIX scale.static MachineTime<SI>
Creates a machine time duration on the UTC scale.static MachineTime<TimeUnit>
ofPosixSeconds(double seconds)
Creates a machine time duration on the POSIX scale.static MachineTime<TimeUnit>
ofPosixSeconds(BigDecimal seconds)
Creates a machine time duration on the POSIX scale.static MachineTime<TimeUnit>
ofPosixUnits(long seconds, int fraction)
Creates a machine time duration on the POSIX scale.static MachineTime<SI>
ofSISeconds(double seconds)
Creates a machine time duration on the UTC scale.static MachineTime<SI>
ofSISeconds(BigDecimal seconds)
Creates a machine time duration on the UTC scale.static MachineTime<SI>
ofSIUnits(long seconds, int fraction)
Creates a machine time duration on the UTC scale.Add given temporal amount to this machine time.plus(MachineTime<U> duration)
Add given temporal amount to this machine time.subtractFrom(T time)
Subtracts this time span from given time point.static Collector<MachineTime<TimeUnit>,?,MachineTime<TimeUnit>>
Helps to sum up durations of a stream.static Collector<MachineTime<SI>,?,MachineTime<SI>>
Helps to sum up durations of a stream.Converts this machine time duration into a decimal number of seconds.toString()
Returns a format in technical notation including the name of the underlying time scale.Converts this machine time to its JSR-310-equivalent.
-
Field Details
-
ON_POSIX_SCALE
Reversible metric on the POSIX scale (without leap seconds).- Since:
- 2.0
-
ON_UTC_SCALE
Reversible metric on the UTC scale (inclusive leap seconds).
Time points before 1972 are not supported.
- Since:
- 2.0
-
-
Method Details
-
of
Creates a machine time duration on the POSIX scale.
- Parameters:
amount
- of unitsunit
- helps to interprete given amount- Returns:
- new machine time duration
- Throws:
ArithmeticException
- in case of numerical overflow- Since:
- 2.0
-
of
Creates a machine time duration on the UTC scale.
- Parameters:
amount
- of unitsunit
- helps to interprete given amount- Returns:
- new machine time duration
- Since:
- 2.0
-
ofPosixUnits
Creates a machine time duration on the POSIX scale.
- Parameters:
seconds
- POSIX-secondsfraction
- nanosecond part- Returns:
- new machine time duration
- Throws:
ArithmeticException
- in case of numerical overflow- Since:
- 2.0
-
ofSIUnits
Creates a machine time duration on the UTC scale.
- Parameters:
seconds
- SI-secondsfraction
- nanosecond part- Returns:
- new machine time duration
- Throws:
ArithmeticException
- in case of numerical overflow- Since:
- 2.0
-
ofPosixSeconds
Creates a machine time duration on the POSIX scale.
- Parameters:
seconds
- decimal POSIX-seconds- Returns:
- new machine time duration
- Throws:
ArithmeticException
- in case of numerical overflowIllegalArgumentException
- if the argument is infinite or NaN- Since:
- 2.0
-
ofPosixSeconds
Creates a machine time duration on the POSIX scale.
- Parameters:
seconds
- decimal POSIX-seconds- Returns:
- new machine time duration
- Throws:
ArithmeticException
- in case of numerical overflow- Since:
- 2.0
-
ofSISeconds
Creates a machine time duration on the UTC scale.
- Parameters:
seconds
- decimal SI-seconds- Returns:
- new machine time duration
- Throws:
ArithmeticException
- in case of numerical overflowIllegalArgumentException
- if the argument is infinite or NaN- Since:
- 2.0
-
ofSISeconds
Creates a machine time duration on the UTC scale.
- Parameters:
seconds
- decimal SI-seconds- Returns:
- new machine time duration
- Throws:
ArithmeticException
- in case of numerical overflow- Since:
- 2.0
-
getSeconds
public long getSeconds()Yields the normalized seconds of this duration.
The normalization happens in case of a negative duration such that any fraction part falls into the range
0-999999999
. In this case, following expression is NOT true:Math.abs(getSeconds()) == getPartialAmount(TimeUnit.SECONDS)
- Returns:
- long
- See Also:
getFraction()
-
getFraction
public int getFraction()Yields the normalized nanosecond fraction of this duration.
- Returns:
- nanosecond in range
0-999999999
- See Also:
getSeconds()
-
getScale
Yields the related time scale.
- Returns:
- either
TimeScale.POSIX
orTimeScale.UTC
- Since:
- 2.0
-
getTotalLength
Description copied from interface:TimeSpan
Yields all containted time span items with amount and unit in the order from largest to smallest time units.
- Specified by:
getTotalLength
in interfaceTimeSpan<U>
- Returns:
- unmodifiable list sorted by precision of units in ascending order where every time unit exists at most once
-
contains
Description copied from interface:TimeSpan
Queries if given time unit is part of this time span.
By default the implementation uses following expression:
for (Item<?> item : getTotalLength()) { if (item.getUnit().equals(unit)) { return (item.getAmount() > 0); } } return false;
- Specified by:
contains
in interfaceTimeSpan<U>
- Parameters:
unit
- time unit to be asked (optional)- Returns:
true
if exists elsefalse
- See Also:
getPartialAmount(U)
-
getPartialAmount
Description copied from interface:TimeSpan
Yields the partial amount associated with given time unit.
The method returns
0
if this time span does not contain given time unit. In order to get the total length/amount of this time span users have to evaluate the methodTimeSpan.getTotalLength()
instead.- Specified by:
getPartialAmount
in interfaceTimeSpan<U>
- Parameters:
unit
- time unit (optional)- Returns:
- amount as part of time span (
>= 0
)
-
isNegative
public boolean isNegative()Description copied from interface:TimeSpan
Queries if this time span is negative.
A negative time span relates to the subtraction of two time points where first one is after second one. The partial amounts of every time span are never negative. Hence this attribute is not associated with the partial amounts but only with the time span itself.
Note: An empty time span itself is never negative in agreement with the mathematical relation
(-1) * 0 = 0
.- Specified by:
isNegative
in interfaceTimeSpan<U>
- Returns:
true
if negative and not empty elsefalse
-
isPositive
public boolean isPositive()Description copied from interface:TimeSpan
Queries if this time span is positive.
A time span is positive if it is neither empty nor negative.
- Specified by:
isPositive
in interfaceTimeSpan<U>
- Returns:
true
if positive and not empty elsefalse
- See Also:
TimeSpan.isEmpty()
,TimeSpan.isNegative()
-
isEmpty
public boolean isEmpty()Description copied from interface:TimeSpan
Queries if this time span is empty.
Per definition an empty time span has no items with a partial amount different from
0
. -
plus
Add given temporal amount to this machine time.
- Parameters:
amount
- the amount to be addedunit
- the related time unit- Returns:
- result of addition
- Throws:
ArithmeticException
- in case of numerical overflow- Since:
- 2.0
-
plus
Add given temporal amount to this machine time.
- Parameters:
duration
- other machine time to be added- Returns:
- result of addition
- Throws:
ArithmeticException
- in case of numerical overflow- Since:
- 2.0
-
minus
Subtracts given temporal amount from this machine time.
- Parameters:
amount
- the amount to be subtractedunit
- the related time unit- Returns:
- difference result
- Throws:
ArithmeticException
- in case of numerical overflow- Since:
- 2.0
-
minus
Subtracts given temporal amount from this machine time.
- Parameters:
duration
- other machine time to be subtracted- Returns:
- difference result
- Throws:
ArithmeticException
- in case of numerical overflow- Since:
- 2.0
-
abs
Converts this machine duration to its absolute amount.
- Returns:
- absolute machine time duration, always non-negative
- Throws:
ArithmeticException
- in case of numerical overflow- Since:
- 2.0
-
inverse
Creates a copy with inversed sign.
- Returns:
- negated machine time duration
- Throws:
ArithmeticException
- in case of numerical overflow- Since:
- 2.0
-
multipliedBy
Multiplies this duration with given factor.
- Parameters:
factor
- multiplicand- Returns:
- changed copy of this duration
-
multipliedBy
Multiplies this duration with given decimal factor.
If more rounding control is needed then users might consider the alternatives
multipliedBy(long)
anddividedBy(long, RoundingMode)
.- Parameters:
factor
- multiplicand- Returns:
- changed copy of this duration
- Throws:
IllegalArgumentException
- if given factor is not a finite number
-
dividedBy
Divides this duration by given divisor using given rounding mode.
- Parameters:
divisor
- divisorroundingMode
- rounding mode to be used in division- Returns:
- changed copy of this duration
- Since:
- 3.23/4.19
-
addTo
Description copied from interface:TimeSpan
Adds this time span to given time point.
Is equivalent to the expression
time.plus(this)
. Due to better readability usage of theTimePoint
-method is recommended. Implementations are required to document the used algorithm in detailed manner.- Specified by:
addTo
in interfaceTimeSpan<U>
- Type Parameters:
T
- generic type of time point- Parameters:
time
- reference time point to add this time span to- Returns:
- new time point as result of addition
- See Also:
TimeSpan.subtractFrom(TimePoint)
-
subtractFrom
Description copied from interface:TimeSpan
Subtracts this time span from given time point.
Is equivalent to the expression
time.minus(this)
. Due to better readability usage of theTimePoint
-method is recommended. Implementations are required to document the used algorithm in detailed manner.- Specified by:
subtractFrom
in interfaceTimeSpan<U>
- Type Parameters:
T
- generic type of time point- Parameters:
time
- reference time point to subtract this time span from- Returns:
- new time point as result of subtraction
- See Also:
TimeSpan.addTo(TimePoint)
-
summingUpPosix
Helps to sum up durations of a stream.
A final normalization is automatically done.
- Returns:
- Collector for summing up durations in a stream
- Since:
- 5.0
- See Also:
Duration.summingUp()
,summingUpReal()
-
summingUpReal
Helps to sum up durations of a stream.
A final normalization is automatically done.
- Returns:
- Collector for summing up durations in a stream
- Since:
- 5.0
- See Also:
Duration.summingUp()
,summingUpPosix()
-
isShorterThan
Compares the absolute lengths and is equivalent to
abs().compareTo(other.abs()) < 0
.- Parameters:
other
- another machine time to be compared with- Returns:
- boolean
- Since:
- 3.20/4.16
- See Also:
compareTo(MachineTime)
,isLongerThan(MachineTime)
-
isLongerThan
Compares the absolute lengths and is equivalent to
abs().compareTo(other.abs()) > 0
.- Parameters:
other
- another machine time to be compared with- Returns:
- boolean
- Since:
- 3.20/4.16
- See Also:
compareTo(MachineTime)
,isShorterThan(MachineTime)
-
compareTo
Method of the
Comparable
-interface.- Specified by:
compareTo
in interfaceComparable<U>
- Parameters:
other
- another machine time to be compared with- Returns:
- negative, zero or positive integer if this instance is shorter, equal or longer than other one
- Throws:
ClassCastException
- if this and the other machine time have different time scales- Since:
- 3.20/4.16
- See Also:
isShorterThan(MachineTime)
,isLongerThan(MachineTime)
-
equals
-
hashCode
public int hashCode() -
toString
Returns a format in technical notation including the name of the underlying time scale.
-
toBigDecimal
Converts this machine time duration into a decimal number of seconds.
- Returns:
- BigDecimal
-
toTemporalAmount
Converts this machine time to its JSR-310-equivalent.
- Returns:
- java.time.Duration
- Throws:
IllegalStateException
- if this instance is not based onjava.util.concurrent.TimeUnit
- Since:
- 4.28
- See Also:
from(java.time.Duration)
-
from
Converts given JSR-310-duration to a machine time.
- Parameters:
threeten
- duration defined in seconds and nanoseconds (JSR-310-API)- Returns:
- MachineTime
- Since:
- 4.28
- See Also:
toTemporalAmount()
-