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