Package org.joda.time.base
Class BaseSingleFieldPeriod
- java.lang.Object
-
- org.joda.time.base.BaseSingleFieldPeriod
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<BaseSingleFieldPeriod>
,ReadablePeriod
public abstract class BaseSingleFieldPeriod extends java.lang.Object implements ReadablePeriod, java.lang.Comparable<BaseSingleFieldPeriod>, java.io.Serializable
BaseSingleFieldPeriod is an abstract implementation of ReadablePeriod that manages a single duration field, such as days or minutes.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
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description int
compareTo(BaseSingleFieldPeriod other)
Compares this period to another object of the same class.boolean
equals(java.lang.Object period)
Compares this object with the specified object for equality based on the value of each field.int
get(DurationFieldType type)
Gets the value of a duration field represented by this period.abstract DurationFieldType
getFieldType()
Gets the single duration field type.DurationFieldType
getFieldType(int index)
Gets the field type at the specified index.abstract PeriodType
getPeriodType()
Gets the period type which matches the duration field type.int
getValue(int index)
Gets the value at the specified index.int
hashCode()
Gets a hash code for the period as defined by ReadablePeriod.boolean
isSupported(DurationFieldType type)
Checks whether the duration field specified is supported by this period.int
size()
Gets the number of fields that this period supports, which is one.MutablePeriod
toMutablePeriod()
Get this object as aMutablePeriod
.Period
toPeriod()
Get this period as an immutablePeriod
object.-
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.joda.time.ReadablePeriod
toString
-
-
-
-
Method Detail
-
getFieldType
public abstract DurationFieldType getFieldType()
Gets the single duration field type.- Returns:
- the duration field type, not null
-
getPeriodType
public abstract PeriodType getPeriodType()
Gets the period type which matches the duration field type.- Specified by:
getPeriodType
in 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:
size
in interfaceReadablePeriod
- Returns:
- the number of fields supported, which is one
-
getFieldType
public DurationFieldType getFieldType(int index)
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:
getFieldType
in interfaceReadablePeriod
- Parameters:
index
- the index to retrieve, which must be zero- Returns:
- the field at the specified index
- Throws:
java.lang.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:
getValue
in interfaceReadablePeriod
- Parameters:
index
- the index to retrieve, which must be zero- Returns:
- the value of the field at the specified index
- Throws:
java.lang.IndexOutOfBoundsException
- if the index is invalid
-
get
public int get(DurationFieldType type)
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:
get
in interfaceReadablePeriod
- Parameters:
type
- the field type to query, null returns zero- Returns:
- the value of that field, zero if field not supported
-
isSupported
public boolean isSupported(DurationFieldType type)
Checks whether the duration field specified is supported by this period.- Specified by:
isSupported
in interfaceReadablePeriod
- Parameters:
type
- the type to check, may be null which returns false- Returns:
- true if the field is supported
-
toPeriod
public Period toPeriod()
Get this period as an immutablePeriod
object. The period will usePeriodType.standard()
.- Specified by:
toPeriod
in interfaceReadablePeriod
- Returns:
- a
Period
representing the same number of days
-
toMutablePeriod
public MutablePeriod toMutablePeriod()
Get this object as aMutablePeriod
.This will always return a new
MutablePeriod
with the same fields. The period will usePeriodType.standard()
.- Specified by:
toMutablePeriod
in interfaceReadablePeriod
- Returns:
- a MutablePeriod using the same field set and values
-
equals
public boolean equals(java.lang.Object period)
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 matchingPeriodType
can return true.- Specified by:
equals
in interfaceReadablePeriod
- Overrides:
equals
in classjava.lang.Object
- 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:
hashCode
in interfaceReadablePeriod
- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- a hash code
-
compareTo
public int compareTo(BaseSingleFieldPeriod other)
Compares this period to another object of the same class.- Specified by:
compareTo
in interfacejava.lang.Comparable<BaseSingleFieldPeriod>
- Parameters:
other
- the other period, must not be null- Returns:
- zero if equal, positive if greater, negative if less
- Throws:
java.lang.NullPointerException
- if the other period is nulljava.lang.ClassCastException
- if the other period is of a different type
-
-