1 /*
   2  * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
  28  *
  29  * All rights reserved.
  30  *
  31  * Redistribution and use in source and binary forms, with or without
  32  * modification, are permitted provided that the following conditions are met:
  33  *
  34  *  * Redistributions of source code must retain the above copyright notice,
  35  *    this list of conditions and the following disclaimer.
  36  *
  37  *  * Redistributions in binary form must reproduce the above copyright notice,
  38  *    this list of conditions and the following disclaimer in the documentation
  39  *    and/or other materials provided with the distribution.
  40  *
  41  *  * Neither the name of JSR-310 nor the names of its contributors
  42  *    may be used to endorse or promote products derived from this software
  43  *    without specific prior written permission.
  44  *
  45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56  */
  57 
  58 package java.time.chrono;
  59 
  60 import static java.time.temporal.ChronoField.EPOCH_DAY;
  61 
  62 import java.io.FilePermission;
  63 import java.io.InputStream;
  64 import java.io.InvalidObjectException;
  65 import java.io.ObjectInputStream;
  66 import java.io.Serializable;
  67 import java.security.AccessController;
  68 import java.security.PrivilegedAction;
  69 import java.time.Clock;
  70 import java.time.DateTimeException;
  71 import java.time.Instant;
  72 import java.time.LocalDate;
  73 import java.time.ZoneId;
  74 import java.time.format.ResolverStyle;
  75 import java.time.temporal.ChronoField;
  76 import java.time.temporal.TemporalAccessor;
  77 import java.time.temporal.TemporalField;
  78 import java.time.temporal.ValueRange;
  79 import java.util.Arrays;
  80 import java.util.HashMap;
  81 import java.util.List;
  82 import java.util.Map;
  83 import java.util.Properties;
  84 
  85 import sun.util.logging.PlatformLogger;
  86 
  87 /**
  88  * The Hijrah calendar is a lunar calendar supporting Islamic calendars.
  89  * <p>
  90  * The HijrahChronology follows the rules of the Hijrah calendar system. The Hijrah
  91  * calendar has several variants based on differences in when the new moon is
  92  * determined to have occurred and where the observation is made.
  93  * In some variants the length of each month is
  94  * computed algorithmically from the astronomical data for the moon and earth and
  95  * in others the length of the month is determined by an authorized sighting
  96  * of the new moon. For the algorithmically based calendars the calendar
  97  * can project into the future.
  98  * For sighting based calendars only historical data from past
  99  * sightings is available.
 100  * <p>
 101  * The length of each month is 29 or 30 days.
 102  * Ordinary years have 354 days; leap years have 355 days.
 103  *
 104  * <p>
 105  * CLDR and LDML identify variants:
 106  * <table cellpadding="2" summary="Variants of Hijrah Calendars">
 107  * <thead>
 108  * <tr class="tableSubHeadingColor">
 109  * <th class="colFirst" style="text-align:left" >Chronology ID</th>
 110  * <th class="colFirst" style="text-align:left" >Calendar Type</th>
 111  * <th class="colFirst" style="text-align:left" >Locale extension, see {@link java.util.Locale}</th>
 112  * <th class="colLast" style="text-align:left" >Description</th>
 113  * </tr>
 114  * </thead>
 115  * <tbody>
 116  * <tr class="altColor">
 117  * <td>Hijrah-umalqura</td>
 118  * <td>islamic-umalqura</td>
 119  * <td>ca-islamic-umalqura</td>
 120  * <td>Islamic - Umm Al-Qura calendar of Saudi Arabia</td>
 121  * </tr>
 122  * </tbody>
 123  * </table>
 124  * <p>Additional variants may be available through {@link Chronology#getAvailableChronologies()}.
 125  *
 126  * <p>Example</p>
 127  * <p>
 128  * Selecting the chronology from the locale uses {@link Chronology#ofLocale}
 129  * to find the Chronology based on Locale supported BCP 47 extension mechanism
 130  * to request a specific calendar ("ca"). For example,
 131  * </p>
 132  * <pre>
 133  *      Locale locale = Locale.forLanguageTag("en-US-u-ca-islamic-umalqura");
 134  *      Chronology chrono = Chronology.ofLocale(locale);
 135  * </pre>
 136  *
 137  * @implSpec
 138  * This class is immutable and thread-safe.
 139  *
 140  * @implNote
 141  * Each Hijrah variant is configured individually. Each variant is defined by a
 142  * property resource that defines the {@code ID}, the {@code calendar type},
 143  * the start of the calendar, the alignment with the
 144  * ISO calendar, and the length of each month for a range of years.
 145  * The variants are loaded by HijrahChronology as a resource from
 146  * hijrah-config-&lt;calendar type&gt;.properties.
 147  * <p>
 148  * The Hijrah property resource is a set of properties that describe the calendar.
 149  * The syntax is defined by {@code java.util.Properties#load(Reader)}.
 150  * <table cellpadding="2" summary="Configuration of Hijrah Calendar">
 151  * <thead>
 152  * <tr class="tableSubHeadingColor">
 153  * <th class="colFirst" style="text-align:left" > Property Name</th>
 154  * <th class="colFirst" style="text-align:left" > Property value</th>
 155  * <th class="colLast" style="text-align:left" > Description </th>
 156  * </tr>
 157  * </thead>
 158  * <tbody>
 159  * <tr class="altColor">
 160  * <td>id</td>
 161  * <td>Chronology Id, for example, "Hijrah-umalqura"</td>
 162  * <td>The Id of the calendar in common usage</td>
 163  * </tr>
 164  * <tr class="rowColor">
 165  * <td>type</td>
 166  * <td>Calendar type, for example, "islamic-umalqura"</td>
 167  * <td>LDML defines the calendar types</td>
 168  * </tr>
 169  * <tr class="altColor">
 170  * <td>version</td>
 171  * <td>Version, for example: "1.8.0_1"</td>
 172  * <td>The version of the Hijrah variant data</td>
 173  * </tr>
 174  * <tr class="rowColor">
 175  * <td>iso-start</td>
 176  * <td>ISO start date, formatted as {@code yyyy-MM-dd}, for example: "1900-04-30"</td>
 177  * <td>The ISO date of the first day of the minimum Hijrah year.</td>
 178  * </tr>
 179  * <tr class="altColor">
 180  * <td>yyyy - a numeric 4 digit year, for example "1434"</td>
 181  * <td>The value is a sequence of 12 month lengths,
 182  * for example: "29 30 29 30 29 30 30 30 29 30 29 29"</td>
 183  * <td>The lengths of the 12 months of the year separated by whitespace.
 184  * A numeric year property must be present for every year without any gaps.
 185  * The month lengths must be between 29-32 inclusive.
 186  * </td>
 187  * </tr>
 188  * </tbody>
 189  * </table>
 190  *
 191  * @since 1.8
 192  */
 193 public final class HijrahChronology extends AbstractChronology implements Serializable {
 194 
 195     /**
 196      * The Hijrah Calendar id.
 197      */
 198     private final transient String typeId;
 199     /**
 200      * The Hijrah calendarType.
 201      */
 202     private final transient String calendarType;
 203     /**
 204      * Serialization version.
 205      */
 206     private static final long serialVersionUID = 3127340209035924785L;
 207     /**
 208      * Singleton instance of the Islamic Umm Al-Qura calendar of Saudi Arabia.
 209      * Other Hijrah chronology variants may be available from
 210      * {@link Chronology#getAvailableChronologies}.
 211      */
 212     public static final HijrahChronology INSTANCE;
 213     /**
 214      * Flag to indicate the initialization of configuration data is complete.
 215      * @see #checkCalendarInit()
 216      */
 217     private transient volatile boolean initComplete;
 218     /**
 219      * Array of epoch days indexed by Hijrah Epoch month.
 220      * Computed by {@link #loadCalendarData}.
 221      */
 222     private transient int[] hijrahEpochMonthStartDays;
 223     /**
 224      * The minimum epoch day of this Hijrah calendar.
 225      * Computed by {@link #loadCalendarData}.
 226      */
 227     private transient int minEpochDay;
 228     /**
 229      * The maximum epoch day for which calendar data is available.
 230      * Computed by {@link #loadCalendarData}.
 231      */
 232     private transient int maxEpochDay;
 233     /**
 234      * The minimum epoch month.
 235      * Computed by {@link #loadCalendarData}.
 236      */
 237     private transient int hijrahStartEpochMonth;
 238     /**
 239      * The minimum length of a month.
 240      * Computed by {@link #createEpochMonths}.
 241      */
 242     private transient int minMonthLength;
 243     /**
 244      * The maximum length of a month.
 245      * Computed by {@link #createEpochMonths}.
 246      */
 247     private transient int maxMonthLength;
 248     /**
 249      * The minimum length of a year in days.
 250      * Computed by {@link #createEpochMonths}.
 251      */
 252     private transient int minYearLength;
 253     /**
 254      * The maximum length of a year in days.
 255      * Computed by {@link #createEpochMonths}.
 256      */
 257     private transient int maxYearLength;
 258 
 259     /**
 260      * Prefix of resource names for Hijrah calendar variants.
 261      */
 262     private static final String RESOURCE_PREFIX = "hijrah-config-";
 263 
 264     /**
 265      * Suffix of resource names for Hijrah calendar variants.
 266      */
 267     private static final String RESOURCE_SUFFIX = ".properties";
 268 
 269     /**
 270      * Static initialization of the built-in calendars.
 271      * The data is not loaded until it is used.
 272      */
 273     static {
 274         INSTANCE = new HijrahChronology("Hijrah-umalqura", "islamic-umalqura");
 275         // Register it by its aliases
 276         AbstractChronology.registerChrono(INSTANCE, "Hijrah");
 277         AbstractChronology.registerChrono(INSTANCE, "islamic");
 278     }
 279 
 280     /**
 281      * Create a HijrahChronology for the named variant and type.
 282      *
 283      * @param id the id of the calendar
 284      * @param calType the typeId of the calendar
 285      * @throws IllegalArgumentException if the id or typeId is empty
 286      */
 287     private HijrahChronology(String id, String calType) {
 288         if (id.isEmpty()) {
 289             throw new IllegalArgumentException("calendar id is empty");
 290         }
 291         if (calType.isEmpty()) {
 292             throw new IllegalArgumentException("calendar typeId is empty");
 293         }
 294         this.typeId = id;
 295         this.calendarType = calType;
 296     }
 297 
 298     /**
 299      * Check and ensure that the calendar data has been initialized.
 300      * The initialization check is performed at the boundary between
 301      * public and package methods.  If a public calls another public method
 302      * a check is not necessary in the caller.
 303      * The constructors of HijrahDate call {@link #getEpochDay} or
 304      * {@link #getHijrahDateInfo} so every call from HijrahDate to a
 305      * HijrahChronology via package private methods has been checked.
 306      *
 307      * @throws DateTimeException if the calendar data configuration is
 308      *     malformed or IOExceptions occur loading the data
 309      */
 310     private void checkCalendarInit() {
 311         // Keep this short so it can be inlined for performance
 312         if (initComplete == false) {
 313             loadCalendarData();
 314             initComplete = true;
 315         }
 316     }
 317 
 318     //-----------------------------------------------------------------------
 319     /**
 320      * Gets the ID of the chronology.
 321      * <p>
 322      * The ID uniquely identifies the {@code Chronology}. It can be used to
 323      * lookup the {@code Chronology} using {@link Chronology#of(String)}.
 324      *
 325      * @return the chronology ID, non-null
 326      * @see #getCalendarType()
 327      */
 328     @Override
 329     public String getId() {
 330         return typeId;
 331     }
 332 
 333     /**
 334      * Gets the calendar type of the Islamic calendar.
 335      * <p>
 336      * The calendar type is an identifier defined by the
 337      * <em>Unicode Locale Data Markup Language (LDML)</em> specification.
 338      * It can be used to lookup the {@code Chronology} using {@link Chronology#of(String)}.
 339      *
 340      * @return the calendar system type; non-null if the calendar has
 341      *    a standard type, otherwise null
 342      * @see #getId()
 343      */
 344     @Override
 345     public String getCalendarType() {
 346         return calendarType;
 347     }
 348 
 349     //-----------------------------------------------------------------------
 350     /**
 351      * Obtains a local date in Hijrah calendar system from the
 352      * era, year-of-era, month-of-year and day-of-month fields.
 353      *
 354      * @param era  the Hijrah era, not null
 355      * @param yearOfEra  the year-of-era
 356      * @param month  the month-of-year
 357      * @param dayOfMonth  the day-of-month
 358      * @return the Hijrah local date, not null
 359      * @throws DateTimeException if unable to create the date
 360      * @throws ClassCastException if the {@code era} is not a {@code HijrahEra}
 361      */
 362     @Override
 363     public HijrahDate date(Era era, int yearOfEra, int month, int dayOfMonth) {
 364         return date(prolepticYear(era, yearOfEra), month, dayOfMonth);
 365     }
 366 
 367     /**
 368      * Obtains a local date in Hijrah calendar system from the
 369      * proleptic-year, month-of-year and day-of-month fields.
 370      *
 371      * @param prolepticYear  the proleptic-year
 372      * @param month  the month-of-year
 373      * @param dayOfMonth  the day-of-month
 374      * @return the Hijrah local date, not null
 375      * @throws DateTimeException if unable to create the date
 376      */
 377     @Override
 378     public HijrahDate date(int prolepticYear, int month, int dayOfMonth) {
 379         return HijrahDate.of(this, prolepticYear, month, dayOfMonth);
 380     }
 381 
 382     /**
 383      * Obtains a local date in Hijrah calendar system from the
 384      * era, year-of-era and day-of-year fields.
 385      *
 386      * @param era  the Hijrah era, not null
 387      * @param yearOfEra  the year-of-era
 388      * @param dayOfYear  the day-of-year
 389      * @return the Hijrah local date, not null
 390      * @throws DateTimeException if unable to create the date
 391      * @throws ClassCastException if the {@code era} is not a {@code HijrahEra}
 392      */
 393     @Override
 394     public HijrahDate dateYearDay(Era era, int yearOfEra, int dayOfYear) {
 395         return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear);
 396     }
 397 
 398     /**
 399      * Obtains a local date in Hijrah calendar system from the
 400      * proleptic-year and day-of-year fields.
 401      *
 402      * @param prolepticYear  the proleptic-year
 403      * @param dayOfYear  the day-of-year
 404      * @return the Hijrah local date, not null
 405      * @throws DateTimeException if the value of the year is out of range,
 406      *  or if the day-of-year is invalid for the year
 407      */
 408     @Override
 409     public HijrahDate dateYearDay(int prolepticYear, int dayOfYear) {
 410         HijrahDate date = HijrahDate.of(this, prolepticYear, 1, 1);
 411         if (dayOfYear > date.lengthOfYear()) {
 412             throw new DateTimeException("Invalid dayOfYear: " + dayOfYear);
 413         }
 414         return date.plusDays(dayOfYear - 1);
 415     }
 416 
 417     /**
 418      * Obtains a local date in the Hijrah calendar system from the epoch-day.
 419      *
 420      * @param epochDay  the epoch day
 421      * @return the Hijrah local date, not null
 422      * @throws DateTimeException if unable to create the date
 423      */
 424     @Override  // override with covariant return type
 425     public HijrahDate dateEpochDay(long epochDay) {
 426         return HijrahDate.ofEpochDay(this, epochDay);
 427     }
 428 
 429     @Override
 430     public HijrahDate dateNow() {
 431         return dateNow(Clock.systemDefaultZone());
 432     }
 433 
 434     @Override
 435     public HijrahDate dateNow(ZoneId zone) {
 436         return dateNow(Clock.system(zone));
 437     }
 438 
 439     @Override
 440     public HijrahDate dateNow(Clock clock) {
 441         return date(LocalDate.now(clock));
 442     }
 443 
 444     @Override
 445     public HijrahDate date(TemporalAccessor temporal) {
 446         if (temporal instanceof HijrahDate) {
 447             return (HijrahDate) temporal;
 448         }
 449         return HijrahDate.ofEpochDay(this, temporal.getLong(EPOCH_DAY));
 450     }
 451 
 452     @Override
 453     @SuppressWarnings("unchecked")
 454     public ChronoLocalDateTime<HijrahDate> localDateTime(TemporalAccessor temporal) {
 455         return (ChronoLocalDateTime<HijrahDate>) super.localDateTime(temporal);
 456     }
 457 
 458     @Override
 459     @SuppressWarnings("unchecked")
 460     public ChronoZonedDateTime<HijrahDate> zonedDateTime(TemporalAccessor temporal) {
 461         return (ChronoZonedDateTime<HijrahDate>) super.zonedDateTime(temporal);
 462     }
 463 
 464     @Override
 465     @SuppressWarnings("unchecked")
 466     public ChronoZonedDateTime<HijrahDate> zonedDateTime(Instant instant, ZoneId zone) {
 467         return (ChronoZonedDateTime<HijrahDate>) super.zonedDateTime(instant, zone);
 468     }
 469 
 470     //-----------------------------------------------------------------------
 471     @Override
 472     public boolean isLeapYear(long prolepticYear) {
 473         checkCalendarInit();
 474         if (prolepticYear < getMinimumYear() || prolepticYear > getMaximumYear()) {
 475             return false;
 476         }
 477         int len = getYearLength((int) prolepticYear);
 478         return (len > 354);
 479     }
 480 
 481     @Override
 482     public int prolepticYear(Era era, int yearOfEra) {
 483         if (era instanceof HijrahEra == false) {
 484             throw new ClassCastException("Era must be HijrahEra");
 485         }
 486         return yearOfEra;
 487     }
 488 
 489     /**
 490      * Creates the HijrahEra object from the numeric value.
 491      * The Hijrah calendar system has only one era covering the
 492      * proleptic years greater than zero.
 493      * This method returns the singleton HijrahEra for the value 1.
 494      *
 495      * @param eraValue  the era value
 496      * @return the calendar system era, not null
 497      * @throws DateTimeException if unable to create the era
 498      */
 499     @Override
 500     public HijrahEra eraOf(int eraValue) {
 501         switch (eraValue) {
 502             case 1:
 503                 return HijrahEra.AH;
 504             default:
 505                 throw new DateTimeException("invalid Hijrah era");
 506         }
 507     }
 508 
 509     @Override
 510     public List<Era> eras() {
 511         return List.of(HijrahEra.values());
 512     }
 513 
 514     //-----------------------------------------------------------------------
 515     @Override
 516     public ValueRange range(ChronoField field) {
 517         checkCalendarInit();
 518         if (field instanceof ChronoField) {
 519             ChronoField f = field;
 520             switch (f) {
 521                 case DAY_OF_MONTH:
 522                     return ValueRange.of(1, 1, getMinimumMonthLength(), getMaximumMonthLength());
 523                 case DAY_OF_YEAR:
 524                     return ValueRange.of(1, getMaximumDayOfYear());
 525                 case ALIGNED_WEEK_OF_MONTH:
 526                     return ValueRange.of(1, 5);
 527                 case YEAR:
 528                 case YEAR_OF_ERA:
 529                     return ValueRange.of(getMinimumYear(), getMaximumYear());
 530                 case ERA:
 531                     return ValueRange.of(1, 1);
 532                 default:
 533                     return field.range();
 534             }
 535         }
 536         return field.range();
 537     }
 538 
 539     //-----------------------------------------------------------------------
 540     @Override  // override for return type
 541     public HijrahDate resolveDate(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
 542         return (HijrahDate) super.resolveDate(fieldValues, resolverStyle);
 543     }
 544 
 545     //-----------------------------------------------------------------------
 546     /**
 547      * Check the validity of a year.
 548      *
 549      * @param prolepticYear the year to check
 550      */
 551     int checkValidYear(long prolepticYear) {
 552         if (prolepticYear < getMinimumYear() || prolepticYear > getMaximumYear()) {
 553             throw new DateTimeException("Invalid Hijrah year: " + prolepticYear);
 554         }
 555         return (int) prolepticYear;
 556     }
 557 
 558     void checkValidDayOfYear(int dayOfYear) {
 559         if (dayOfYear < 1 || dayOfYear > getMaximumDayOfYear()) {
 560             throw new DateTimeException("Invalid Hijrah day of year: " + dayOfYear);
 561         }
 562     }
 563 
 564     void checkValidMonth(int month) {
 565         if (month < 1 || month > 12) {
 566             throw new DateTimeException("Invalid Hijrah month: " + month);
 567         }
 568     }
 569 
 570     //-----------------------------------------------------------------------
 571     /**
 572      * Returns an array containing the Hijrah year, month and day
 573      * computed from the epoch day.
 574      *
 575      * @param epochDay  the EpochDay
 576      * @return int[0] = YEAR, int[1] = MONTH, int[2] = DATE
 577      */
 578     int[] getHijrahDateInfo(int epochDay) {
 579         checkCalendarInit();    // ensure that the chronology is initialized
 580         if (epochDay < minEpochDay || epochDay >= maxEpochDay) {
 581             throw new DateTimeException("Hijrah date out of range");
 582         }
 583 
 584         int epochMonth = epochDayToEpochMonth(epochDay);
 585         int year = epochMonthToYear(epochMonth);
 586         int month = epochMonthToMonth(epochMonth);
 587         int day1 = epochMonthToEpochDay(epochMonth);
 588         int date = epochDay - day1; // epochDay - dayOfEpoch(year, month);
 589 
 590         int dateInfo[] = new int[3];
 591         dateInfo[0] = year;
 592         dateInfo[1] = month + 1; // change to 1-based.
 593         dateInfo[2] = date + 1; // change to 1-based.
 594         return dateInfo;
 595     }
 596 
 597     /**
 598      * Return the epoch day computed from Hijrah year, month, and day.
 599      *
 600      * @param prolepticYear the year to represent, 0-origin
 601      * @param monthOfYear the month-of-year to represent, 1-origin
 602      * @param dayOfMonth the day-of-month to represent, 1-origin
 603      * @return the epoch day
 604      */
 605     long getEpochDay(int prolepticYear, int monthOfYear, int dayOfMonth) {
 606         checkCalendarInit();    // ensure that the chronology is initialized
 607         checkValidMonth(monthOfYear);
 608         int epochMonth = yearToEpochMonth(prolepticYear) + (monthOfYear - 1);
 609         if (epochMonth < 0 || epochMonth >= hijrahEpochMonthStartDays.length) {
 610             throw new DateTimeException("Invalid Hijrah date, year: " +
 611                     prolepticYear +  ", month: " + monthOfYear);
 612         }
 613         if (dayOfMonth < 1 || dayOfMonth > getMonthLength(prolepticYear, monthOfYear)) {
 614             throw new DateTimeException("Invalid Hijrah day of month: " + dayOfMonth);
 615         }
 616         return epochMonthToEpochDay(epochMonth) + (dayOfMonth - 1);
 617     }
 618 
 619     /**
 620      * Returns day of year for the year and month.
 621      *
 622      * @param prolepticYear a proleptic year
 623      * @param month a month, 1-origin
 624      * @return the day of year, 1-origin
 625      */
 626     int getDayOfYear(int prolepticYear, int month) {
 627         return yearMonthToDayOfYear(prolepticYear, (month - 1));
 628     }
 629 
 630     /**
 631      * Returns month length for the year and month.
 632      *
 633      * @param prolepticYear a proleptic year
 634      * @param monthOfYear a month, 1-origin.
 635      * @return the length of the month
 636      */
 637     int getMonthLength(int prolepticYear, int monthOfYear) {
 638         int epochMonth = yearToEpochMonth(prolepticYear) + (monthOfYear - 1);
 639         if (epochMonth < 0 || epochMonth >= hijrahEpochMonthStartDays.length) {
 640             throw new DateTimeException("Invalid Hijrah date, year: " +
 641                     prolepticYear +  ", month: " + monthOfYear);
 642         }
 643         return epochMonthLength(epochMonth);
 644     }
 645 
 646     /**
 647      * Returns year length.
 648      * Note: The 12th month must exist in the data.
 649      *
 650      * @param prolepticYear a proleptic year
 651      * @return year length in days
 652      */
 653     int getYearLength(int prolepticYear) {
 654         return yearMonthToDayOfYear(prolepticYear, 12);
 655     }
 656 
 657     /**
 658      * Return the minimum supported Hijrah year.
 659      *
 660      * @return the minimum
 661      */
 662     int getMinimumYear() {
 663         return epochMonthToYear(0);
 664     }
 665 
 666     /**
 667      * Return the maximum supported Hijrah year.
 668      *
 669      * @return the minimum
 670      */
 671     int getMaximumYear() {
 672         return epochMonthToYear(hijrahEpochMonthStartDays.length - 1) - 1;
 673     }
 674 
 675     /**
 676      * Returns maximum day-of-month.
 677      *
 678      * @return maximum day-of-month
 679      */
 680     int getMaximumMonthLength() {
 681         return maxMonthLength;
 682     }
 683 
 684     /**
 685      * Returns smallest maximum day-of-month.
 686      *
 687      * @return smallest maximum day-of-month
 688      */
 689     int getMinimumMonthLength() {
 690         return minMonthLength;
 691     }
 692 
 693     /**
 694      * Returns maximum day-of-year.
 695      *
 696      * @return maximum day-of-year
 697      */
 698     int getMaximumDayOfYear() {
 699         return maxYearLength;
 700     }
 701 
 702     /**
 703      * Returns smallest maximum day-of-year.
 704      *
 705      * @return smallest maximum day-of-year
 706      */
 707     int getSmallestMaximumDayOfYear() {
 708         return minYearLength;
 709     }
 710 
 711     /**
 712      * Returns the epochMonth found by locating the epochDay in the table. The
 713      * epochMonth is the index in the table
 714      *
 715      * @param epochDay
 716      * @return The index of the element of the start of the month containing the
 717      * epochDay.
 718      */
 719     private int epochDayToEpochMonth(int epochDay) {
 720         // binary search
 721         int ndx = Arrays.binarySearch(hijrahEpochMonthStartDays, epochDay);
 722         if (ndx < 0) {
 723             ndx = -ndx - 2;
 724         }
 725         return ndx;
 726     }
 727 
 728     /**
 729      * Returns the year computed from the epochMonth
 730      *
 731      * @param epochMonth the epochMonth
 732      * @return the Hijrah Year
 733      */
 734     private int epochMonthToYear(int epochMonth) {
 735         return (epochMonth + hijrahStartEpochMonth) / 12;
 736     }
 737 
 738     /**
 739      * Returns the epochMonth for the Hijrah Year.
 740      *
 741      * @param year the HijrahYear
 742      * @return the epochMonth for the beginning of the year.
 743      */
 744     private int yearToEpochMonth(int year) {
 745         return (year * 12) - hijrahStartEpochMonth;
 746     }
 747 
 748     /**
 749      * Returns the Hijrah month from the epochMonth.
 750      *
 751      * @param epochMonth the epochMonth
 752      * @return the month of the Hijrah Year
 753      */
 754     private int epochMonthToMonth(int epochMonth) {
 755         return (epochMonth + hijrahStartEpochMonth) % 12;
 756     }
 757 
 758     /**
 759      * Returns the epochDay for the start of the epochMonth.
 760      *
 761      * @param epochMonth the epochMonth
 762      * @return the epochDay for the start of the epochMonth.
 763      */
 764     private int epochMonthToEpochDay(int epochMonth) {
 765         return hijrahEpochMonthStartDays[epochMonth];
 766 
 767     }
 768 
 769     /**
 770      * Returns the day of year for the requested HijrahYear and month.
 771      *
 772      * @param prolepticYear the Hijrah year
 773      * @param month the Hijrah month
 774      * @return the day of year for the start of the month of the year
 775      */
 776     private int yearMonthToDayOfYear(int prolepticYear, int month) {
 777         int epochMonthFirst = yearToEpochMonth(prolepticYear);
 778         return epochMonthToEpochDay(epochMonthFirst + month)
 779                 - epochMonthToEpochDay(epochMonthFirst);
 780     }
 781 
 782     /**
 783      * Returns the length of the epochMonth. It is computed from the start of
 784      * the following month minus the start of the requested month.
 785      *
 786      * @param epochMonth the epochMonth; assumed to be within range
 787      * @return the length in days of the epochMonth
 788      */
 789     private int epochMonthLength(int epochMonth) {
 790         // The very last entry in the epochMonth table is not the start of a month
 791         return hijrahEpochMonthStartDays[epochMonth + 1]
 792                 - hijrahEpochMonthStartDays[epochMonth];
 793     }
 794 
 795     //-----------------------------------------------------------------------
 796     private static final String KEY_ID = "id";
 797     private static final String KEY_TYPE = "type";
 798     private static final String KEY_VERSION = "version";
 799     private static final String KEY_ISO_START = "iso-start";
 800 
 801     /**
 802      * Return the configuration properties from the resource.
 803      * <p>
 804      * The location of the variant configuration resource is:
 805      * <pre>
 806      *   "/java/time/chrono/hijrah-config-" + calendarType + ".properties"
 807      * </pre>
 808      *
 809      * @param calendarType the calendarType of the calendar variant
 810      * @return a Properties containing the properties read from the resource.
 811      * @throws Exception if access to the property resource fails
 812      */
 813     private Properties readConfigProperties(final String calendarType) throws Exception {
 814         String resourceName = RESOURCE_PREFIX + calendarType + RESOURCE_SUFFIX;
 815         PrivilegedAction<InputStream> getResourceAction =  () -> HijrahChronology.class.getResourceAsStream(resourceName);
 816         FilePermission perm1 = new FilePermission("<<ALL FILES>>", "read");
 817         RuntimePermission perm2 = new RuntimePermission("accessSystemModules");
 818         try (InputStream is = AccessController.doPrivileged(getResourceAction, null, perm1, perm2)) {
 819             if (is == null) {
 820                 throw new RuntimeException("Hijrah calendar resource not found: /java/time/chrono/" + resourceName);
 821             }
 822             Properties props = new Properties();
 823             props.load(is);
 824             return props;
 825         }
 826     }
 827 
 828     /**
 829      * Loads and processes the Hijrah calendar properties file for this calendarType.
 830      * The starting Hijrah date and the corresponding ISO date are
 831      * extracted and used to calculate the epochDate offset.
 832      * The version number is identified and ignored.
 833      * Everything else is the data for a year with containing the length of each
 834      * of 12 months.
 835      *
 836      * @throws DateTimeException if initialization of the calendar data from the
 837      *     resource fails
 838      */
 839     private void loadCalendarData() {
 840         try {
 841             Properties props = readConfigProperties(calendarType);
 842 
 843             Map<Integer, int[]> years = new HashMap<>();
 844             int minYear = Integer.MAX_VALUE;
 845             int maxYear = Integer.MIN_VALUE;
 846             String id = null;
 847             String type = null;
 848             String version = null;
 849             int isoStart = 0;
 850             for (Map.Entry<Object, Object> entry : props.entrySet()) {
 851                 String key = (String) entry.getKey();
 852                 switch (key) {
 853                     case KEY_ID:
 854                         id = (String)entry.getValue();
 855                         break;
 856                     case KEY_TYPE:
 857                         type = (String)entry.getValue();
 858                         break;
 859                     case KEY_VERSION:
 860                         version = (String)entry.getValue();
 861                         break;
 862                     case KEY_ISO_START: {
 863                         int[] ymd = parseYMD((String) entry.getValue());
 864                         isoStart = (int) LocalDate.of(ymd[0], ymd[1], ymd[2]).toEpochDay();
 865                         break;
 866                     }
 867                     default:
 868                         try {
 869                             // Everything else is either a year or invalid
 870                             int year = Integer.parseInt(key);
 871                             int[] months = parseMonths((String) entry.getValue());
 872                             years.put(year, months);
 873                             maxYear = Math.max(maxYear, year);
 874                             minYear = Math.min(minYear, year);
 875                         } catch (NumberFormatException nfe) {
 876                             throw new IllegalArgumentException("bad key: " + key);
 877                         }
 878                 }
 879             }
 880 
 881             if (!getId().equals(id)) {
 882                 throw new IllegalArgumentException("Configuration is for a different calendar: " + id);
 883             }
 884             if (!getCalendarType().equals(type)) {
 885                 throw new IllegalArgumentException("Configuration is for a different calendar type: " + type);
 886             }
 887             if (version == null || version.isEmpty()) {
 888                 throw new IllegalArgumentException("Configuration does not contain a version");
 889             }
 890             if (isoStart == 0) {
 891                 throw new IllegalArgumentException("Configuration does not contain a ISO start date");
 892             }
 893 
 894             // Now create and validate the array of epochDays indexed by epochMonth
 895             hijrahStartEpochMonth = minYear * 12;
 896             minEpochDay = isoStart;
 897             hijrahEpochMonthStartDays = createEpochMonths(minEpochDay, minYear, maxYear, years);
 898             maxEpochDay = hijrahEpochMonthStartDays[hijrahEpochMonthStartDays.length - 1];
 899 
 900             // Compute the min and max year length in days.
 901             for (int year = minYear; year < maxYear; year++) {
 902                 int length = getYearLength(year);
 903                 minYearLength = Math.min(minYearLength, length);
 904                 maxYearLength = Math.max(maxYearLength, length);
 905             }
 906         } catch (Exception ex) {
 907             // Log error and throw a DateTimeException
 908             PlatformLogger logger = PlatformLogger.getLogger("java.time.chrono");
 909             logger.severe("Unable to initialize Hijrah calendar proxy: " + typeId, ex);
 910             throw new DateTimeException("Unable to initialize HijrahCalendar: " + typeId, ex);
 911         }
 912     }
 913 
 914     /**
 915      * Converts the map of year to month lengths ranging from minYear to maxYear
 916      * into a linear contiguous array of epochDays. The index is the hijrahMonth
 917      * computed from year and month and offset by minYear. The value of each
 918      * entry is the epochDay corresponding to the first day of the month.
 919      *
 920      * @param minYear The minimum year for which data is provided
 921      * @param maxYear The maximum year for which data is provided
 922      * @param years a Map of year to the array of 12 month lengths
 923      * @return array of epochDays for each month from min to max
 924      */
 925     private int[] createEpochMonths(int epochDay, int minYear, int maxYear, Map<Integer, int[]> years) {
 926         // Compute the size for the array of dates
 927         int numMonths = (maxYear - minYear + 1) * 12 + 1;
 928 
 929         // Initialize the running epochDay as the corresponding ISO Epoch day
 930         int epochMonth = 0; // index into array of epochMonths
 931         int[] epochMonths = new int[numMonths];
 932         minMonthLength = Integer.MAX_VALUE;
 933         maxMonthLength = Integer.MIN_VALUE;
 934 
 935         // Only whole years are valid, any zero's in the array are illegal
 936         for (int year = minYear; year <= maxYear; year++) {
 937             int[] months = years.get(year);// must not be gaps
 938             for (int month = 0; month < 12; month++) {
 939                 int length = months[month];
 940                 epochMonths[epochMonth++] = epochDay;
 941 
 942                 if (length < 29 || length > 32) {
 943                     throw new IllegalArgumentException("Invalid month length in year: " + minYear);
 944                 }
 945                 epochDay += length;
 946                 minMonthLength = Math.min(minMonthLength, length);
 947                 maxMonthLength = Math.max(maxMonthLength, length);
 948             }
 949         }
 950 
 951         // Insert the final epochDay
 952         epochMonths[epochMonth++] = epochDay;
 953 
 954         if (epochMonth != epochMonths.length) {
 955             throw new IllegalStateException("Did not fill epochMonths exactly: ndx = " + epochMonth
 956                     + " should be " + epochMonths.length);
 957         }
 958 
 959         return epochMonths;
 960     }
 961 
 962     /**
 963      * Parses the 12 months lengths from a property value for a specific year.
 964      *
 965      * @param line the value of a year property
 966      * @return an array of int[12] containing the 12 month lengths
 967      * @throws IllegalArgumentException if the number of months is not 12
 968      * @throws NumberFormatException if the 12 tokens are not numbers
 969      */
 970     private int[] parseMonths(String line) {
 971         int[] months = new int[12];
 972         String[] numbers = line.split("\\s");
 973         if (numbers.length != 12) {
 974             throw new IllegalArgumentException("wrong number of months on line: " + Arrays.toString(numbers) + "; count: " + numbers.length);
 975         }
 976         for (int i = 0; i < 12; i++) {
 977             try {
 978                 months[i] = Integer.parseInt(numbers[i]);
 979             } catch (NumberFormatException nfe) {
 980                 throw new IllegalArgumentException("bad key: " + numbers[i]);
 981             }
 982         }
 983         return months;
 984     }
 985 
 986     /**
 987      * Parse yyyy-MM-dd into a 3 element array [yyyy, mm, dd].
 988      *
 989      * @param string the input string
 990      * @return the 3 element array with year, month, day
 991      */
 992     private int[] parseYMD(String string) {
 993         // yyyy-MM-dd
 994         string = string.trim();
 995         try {
 996             if (string.charAt(4) != '-' || string.charAt(7) != '-') {
 997                 throw new IllegalArgumentException("date must be yyyy-MM-dd");
 998             }
 999             int[] ymd = new int[3];
1000             ymd[0] = Integer.parseInt(string, 0, 4, 10);
1001             ymd[1] = Integer.parseInt(string, 5, 7, 10);
1002             ymd[2] = Integer.parseInt(string, 8, 10, 10);
1003             return ymd;
1004         } catch (NumberFormatException ex) {
1005             throw new IllegalArgumentException("date must be yyyy-MM-dd", ex);
1006         }
1007     }
1008 
1009     //-----------------------------------------------------------------------
1010     /**
1011      * Writes the Chronology using a
1012      * <a href="../../../serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
1013      * @serialData
1014      * <pre>
1015      *  out.writeByte(1);     // identifies a Chronology
1016      *  out.writeUTF(getId());
1017      * </pre>
1018      *
1019      * @return the instance of {@code Ser}, not null
1020      */
1021     @Override
1022     Object writeReplace() {
1023         return super.writeReplace();
1024     }
1025 
1026     /**
1027      * Defend against malicious streams.
1028      *
1029      * @param s the stream to read
1030      * @throws InvalidObjectException always
1031      */
1032     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1033         throw new InvalidObjectException("Deserialization via serialization delegate");
1034     }
1035 }