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