U - either TimeUnit or SIpublic final class MachineTime<U> extends Object implements TimeSpan<U>, Comparable<MachineTime<U>>, Serializable
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
TimeUnit.SECONDS,
TimeUnit.NANOSECONDS,
SI.SECONDS,
SI.NANOSECONDS,
Serialized Form| Modifier and Type | Class and Description |
|---|---|
static class |
MachineTime.Formatter
Non-localized and user-defined format for machine-time-durations based on a
pattern containing some standard symbols and literals.
|
TimeSpan.Item<U>| Modifier and Type | Field and Description |
|---|---|
static TimeMetric<TimeUnit,MachineTime<TimeUnit>> |
ON_POSIX_SCALE
Reversible metric on the POSIX scale (without leap seconds).
|
static TimeMetric<TimeUnit,MachineTime<SI>> |
ON_UTC_SCALE
Reversible metric on the UTC scale (inclusive leap seconds).
|
| Modifier and Type | Method and Description |
|---|---|
MachineTime<U> |
abs()
Converts this machine duration to its absolute amount.
|
<T extends TimePoint<? super U,T>> |
addTo(T time)
Adds this time span to given time point.
|
int |
compareTo(MachineTime<U> other)
Method of the
Comparable-interface. |
boolean |
contains(Object unit)
Queries if given time unit is part of this time span.
|
MachineTime<U> |
dividedBy(long divisor,
RoundingMode roundingMode)
Divides this duration by given divisor using given rounding mode.
|
boolean |
equals(Object obj) |
static MachineTime<TimeUnit> |
from(Duration threeten)
Converts given JSR-310-duration to a machine time.
|
int |
getFraction()
Yields the normalized nanosecond fraction of this duration.
|
long |
getPartialAmount(Object unit)
Yields the partial amount associated with given time unit.
|
TimeScale |
getScale()
Yields the related time scale.
|
long |
getSeconds()
Yields the normalized seconds of this duration.
|
List<TimeSpan.Item<U>> |
getTotalLength()
Yields all containted time span items with amount and unit in
the order from largest to smallest time units.
|
int |
hashCode() |
MachineTime<U> |
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 to
abs().compareTo(other.abs()) > 0. |
boolean |
isNegative()
Queries if this time span is negative.
|
boolean |
isPositive()
Queries if this time span is positive.
|
boolean |
isShorterThan(MachineTime<U> other)
Compares the absolute lengths and is equivalent to
abs().compareTo(other.abs()) < 0. |
MachineTime<U> |
minus(long amount,
U unit)
Subtracts given temporal amount from this machine time.
|
MachineTime<U> |
minus(MachineTime<U> duration)
Subtracts given temporal amount from this machine time.
|
MachineTime<U> |
multipliedBy(double factor)
Multiplies this duration with given decimal factor.
|
MachineTime<U> |
multipliedBy(long factor)
Multiplies this duration with given factor.
|
static MachineTime<SI> |
of(long amount,
SI unit)
Creates a machine time duration on the UTC scale.
|
static MachineTime<TimeUnit> |
of(long amount,
TimeUnit unit)
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> |
ofPosixSeconds(double 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(BigDecimal seconds)
Creates a machine time duration on the UTC scale.
|
static MachineTime<SI> |
ofSISeconds(double 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.
|
MachineTime<U> |
plus(long amount,
U unit)
Add given temporal amount to this machine time.
|
MachineTime<U> |
plus(MachineTime<U> duration)
Add given temporal amount to this machine time.
|
<T extends TimePoint<? super U,T>> |
subtractFrom(T time)
Subtracts this time span from given time point.
|
static Collector<MachineTime<TimeUnit>,?,MachineTime<TimeUnit>> |
summingUpPosix()
Helps to sum up durations of a stream.
|
static Collector<MachineTime<SI>,?,MachineTime<SI>> |
summingUpReal()
Helps to sum up durations of a stream.
|
BigDecimal |
toBigDecimal()
Converts this machine time duration into a decimal number of seconds.
|
String |
toString()
Returns a format in technical notation including the name of the underlying time scale.
|
Duration |
toTemporalAmount()
Converts this machine time to its JSR-310-equivalent.
|
public static final TimeMetric<TimeUnit,MachineTime<TimeUnit>> ON_POSIX_SCALE
public static final TimeMetric<TimeUnit,MachineTime<SI>> ON_UTC_SCALE
Reversible metric on the UTC scale (inclusive leap seconds).
Time points before 1972 are not supported.
public static MachineTime<TimeUnit> of(long amount, TimeUnit unit)
Creates a machine time duration on the POSIX scale.
amount - of unitsunit - helps to interprete given amountArithmeticException - in case of numerical overflowpublic static MachineTime<SI> of(long amount, SI unit)
Creates a machine time duration on the UTC scale.
amount - of unitsunit - helps to interprete given amountpublic static MachineTime<TimeUnit> ofPosixUnits(long seconds, int fraction)
Creates a machine time duration on the POSIX scale.
seconds - POSIX-secondsfraction - nanosecond partArithmeticException - in case of numerical overflowpublic static MachineTime<SI> ofSIUnits(long seconds, int fraction)
Creates a machine time duration on the UTC scale.
seconds - SI-secondsfraction - nanosecond partArithmeticException - in case of numerical overflowpublic static MachineTime<TimeUnit> ofPosixSeconds(double seconds)
Creates a machine time duration on the POSIX scale.
seconds - decimal POSIX-secondsArithmeticException - in case of numerical overflowIllegalArgumentException - if the argument is infinite or NaNpublic static MachineTime<TimeUnit> ofPosixSeconds(BigDecimal seconds)
Creates a machine time duration on the POSIX scale.
seconds - decimal POSIX-secondsArithmeticException - in case of numerical overflowpublic static MachineTime<SI> ofSISeconds(double seconds)
Creates a machine time duration on the UTC scale.
seconds - decimal SI-secondsArithmeticException - in case of numerical overflowIllegalArgumentException - if the argument is infinite or NaNpublic static MachineTime<SI> ofSISeconds(BigDecimal seconds)
Creates a machine time duration on the UTC scale.
seconds - decimal SI-secondsArithmeticException - in case of numerical overflowpublic 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)
getFraction()public int getFraction()
Yields the normalized nanosecond fraction of this duration.
0-999999999getSeconds()public TimeScale getScale()
Yields the related time scale.
TimeScale.POSIX or TimeScale.UTCpublic List<TimeSpan.Item<U>> getTotalLength()
TimeSpanYields all containted time span items with amount and unit in the order from largest to smallest time units.
getTotalLength in interface TimeSpan<U>public boolean contains(Object unit)
TimeSpanQueries 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;
contains in interface TimeSpan<U>unit - time unit to be asked (optional)true if exists else falsegetPartialAmount(U)public long getPartialAmount(Object unit)
TimeSpanYields 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 method TimeSpan.getTotalLength()
instead.
getPartialAmount in interface TimeSpan<U>unit - time unit (optional)>= 0)public boolean isNegative()
TimeSpanQueries 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.
isNegative in interface TimeSpan<U>true if negative and not empty else falsepublic boolean isPositive()
TimeSpanQueries if this time span is positive.
A time span is positive if it is neither empty nor negative.
isPositive in interface TimeSpan<U>true if positive and not empty else falseTimeSpan.isEmpty(),
TimeSpan.isNegative()public boolean isEmpty()
TimeSpanQueries if this time span is empty.
Per definition an empty time span has no items with a partial
amount different from 0.
public MachineTime<U> plus(long amount, U unit)
Add given temporal amount to this machine time.
amount - the amount to be addedunit - the related time unitArithmeticException - in case of numerical overflowpublic MachineTime<U> plus(MachineTime<U> duration)
Add given temporal amount to this machine time.
duration - other machine time to be addedArithmeticException - in case of numerical overflowpublic MachineTime<U> minus(long amount, U unit)
Subtracts given temporal amount from this machine time.
amount - the amount to be subtractedunit - the related time unitArithmeticException - in case of numerical overflowpublic MachineTime<U> minus(MachineTime<U> duration)
Subtracts given temporal amount from this machine time.
duration - other machine time to be subtractedArithmeticException - in case of numerical overflowpublic MachineTime<U> abs()
Converts this machine duration to its absolute amount.
ArithmeticException - in case of numerical overflowpublic MachineTime<U> inverse()
Creates a copy with inversed sign.
ArithmeticException - in case of numerical overflowpublic MachineTime<U> multipliedBy(long factor)
Multiplies this duration with given factor.
factor - multiplicandpublic MachineTime<U> multipliedBy(double factor)
Multiplies this duration with given decimal factor.
If more rounding control is needed then users might consider the alternatives
multipliedBy(long) and dividedBy(long, RoundingMode).
factor - multiplicandIllegalArgumentException - if given factor is not a finite numberpublic MachineTime<U> dividedBy(long divisor, RoundingMode roundingMode)
Divides this duration by given divisor using given rounding mode.
divisor - divisorroundingMode - rounding mode to be used in divisionpublic <T extends TimePoint<? super U,T>> T addTo(T time)
TimeSpanAdds this time span to given time point.
Is equivalent to the expression time.plus(this). Due to better readability usage of the
TimePoint-method is recommended. Implementations are
required to document the used algorithm in detailed manner.
addTo in interface TimeSpan<U>T - generic type of time pointtime - reference time point to add this time span toTimeSpan.subtractFrom(TimePoint)public <T extends TimePoint<? super U,T>> T subtractFrom(T time)
TimeSpanSubtracts this time span from given time point.
Is equivalent to the expression time.minus(this). Due to better readability usage of the
TimePoint-method is recommended. Implementations are
required to document the used algorithm in detailed manner.
subtractFrom in interface TimeSpan<U>T - generic type of time pointtime - reference time point to subtract this time span fromTimeSpan.addTo(TimePoint)public static Collector<MachineTime<TimeUnit>,?,MachineTime<TimeUnit>> summingUpPosix()
Helps to sum up durations of a stream.
A final normalization is automatically done.
Duration.summingUp(),
summingUpReal()public static Collector<MachineTime<SI>,?,MachineTime<SI>> summingUpReal()
Helps to sum up durations of a stream.
A final normalization is automatically done.
Duration.summingUp(),
summingUpPosix()public boolean isShorterThan(MachineTime<U> other)
Compares the absolute lengths and is equivalent to abs().compareTo(other.abs()) < 0.
other - another machine time to be compared withcompareTo(MachineTime),
isLongerThan(MachineTime)public boolean isLongerThan(MachineTime<U> other)
Compares the absolute lengths and is equivalent to abs().compareTo(other.abs()) > 0.
other - another machine time to be compared withcompareTo(MachineTime),
isShorterThan(MachineTime)public int compareTo(MachineTime<U> other)
Method of the Comparable-interface.
compareTo in interface Comparable<MachineTime<U>>other - another machine time to be compared withClassCastException - if this and the other machine time have different time scalesisShorterThan(MachineTime),
isLongerThan(MachineTime)public String toString()
Returns a format in technical notation including the name of the underlying time scale.
public BigDecimal toBigDecimal()
Converts this machine time duration into a decimal number of seconds.
public Duration toTemporalAmount()
Converts this machine time to its JSR-310-equivalent.
IllegalStateException - if this instance is not based on java.util.concurrent.TimeUnitfrom(java.time.Duration)public static MachineTime<TimeUnit> from(Duration threeten)
Converts given JSR-310-duration to a machine time.
threeten - duration defined in seconds and nanoseconds (JSR-310-API)toTemporalAmount()Copyright © 2014–2021. All rights reserved.