public abstract class PeriodField
extends java.lang.Object
PeriodField is an immutable period that can only store years. It is a type-safe way of representing a period in an application.
Static factory methods allow you to construct instances. The number of years may be queried using getYears(). Basic mathematical operations are provided - plus(), minus(), multipliedBy(), dividedBy() and negated(), all of which return a new instance
PeriodField is an abstract class and must be implemented with care to ensure other classes in the framework operate correctly. All instantiable subclasses must be final, immutable and thread-safe.
Modifier | Constructor and Description |
---|---|
protected |
PeriodField()
Constructs a new instance.
|
Modifier and Type | Method and Description |
---|---|
PeriodField |
dividedBy(int divisor)
Returns a new instance with the amount divided by the specified divisor.
|
boolean |
equals(java.lang.Object obj)
Is this instance equal to that specified.
|
abstract int |
getAmount()
Gets the amount of time in this period field.
|
abstract PeriodUnit |
getUnit()
Gets the unit defining the amount of time.
|
int |
hashCode()
Returns the hash code for this period field.
|
PeriodField |
minus(int amount)
Returns a new instance with the specified amount of time taken away.
|
PeriodField |
multipliedBy(int scalar)
Returns a new instance with the amount multiplied by the specified scalar.
|
PeriodField |
negated()
Returns a new instance with the amount negated.
|
PeriodField |
plus(int amount)
Returns a new instance with the specified amount of time added.
|
PeriodFields |
toPeriodFields()
Converts this instance to a
PeriodFields . |
abstract java.lang.String |
toString()
Returns a string representation of the amount of time.
|
abstract PeriodField |
withAmount(int amount)
Returns a new instance of the subclass with a different amount of time.
|
public abstract int getAmount()
public abstract PeriodField withAmount(int amount)
amount
- the amount of time to set in the new period field, may be negativepublic abstract PeriodUnit getUnit()
public PeriodField plus(int amount)
This instance is immutable and unaffected by this method call.
amount
- the amount of time to add, may be negativejava.lang.ArithmeticException
- if the result overflows an intpublic PeriodField minus(int amount)
This instance is immutable and unaffected by this method call.
amount
- the amount of time to take away, may be negativejava.lang.ArithmeticException
- if the result overflows an intpublic PeriodField multipliedBy(int scalar)
This instance is immutable and unaffected by this method call.
scalar
- the amount to multiply by, may be negativejava.lang.ArithmeticException
- if the result overflows an intpublic PeriodField dividedBy(int divisor)
This instance is immutable and unaffected by this method call.
divisor
- the amount to divide by, may be negativejava.lang.ArithmeticException
- if the divisor is zeropublic PeriodField negated()
java.lang.ArithmeticException
- if the result overflows an intpublic PeriodFields toPeriodFields()
PeriodFields
.public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
obj
- the other amount of time, null returns falsepublic int hashCode()
hashCode
in class java.lang.Object
public abstract java.lang.String toString()
toString
in class java.lang.Object