AccountingChronology.java

  1. /*
  2.  * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
  3.  *
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions are met:
  8.  *
  9.  *  * Redistributions of source code must retain the above copyright notice,
  10.  *    this list of conditions and the following disclaimer.
  11.  *
  12.  *  * Redistributions in binary form must reproduce the above copyright notice,
  13.  *    this list of conditions and the following disclaimer in the documentation
  14.  *    and/or other materials provided with the distribution.
  15.  *
  16.  *  * Neither the name of JSR-310 nor the names of its contributors
  17.  *    may be used to endorse or promote products derived from this software
  18.  *    without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  */
  32. package org.threeten.extra.chrono;

  33. import static org.threeten.extra.chrono.AccountingYearDivision.THIRTEEN_EVEN_MONTHS_OF_4_WEEKS;

  34. import java.io.Serializable;
  35. import java.time.Clock;
  36. import java.time.DateTimeException;
  37. import java.time.DayOfWeek;
  38. import java.time.Instant;
  39. import java.time.LocalDate;
  40. import java.time.Month;
  41. import java.time.ZoneId;
  42. import java.time.chrono.AbstractChronology;
  43. import java.time.chrono.ChronoLocalDateTime;
  44. import java.time.chrono.ChronoZonedDateTime;
  45. import java.time.chrono.Chronology;
  46. import java.time.chrono.Era;
  47. import java.time.temporal.ChronoField;
  48. import java.time.temporal.ChronoUnit;
  49. import java.time.temporal.TemporalAccessor;
  50. import java.time.temporal.TemporalAdjusters;
  51. import java.time.temporal.ValueRange;
  52. import java.util.Arrays;
  53. import java.util.List;

  54. /**
  55.  * An Accounting calendar system.
  56.  * <p>
  57.  * This chronology defines the rules of a proleptic 52/53-week Accounting calendar system.
  58.  * This calendar system follows the rules as laid down in <a href="https://www.irs.gov/publications/p538">IRS Publication 538</a>
  59.  * and the <a href="https://www.ifrs.org/">International Financial Reporting Standards</a>.
  60.  * The start of the Accounting calendar will vary against the ISO calendar.
  61.  * Depending on options chosen, it can start as early as {@code 0000-01-26 (ISO)} or as late as {@code 0001-01-04 (ISO)}.
  62.  * <p>
  63.  * This class is proleptic. It implements Accounting chronology rules for the entire time-line.
  64.  * <p>
  65.  * The fields are defined as follows:
  66.  * <ul>
  67.  * <li>era - There are two eras, the current 'Current Era' (CE) and the previous era 'Before Current Era' (BCE).
  68.  * <li>year-of-era - The year-of-era for the current era increases uniformly from the epoch at year one.
  69.  *  For the previous era the year increases from one as time goes backwards.
  70.  * <li>proleptic-year - The proleptic year is the same as the year-of-era for the
  71.  *  current era. For the previous era, years have zero, then negative values.
  72.  * <li>month-of-year - There are 12 or 13 months (periods) in an Accounting year, numbered from 1 to 12 or 13.
  73.  * <li>day-of-month - There are 28 or 35 days in each Accounting month, numbered from 1 to 35.
  74.  *  Month length depends on how the year has been divided.
  75.  *  When the Accounting leap year occurs, a week (7 days) is added to a specific month;
  76.  *  this increases to maximum day-of-month numbering to 35 or 42.
  77.  * <li>day-of-year - There are 364 days in a standard Accounting year and 371 in a leap year.
  78.  *  The days are numbered from 1 to 364 or 1 to 371.
  79.  * <li>leap-year - Leap years usually occur every 5 or 6 years.  Timing depends on settings chosen.
  80.  * </ul>
  81.  *
  82.  * <h3>Implementation Requirements</h3>
  83.  * This class is immutable and thread-safe.
  84.  */
  85. public final class AccountingChronology extends AbstractChronology implements Serializable {

  86.     /**
  87.      * Serialization version.
  88.      */
  89.     private static final long serialVersionUID = 7291205177830286973L;
  90.     /**
  91.      * Range of proleptic month for 12-month (period) year.
  92.      */
  93.     private static final ValueRange PROLEPTIC_MONTH_RANGE_12 = ValueRange.of(-999_999 * 12L, 999_999 * 12L + 11);
  94.     /**
  95.      * Range of proleptic month for 13-month (period) year.
  96.      */
  97.     private static final ValueRange PROLEPTIC_MONTH_RANGE_13 = ValueRange.of(-999_999 * 13L, 999_999 * 13L + 12);
  98.     /**
  99.      * Range of weeks in year.
  100.      */
  101.     private static final ValueRange ALIGNED_WEEK_OF_YEAR_RANGE = ValueRange.of(1, 52, 53);
  102.     /**
  103.      * Range of days in year.
  104.      */
  105.     static final ValueRange DAY_OF_YEAR_RANGE = ValueRange.of(1, 364, 371);

  106.     /**
  107.      * The day of the week on which a given Accounting year ends.
  108.      */
  109.     private final DayOfWeek endsOn;
  110.     /**
  111.      * Whether the calendar ends in the last week of a given Gregorian/ISO month,
  112.      * or nearest to the last day of the month (will sometimes be in the next month).
  113.      */
  114.     private final boolean inLastWeek;
  115.     /**
  116.      * Which Gregorian/ISO end-of-month the year ends in/is nearest to.
  117.      */
  118.     private final Month end;
  119.     /**
  120.      * How to divide an accounting year.
  121.      */
  122.     private final AccountingYearDivision division;
  123.     /**
  124.      * The month which will have the leap-week added.
  125.      */
  126.     private final int leapWeekInMonth;
  127.     /**
  128.      * The year offset.
  129.      */
  130.     private final int yearOffset;

  131.     /**
  132.      * Difference in days between accounting year end and ISO month end, in ISO year 0.
  133.      */
  134.     private final transient int yearZeroDifference;
  135.     /**
  136.      * Number of weeks in a month range.
  137.      */
  138.     private final transient ValueRange alignedWeekOfMonthRange;
  139.     /**
  140.      * Number of days in a month range.
  141.      */
  142.     private final transient ValueRange dayOfMonthRange;
  143.     /**
  144.      * Number of days from the start of Accounting year 1 (for this chronology) to the start of ISO 1970
  145.      */
  146.     private final transient int days0001ToIso1970;

  147.     //-----------------------------------------------------------------------
  148.     /**
  149.      * Creates an {@code AccountingChronology} validating the input.
  150.      * Package private as only meant to be called from the builder.
  151.      *
  152.      * @param endsOn  The day-of-week a given year ends on.
  153.      * @param end The  month-end the year is based on.
  154.      * @param inLastWeek  Whether the year ends in the last week of the month, or nearest the end-of-month.
  155.      * @param division  How the year is divided.
  156.      * @param leapWeekInMonth  The month in which the leap-week resides.
  157.      * @return The created Chronology, not null.
  158.      * @throws DateTimeException if the chronology cannot be built.
  159.      */
  160.     static AccountingChronology create(DayOfWeek endsOn, Month end, boolean inLastWeek, AccountingYearDivision division,
  161.             int leapWeekInMonth, int yearOffset) {
  162.         if (endsOn == null || end == null || division == null || leapWeekInMonth == 0) {
  163.             throw new IllegalStateException("AccountingCronology cannot be built: "
  164.                     + (endsOn == null ? "| ending day-of-week |" : "")
  165.                     + (end == null ? "| month ending in/nearest to |" : "")
  166.                     + (division == null ? "| how year divided |" : "")
  167.                     + (leapWeekInMonth == 0 ? "| leap-week month |" : "")
  168.                     + " not set.");
  169.         }
  170.         if (!division.getMonthsInYearRange().isValidValue(leapWeekInMonth)) {
  171.             throw new IllegalStateException("Leap week cannot not be placed in non-existent month " + leapWeekInMonth
  172.                     + ", range is [" + division.getMonthsInYearRange() + "].");
  173.         }

  174.         // Derive cached information.
  175.         LocalDate endingLimit = inLastWeek ? LocalDate.of(0 + yearOffset, end, 1).with(TemporalAdjusters.lastDayOfMonth()) :
  176.                 LocalDate.of(0 + yearOffset, end, 1).with(TemporalAdjusters.lastDayOfMonth()).plusDays(3);
  177.         LocalDate yearZeroEnd = endingLimit.with(TemporalAdjusters.previousOrSame(endsOn));
  178.         int yearZeroDifference = (int) yearZeroEnd.until(endingLimit, ChronoUnit.DAYS);
  179.         // Longest/shortest month lengths and related
  180.         int longestMonthLength = 0;
  181.         int shortestMonthLength = Integer.MAX_VALUE;
  182.         for (int month = 1; month <= division.getMonthsInYearRange().getMaximum(); month++) {
  183.             int monthLength = division.getWeeksInMonth(month);
  184.             shortestMonthLength = Math.min(shortestMonthLength, monthLength);
  185.             longestMonthLength = Math.max(longestMonthLength, monthLength + (month == leapWeekInMonth ? 1 : 0));
  186.         }
  187.         ValueRange alignedWeekOfMonthRange = ValueRange.of(1, shortestMonthLength, longestMonthLength);
  188.         ValueRange dayOfMonthRange = ValueRange.of(1, shortestMonthLength * 7, longestMonthLength * 7);
  189.         int daysToEpoch = Math.toIntExact(0 - yearZeroEnd.plusDays(1).toEpochDay());

  190.         return new AccountingChronology(endsOn, end, inLastWeek, division, leapWeekInMonth, yearZeroDifference,
  191.                 alignedWeekOfMonthRange, dayOfMonthRange, daysToEpoch, yearOffset);
  192.     }

  193.     //-----------------------------------------------------------------------
  194.     /**
  195.      * Creates an instance from validated data, and cached data.
  196.      *
  197.      * @param endsOn  The day-of-week a given year ends on.
  198.      * @param end  The month-end the year is based on.
  199.      * @param inLastWeek  Whether the year ends in the last week of the month, or nearest the end-of-month.
  200.      * @param division  How the year is divided.
  201.      * @param leapWeekInMonth  The month in which the leap-week resides.
  202.      * @param yearZeroDifference  Difference in days between accounting year end and ISO month end, in ISO year 0.
  203.      * @param alignedWeekOfMonthRange  Range of weeks in month.
  204.      * @param dayOfMonthRange  Range of days in month.
  205.      * @param daysToEpoch  The number of days between the start of Accounting 1 and ISO 1970.
  206.      */
  207.     private AccountingChronology(DayOfWeek endsOn, Month end, boolean inLastWeek, AccountingYearDivision division, int leapWeekInMonth, int yearZeroDifference, ValueRange alignedWeekOfMonthRange,
  208.             ValueRange dayOfMonthRange, int daysToEpoch, int yearOffset) {
  209.         this.endsOn = endsOn;
  210.         this.end = end;
  211.         this.inLastWeek = inLastWeek;
  212.         this.division = division;
  213.         this.leapWeekInMonth = leapWeekInMonth;
  214.         this.yearZeroDifference = yearZeroDifference;
  215.         this.alignedWeekOfMonthRange = alignedWeekOfMonthRange;
  216.         this.dayOfMonthRange = dayOfMonthRange;
  217.         this.days0001ToIso1970 = daysToEpoch;
  218.         this.yearOffset = yearOffset;
  219.     }

  220.     /**
  221.      * Resolve stored instances.
  222.      *
  223.      * @return a built, validated instance.
  224.      */
  225.     private Object readResolve() {
  226.         return AccountingChronology.create(endsOn, end, inLastWeek, getDivision(), leapWeekInMonth, yearOffset);
  227.     }

  228.     //-----------------------------------------------------------------------
  229.     AccountingYearDivision getDivision() {
  230.         return division;
  231.     }

  232.     int getLeapWeekInMonth() {
  233.         return leapWeekInMonth;
  234.     }

  235.     int getDays0001ToIso1970() {
  236.         return days0001ToIso1970;
  237.     }

  238.     //-----------------------------------------------------------------------
  239.     /**
  240.      * Gets the ID of the chronology - 'Accounting'.
  241.      * <p>
  242.      * The ID uniquely identifies the {@code Chronology},
  243.      * but does not differentiate between instances of {@code AccountingChronology}.
  244.      * It cannot be used to lookup the {@code Chronology} using {@link Chronology#of(String)},
  245.      * because each instance requires setup.
  246.      *
  247.      * @return the chronology ID - 'Accounting'
  248.      * @see #getCalendarType()
  249.      */
  250.     @Override
  251.     public String getId() {
  252.         return "Accounting";
  253.     }

  254.     /**
  255.      * Gets the calendar type of the underlying calendar system, which is null.
  256.      * <p>
  257.      * The <em>Unicode Locale Data Markup Language (LDML)</em> specification
  258.      * does not define an identifier for 52/53 week calendars used for accounting purposes,
  259.      * and given that setup required is unlikely to do so.
  260.      * For this reason, the calendar type is null.
  261.      *
  262.      * @return null, as the calendar is unlikely to be specified in LDML
  263.      * @see #getId()
  264.      */
  265.     @Override
  266.     public String getCalendarType() {
  267.         return null;
  268.     }

  269.     //-----------------------------------------------------------------------
  270.     /**
  271.      * Obtains a local date in Accounting calendar system from the
  272.      * era, year-of-era, month-of-year and day-of-month fields.
  273.      *
  274.      * @param era  the Accounting era, not null
  275.      * @param yearOfEra  the year-of-era
  276.      * @param month  the month-of-year
  277.      * @param dayOfMonth  the day-of-month
  278.      * @return the Accounting local date, not null
  279.      * @throws DateTimeException if unable to create the date
  280.      * @throws ClassCastException if the {@code era} is not a {@code AccountingEra}
  281.      */
  282.     @Override
  283.     public AccountingDate date(Era era, int yearOfEra, int month, int dayOfMonth) {
  284.         return date(prolepticYear(era, yearOfEra), month, dayOfMonth);
  285.     }

  286.     /**
  287.      * Obtains a local date in Accounting calendar system from the
  288.      * proleptic-year, month-of-year and day-of-month fields.
  289.      *
  290.      * @param prolepticYear  the proleptic-year
  291.      * @param month  the month-of-year
  292.      * @param dayOfMonth  the day-of-month
  293.      * @return the Accounting local date, not null
  294.      * @throws DateTimeException if unable to create the date
  295.      */
  296.     @Override
  297.     public AccountingDate date(int prolepticYear, int month, int dayOfMonth) {
  298.         return AccountingDate.of(this, prolepticYear, month, dayOfMonth);
  299.     }

  300.     /**
  301.      * Obtains a local date in Accounting calendar system from the
  302.      * era, year-of-era and day-of-year fields.
  303.      *
  304.      * @param era  the Accounting era, not null
  305.      * @param yearOfEra  the year-of-era
  306.      * @param dayOfYear  the day-of-year
  307.      * @return the Accounting local date, not null
  308.      * @throws DateTimeException if unable to create the date
  309.      * @throws ClassCastException if the {@code era} is not a {@code AccountingEra}
  310.      */
  311.     @Override
  312.     public AccountingDate dateYearDay(Era era, int yearOfEra, int dayOfYear) {
  313.         return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear);
  314.     }

  315.     /**
  316.      * Obtains a local date in Accounting calendar system from the
  317.      * proleptic-year and day-of-year fields.
  318.      *
  319.      * @param prolepticYear  the proleptic-year
  320.      * @param dayOfYear  the day-of-year
  321.      * @return the Accounting local date, not null
  322.      * @throws DateTimeException if unable to create the date
  323.      */
  324.     @Override
  325.     public AccountingDate dateYearDay(int prolepticYear, int dayOfYear) {
  326.         return AccountingDate.ofYearDay(this, prolepticYear, dayOfYear);
  327.     }

  328.     /**
  329.      * Obtains a local date in the Accounting calendar system from the epoch-day.
  330.      *
  331.      * @param epochDay  the epoch day
  332.      * @return the Accounting local date, not null
  333.      * @throws DateTimeException if unable to create the date
  334.      */
  335.     @Override  // override with covariant return type
  336.     public AccountingDate dateEpochDay(long epochDay) {
  337.         return AccountingDate.ofEpochDay(this, epochDay);
  338.     }

  339.     //-------------------------------------------------------------------------
  340.     /**
  341.      * Obtains the current Accounting local date from the system clock in the default time-zone.
  342.      * <p>
  343.      * This will query the {@link Clock#systemDefaultZone() system clock} in the default
  344.      * time-zone to obtain the current date.
  345.      * <p>
  346.      * Using this method will prevent the ability to use an alternate clock for testing
  347.      * because the clock is hard-coded.
  348.      *
  349.      * @return the current Accounting local date using the system clock and default time-zone, not null
  350.      * @throws DateTimeException if unable to create the date
  351.      */
  352.     @Override  // override with covariant return type
  353.     public AccountingDate dateNow() {
  354.         return AccountingDate.now(this);
  355.     }

  356.     /**
  357.      * Obtains the current Accounting local date from the system clock in the specified time-zone.
  358.      * <p>
  359.      * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date.
  360.      * Specifying the time-zone avoids dependence on the default time-zone.
  361.      * <p>
  362.      * Using this method will prevent the ability to use an alternate clock for testing
  363.      * because the clock is hard-coded.
  364.      *
  365.      * @param zone the zone ID to use, not null
  366.      * @return the current Accounting local date using the system clock, not null
  367.      * @throws DateTimeException if unable to create the date
  368.      */
  369.     @Override  // override with covariant return type
  370.     public AccountingDate dateNow(ZoneId zone) {
  371.         return AccountingDate.now(this, zone);
  372.     }

  373.     /**
  374.      * Obtains the current Accounting local date from the specified clock.
  375.      * <p>
  376.      * This will query the specified clock to obtain the current date - today.
  377.      * Using this method allows the use of an alternate clock for testing.
  378.      * The alternate clock may be introduced using {@link Clock dependency injection}.
  379.      *
  380.      * @param clock  the clock to use, not null
  381.      * @return the current Accounting local date, not null
  382.      * @throws DateTimeException if unable to create the date
  383.      */
  384.     @Override  // override with covariant return type
  385.     public AccountingDate dateNow(Clock clock) {
  386.         return AccountingDate.now(this, clock);
  387.     }

  388.     //-------------------------------------------------------------------------
  389.     /**
  390.      * Obtains a Accounting local date from another date-time object.
  391.      *
  392.      * @param temporal  the date-time object to convert, not null
  393.      * @return the Accounting local date, not null
  394.      * @throws DateTimeException if unable to create the date
  395.      */
  396.     @Override
  397.     public AccountingDate date(TemporalAccessor temporal) {
  398.         return AccountingDate.from(this, temporal);
  399.     }

  400.     /**
  401.      * Obtains a Accounting local date-time from another date-time object.
  402.      *
  403.      * @param temporal  the date-time object to convert, not null
  404.      * @return the Accounting local date-time, not null
  405.      * @throws DateTimeException if unable to create the date-time
  406.      */
  407.     @Override
  408.     @SuppressWarnings("unchecked")
  409.     public ChronoLocalDateTime<AccountingDate> localDateTime(TemporalAccessor temporal) {
  410.         return (ChronoLocalDateTime<AccountingDate>) super.localDateTime(temporal);
  411.     }

  412.     /**
  413.      * Obtains a Accounting zoned date-time from another date-time object.
  414.      *
  415.      * @param temporal  the date-time object to convert, not null
  416.      * @return the Accounting zoned date-time, not null
  417.      * @throws DateTimeException if unable to create the date-time
  418.      */
  419.     @Override
  420.     @SuppressWarnings("unchecked")
  421.     public ChronoZonedDateTime<AccountingDate> zonedDateTime(TemporalAccessor temporal) {
  422.         return (ChronoZonedDateTime<AccountingDate>) super.zonedDateTime(temporal);
  423.     }

  424.     /**
  425.      * Obtains a Accounting zoned date-time in this chronology from an {@code Instant}.
  426.      *
  427.      * @param instant  the instant to create the date-time from, not null
  428.      * @param zone  the time-zone, not null
  429.      * @return the Accounting zoned date-time, not null
  430.      * @throws DateTimeException if the result exceeds the supported range
  431.      */
  432.     @Override
  433.     @SuppressWarnings("unchecked")
  434.     public ChronoZonedDateTime<AccountingDate> zonedDateTime(Instant instant, ZoneId zone) {
  435.         return (ChronoZonedDateTime<AccountingDate>) super.zonedDateTime(instant, zone);
  436.     }

  437.     //-----------------------------------------------------------------------
  438.     /**
  439.      * Checks if the specified year is a leap year.
  440.      * <p>
  441.      * An Accounting proleptic-year is leap if the time between the end of the previous year
  442.      * and the end of the current year is 371 days.
  443.      * This method does not validate the year passed in, and only has a
  444.      * well-defined result for years in the supported range.
  445.      *
  446.      * @param prolepticYear  the proleptic-year to check, not validated for range
  447.      * @return true if the year is a leap year
  448.      */
  449.     @Override
  450.     public boolean isLeapYear(long prolepticYear) {
  451.         return Math.floorMod(prolepticYear + getISOLeapYearCount(prolepticYear) + yearZeroDifference, 7) == 0
  452.                 || Math.floorMod(prolepticYear + getISOLeapYearCount(prolepticYear + 1) + yearZeroDifference, 7) == 0;
  453.     }

  454.     /**
  455.      * Return the number of ISO Leap Years since Accounting Year 1.
  456.      * <p>
  457.      * This method calculates how many ISO leap years have passed since year 1.
  458.      * The count returned may be negative for years before 1.
  459.      * This method does not validate the year passed in, and only has a
  460.      * well-defined result for years in the supported range.
  461.      *
  462.      * @param prolepticYear  the proleptic-year to check, not validated for range
  463.      * @return the count of leap years since year 1.
  464.      */
  465.     private long getISOLeapYearCount(long prolepticYear) {
  466.         long offsetYear = prolepticYear - (end == Month.JANUARY? 1 : 0) - 1 + yearOffset;
  467.         return Math.floorDiv(offsetYear, 4) - Math.floorDiv(offsetYear, 100) + Math.floorDiv(offsetYear, 400) + (end == Month.JANUARY && yearOffset == 0 ? 1 : 0);
  468.     }

  469.     /**
  470.      * Returns the count of leap years since year 1.
  471.      * <p>
  472.      * This method calculates how many Accounting leap years have passed since year 1.
  473.      * The count returned may be negative for years before 1.
  474.      * This method does not validate the year passed in, and only has a
  475.      * well-defined result for years in the supported range.
  476.      *
  477.      * @param prolepticYear  the proleptic-year to check, not validated for range
  478.      * @return the count of leap years since year 1.
  479.      */
  480.     long previousLeapYears(long prolepticYear) {
  481.         return Math.floorDiv(prolepticYear - 1 + getISOLeapYearCount(prolepticYear) + yearZeroDifference, 7);
  482.     }

  483.     @Override
  484.     public int prolepticYear(Era era, int yearOfEra) {
  485.         if (!(era instanceof AccountingEra)) {
  486.             throw new ClassCastException("Era must be AccountingEra");
  487.         }
  488.         return (era == AccountingEra.CE ? yearOfEra : 1 - yearOfEra);
  489.     }

  490.     @Override
  491.     public AccountingEra eraOf(int era) {
  492.         return AccountingEra.of(era);
  493.     }

  494.     @Override
  495.     public List<Era> eras() {
  496.         return Arrays.<Era>asList(AccountingEra.values());
  497.     }

  498.     //-----------------------------------------------------------------------
  499.     @Override
  500.     public ValueRange range(ChronoField field) {
  501.         switch (field) {
  502.             case ALIGNED_WEEK_OF_MONTH:
  503.                 return alignedWeekOfMonthRange;
  504.             case ALIGNED_WEEK_OF_YEAR:
  505.                 return ALIGNED_WEEK_OF_YEAR_RANGE;
  506.             case DAY_OF_MONTH:
  507.                 return dayOfMonthRange;
  508.             case DAY_OF_YEAR:
  509.                 return DAY_OF_YEAR_RANGE;
  510.             case MONTH_OF_YEAR:
  511.                 return getDivision().getMonthsInYearRange();
  512.             case PROLEPTIC_MONTH:
  513.                 return getDivision() == THIRTEEN_EVEN_MONTHS_OF_4_WEEKS ? PROLEPTIC_MONTH_RANGE_13 : PROLEPTIC_MONTH_RANGE_12;
  514.             default:
  515.                 break;
  516.         }
  517.         return field.range();
  518.     }

  519.     //-------------------------------------------------------------------------
  520.     @Override
  521.     public boolean equals(Object obj) {
  522.         if (this == obj) {
  523.             return true;
  524.         }
  525.         if (obj instanceof AccountingChronology) {
  526.             AccountingChronology other = (AccountingChronology) obj;
  527.             return this.endsOn == other.endsOn &&
  528.                     this.inLastWeek == other.inLastWeek &&
  529.                     this.end == other.end &&
  530.                     this.getDivision() == other.getDivision() &&
  531.                     this.leapWeekInMonth == other.leapWeekInMonth &&
  532.                     this.yearOffset == other.yearOffset;
  533.         }
  534.         return false;
  535.     }

  536.     @Override
  537.     public int hashCode() {
  538.         final int prime = 31;
  539.         int result = 0;
  540.         result = prime * result + endsOn.hashCode();
  541.         result = prime * result + (inLastWeek ? 1231 : 1237);
  542.         result = prime * result + end.hashCode();
  543.         result = prime * result + leapWeekInMonth;
  544.         result = prime * result + getDivision().hashCode();
  545.         result = prime * result + yearOffset;
  546.         return result;
  547.     }

  548.     @Override
  549.     public String toString() {
  550.         StringBuilder bld = new StringBuilder(30);
  551.         bld.append(getId())
  552.                 .append(" calendar ends on ")
  553.                 .append(endsOn)
  554.                 .append(inLastWeek ? " in last week of " : " nearest end of ")
  555.                 .append(end)
  556.                 .append(", year divided in ")
  557.                 .append(getDivision())
  558.                 .append(" with leap-week in month ")
  559.                 .append(leapWeekInMonth)
  560.                 .append(yearOffset == 0 ? " ending in the given ISO year" : " starting in the given ISO year");
  561.         return bld.toString();
  562.     }

  563. }