Class BaseSingleFieldPeriod
- All Implemented Interfaces:
Serializable,Comparable<BaseSingleFieldPeriod>,ReadablePeriod
This class should generally not be used directly by API users.
The ReadablePeriod interface should be used when different
kinds of period objects are to be referenced.
BaseSingleFieldPeriod subclasses may be mutable and not thread-safe.
- Since:
- 1.4
- Author:
- Stephen Colebourne
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBaseSingleFieldPeriod(int period) Creates a new instance representing the specified period. -
Method Summary
Modifier and TypeMethodDescriptionprotected static intbetween(ReadableInstant start, ReadableInstant end, DurationFieldType field) Calculates the number of whole units between the two specified datetimes.protected static intbetween(ReadablePartial start, ReadablePartial end, ReadablePeriod zeroInstance) Calculates the number of whole units between the two specified partial datetimes.intcompareTo(BaseSingleFieldPeriod other) Compares this period to another object of the same class.booleanCompares this object with the specified object for equality based on the value of each field.intget(DurationFieldType type) Gets the value of a duration field represented by this period.abstract DurationFieldTypeGets the single duration field type.getFieldType(int index) Gets the field type at the specified index.abstract PeriodTypeGets the period type which matches the duration field type.protected intgetValue()Gets the amount of this period.intgetValue(int index) Gets the value at the specified index.inthashCode()Gets a hash code for the period as defined by ReadablePeriod.booleanisSupported(DurationFieldType type) Checks whether the duration field specified is supported by this period.protected voidsetValue(int value) Sets the amount of this period.intsize()Gets the number of fields that this period supports, which is one.protected static intstandardPeriodIn(ReadablePeriod period, long millisPerUnit) Creates a new instance representing the number of complete standard length units in the specified period.Get this object as aMutablePeriod.toPeriod()Get this period as an immutablePeriodobject.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.joda.time.ReadablePeriod
toString
-
Constructor Details
-
BaseSingleFieldPeriod
protected BaseSingleFieldPeriod(int period) Creates a new instance representing the specified period.- Parameters:
period- the period to represent
-
-
Method Details
-
between
Calculates the number of whole units between the two specified datetimes.- Parameters:
start- the start instant, validated to not be nullend- the end instant, validated to not be nullfield- the field type to use, must not be null- Returns:
- the period
- Throws:
IllegalArgumentException- if the instants are null or invalid
-
between
protected static int between(ReadablePartial start, ReadablePartial end, ReadablePeriod zeroInstance) Calculates the number of whole units between the two specified partial datetimes.The two partials must contain the same fields, for example you can specify two
LocalDateobjects.- Parameters:
start- the start partial date, validated to not be nullend- the end partial date, validated to not be nullzeroInstance- the zero instance constant, must not be null- Returns:
- the period
- Throws:
IllegalArgumentException- if the partials are null or invalid
-
standardPeriodIn
Creates a new instance representing the number of complete standard length units in the specified period.This factory method converts all fields from the period to hours using standardised durations for each field. Only those fields which have a precise duration in the ISO UTC chronology can be converted.
- One week consists of 7 days.
- One day consists of 24 hours.
- One hour consists of 60 minutes.
- One minute consists of 60 seconds.
- One second consists of 1000 milliseconds.
- Parameters:
period- the period to get the number of hours from, must not be nullmillisPerUnit- the number of milliseconds in one standard unit of this period- Returns:
- the int value
- Throws:
IllegalArgumentException- if the period contains imprecise duration values
-
getValue
protected int getValue()Gets the amount of this period.- Returns:
- the period value
-
setValue
protected void setValue(int value) Sets the amount of this period. To make a subclass immutable you must declare it final, or block this method.- Parameters:
value- the period value
-
getFieldType
Gets the single duration field type.- Returns:
- the duration field type, not null
-
getPeriodType
Gets the period type which matches the duration field type.- Specified by:
getPeriodTypein interfaceReadablePeriod- Returns:
- the period type, not null
-
size
public int size()Gets the number of fields that this period supports, which is one.- Specified by:
sizein interfaceReadablePeriod- Returns:
- the number of fields supported, which is one
-
getFieldType
Gets the field type at the specified index.The only index supported by this period is zero which returns the field type of this class.
- Specified by:
getFieldTypein interfaceReadablePeriod- Parameters:
index- the index to retrieve, which must be zero- Returns:
- the field at the specified index
- Throws:
IndexOutOfBoundsException- if the index is invalid
-
getValue
public int getValue(int index) Gets the value at the specified index.The only index supported by this period is zero.
- Specified by:
getValuein interfaceReadablePeriod- Parameters:
index- the index to retrieve, which must be zero- Returns:
- the value of the field at the specified index
- Throws:
IndexOutOfBoundsException- if the index is invalid
-
get
Gets the value of a duration field represented by this period.If the field type specified does not match the type used by this class then zero is returned.
- Specified by:
getin interfaceReadablePeriod- Parameters:
type- the field type to query, null returns zero- Returns:
- the value of that field, zero if field not supported
-
isSupported
Checks whether the duration field specified is supported by this period.- Specified by:
isSupportedin interfaceReadablePeriod- Parameters:
type- the type to check, may be null which returns false- Returns:
- true if the field is supported
-
toPeriod
Get this period as an immutablePeriodobject. The period will usePeriodType.standard().- Specified by:
toPeriodin interfaceReadablePeriod- Returns:
- a
Periodrepresenting the same number of days
-
toMutablePeriod
Get this object as aMutablePeriod.This will always return a new
MutablePeriodwith the same fields. The period will usePeriodType.standard().- Specified by:
toMutablePeriodin interfaceReadablePeriod- Returns:
- a MutablePeriod using the same field set and values
-
equals
Compares this object with the specified object for equality based on the value of each field. All ReadablePeriod instances are accepted, but only those with a matchingPeriodTypecan return true.- Specified by:
equalsin interfaceReadablePeriod- Overrides:
equalsin classObject- Parameters:
period- a readable period to check against- Returns:
- true if all the field values are equal, false if not or the period is null or of an incorrect type
-
hashCode
public int hashCode()Gets a hash code for the period as defined by ReadablePeriod.- Specified by:
hashCodein interfaceReadablePeriod- Overrides:
hashCodein classObject- Returns:
- a hash code
-
compareTo
Compares this period to another object of the same class.- Specified by:
compareToin interfaceComparable<BaseSingleFieldPeriod>- Parameters:
other- the other period, must not be null- Returns:
- zero if equal, positive if greater, negative if less
- Throws:
NullPointerException- if the other period is nullClassCastException- if the other period is of a different type
-