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