T
- the underlying type representing the data, typically a Calendrical
,
Number
or Enum
, must be immutable, should be comparablepublic abstract class DateTimeFieldRule<T> extends CalendricalRule<T>
Rule implementations define how a field like day-of-month operates. This includes the field name and minimum/maximum values.
DateTimeFieldRule 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 and must ensure serialization works correctly.
Modifier and Type | Class and Description |
---|---|
static class |
DateTimeFieldRule.TextStore
The mapping between integer values and textual representations.
|
Modifier | Constructor and Description |
---|---|
protected |
DateTimeFieldRule(Class<T> reifiedClass,
Chronology chronology,
String name,
PeriodUnit periodUnit,
PeriodUnit periodRange,
int minimumValue,
int maximumValue)
Constructor.
|
protected |
DateTimeFieldRule(Class<T> reifiedClass,
Chronology chronology,
String name,
PeriodUnit periodUnit,
PeriodUnit periodRange,
int minimumValue,
int maximumValue,
boolean hasText)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
int |
checkValue(int value)
Checks if the value is invalid and throws an exception if it is.
|
int |
checkValue(long value)
Checks if the value is invalid and throws an exception if it is.
|
int |
convertFractionToInt(BigDecimal fraction)
Converts a fraction from 0 to 1 for this field to a value.
|
BigDecimal |
convertIntToFraction(int value)
Converts a value for this field to a fraction between 0 and 1.
|
T |
convertIntToValue(int value)
Converts the
int to a typed value of the rule. |
int |
convertValueToInt(T value)
Converts the typed value of the rule to the
int equivalent. |
protected void |
createTextStores(EnumMap<DateTimeFormatterBuilder.TextStyle,DateTimeFieldRule.TextStore> textStores,
Locale locale)
Creates the text store for each style for the specified locale.
|
int |
getInt(Calendrical calendrical)
Gets the
int value of this field from the specified calendrical
throwing an exception if the value cannot be returned. |
Integer |
getInteger(Calendrical calendrical)
Gets the
Integer value of this field from the specified calendrical
returning null if the value cannot be returned. |
int |
getLargestMinimumValue()
Gets the largest possible minimum value that the field can take.
|
int |
getMaximumValue()
Gets the maximum value that the field can take.
|
int |
getMaximumValue(Calendrical calendrical)
Gets the minimum value that the field can take using the specified
calendrical information to refine the accuracy of the response.
|
int |
getMinimumValue()
Gets the minimum value that the field can take.
|
int |
getMinimumValue(Calendrical calendrical)
Gets the minimum value that the field can take using the specified
calendrical information to refine the accuracy of the response.
|
int |
getSmallestMaximumValue()
Gets the smallest possible maximum value that the field can take.
|
String |
getText(int value,
Locale locale,
DateTimeFormatterBuilder.TextStyle textStyle)
Gets the text for this field.
|
DateTimeFieldRule.TextStore |
getTextStore(Locale locale,
DateTimeFormatterBuilder.TextStyle textStyle)
Gets the text map for this field with the specified locale and style.
|
boolean |
isFixedValueSet()
Is the set of values, from the minimum value to the maximum, a fixed
set, or does it vary according to other fields.
|
boolean |
isValidValue(int value)
Checks if the value is valid or invalid for this field.
|
boolean |
isValidValue(long value)
Checks if the value is valid or invalid for this field.
|
compare, compareTo, derive, deriveValueFor, deriveValueFrom, equals, getChronology, getID, getName, getPeriodRange, getPeriodUnit, getReifiedType, getValue, getValueChecked, hashCode, interpret, merge, reify, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
comparing, comparing, comparingDouble, comparingInt, comparingLong, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
protected DateTimeFieldRule(Class<T> reifiedClass, Chronology chronology, String name, PeriodUnit periodUnit, PeriodUnit periodRange, int minimumValue, int maximumValue)
reifiedClass
- the reified class, not nullchronology
- the chronology, not nullname
- the name of the type, not nullperiodUnit
- the period unit, not nullperiodRange
- the period range, not nullminimumValue
- the minimum valuemaximumValue
- the minimum valueprotected DateTimeFieldRule(Class<T> reifiedClass, Chronology chronology, String name, PeriodUnit periodUnit, PeriodUnit periodRange, int minimumValue, int maximumValue, boolean hasText)
reifiedClass
- the reified class, not nullchronology
- the chronology, not nullname
- the name of the type, not nullperiodUnit
- the period unit, not nullperiodRange
- the period range, not nullminimumValue
- the minimum valuemaximumValue
- the minimum valuehasText
- true if this field has a text representationpublic final Integer getInteger(Calendrical calendrical)
Integer
value of this field from the specified calendrical
returning null
if the value cannot be returned.
This uses CalendricalRule.getValue(Calendrical)
to find the value and then
converts it to an Integer
.
calendrical
- the calendrical to get the field value from, not nullpublic final int getInt(Calendrical calendrical)
int
value of this field from the specified calendrical
throwing an exception if the value cannot be returned.
This uses CalendricalRule.getValue(Calendrical)
to find the value and then
converts it to an int
ensuring it isn't null
.
calendrical
- the calendrical to get the field value from, not nullUnsupportedRuleException
- if the field cannot be extractedpublic int convertValueToInt(T value)
int
equivalent.
This default implementation handles Integer
and Enum
.
When the reified type is another type, this method must be overridden.
value
- the value to convert, not nullClassCastException
- if the value cannot be convertedpublic T convertIntToValue(int value)
int
to a typed value of the rule.
The int
will be checked to ensure that it is within the
valid range of values for the field.
This default implementation handles Integer
and Enum
.
When the reified type is another type, this method must be overridden.
value
- the value to convert, not nullIllegalCalendarFieldValueException
- if the value is invalidClassCastException
- if the value cannot be convertedpublic boolean isValidValue(int value)
This method has no knowledge of other calendrical fields, thus only the outer minimum and maximum range for the field is validated.
This method performs the same check as isValidValue(long)
.
value
- the value to checkpublic boolean isValidValue(long value)
This method has no knowledge of other calendrical fields, thus only the outer minimum and maximum range for the field is validated.
This method performs the same check as isValidValue(int)
.
value
- the value to checkpublic int checkValue(int value)
This method has no knowledge of other calendrical fields, thus only the outer minimum and maximum range for the field is validated.
This method performs the same check as checkValue(long)
.
The implementation uses isValidValue(int)
.
value
- the value to checkIllegalCalendarFieldValueException
- if the value is invalidpublic int checkValue(long value)
This method has no knowledge of other calendrical fields, thus only the outer minimum and maximum range for the field is validated.
This method performs the same check as checkValue(int)
.
The implementation uses isValidValue(long)
.
value
- the value to checkIllegalCalendarFieldValueException
- if the value is invalidpublic boolean isFixedValueSet()
public int getMinimumValue()
public int getLargestMinimumValue()
The default implementation returns getMinimumValue()
.
Subclasses must override this as necessary.
public int getMinimumValue(Calendrical calendrical)
The result of this method may still be inaccurate, if there is insufficient information in the calendrical.
The default implementation returns getMinimumValue()
.
Subclasses must override this as necessary.
calendrical
- context calendrical, not nullpublic int getMaximumValue()
public int getSmallestMaximumValue()
The default implementation returns getMaximumValue()
.
Subclasses must override this as necessary.
public int getMaximumValue(Calendrical calendrical)
The result of this method will still be inaccurate if there is insufficient information in the calendrical.
For example, if this field is the ISO day-of-month field, then the number
of days in the month varies depending on the month and year. If both the
month and year can be derived from the calendrical, then the maximum value
returned will be accurate. Otherwise the 'best guess' value from
getMaximumValue()
will be returned.
The default implementation returns getMaximumValue()
.
Subclasses must override this as necessary.
calendrical
- context calendrical, not nullpublic String getText(int value, Locale locale, DateTimeFormatterBuilder.TextStyle textStyle)
Some fields have a textual representation, such as day-of-week or
month-of-year. This method provides a convenient way to convert a value
to such a textual representation.
More control is available using getTextStore(java.util.Locale, javax.time.calendar.format.DateTimeFormatterBuilder.TextStyle)
.
If there is no textual mapping, then the value is returned as per
Integer.toString()
. Note that this is different to what occurs
in printing /parsing, where a more advanced localized conversion from
int to String is used.
value
- the value to convert to text, not nulllocale
- the locale to use, not nulltextStyle
- the text style, not nullpublic DateTimeFieldRule.TextStore getTextStore(Locale locale, DateTimeFormatterBuilder.TextStyle textStyle)
Some fields have a textual representation, such as day-of-week or month-of-year. The text store provides details of those textual representations.
To supply text, subclasses should pass true in the constructor and
override createTextStores(java.util.EnumMap<javax.time.calendar.format.DateTimeFormatterBuilder.TextStyle, javax.time.calendar.DateTimeFieldRule.TextStore>, java.util.Locale)
. This method is not normally overridden.
locale
- the locale to use, not nulltextStyle
- the text style, not nullprotected void createTextStores(EnumMap<DateTimeFormatterBuilder.TextStyle,DateTimeFieldRule.TextStore> textStores, Locale locale)
It is intended that a new copy of the text store should be created in
response to calling this method as the result is cached by getTextStore(java.util.Locale, javax.time.calendar.format.DateTimeFormatterBuilder.TextStyle)
.
textStores
- the map to populate with TextStore instances, not nulllocale
- the locale to use, not nullpublic BigDecimal convertIntToFraction(int value)
The fractional value is between 0 (inclusive) and 1 (exclusive).
It can only be returned if isFixedValueSet()
returns true and the
getMinimumValue()
returns zero.
The fraction is obtained by calculation from the field range using 9 decimal
places and a rounding mode of FLOOR
.
For example, the second-of-minute value of 15 would be returned as 0.25, assuming the standard definition of 60 seconds in a minute.
value
- the value to convert, valid for this fieldUnsupportedRuleException
- if the value cannot be convertedIllegalCalendarFieldValueException
- if the value is invalidpublic int convertFractionToInt(BigDecimal fraction)
The fractional value must be between 0 (inclusive) and 1 (exclusive).
It can only be returned if isFixedValueSet()
returns true and the
getMinimumValue()
returns zero.
The value is obtained by calculation from the field range and a rounding
mode of FLOOR
.
For example, the fractional second-of-minute of 0.25 would be converted to 15, assuming the standard definition of 60 seconds in a minute.
fraction
- the fraction to convert, not nullUnsupportedRuleException
- if the value cannot be convertedIllegalCalendarFieldValueException
- if the value is invalidCopyright © 2014. All rights reserved.