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