Class SingleUnitTimeSpan<U extends IsoDateUnit,D extends SingleUnitTimeSpan<U,D>>
- Type Parameters:
U
- generic type of calendrical units
- All Implemented Interfaces:
Serializable
,Comparable<D>
,TimeSpan<U>
Represents a time span in one calendrical unit only.
- Since:
- 3.21/4.17
- Author:
- Meno Hochschild
- See Also:
- Serialized Form
-
Nested Class Summary
Nested classes/interfaces inherited from interface net.time4j.engine.TimeSpan
TimeSpan.Item<U>
-
Method Summary
Modifier and TypeMethodDescriptionabs()
Yields a copy with the absolute amount.addTo(T time)
Adds this time span to given time point.int
boolean
contains(IsoDateUnit unit)
Queries if given time unit is part of this time span.boolean
int
Yields the count of units as integer-based amount.long
getPartialAmount(IsoDateUnit unit)
Yields the partial amount associated with given time unit.Yields all containted time span items with amount and unit in the order from largest to smallest time units.getUnit()
Yields the associated unit.int
hashCode()
inverse()
Yields a copy with the negated amount.boolean
isEmpty()
Queries if this time span is empty.boolean
Queries if this time span is negative.boolean
Queries if this time span is positive.minus(int amount)
Yields a copy with the subtracted amount.Yields a copy with the subtracted duration.multipliedBy(int factor)
Yields a copy with the multiplied amount.plus(int amount)
Yields a copy with the added amount.Yields a copy with the added duration.subtractFrom(T time)
Subtracts this time span from given time point.Converts this instance to a general duration with the same amount and unit.toString()
Prints in ISO-8601-format "PnU" (n=amount, U=unit).Prints this duration in a localized way with given text width.
-
Method Details
-
getAmount
public int getAmount()Yields the count of units as integer-based amount.
- Returns:
- int
-
getUnit
Yields the associated unit.
- Returns:
- calendrical unit
-
compareTo
- Specified by:
compareTo
in interfaceComparable<U extends IsoDateUnit>
-
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 extends IsoDateUnit>
- 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 extends IsoDateUnit>
- 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 extends IsoDateUnit>
- 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 extends IsoDateUnit>
- 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 extends IsoDateUnit>
- 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
.- Specified by:
isEmpty
in interfaceTimeSpan<U extends IsoDateUnit>
- Returns:
true
if empty elsefalse
-
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 extends IsoDateUnit>
- 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 extends IsoDateUnit>
- 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)
-
equals
-
hashCode
public int hashCode() -
toString
Prints in ISO-8601-format "PnU" (n=amount, U=unit).
Negative durations will get a preceding sign before "P". Note that gregorian and week-based years have the same representation using the symbol Y.
-
toString
Prints this duration in a localized way with given text width.
- Parameters:
locale
- the locale to be appliedwidth
- the text width to be applied- Returns:
- formatted localized representation of this duration
- Since:
- 5.0
-
abs
Yields a copy with the absolute amount.
- Returns:
- immutable copy with the absolute amount
- Throws:
ArithmeticException
- if numeric overflow occurs (only in case ofInteger.MIN_VALUE
)
-
inverse
Yields a copy with the negated amount.
- Returns:
- immutable copy with the reverse sign
- Throws:
ArithmeticException
- if numeric overflow occurs (only in case ofInteger.MIN_VALUE
)
-
plus
Yields a copy with the added amount.
- Parameters:
amount
- the amount to be added- Returns:
- result of addition as immutable copy
- Throws:
ArithmeticException
- if numeric overflow occurs
-
plus
Yields a copy with the added duration.
- Parameters:
duration
- the duration to be added- Returns:
- result of addition as immutable copy
- Throws:
ArithmeticException
- if numeric overflow occurs
-
minus
Yields a copy with the subtracted amount.
- Parameters:
amount
- the amount to be subtracted- Returns:
- result of subtraction as immutable copy
- Throws:
ArithmeticException
- if numeric overflow occurs
-
minus
Yields a copy with the subtracted duration.
- Parameters:
duration
- the duration to be added- Returns:
- result of subtraction as immutable copy
- Throws:
ArithmeticException
- if numeric overflow occurs
-
multipliedBy
Yields a copy with the multiplied amount.
- Parameters:
factor
- multiplication factor to be applied- Returns:
- immutable copy with the multiplied amount
- Throws:
ArithmeticException
- if numeric overflow occurs
-
toStdDuration
Converts this instance to a general duration with the same amount and unit.
- Returns:
- Duration
-