Class ZoneRulesBuilder

java.lang.Object
javax.time.calendar.zone.ZoneRulesBuilder

public class ZoneRulesBuilder extends Object
A mutable builder used to create all the rules for a historic time-zone.

The rules of a time-zone describe how the offset changes over time. The rules are created by building windows on the time-line within which the different rules apply. The rules may be one of two kinds:

  • Fixed savings - A single fixed amount of savings from the standard offset will apply.
  • Rules - A set of one or more rules describe how daylight savings changes during the window.

TransitionRulesBuilder is a mutable class used to create instances of TimeZone. It must only be used from a single thread. The created TimeZone instances are immutable and thread-safe.

  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      A serialization identifier for this class.
      See Also:
    • MAX_DATE_TIME

      private static final LocalDateTime MAX_DATE_TIME
      The maximum date-time.
    • windowList

      private List<ZoneRulesBuilder.TZWindow> windowList
      The list of windows.
    • deduplicateMap

      private Map<Object,Object> deduplicateMap
      A map for deduplicating the output.
  • Constructor Details

    • ZoneRulesBuilder

      public ZoneRulesBuilder()
      Constructs an instance of the builder that can be used to create zone rules.

      The builder is used by adding one or more windows representing portions of the time-line. The standard offset from UTC will be constant within a window, although two adjacent windows can have the same standard offset.

      Within each window, there can either be a fixed savings amount or a list of rules.

  • Method Details

    • checkNotNull

      private static void checkNotNull(Object object, String errorMessage)
      Validates that the input value is not null.
      Parameters:
      object - the object to check
      errorMessage - the error to throw
      Throws:
      NullPointerException - if the object is null
    • addWindow

      public ZoneRulesBuilder addWindow(ZoneOffset standardOffset, LocalDateTime until, ZoneRulesBuilder.TimeDefinition untilDefinition)
      Adds a window to the builder that can be used to filter a set of rules.

      This method defines and adds a window to the zone where the standard offset is specified. The window limits the effect of subsequent additions of transition rules or fixed savings. If neither rules or fixed savings are added to the window then the window will default to no savings.

      Each window must be added sequentially, as the start instant of the window is derived from the until instant of the previous window.

      Parameters:
      standardOffset - the standard offset, not null
      until - the date-time that the offset applies until, not null
      untilDefinition - the time type for the until date-time, not null
      Returns:
      this, for chaining
      Throws:
      IllegalStateException - if the window order is invalid
    • addWindowForever

      public ZoneRulesBuilder addWindowForever(ZoneOffset standardOffset)
      Adds a window that applies until the end of time to the builder that can be used to filter a set of rules.

      This method defines and adds a window to the zone where the standard offset is specified. The window limits the effect of subsequent additions of transition rules or fixed savings. If neither rules or fixed savings are added to the window then the window will default to no savings.

      This must be added after all other windows. No more windows can be added after this one.

      Parameters:
      standardOffset - the standard offset, not null
      Returns:
      this, for chaining
      Throws:
      IllegalStateException - if a forever window has already been added
    • setFixedSavingsToWindow

      public ZoneRulesBuilder setFixedSavingsToWindow(Period fixedSavingAmount)
      Sets the previously added window to have fixed savings.

      Setting a window to have fixed savings simply means that a single daylight savings amount applies throughout the window. The window could be small, such as a single summer, or large, such as a multi-year daylight savings.

      A window can either have fixed savings or rules but not both.

      Parameters:
      fixedSavingAmount - the amount of saving to use for the whole window, not null
      Returns:
      this, for chaining
      Throws:
      IllegalStateException - if no window has yet been added
      IllegalStateException - if the window already has rules
    • addRuleToWindow

      public ZoneRulesBuilder addRuleToWindow(LocalDateTime dateTime, ZoneRulesBuilder.TimeDefinition timeDefinition, Period savingAmount)
      Adds a single transition rule to the current window.

      This adds a rule such that the offset, expressed as a daylight savings amount, changes at the specified date-time.

      Parameters:
      dateTime - the date-time that the transition occurs as defined by timeDefintion, not null
      timeDefinition - the definition of how to convert local to actual time, not null
      savingAmount - the amount of saving from the standard offset after the transition, not null
      Returns:
      this, for chaining
      Throws:
      IllegalStateException - if no window has yet been added
      IllegalStateException - if the window already has fixed savings
      IllegalStateException - if the window has reached the maximum capacity of 2000 rules
    • addRuleToWindow

      public ZoneRulesBuilder addRuleToWindow(int year, MonthOfYear month, int dayOfMonthIndicator, LocalTime time, boolean timeEndOfDay, ZoneRulesBuilder.TimeDefinition timeDefinition, Period savingAmount)
      Adds a single transition rule to the current window.

      This adds a rule such that the offset, expressed as a daylight savings amount, changes at the specified date-time.

      Parameters:
      year - the year of the transition, from MIN_YEAR to MAX_YEAR
      month - the month of the transition, not null
      dayOfMonthIndicator - the day-of-month of the transition, adjusted by dayOfWeek, from 1 to 31 adjusted later, or -1 to -28 adjusted earlier from the last day of the month
      time - the time that the transition occurs as defined by timeDefintion, not null
      timeEndOfDay - whether midnight is at the end of day
      timeDefinition - the definition of how to convert local to actual time, not null
      savingAmount - the amount of saving from the standard offset after the transition, not null
      Returns:
      this, for chaining
      Throws:
      IllegalCalendarFieldValueException - if a date-time field is out of range
      IllegalStateException - if no window has yet been added
      IllegalStateException - if the window already has fixed savings
      IllegalStateException - if the window has reached the maximum capacity of 2000 rules
    • addRuleToWindow

      public ZoneRulesBuilder addRuleToWindow(int startYear, int endYear, MonthOfYear month, int dayOfMonthIndicator, DayOfWeek dayOfWeek, LocalTime time, boolean timeEndOfDay, ZoneRulesBuilder.TimeDefinition timeDefinition, Period savingAmount)
      Adds a multi-year transition rule to the current window.

      This adds a rule such that the offset, expressed as a daylight savings amount, changes at the specified date-time for each year in the range.

      Parameters:
      startYear - the start year of the rule, from MIN_YEAR to MAX_YEAR
      endYear - the end year of the rule, from MIN_YEAR to MAX_YEAR
      month - the month of the transition, not null
      dayOfMonthIndicator - the day-of-month of the transition, adjusted by dayOfWeek, from 1 to 31 adjusted later, or -1 to -28 adjusted earlier from the last day of the month
      dayOfWeek - the day-of-week to adjust to, null if day-of-month should not be adjusted
      time - the time that the transition occurs as defined by timeDefintion, not null
      timeEndOfDay - whether midnight is at the end of day
      timeDefinition - the definition of how to convert local to actual time, not null
      savingAmount - the amount of saving from the standard offset after the transition, not null
      Returns:
      this, for chaining
      Throws:
      IllegalCalendarFieldValueException - if a date-time field is out of range
      IllegalArgumentException - if the day of month indicator is invalid
      IllegalArgumentException - if the end of day midnight flag does not match the time
      IllegalStateException - if no window has yet been added
      IllegalStateException - if the window already has fixed savings
      IllegalStateException - if the window has reached the maximum capacity of 2000 rules
    • toRules

      public ZoneRules toRules(String id)
      Completes the build converting the builder to a set of time-zone rules.

      Calling this method alters the state of the builder. Further rules should not be added to this builder once this method is called.

      Parameters:
      id - the time-zone id, not null
      Returns:
      the zone rules, never null
      Throws:
      IllegalStateException - if no windows have been added
      IllegalStateException - if there is only one rule defined as being forever for any given window
    • toRules

      ZoneRules toRules(String id, Map<Object,Object> deduplicateMap)
      Completes the build converting the builder to a set of time-zone rules.

      Calling this method alters the state of the builder. Further rules should not be added to this builder once this method is called.

      Parameters:
      id - the time-zone id, not null
      deduplicateMap - a map for deduplicating the values, not null
      Returns:
      the zone rules, never null
      Throws:
      IllegalStateException - if no windows have been added
      IllegalStateException - if there is only one rule defined as being forever for any given window
    • deduplicate

      <T> T deduplicate(T object)
      Deduplicates an object instance.
      Type Parameters:
      T - the generic type
      Parameters:
      object - the object to deduplicate
      Returns:
      the deduplicated object