|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.time.calendar.CalendricalRule<T>
T
- the underlying type representing the data, typically a Calendrical
,
Number
or Enum
, must be immutable, should be comparablepublic abstract class CalendricalRule<T>
A rule defining how a single well-defined calendrical element operates.
Calendrical rules may define fields like day-of-month, combinations like date-time, or other related types like time-zone.
Each rule uses an underlying type to represent the data.
This is captured in the generic type of the rule.
The underlying type is reified and made available via getType()
.
It is expected, but not enforced, that the underlying type is Comparable
.
This 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.
Subclasses should implement equals
and hashCode
.
The subclass is also fully responsible for serialization as all fields in this class are
transient. The subclass must use readResolve
to replace the deserialized
class with a valid one created via a constructor.
Constructor Summary | |
---|---|
protected |
CalendricalRule(java.lang.Class<T> type,
java.lang.String name)
Constructor used to create a rule. |
Method Summary | |
---|---|
int |
compare(Calendrical cal1,
Calendrical cal2)
Compares two Calendrical implementations based on the value
of this rule extracted from each calendrical. |
protected T |
deriveFrom(CalendricalEngine engine)
Override point to derive the value for this rule from the merger. |
java.lang.String |
getName()
Gets the name of the rule. |
java.lang.Class<T> |
getType()
Gets the type of the rule, which is a reification of the generic type. |
T |
getValue(Calendrical calendrical)
Gets the value of this rule from the specified calendrical returning null if the value cannot be returned. |
T |
getValueChecked(Calendrical calendrical)
Gets the value of the rule from the specified calendrical throwing an exception if the rule cannot be returned. |
protected void |
merge(CalendricalEngine engine,
java.util.List<CalendricalEngine> engines)
Override point to affect the merging process. |
T |
reify(java.lang.Object value)
Returns the input value cast to the correct generic type. |
java.lang.String |
toString()
Outputs this rule as a String , using the name. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.util.Comparator |
---|
equals |
Constructor Detail |
---|
protected CalendricalRule(java.lang.Class<T> type, java.lang.String name)
type
- the reified class, not nullname
- the name of the type, not nullMethod Detail |
---|
public final java.lang.String getName()
Implementations should use the name that best represents themselves. If the rule represents a field, then the form 'UnitOfRange' should be used. Otherwise, use the simple class name of the generic type, such as 'ZoneOffset'. For debugging reasons, the name should be as unique as possible.
public final java.lang.Class<T> getType()
Each rule uses an underlying type to represent the data.
This is captured in the generic type of the rule.
Since the generic implementation is Java is limited to the compiler, the
underlying type has been reified and made available through this method.
It is expected, but not enforced, that the underlying type is Comparable
.
public final T reify(java.lang.Object value)
Each rule uses an underlying type to represent the data. This is captured in the generic type of the rule. Since the generic implementation is Java is limited to the compiler, the underlying type has been reified which allows this method to validate the generic type fully. The implementation simply returns the input value typed as the generic type.
value
- the value to reify, may be null
java.lang.ClassCastException
- if the value is not of the reified typepublic final T getValue(Calendrical calendrical)
null
if the value cannot be returned.
This method simply queries the calendrical.
calendrical
- the calendrical to get the field value from, not null
public final T getValueChecked(Calendrical calendrical)
This convenience method uses getValue(Calendrical)
to find the value
and then ensures it isn't null
.
calendrical
- the calendrical to get the field value from, not null
CalendricalException
- if the rule cannot be extractedprotected void merge(CalendricalEngine engine, java.util.List<CalendricalEngine> engines)
This method is called during merging on the rule associated with each semi-normalized merger.
The implementation has the opportunity to adjust the mergers, potentially handling
any conflicts. For example, the OffsetTime
rule could check to see if there
is an OffsetDate
with a conflicting offset, and adjust the time accordingly.
engine
- the engine that was created from an instance of the object associated with this rule, not nullengines
- all the engines being processed, unmodifiable, but containing modifiable mergers, not nullprotected T deriveFrom(CalendricalEngine engine)
This is part of the merge process, which exists to extract the maximum information possible from a set calendrical data. Before this method is called, the engine will be normalized, which ensures that any fields that can be converted to objects will have been. Thus this method is primarily used to create objects from the normalized form.
A typical implementation will check the objects and determine if the value can be
derived from them. For example, a LocalDateTime
can be derived from a
LocalDate
and a LocalTime
.
In general, only the objects should be used for derivation, as derivation from any
remaining fields is handled directly by the engine.
Implementations should avoid throwing exceptions and use the merger error mechanism instead. It is strongly recommended to treat the data in the engine as immutable.
This implementation uses CalendricalEngine.getFieldDerived(javax.time.calendar.DateTimeRule, boolean)
engine
- the engine to derive from, not null
public int compare(Calendrical cal1, Calendrical cal2)
Calendrical
implementations based on the value
of this rule extracted from each calendrical.
This implements the Comparator
interface and allows any two
Calendrical
implementations to be compared using this rule.
The comparison is based on the result of calling Calendrical.get(javax.time.calendar.CalendricalRule
on each calendrical, and comparing those values.
For example, to sort a list into year order when the list may contain any
mixture of calendricals, such as a LocalDate
, YearMonth
and ZonedDateTime
:
ListIf the value of this rule cannot be obtained from a calendrical, then an exception is thrown.list = ... Collections.sort(list, ISODateTimeRule.YEAR);
If the underlying type of this rule does not implement Comparable
then an exception will be thrown.
compare
in interface java.util.Comparator<Calendrical>
cal1
- the first calendrical to compare, not nullcal2
- the second calendrical to compare, not null
java.lang.NullPointerException
- if either input is null
java.lang.ClassCastException
- if this rule has a type that is not comparable
java.lang.IllegalArgumentException
- if this rule cannot be extracted from either input parameterpublic java.lang.String toString()
String
, using the name.
toString
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |