Class ChronoEntity<T extends ChronoEntity<T>>

java.lang.Object
net.time4j.engine.ChronoEntity<T>
Type Parameters:
T - generic type of self reference
All Implemented Interfaces:
ChronoDisplay
Direct Known Subclasses:
AnnualDate, CalendarVariant, FixedCalendarInterval, SpanOfWeekdays, TimePoint

public abstract class ChronoEntity<T extends ChronoEntity<T>> extends Object implements ChronoDisplay

Represents a temporal object which associates partial temporal values with chronological elements and also allows some manipulations of these element values.

A ChronoEntity is usually a TimePoint, where the (primary) element values determine the position on a time axis such that a time arithmetic is possible. Alternatively a ChronoEntity can also represent a partial information like the combination of month and day-of-month for a simple display of a birthday.

Chronological elements are either statically registered such that a direct access is enabled, or there is an (external) rule which enables read- and write-access. If no element rule can be found a RuleNotFoundException will be thrown.

Note: All public implementations must be immutable.

Author:
Meno Hochschild
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    contains​(ChronoElement<?> element)
    Queries if the value for given chronological element can be accessed via get(element).
    <V> V
    get​(ChronoElement<V> element)
    Returns the partial value associated with given chronological element.
    <R> R
    get​(ChronoFunction<? super T,​R> function)
    Lets given query evaluate this entity.
    int
    Returns the partial value associated with given chronological element.
    <V> V
    getMaximum​(ChronoElement<V> element)
    Yields the maximum value of given chronological element in the current context of this object.
    <V> V
    getMinimum​(ChronoElement<V> element)
    Yields the minimum value of given chronological element in the current context of this object.
    Yields all registered elements of this instance.
    Returns the associated timezone ID for display purposes if available.
    boolean
    Queries if this entity contains a timezone for display purposes.
    boolean
    isValid​(ChronoElement<Integer> element, int value)
    Tests if the value for given chronological value is invalid.
    boolean
    isValid​(ChronoElement<Long> element, long value)
    Tests if the value for given chronological value is invalid.
    <V> boolean
    isValid​(ChronoElement<V> element, V value)
    Tests if the value for given chronological value is invalid.
    boolean
    matches​(ChronoCondition<? super T> condition)
    Queries if this entity matches given condition.
    with​(ChronoElement<Integer> element, int value)
    Creates a copy of this instance with the changed element value.
    with​(ChronoElement<Long> element, long value)
    Creates a copy of this instance with the changed element value.
    <V> T
    with​(ChronoElement<V> element, V value)
    Creates a copy of this instance with the changed element value.
    with​(ChronoOperator<T> operator)
    Creates a copy of this instance which is adjusted by given ChronoOperator using a strategy pattern approach.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ChronoEntity

      public ChronoEntity()
  • Method Details

    • contains

      public boolean contains(ChronoElement<?> element)
      Description copied from interface: ChronoDisplay

      Queries if the value for given chronological element can be accessed via get(element).

      If the argument is missing then this method will yield false. Note: Elements which are not registered but define a suitable rule are also accessible.

      Specified by:
      contains in interface ChronoDisplay
      Parameters:
      element - chronological element to be asked (optional)
      Returns:
      true if the element is registered or there is an element rule for evaluating the value else false
      See Also:
      ChronoDisplay.get(ChronoElement)
    • get

      public <V> V get(ChronoElement<V> element)
      Description copied from interface: ChronoDisplay

      Returns the partial value associated with given chronological element.

      Specified by:
      get in interface ChronoDisplay
      Type Parameters:
      V - generic type of element value
      Parameters:
      element - element which has the value
      Returns:
      associated element value as object (never null)
      See Also:
      ChronoDisplay.contains(ChronoElement)
    • getInt

      public int getInt(ChronoElement<Integer> element)
      Description copied from interface: ChronoDisplay

      Returns the partial value associated with given chronological element.

      Specified by:
      getInt in interface ChronoDisplay
      Parameters:
      element - element which has the value
      Returns:
      associated element value as int primitive or Integer.MIN_VALUE if not available
      See Also:
      ChronoDisplay.get(ChronoElement)
    • getMinimum

      public <V> V getMinimum(ChronoElement<V> element)
      Description copied from interface: ChronoDisplay

      Yields the minimum value of given chronological element in the current context of this object.

      The definition of a minimum and a maximum does generally not imply that every intermediate value between minimum and maximum is valid in this context. For example in the timezone Europe/Berlin the hour [T02:00] will be invalid if switching to summer time.

      In most cases the minimum value is not dependent on this context.

      Specified by:
      getMinimum in interface ChronoDisplay
      Type Parameters:
      V - generic type of element value
      Parameters:
      element - element whose minimum value is to be evaluated
      Returns:
      minimum maybe context-dependent element value
      See Also:
      ChronoElement.getDefaultMinimum(), ChronoDisplay.getMaximum(ChronoElement)
    • getMaximum

      public <V> V getMaximum(ChronoElement<V> element)
      Description copied from interface: ChronoDisplay

      Yields the maximum value of given chronological element in the current context of this object.

      Maximum values are different from minimum case often dependent on the context. An example is the element SECOND_OF_MINUTE whose maximum is normally 59 but can differ in UTC-context with leap seconds. Another more common example is the maximum of the element DAY_OF_MONTH (28-31) which is dependent on the month and year of this context (leap years!).

      Note: In timezone-related timestamps possible offset jumps inducing gaps on the local timeline will be conserved. That means that minimum and maximum do not guarantee a continuum of valid intermediate values.

      Specified by:
      getMaximum in interface ChronoDisplay
      Type Parameters:
      V - generic type of element value
      Parameters:
      element - element whose maximum value is to be evaluated
      Returns:
      maximum maybe context-dependent element value
      See Also:
      ChronoElement.getDefaultMaximum(), ChronoDisplay.getMinimum(ChronoElement)
    • get

      public final <R> R get(ChronoFunction<? super T,​R> function)

      Lets given query evaluate this entity.

      Is equivalent to function.apply(this). The applied strategy pattern hereby enables the externalization of querying the interpretation and evaluation of this entity, consequently enabling user-defined queries with arbitrary result types R. Main difference to chronological elements are lack of formattability and read-only access. Users have to consult the documentation of given query to decide if this method will yield null or throws an exception if the result is undefined or otherwise not obtainable.

      Type Parameters:
      R - generic type of result of query
      Parameters:
      function - time query
      Returns:
      result of query or null if undefined
      Throws:
      ChronoException - if the given query is not executable
    • matches

      public boolean matches(ChronoCondition<? super T> condition)

      Queries if this entity matches given condition.

      Is equivalent to condition.test(this). This method will never throw an exception. This behaviour is in contrast to that of ChronoFunction.

      Parameters:
      condition - temporal condition
      Returns:
      true if the given condition is matched by this entity else false
      See Also:
      get(ChronoFunction)
    • isValid

      public <V> boolean isValid(ChronoElement<V> element, V value)

      Tests if the value for given chronological value is invalid.

      Notes: This method tests if given value to be in question can be set via the expression with(element, value). A numerical overflow situation (causing an ArithmeticException) will usually not be checked.

      Type Parameters:
      V - generic type of element value
      Parameters:
      element - element the given value shall be assigned to
      value - candidate value to be validated (optional)
      Returns:
      true if the method with() can be called without exception else false
      See Also:
      with(ChronoElement, V)
    • isValid

      public boolean isValid(ChronoElement<Integer> element, int value)

      Tests if the value for given chronological value is invalid.

      Notes: This method tests if given value to be in question can be set via the expression with(element, value). A numerical overflow situation (causing an ArithmeticException) will usually not be checked.

      Parameters:
      element - element the given value shall be assigned to
      value - candidate value to be validated
      Returns:
      true if the method with() can be called without exception else false
      See Also:
      with(ChronoElement, int)
    • isValid

      public boolean isValid(ChronoElement<Long> element, long value)

      Tests if the value for given chronological value is invalid.

      Notes: This method tests if given value to be in question can be set via the expression with(element, value). A numerical overflow situation (causing an ArithmeticException) will usually not be checked.

      Parameters:
      element - element the given value shall be assigned to
      value - candidate value to be validated
      Returns:
      true if the method with() can be called without exception else false
      See Also:
      with(ChronoElement, long)
    • with

      public <V> T with(ChronoElement<V> element, V value)

      Creates a copy of this instance with the changed element value.

      A null value will almost ever be seen as invalid causing an IllegalArgumentException. Subclasses which permit null must explicitly document this feature.

      Type Parameters:
      V - generic type of element value
      Parameters:
      element - chronological element
      value - new element value
      Returns:
      changed copy of the same type, this instance remains unaffected
      Throws:
      ChronoException - if the element is not registered and there is no element rule for setting the value
      IllegalArgumentException - if the value is not valid
      ArithmeticException - in case of arithmetic overflow
      See Also:
      isValid(ChronoElement, V)
    • with

      public T with(ChronoElement<Integer> element, int value)

      Creates a copy of this instance with the changed element value.

      Parameters:
      element - chronological element
      value - new element value
      Returns:
      changed copy of the same type, this instance remains unaffected
      Throws:
      ChronoException - if the element is not registered and there is no element rule for setting the value
      IllegalArgumentException - if the value is not valid
      ArithmeticException - in case of arithmetic overflow
      See Also:
      isValid(ChronoElement, V)
    • with

      public T with(ChronoElement<Long> element, long value)

      Creates a copy of this instance with the changed element value.

      Parameters:
      element - chronological element
      value - new element value
      Returns:
      changed copy of the same type, this instance remains unaffected
      Throws:
      ChronoException - if the element is not registered and there is no element rule for setting the value
      IllegalArgumentException - if the value is not valid
      ArithmeticException - in case of arithmetic overflow
      See Also:
      isValid(ChronoElement, V)
    • with

      public T with(ChronoOperator<T> operator)

      Creates a copy of this instance which is adjusted by given ChronoOperator using a strategy pattern approach.

      Is equivalent to operator.apply(this). Hereby a user-defined manipulation will be externalized and is semantically similar to the reading counterpart ChronoFunction.

      Parameters:
      operator - operator for adjusting the element values
      Returns:
      changed copy of the same type, this instance remains unaffected
      Throws:
      ChronoException - if no element rule exists for setting the values
      IllegalArgumentException - if any new value is not valid
      ArithmeticException - in case of arithmetic overflow
      See Also:
      get(ChronoFunction)
    • hasTimezone

      public boolean hasTimezone()

      Queries if this entity contains a timezone for display purposes.

      This implementation has no timezone by default and yields false. Subclasses with a timezone reference intended for formatted output will override the method in a suitable way.

      Specified by:
      hasTimezone in interface ChronoDisplay
      Returns:
      true if a timezone is available and can be achieved with getTimezone() else false
    • getTimezone

      public TZID getTimezone()

      Returns the associated timezone ID for display purposes if available.

      This implementation throws a ChronoException by default. Subclasses with a timezone reference intended for formatted output will override the method in a suitable way.

      Specified by:
      getTimezone in interface ChronoDisplay
      Returns:
      timezone identifier if available
      Throws:
      ChronoException - if the timezone is not available
      See Also:
      hasTimezone()
    • getRegisteredElements

      public Set<ChronoElement<?>> getRegisteredElements()

      Yields all registered elements of this instance.

      Note that some (external) elements can be supported but not registered.

      Returns:
      unmodifiable set of internally registered elements
      Since:
      3.13/4.10