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