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