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