|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.time.calendar.PeriodFields
public final class PeriodFields
A period of time measured using a number of different units, such as '3 Months, 4 Days and 7 Hours'.
PeriodFields
is an immutable period that stores an amount of human-scale
time for a number of units. For example, humans typically measure periods of time
in units of years, months, days, hours, minutes and seconds. These concepts are
defined by instances of PeriodUnit
in the chronology classes. This class
allows an amount to be specified for a number of the units, such as '3 Days and 65 Seconds'.
Basic mathematical operations are provided - plus(), minus(), multipliedBy(), dividedBy() and negated(), all of which return a new instance
A value of zero can also be stored for any unit. This means that a
period of zero hours is not equal to a period of zero minutes.
However, an empty instance constant exists to represent zero irrespective of unit.
The withZeroesRemoved()
method removes zero values.
PeriodFields
can store units of any kind which makes it usable with
any calendar system.
PeriodFields is immutable and thread-safe.
Field Summary | |
---|---|
static PeriodFields |
ZERO
A constant for a period of zero. |
Method Summary | |
---|---|
boolean |
contains(PeriodUnit unit)
Checks if this set of fields contains a field with the specified unit. |
PeriodFields |
dividedBy(long divisor)
Returns a copy of this period with each amount in this period divided by the specified value. |
boolean |
equals(java.lang.Object obj)
Checks if this set of fields is equal to another set of fields. |
long |
getAmount(PeriodUnit unit)
Gets the amount for the specified unit. |
int |
getAmountInt(PeriodUnit unit)
Gets the amount for the specified unit converted to an int . |
PeriodField |
getField(PeriodUnit unit)
Gets the stored field for the specified unit. |
int |
hashCode()
A hash code for this set of fields. |
boolean |
isPositive()
Checks if this period is fully positive, excluding zero. |
boolean |
isPositiveOrZero()
Checks if this period is fully positive, including zero. |
boolean |
isZero()
Checks if this period is zero-length. |
java.util.Iterator<PeriodField> |
iterator()
Iterates through all the fields. |
PeriodFields |
minus(long amountToSubtract,
PeriodUnit unit)
Returns a copy of this period with the specified period subtracted. |
PeriodFields |
minus(PeriodProvider periodToSubtract)
Returns a copy of this period with the specified period subtracted. |
PeriodFields |
multipliedBy(long scalar)
Returns a copy of this period with each amount in this period multiplied by the specified scalar. |
PeriodFields |
negated()
Returns a copy of this period with each amount in this period negated. |
PeriodFields |
normalized()
Returns a copy of this period with the amounts normalized. |
PeriodFields |
normalizedTo(PeriodUnit... units)
Returns a copy of this period with the amounts normalized to the specified units. |
static PeriodFields |
of(Duration duration)
Obtains a PeriodFields from a Duration based on the standard
durations of seconds and nanoseconds. |
static PeriodFields |
of(java.lang.Iterable<PeriodField> periods)
Obtains a PeriodFields from an array of single-unit periods. |
static PeriodFields |
of(long amount,
PeriodUnit unit)
Obtains a PeriodFields from an amount and unit. |
static PeriodFields |
of(PeriodField... periods)
Obtains a PeriodFields from an array of single-unit periods. |
static PeriodFields |
of(PeriodField period)
Obtains a PeriodFields from a single-unit period. |
static PeriodFields |
of(PeriodProvider periodProvider)
Obtains a PeriodFields from a PeriodProvider . |
static PeriodFields |
ofTotal(PeriodProvider... periodProviders)
Obtains a PeriodFields by totalling the amounts in a list of
PeriodProvider instances. |
PeriodFields |
plus(long amountToAdd,
PeriodUnit unit)
Returns a copy of this period with the specified period added. |
PeriodFields |
plus(PeriodProvider periodToAdd)
Returns a copy of this period with the specified period added. |
PeriodFields |
remainder(PeriodField period)
Returns a copy of this period with the modular division remainder of each field calculated with respect to the specified period. |
PeriodFields |
retain(PeriodUnit... units)
Returns a copy of this period with the specified units retained. |
PeriodFields |
retainConvertible(PeriodUnit... units)
Returns a copy of this period with only those units that can be converted to the specified units. |
int |
size()
Gets the number of fields. |
Duration |
toDuration()
Calculates the accurate duration of this period. |
Duration |
toDurationEstimate()
Estimates the duration of this period. |
PeriodFields |
toEquivalent(PeriodUnit... units)
Converts this period to one containing only the units specified. |
java.util.SortedMap<PeriodUnit,PeriodField> |
toMap()
Returns a Map equivalent to this set of fields. |
PeriodFields |
toPeriodFields()
Converts this period to a PeriodFields , trivially
returning this . |
java.lang.String |
toString()
Outputs this set of fields as a String , such as [6 Days, 13 Hours] . |
PeriodField |
toTotal(PeriodUnit unit)
Totals this period in terms of a single unit. |
PeriodFields |
with(long amount,
PeriodUnit unit)
Returns a copy of these fields with the specified unit and amount. |
PeriodFields |
with(PeriodProvider periodProvider)
Returns a copy of this period with the specified values altered. |
PeriodFields |
without(PeriodUnit unit)
Returns a copy of these fields with the specified unit removed. |
PeriodFields |
withZeroesRemoved()
Returns a copy of this period with all zero amounts removed. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final PeriodFields ZERO
Method Detail |
---|
public static PeriodFields of(long amount, PeriodUnit unit)
PeriodFields
from an amount and unit.
The parameters represent the two parts of a phrase like '6 Days'.
amount
- the amount to create with, positive or negativeunit
- the period unit, not null
PeriodFields
instance, not nullpublic static PeriodFields of(PeriodField period)
PeriodFields
from a single-unit period.
period
- the single-unit period, not null
PeriodFields
instance, not nullpublic static PeriodFields of(PeriodField... periods)
PeriodFields
from an array of single-unit periods.
The period fields must all have different units.
periods
- the array of single-unit periods, not null
PeriodFields
instance, not null
java.lang.IllegalArgumentException
- if the same period unit occurs twicepublic static PeriodFields of(java.lang.Iterable<PeriodField> periods)
PeriodFields
from an array of single-unit periods.
periods
- the array of single-unit periods, not null
PeriodFields
instance, not null
java.lang.IllegalArgumentException
- if the same period unit occurs twicepublic static PeriodFields of(PeriodProvider periodProvider)
PeriodFields
from a PeriodProvider
.
This method provides null-checking around PeriodProvider.toPeriodFields()
.
periodProvider
- the provider to create from, not null
PeriodFields
instance, not null
java.lang.NullPointerException
- if the period provider is null or returns nullpublic static PeriodFields ofTotal(PeriodProvider... periodProviders)
PeriodFields
by totalling the amounts in a list of
PeriodProvider
instances.
This method returns a period with all the unit-amount pairs from the providers totalled. Thus a period of '2 Months and 5 Days' combined with a period of '7 Days and 21 Hours' will yield a result of '2 Months, 12 Days and 21 Hours'.
periodProviders
- the providers to total, not null
PeriodFields
instance, not null
java.lang.NullPointerException
- if any period provider is null or returns nullpublic static PeriodFields of(Duration duration)
PeriodFields
from a Duration
based on the standard
durations of seconds and nanoseconds.
The conversion will create an instance with two units - the ISOChronology
seconds and nanoseconds units. This matches the toDuration()
method.
duration
- the duration to create from, not null
PeriodFields
instance, not nullpublic boolean isZero()
This checks whether all the amounts in this period are zero.
public boolean isPositive()
This checks whether all the amounts in this period are positive, defined as greater than zero.
public boolean isPositiveOrZero()
This checks whether all the amounts in this period are positive, defined as greater than or equal to zero.
public int size()
This method returns the number of stored PeriodField
instances.
public java.util.Iterator<PeriodField> iterator()
This method fulfills the Iterable
interface and allows looping
around the fields using the for-each loop.
The fields are returned sorted
in reverse order.
iterator
in interface java.lang.Iterable<PeriodField>
public boolean contains(PeriodUnit unit)
This method returns true one of the stored PeriodField
instances has the specified unit.
unit
- the unit to query, null returns false
public PeriodField getField(PeriodUnit unit)
This method queries the stored PeriodField
instances by unit.
If no field is found with the specified unit then null is returned.
unit
- the unit to query, not null
public long getAmount(PeriodUnit unit)
This method queries the stored PeriodField
instances by unit returning the amount.
If no field is found with the specified unit then zero is returned.
unit
- the unit to query, not null
public int getAmountInt(PeriodUnit unit)
int
.
This method queries the stored PeriodField
instances by unit returning the amount.
If no field is found with the specified unit then zero is returned.
unit
- the unit to query, not null
java.lang.ArithmeticException
- if the amount is too large to be returned in an intpublic PeriodFields withZeroesRemoved()
This instance is immutable and unaffected by this method call.
PeriodFields
based on this period with zero amounts removed, not nullpublic PeriodFields with(long amount, PeriodUnit unit)
The result is the set of fields in this instance with the specified field
merged as though using Map.put
with the unit and amount.
This instance is immutable and unaffected by this method call.
amount
- the amount to set in the result, positive or negativeunit
- the unit to set in the result, not null
PeriodFields
based on this fields with the specified field set, not nullpublic PeriodFields with(PeriodProvider periodProvider)
This method operates on each unit in the input in turn. If this period already contains an amount for the unit then the amount is replaced. Otherwise, the unit-amount pair is added.
For example, '6 Years, 7 Months' with '2 Months 3 Days' will return '6 Years, 2 Months, 3 Days'.
This instance is immutable and unaffected by this method call.
periodProvider
- the period to merge over this period, not null
PeriodFields
based on this period with the specified period overlaid, not nullpublic PeriodFields without(PeriodUnit unit)
The result is the set of fields in this instance with the specified unit removed. No error occurs if the unit is not present.
This instance is immutable and unaffected by this method call.
unit
- the unit to remove from the result, not null
PeriodFields
based on this fields with the specified unit removed, not nullpublic PeriodFields plus(PeriodProvider periodToAdd)
The returned period will take each unit in the provider and add the value to the amount already stored in this period, returning a new one. If this period does not contain an amount for the unit then the unit and amount are simply returned directly in the result. The result will have the union of the units in this instance and the units in the specified instance.
This instance is immutable and unaffected by this method call.
periodToAdd
- the period to add, not null
PeriodFields
based on this period with the specified period added, not null
java.lang.ArithmeticException
- if the calculation overflowspublic PeriodFields plus(long amountToAdd, PeriodUnit unit)
The result will contain the units and amounts from this period plus the specified unit and amount. The specified unit will always be in the result even if the amount is zero.
This instance is immutable and unaffected by this method call.
amountToAdd
- the amount to add, measured in the specified unit, positive or negativeunit
- the unit defining the amount, not null
PeriodFields
based on this period with the specified period added, not null
java.lang.ArithmeticException
- if the calculation overflowspublic PeriodFields minus(PeriodProvider periodToSubtract)
The returned period will take each unit in the provider and subtract the value from the amount already stored in this period, returning a new one. If this period does not contain an amount for the unit then the unit and amount are simply returned directly in the result. The result will have the union of the units in this instance and the units in the specified instance.
This instance is immutable and unaffected by this method call.
periodToSubtract
- the period to subtract, not null
PeriodFields
based on this period with the specified period subtracted, not null
java.lang.ArithmeticException
- if the calculation overflowspublic PeriodFields minus(long amountToSubtract, PeriodUnit unit)
The result will contain the units and amounts from this period minus the specified unit and amount. The specified unit will always be in the result even if the amount is zero.
This instance is immutable and unaffected by this method call.
amountToSubtract
- the amount to subtract, measured in the specified unit, positive or negativeunit
- the unit defining the amount, not null
PeriodFields
based on this period with the specified period subtracted, not null
java.lang.ArithmeticException
- if the calculation overflowspublic PeriodFields multipliedBy(long scalar)
scalar
- the scalar to multiply by, not null
PeriodFields
based on this period with the amounts multiplied by the scalar, not null
java.lang.ArithmeticException
- if the calculation overflowspublic PeriodFields dividedBy(long divisor)
divisor
- the value to divide by, not null, not zero
PeriodFields
based on this period with the amounts divided by the divisor, not null
java.lang.ArithmeticException
- if dividing by zeropublic PeriodFields negated()
PeriodFields
based on this period with the amounts negated, not null
java.lang.ArithmeticException
- if the calculation overflowspublic PeriodFields retain(PeriodUnit... units)
This method will return a new period that only has the specified units. All units not present in the input will not be present in the result. In most cases, the result will not be equivalent to this period.
This instance is immutable and unaffected by this method call.
units
- the units to retain, not altered, not null, no nulls
PeriodFields
based on this period with the specified units retained, not nullpublic PeriodFields retainConvertible(PeriodUnit... units)
This method will return a new period where every field can be converted to one of the specified units. In the result, each of the retained periods will have the same amount as they do in this period - no conversion or normalization occurs.
For example, if this period is '2 Days, 5 Hours, 7 Minutes' and the specified unit array contains 'Seconds' then the output will be '5 Hours, 7 Minutes'. The 'Days' unit is not retained as it cannot be converted to 'Seconds'.
This instance is immutable and unaffected by this method call.
units
- the units to retain, not altered, not null, no nulls
PeriodFields
based on this period with the specified units retained, not nullpublic PeriodFields remainder(PeriodField period)
This method will return a new period where every field represents a period less than the specified period. If this period contains a period that cannot be converted to the specified unit then an exception is thrown.
For example, if this period is '37 Hours, 7 Minutes' and the specified period is '24 Hours' then the output will be '13 Hours, 7 Minutes'.
This method requires this period to be convertible to the specified period.
To ensure this is true, call retainConvertible(javax.time.calendar.PeriodUnit...)
, with the base unit of the
period passed into this method, before calling this method.
This instance is immutable and unaffected by this method call.
period
- the period to calculate the remainder against, not null
PeriodFields
based on this period with the remainder, not null
CalendricalException
- if any field cannot be converted to the unit of the periodpublic PeriodFields normalized()
This will normalize the period around the units in this period.
For example, a period of '2 Years, 14 Months, 6 Hours, -7 Minutes' will
be normalized to '3 Years, 2 Months, 5 Hours, 53 Minutes'.
See normalizedTo(javax.time.calendar.PeriodUnit...)
for details of the algorithm.
java.lang.ArithmeticException
- if the calculation overflowspublic PeriodFields normalizedTo(PeriodUnit... units)
This will normalize the period around the specified units. The calculation normalizes amounts within groups of units that share a common base unit. For example, hours, minutes and seconds share a base unit or nanoseconds, while years and months share a base unit of months. Thus, normalization will occur between hours and minutes, but not between hours and months.
Normalization ensures that within any group of units with a common base, the smaller units will all have values between zero and that of the next larger unit. For example, a period of '2 Decades, 2 Years, 17 Months' normalized using 'Years' and 'Months' will return '23 Years, 5 Months', because months must be between 0 and 11 (inclusive).
After this method completes, negative values will have been moved to the largest unit in the group. For example, a period of '6 Hours, -7 Minutes' normalized using 'Hours' and 'Minutes' will return '5 Hours, 53 Minutes'.
At least one unit must be specified for this method to make any changes. Any part of this period that cannot be converted to one of the specified units will be unaffected in the result. The result will always contain all the specified units, even if they are zero. The result will be equivalent to this period.
units
- the unit array to normalize to, not altered, not null, no nulls
java.lang.ArithmeticException
- if the calculation overflowspublic PeriodField toTotal(PeriodUnit unit)
This will take each of the stored PeriodField
instances and
convert them to the specified unit. The result will be the total of these
converted periods.
For example, '3 Hours, 34 Minutes' can be totalled to minutes resulting in '214 Minutes'.
unit
- the unit to total in, not null
CalendricalException
- if this period cannot be converted to the unit
java.lang.ArithmeticException
- if the calculation overflowspublic PeriodFields toEquivalent(PeriodUnit... units)
This converts this period to one measured in the specified units. It operates by looping through the individual parts of this period, converting each in turn to one of the specified units. These converted periods are then combined to form the result.
No normalization is performed on the result.
This means that an amount in a smaller unit cannot be converted to an amount in a larger unit.
If you need to do this, call normalized()
before calling this method.
This method uses PeriodField.toEquivalent(PeriodUnit...)
and as such,
it is recommended to specify the units from largest to smallest.
For example, '3 Hours' can normally be converted to both minutes and seconds. If the units array contains both 'Minutes' and 'Seconds', then the result will be measured in whichever is first in the array.
units
- the required unit array, not altered, not null, no nulls
CalendricalException
- if this period cannot be converted to any of the units
java.lang.ArithmeticException
- if the calculation overflowspublic Duration toDurationEstimate()
Each PeriodUnit
contains an estimated duration for that unit.
The per-unit estimate allows an estimate to be calculated for the whole period
including fields of variable duration. The estimate will equal the
accurate
calculation if all the fields are based on seconds.
java.lang.ArithmeticException
- if the calculation overflowspublic Duration toDuration()
The conversion is based on the ISOChronology
definition of the seconds and
nanoseconds units. If all the fields in this period can be converted to either seconds
or nanoseconds then the conversion will succeed, subject to calculation overflow.
If any field cannot be converted to these fields above then an exception is thrown.
ISOChronology
fields, not null
CalendricalException
- if this period cannot be converted to an exact duration
java.lang.ArithmeticException
- if the calculation overflowspublic java.util.SortedMap<PeriodUnit,PeriodField> toMap()
Map
equivalent to this set of fields.
The map will connect the unit to the single field period. The sort order is from largest unit to smallest unit.
public PeriodFields toPeriodFields()
PeriodFields
, trivially
returning this
.
toPeriodFields
in interface PeriodProvider
this
, not nullpublic boolean equals(java.lang.Object obj)
The comparison is based on the complete set of fields.
equals
in class java.lang.Object
obj
- the object to check, null returns false
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
String
, such as [6 Days, 13 Hours]
.
The output will include of the complete set of fields.
toString
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |