jdk/src/share/classes/java/time/chrono/Chronology.java
author sherman
Tue, 12 Feb 2013 09:25:43 -0800
changeset 15658 55b829ca2334
child 16852 60207b2b4b42
permissions -rw-r--r--
8007392: JSR 310: DateTime API Updates 8007520: Update date/time classes in j.util and j.sql packages 8007572: Replace existing jdk timezone data at <java.home>/lib/zi with JSR310's tzdb Summary: Integration of JSR310 Date/Time API for M7 Reviewed-by: darcy, alanb, naoto Contributed-by: scolebourne@joda.org, roger.riggs@oracle.com, masayoshi.okutsu@oracle.com, patrick.zhang@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
 * This file is available under and governed by the GNU General Public
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    30
 * file:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    31
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    32
 * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    33
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    34
 * All rights reserved.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    35
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    36
 * Redistribution and use in source and binary forms, with or without
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    37
 * modification, are permitted provided that the following conditions are met:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    38
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    39
 *  * Redistributions of source code must retain the above copyright notice,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    40
 *    this list of conditions and the following disclaimer.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    41
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    42
 *  * Redistributions in binary form must reproduce the above copyright notice,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    43
 *    this list of conditions and the following disclaimer in the documentation
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    44
 *    and/or other materials provided with the distribution.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    45
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    46
 *  * Neither the name of JSR-310 nor the names of its contributors
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    47
 *    may be used to endorse or promote products derived from this software
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    48
 *    without specific prior written permission.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    49
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    50
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    51
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    52
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    53
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    54
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    55
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    56
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    57
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    58
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    59
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    60
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    61
 */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    62
package java.time.chrono;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    63
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    64
import java.io.DataInput;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    65
import java.io.DataOutput;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    66
import java.io.IOException;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    67
import java.io.InvalidObjectException;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    68
import java.io.ObjectStreamException;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    69
import java.time.Clock;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    70
import java.time.DateTimeException;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    71
import java.time.Instant;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    72
import java.time.LocalDate;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    73
import java.time.LocalTime;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    74
import java.time.ZoneId;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    75
import java.time.chrono.HijrahChronology;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    76
import java.time.chrono.JapaneseChronology;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    77
import java.time.chrono.MinguoChronology;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    78
import java.time.chrono.ThaiBuddhistChronology;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    79
import java.time.format.DateTimeFormatterBuilder;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    80
import java.time.format.TextStyle;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    81
import java.time.temporal.ChronoField;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    82
import java.time.temporal.Queries;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    83
import java.time.temporal.Temporal;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    84
import java.time.temporal.TemporalAccessor;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    85
import java.time.temporal.TemporalField;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    86
import java.time.temporal.TemporalQuery;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    87
import java.time.temporal.ValueRange;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    88
import java.util.HashSet;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    89
import java.util.List;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    90
import java.util.Locale;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    91
import java.util.Objects;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    92
import java.util.ServiceLoader;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    93
import java.util.Set;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    94
import java.util.concurrent.ConcurrentHashMap;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    95
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    96
/**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    97
 * A calendar system, used to organize and identify dates.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    98
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    99
 * The main date and time API is built on the ISO calendar system.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   100
 * This class operates behind the scenes to represent the general concept of a calendar system.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   101
 * For example, the Japanese, Minguo, Thai Buddhist and others.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   102
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   103
 * Most other calendar systems also operate on the shared concepts of year, month and day,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   104
 * linked to the cycles of the Earth around the Sun, and the Moon around the Earth.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   105
 * These shared concepts are defined by {@link ChronoField} and are available
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   106
 * for use by any {@code Chronology} implementation:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   107
 * <pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   108
 *   LocalDate isoDate = ...
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   109
 *   ChronoLocalDate&lt;ThaiBuddhistChronology&gt; thaiDate = ...
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   110
 *   int isoYear = isoDate.get(ChronoField.YEAR);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   111
 *   int thaiYear = thaiDate.get(ChronoField.YEAR);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   112
 * </pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   113
 * As shown, although the date objects are in different calendar systems, represented by different
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   114
 * {@code Chronology} instances, both can be queried using the same constant on {@code ChronoField}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   115
 * For a full discussion of the implications of this, see {@link ChronoLocalDate}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   116
 * In general, the advice is to use the known ISO-based {@code LocalDate}, rather than
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   117
 * {@code ChronoLocalDate}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   118
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   119
 * While a {@code Chronology} object typically uses {@code ChronoField} and is based on
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   120
 * an era, year-of-era, month-of-year, day-of-month model of a date, this is not required.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   121
 * A {@code Chronology} instance may represent a totally different kind of calendar system,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   122
 * such as the Mayan.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   123
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   124
 * In practical terms, the {@code Chronology} instance also acts as a factory.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   125
 * The {@link #of(String)} method allows an instance to be looked up by identifier,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   126
 * while the {@link #ofLocale(Locale)} method allows lookup by locale.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   127
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   128
 * The {@code Chronology} instance provides a set of methods to create {@code ChronoLocalDate} instances.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   129
 * The date classes are used to manipulate specific dates.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   130
 * <p><ul>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   131
 * <li> {@link #dateNow() dateNow()}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   132
 * <li> {@link #dateNow(Clock) dateNow(clock)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   133
 * <li> {@link #dateNow(ZoneId) dateNow(zone)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   134
 * <li> {@link #date(int, int, int) date(yearProleptic, month, day)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   135
 * <li> {@link #date(Era, int, int, int) date(era, yearOfEra, month, day)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   136
 * <li> {@link #dateYearDay(int, int) dateYearDay(yearProleptic, dayOfYear)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   137
 * <li> {@link #dateYearDay(Era, int, int) dateYearDay(era, yearOfEra, dayOfYear)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   138
 * <li> {@link #date(TemporalAccessor) date(TemporalAccessor)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   139
 * </ul><p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   140
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   141
 * <h3 id="addcalendars">Adding New Calendars</h3>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   142
 * The set of available chronologies can be extended by applications.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   143
 * Adding a new calendar system requires the writing of an implementation of
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   144
 * {@code Chronology}, {@code ChronoLocalDate} and {@code Era}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   145
 * The majority of the logic specific to the calendar system will be in
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   146
 * {@code ChronoLocalDate}. The {@code Chronology} subclass acts as a factory.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   147
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   148
 * To permit the discovery of additional chronologies, the {@link java.util.ServiceLoader ServiceLoader}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   149
 * is used. A file must be added to the {@code META-INF/services} directory with the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   150
 * name 'java.time.chrono.Chronology' listing the implementation classes.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   151
 * See the ServiceLoader for more details on service loading.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   152
 * For lookup by id or calendarType, the system provided calendars are found
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   153
 * first followed by application provided calendars.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   154
 * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   155
 * Each chronology must define a chronology ID that is unique within the system.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   156
 * If the chronology represents a calendar system defined by the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   157
 * <em>Unicode Locale Data Markup Language (LDML)</em> specification then that
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   158
 * calendar type should also be specified.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   159
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   160
 * <h3>Specification for implementors</h3>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   161
 * This class must be implemented with care to ensure other classes operate correctly.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   162
 * All implementations that can be instantiated must be final, immutable and thread-safe.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   163
 * Subclasses should be Serializable wherever possible.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   164
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   165
 * @since 1.8
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   166
 */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   167
public abstract class Chronology implements Comparable<Chronology> {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   168
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   169
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   170
     * Map of available calendars by ID.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   171
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   172
    private static final ConcurrentHashMap<String, Chronology> CHRONOS_BY_ID = new ConcurrentHashMap<>();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   173
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   174
     * Map of available calendars by calendar type.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   175
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   176
    private static final ConcurrentHashMap<String, Chronology> CHRONOS_BY_TYPE = new ConcurrentHashMap<>();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   177
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   178
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   179
     * Register a Chronology by ID and type for lookup by {@link #of(java.lang.String)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   180
     * Chronos must not be registered until they are completely constructed.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   181
     * Specifically, not in the constructor of Chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   182
     * @param chrono the chronology to register; not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   183
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   184
    private static void registerChrono(Chronology chrono) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   185
        Chronology prev = CHRONOS_BY_ID.putIfAbsent(chrono.getId(), chrono);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   186
        if (prev == null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   187
            String type = chrono.getCalendarType();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   188
            if (type != null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   189
                CHRONOS_BY_TYPE.putIfAbsent(type, chrono);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   190
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   191
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   192
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   193
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   194
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   195
     * Initialization of the maps from id and type to Chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   196
     * The ServiceLoader is used to find and register any implementations
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   197
     * of {@link java.time.chrono.Chronology} found in the bootclass loader.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   198
     * The built-in chronologies are registered explicitly.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   199
     * Calendars configured via the Thread's context classloader are local
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   200
     * to that thread and are ignored.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   201
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   202
     * The initialization is done only once using the registration
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   203
     * of the IsoChronology as the test and the final step.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   204
     * Multiple threads may perform the initialization concurrently.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   205
     * Only the first registration of each Chronology is retained by the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   206
     * ConcurrentHashMap.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   207
     * @return true if the cache was initialized
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   208
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   209
    private static boolean initCache() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   210
        if (CHRONOS_BY_ID.get("ISO") == null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   211
            // Initialization is incomplete
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   212
            @SuppressWarnings("rawtypes")
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   213
            ServiceLoader<Chronology> loader =  ServiceLoader.load(Chronology.class, null);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   214
            for (Chronology chrono : loader) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   215
                registerChrono(chrono);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   216
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   217
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   218
            // Register these calendars; the ServiceLoader configuration is not used
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   219
            registerChrono(HijrahChronology.INSTANCE);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   220
            registerChrono(JapaneseChronology.INSTANCE);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   221
            registerChrono(MinguoChronology.INSTANCE);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   222
            registerChrono(ThaiBuddhistChronology.INSTANCE);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   223
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   224
            // finally, register IsoChronology to mark initialization is complete
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   225
            registerChrono(IsoChronology.INSTANCE);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   226
            return true;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   227
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   228
        return false;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   229
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   230
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   231
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   232
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   233
     * Obtains an instance of {@code Chronology} from a temporal object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   234
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   235
     * This obtains a chronology based on the specified temporal.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   236
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   237
     * which this factory converts to an instance of {@code Chronology}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   238
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   239
     * The conversion will obtain the chronology using {@link Queries#chronology()}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   240
     * If the specified temporal object does not have a chronology, {@link IsoChronology} is returned.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   241
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   242
     * This method matches the signature of the functional interface {@link TemporalQuery}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   243
     * allowing it to be used in queries via method reference, {@code Chronology::from}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   244
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   245
     * @param temporal  the temporal to convert, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   246
     * @return the chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   247
     * @throws DateTimeException if unable to convert to an {@code Chronology}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   248
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   249
    public static Chronology from(TemporalAccessor temporal) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   250
        Objects.requireNonNull(temporal, "temporal");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   251
        Chronology obj = temporal.query(Queries.chronology());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   252
        return (obj != null ? obj : IsoChronology.INSTANCE);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   253
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   254
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   255
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   256
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   257
     * Obtains an instance of {@code Chronology} from a locale.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   258
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   259
     * This returns a {@code Chronology} based on the specified locale,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   260
     * typically returning {@code IsoChronology}. Other calendar systems
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   261
     * are only returned if they are explicitly selected within the locale.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   262
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   263
     * The {@link Locale} class provide access to a range of information useful
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   264
     * for localizing an application. This includes the language and region,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   265
     * such as "en-GB" for English as used in Great Britain.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   266
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   267
     * The {@code Locale} class also supports an extension mechanism that
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   268
     * can be used to identify a calendar system. The mechanism is a form
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   269
     * of key-value pairs, where the calendar system has the key "ca".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   270
     * For example, the locale "en-JP-u-ca-japanese" represents the English
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   271
     * language as used in Japan with the Japanese calendar system.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   272
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   273
     * This method finds the desired calendar system by in a manner equivalent
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   274
     * to passing "ca" to {@link Locale#getUnicodeLocaleType(String)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   275
     * If the "ca" key is not present, then {@code IsoChronology} is returned.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   276
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   277
     * Note that the behavior of this method differs from the older
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   278
     * {@link java.util.Calendar#getInstance(Locale)} method.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   279
     * If that method receives a locale of "th_TH" it will return {@code BuddhistCalendar}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   280
     * By contrast, this method will return {@code IsoChronology}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   281
     * Passing the locale "th-TH-u-ca-buddhist" into either method will
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   282
     * result in the Thai Buddhist calendar system and is therefore the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   283
     * recommended approach going forward for Thai calendar system localization.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   284
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   285
     * A similar, but simpler, situation occurs for the Japanese calendar system.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   286
     * The locale "jp_JP_JP" has previously been used to access the calendar.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   287
     * However, unlike the Thai locale, "ja_JP_JP" is automatically converted by
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   288
     * {@code Locale} to the modern and recommended form of "ja-JP-u-ca-japanese".
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   289
     * Thus, there is no difference in behavior between this method and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   290
     * {@code Calendar#getInstance(Locale)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   291
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   292
     * @param locale  the locale to use to obtain the calendar system, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   293
     * @return the calendar system associated with the locale, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   294
     * @throws DateTimeException if the locale-specified calendar cannot be found
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   295
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   296
    public static Chronology ofLocale(Locale locale) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   297
        Objects.requireNonNull(locale, "locale");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   298
        String type = locale.getUnicodeLocaleType("ca");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   299
        if (type == null || "iso".equals(type) || "iso8601".equals(type)) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   300
            return IsoChronology.INSTANCE;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   301
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   302
        // Not pre-defined; lookup by the type
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   303
        do {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   304
            Chronology chrono = CHRONOS_BY_TYPE.get(type);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   305
            if (chrono != null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   306
                return chrono;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   307
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   308
            // If not found, do the initialization (once) and repeat the lookup
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   309
        } while (initCache());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   310
        throw new DateTimeException("Unknown calendar system: " + type);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   311
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   312
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   313
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   314
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   315
     * Obtains an instance of {@code Chronology} from a chronology ID or
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   316
     * calendar system type.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   317
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   318
     * This returns a chronology based on either the ID or the type.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   319
     * The {@link #getId() chronology ID} uniquely identifies the chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   320
     * The {@link #getCalendarType() calendar system type} is defined by the LDML specification.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   321
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   322
     * The chronology may be a system chronology or a chronology
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   323
     * provided by the application via ServiceLoader configuration.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   324
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   325
     * Since some calendars can be customized, the ID or type typically refers
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   326
     * to the default customization. For example, the Gregorian calendar can have multiple
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   327
     * cutover dates from the Julian, but the lookup only provides the default cutover date.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   328
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   329
     * @param id  the chronology ID or calendar system type, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   330
     * @return the chronology with the identifier requested, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   331
     * @throws DateTimeException if the chronology cannot be found
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   332
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   333
    public static Chronology of(String id) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   334
        Objects.requireNonNull(id, "id");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   335
        do {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   336
            Chronology chrono = of0(id);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   337
            if (chrono != null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   338
                return chrono;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   339
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   340
            // If not found, do the initialization (once) and repeat the lookup
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   341
        } while (initCache());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   342
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   343
        // Look for a Chronology using ServiceLoader of the Thread's ContextClassLoader
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   344
        // Application provided Chronologies must not be cached
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   345
        @SuppressWarnings("rawtypes")
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   346
        ServiceLoader<Chronology> loader = ServiceLoader.load(Chronology.class);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   347
        for (Chronology chrono : loader) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   348
            if (id.equals(chrono.getId()) || id.equals(chrono.getCalendarType())) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   349
                return chrono;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   350
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   351
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   352
        throw new DateTimeException("Unknown chronology: " + id);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   353
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   354
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   355
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   356
     * Obtains an instance of {@code Chronology} from a chronology ID or
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   357
     * calendar system type.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   358
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   359
     * @param id  the chronology ID or calendar system type, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   360
     * @return the chronology with the identifier requested, or {@code null} if not found
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   361
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   362
    private static Chronology of0(String id) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   363
        Chronology chrono = CHRONOS_BY_ID.get(id);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   364
        if (chrono == null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   365
            chrono = CHRONOS_BY_TYPE.get(id);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   366
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   367
        return chrono;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   368
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   369
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   370
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   371
     * Returns the available chronologies.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   372
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   373
     * Each returned {@code Chronology} is available for use in the system.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   374
     * The set of chronologies includes the system chronologies and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   375
     * any chronologies provided by the application via ServiceLoader
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   376
     * configuration.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   377
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   378
     * @return the independent, modifiable set of the available chronology IDs, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   379
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   380
    public static Set<Chronology> getAvailableChronologies() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   381
        initCache();       // force initialization
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   382
        HashSet<Chronology> chronos = new HashSet(CHRONOS_BY_ID.values());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   383
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   384
        /// Add in Chronologies from the ServiceLoader configuration
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   385
        @SuppressWarnings("rawtypes")
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   386
        ServiceLoader<Chronology> loader = ServiceLoader.load(Chronology.class);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   387
        for (Chronology chrono : loader) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   388
            chronos.add(chrono);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   389
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   390
        return chronos;
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
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   394
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   395
     * Creates an instance.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   396
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   397
    protected Chronology() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   398
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   399
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   400
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   401
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   402
     * Casts the {@code Temporal} to {@code ChronoLocalDate} with the same chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   403
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   404
     * @param temporal  a date-time to cast, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   405
     * @return the date-time checked and cast to {@code ChronoLocalDate}, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   406
     * @throws ClassCastException if the date-time cannot be cast to ChronoLocalDate
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   407
     *  or the chronology is not equal this Chronology
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   408
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   409
    ChronoLocalDate ensureChronoLocalDate(Temporal temporal) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   410
        @SuppressWarnings("unchecked")
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   411
        ChronoLocalDate other = (ChronoLocalDate) temporal;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   412
        if (this.equals(other.getChronology()) == false) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   413
            throw new ClassCastException("Chronology mismatch, expected: " + getId() + ", actual: " + other.getChronology().getId());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   414
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   415
        return other;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   416
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   417
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   418
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   419
     * Casts the {@code Temporal} to {@code ChronoLocalDateTime} with the same chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   420
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   421
     * @param temporal   a date-time to cast, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   422
     * @return the date-time checked and cast to {@code ChronoLocalDateTime}, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   423
     * @throws ClassCastException if the date-time cannot be cast to ChronoLocalDateTimeImpl
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   424
     *  or the chronology is not equal this Chronology
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   425
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   426
    ChronoLocalDateTimeImpl<?> ensureChronoLocalDateTime(Temporal temporal) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   427
        @SuppressWarnings("unchecked")
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   428
        ChronoLocalDateTimeImpl<?> other = (ChronoLocalDateTimeImpl<?>) temporal;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   429
        if (this.equals(other.toLocalDate().getChronology()) == false) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   430
            throw new ClassCastException("Chronology mismatch, required: " + getId()
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   431
                    + ", supplied: " + other.toLocalDate().getChronology().getId());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   432
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   433
        return other;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   434
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   435
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   436
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   437
     * Casts the {@code Temporal} to {@code ChronoZonedDateTimeImpl} with the same chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   438
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   439
     * @param temporal  a date-time to cast, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   440
     * @return the date-time checked and cast to {@code ChronoZonedDateTimeImpl}, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   441
     * @throws ClassCastException if the date-time cannot be cast to ChronoZonedDateTimeImpl
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   442
     *  or the chronology is not equal this Chronology
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   443
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   444
    ChronoZonedDateTimeImpl<?> ensureChronoZonedDateTime(Temporal temporal) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   445
        @SuppressWarnings("unchecked")
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   446
        ChronoZonedDateTimeImpl<?> other = (ChronoZonedDateTimeImpl<?>) temporal;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   447
        if (this.equals(other.toLocalDate().getChronology()) == false) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   448
            throw new ClassCastException("Chronology mismatch, required: " + getId()
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   449
                    + ", supplied: " + other.toLocalDate().getChronology().getId());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   450
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   451
        return other;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   452
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   453
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   454
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   455
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   456
     * Gets the ID of the chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   457
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   458
     * The ID uniquely identifies the {@code Chronology}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   459
     * It can be used to lookup the {@code Chronology} using {@link #of(String)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   460
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   461
     * @return the chronology ID, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   462
     * @see #getCalendarType()
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   463
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   464
    public abstract String getId();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   465
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   466
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   467
     * Gets the calendar type of the underlying calendar system.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   468
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   469
     * The calendar type is an identifier defined by the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   470
     * <em>Unicode Locale Data Markup Language (LDML)</em> specification.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   471
     * It can be used to lookup the {@code Chronology} using {@link #of(String)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   472
     * It can also be used as part of a locale, accessible via
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   473
     * {@link Locale#getUnicodeLocaleType(String)} with the key 'ca'.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   474
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   475
     * @return the calendar system type, null if the calendar is not defined by LDML
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   476
     * @see #getId()
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   477
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   478
    public abstract String getCalendarType();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   479
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   480
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   481
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   482
     * Obtains a local date in this chronology from the era, year-of-era,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   483
     * month-of-year and day-of-month fields.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   484
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   485
     * @param era  the era of the correct type for the chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   486
     * @param yearOfEra  the chronology year-of-era
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   487
     * @param month  the chronology month-of-year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   488
     * @param dayOfMonth  the chronology day-of-month
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   489
     * @return the local date in this chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   490
     * @throws DateTimeException if unable to create the date
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   491
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   492
    public ChronoLocalDate date(Era era, int yearOfEra, int month, int dayOfMonth) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   493
        return date(prolepticYear(era, yearOfEra), month, dayOfMonth);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   494
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   495
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   496
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   497
     * Obtains a local date in this chronology from the proleptic-year,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   498
     * month-of-year and day-of-month fields.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   499
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   500
     * @param prolepticYear  the chronology proleptic-year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   501
     * @param month  the chronology month-of-year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   502
     * @param dayOfMonth  the chronology day-of-month
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   503
     * @return the local date in this chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   504
     * @throws DateTimeException if unable to create the date
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   505
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   506
    public abstract ChronoLocalDate date(int prolepticYear, int month, int dayOfMonth);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   507
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   508
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   509
     * Obtains a local date in this chronology from the era, year-of-era and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   510
     * day-of-year fields.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   511
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   512
     * @param era  the era of the correct type for the chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   513
     * @param yearOfEra  the chronology year-of-era
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   514
     * @param dayOfYear  the chronology day-of-year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   515
     * @return the local date in this chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   516
     * @throws DateTimeException if unable to create the date
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   517
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   518
    public ChronoLocalDate dateYearDay(Era era, int yearOfEra, int dayOfYear) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   519
        return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   520
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   521
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   522
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   523
     * Obtains a local date in this chronology from the proleptic-year and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   524
     * day-of-year fields.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   525
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   526
     * @param prolepticYear  the chronology proleptic-year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   527
     * @param dayOfYear  the chronology day-of-year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   528
     * @return the local date in this chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   529
     * @throws DateTimeException if unable to create the date
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   530
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   531
    public abstract ChronoLocalDate dateYearDay(int prolepticYear, int dayOfYear);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   532
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   533
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   534
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   535
     * Obtains the current local date in this chronology from the system clock in the default time-zone.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   536
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   537
     * This will query the {@link Clock#systemDefaultZone() system clock} in the default
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   538
     * time-zone to obtain the current date.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   539
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   540
     * Using this method will prevent the ability to use an alternate clock for testing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   541
     * because the clock is hard-coded.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   542
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   543
     * This implementation uses {@link #dateNow(Clock)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   544
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   545
     * @return the current local date using the system clock and default time-zone, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   546
     * @throws DateTimeException if unable to create the date
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   547
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   548
    public ChronoLocalDate dateNow() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   549
        return dateNow(Clock.systemDefaultZone());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   550
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   551
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   552
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   553
     * Obtains the current local date in this chronology from the system clock in the specified time-zone.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   554
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   555
     * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   556
     * Specifying the time-zone avoids dependence on the default time-zone.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   557
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   558
     * Using this method will prevent the ability to use an alternate clock for testing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   559
     * because the clock is hard-coded.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   560
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   561
     * @param zone  the zone ID to use, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   562
     * @return the current local date using the system clock, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   563
     * @throws DateTimeException if unable to create the date
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   564
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   565
    public ChronoLocalDate dateNow(ZoneId zone) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   566
        return dateNow(Clock.system(zone));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   567
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   568
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   569
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   570
     * Obtains the current local date in this chronology from the specified clock.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   571
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   572
     * This will query the specified clock to obtain the current date - today.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   573
     * Using this method allows the use of an alternate clock for testing.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   574
     * The alternate clock may be introduced using {@link Clock dependency injection}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   575
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   576
     * @param clock  the clock to use, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   577
     * @return the current local date, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   578
     * @throws DateTimeException if unable to create the date
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   579
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   580
    public ChronoLocalDate dateNow(Clock clock) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   581
        Objects.requireNonNull(clock, "clock");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   582
        return date(LocalDate.now(clock));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   583
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   584
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   585
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   586
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   587
     * Obtains a local date in this chronology from another temporal object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   588
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   589
     * This creates a date in this chronology based on the specified temporal.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   590
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   591
     * which this factory converts to an instance of {@code ChronoLocalDate}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   592
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   593
     * The conversion typically uses the {@link ChronoField#EPOCH_DAY EPOCH_DAY}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   594
     * field, which is standardized across calendar systems.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   595
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   596
     * This method matches the signature of the functional interface {@link TemporalQuery}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   597
     * allowing it to be used as a query via method reference, {@code aChronology::date}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   598
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   599
     * @param temporal  the temporal object to convert, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   600
     * @return the local date in this chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   601
     * @throws DateTimeException if unable to create the date
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   602
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   603
    public abstract ChronoLocalDate date(TemporalAccessor temporal);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   604
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   605
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   606
     * Obtains a local date-time in this chronology from another temporal object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   607
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   608
     * This creates a date-time in this chronology based on the specified temporal.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   609
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   610
     * which this factory converts to an instance of {@code ChronoLocalDateTime}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   611
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   612
     * The conversion extracts and combines the {@code ChronoLocalDate} and the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   613
     * {@code LocalTime} from the temporal object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   614
     * Implementations are permitted to perform optimizations such as accessing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   615
     * those fields that are equivalent to the relevant objects.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   616
     * The result uses this chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   617
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   618
     * This method matches the signature of the functional interface {@link TemporalQuery}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   619
     * allowing it to be used as a query via method reference, {@code aChronology::localDateTime}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   620
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   621
     * @param temporal  the temporal object to convert, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   622
     * @return the local date-time in this chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   623
     * @throws DateTimeException if unable to create the date-time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   624
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   625
    public ChronoLocalDateTime<?> localDateTime(TemporalAccessor temporal) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   626
        try {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   627
            return date(temporal).atTime(LocalTime.from(temporal));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   628
        } catch (DateTimeException ex) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   629
            throw new DateTimeException("Unable to obtain ChronoLocalDateTime from TemporalAccessor: " + temporal.getClass(), ex);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   630
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   631
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   632
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   633
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   634
     * Obtains a {@code ChronoZonedDateTime} in this chronology from another temporal object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   635
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   636
     * This creates a zoned date-time in this chronology based on the specified temporal.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   637
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   638
     * which this factory converts to an instance of {@code ChronoZonedDateTime}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   639
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   640
     * The conversion will first obtain a {@code ZoneId} from the temporal object,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   641
     * falling back to a {@code ZoneOffset} if necessary. It will then try to obtain
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   642
     * an {@code Instant}, falling back to a {@code ChronoLocalDateTime} if necessary.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   643
     * The result will be either the combination of {@code ZoneId} or {@code ZoneOffset}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   644
     * with {@code Instant} or {@code ChronoLocalDateTime}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   645
     * Implementations are permitted to perform optimizations such as accessing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   646
     * those fields that are equivalent to the relevant objects.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   647
     * The result uses this chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   648
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   649
     * This method matches the signature of the functional interface {@link TemporalQuery}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   650
     * allowing it to be used as a query via method reference, {@code aChronology::zonedDateTime}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   651
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   652
     * @param temporal  the temporal object to convert, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   653
     * @return the zoned date-time in this chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   654
     * @throws DateTimeException if unable to create the date-time
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   655
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   656
    public ChronoZonedDateTime<?> zonedDateTime(TemporalAccessor temporal) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   657
        try {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   658
            ZoneId zone = ZoneId.from(temporal);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   659
            try {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   660
                Instant instant = Instant.from(temporal);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   661
                return zonedDateTime(instant, zone);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   662
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   663
            } catch (DateTimeException ex1) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   664
                ChronoLocalDateTimeImpl cldt = ensureChronoLocalDateTime(localDateTime(temporal));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   665
                return ChronoZonedDateTimeImpl.ofBest(cldt, zone, null);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   666
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   667
        } catch (DateTimeException ex) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   668
            throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " + temporal.getClass(), ex);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   669
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   670
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   671
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   672
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   673
     * Obtains a {@code ChronoZonedDateTime} in this chronology from an {@code Instant}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   674
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   675
     * This creates a zoned date-time with the same instant as that specified.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   676
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   677
     * @param instant  the instant to create the date-time from, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   678
     * @param zone  the time-zone, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   679
     * @return the zoned date-time, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   680
     * @throws DateTimeException if the result exceeds the supported range
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   681
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   682
    public ChronoZonedDateTime<?> zonedDateTime(Instant instant, ZoneId zone) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   683
        return ChronoZonedDateTimeImpl.ofInstant(this, instant, zone);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   684
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   685
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   686
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   687
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   688
     * Checks if the specified year is a leap year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   689
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   690
     * A leap-year is a year of a longer length than normal.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   691
     * The exact meaning is determined by the chronology according to the following constraints.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   692
     * <p><ul>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   693
     * <li>a leap-year must imply a year-length longer than a non leap-year.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   694
     * <li>a chronology that does not support the concept of a year must return false.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   695
     * </ul><p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   696
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   697
     * @param prolepticYear  the proleptic-year to check, not validated for range
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   698
     * @return true if the year is a leap year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   699
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   700
    public abstract boolean isLeapYear(long prolepticYear);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   701
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   702
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   703
     * Calculates the proleptic-year given the era and year-of-era.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   704
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   705
     * This combines the era and year-of-era into the single proleptic-year field.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   706
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   707
     * @param era  the era of the correct type for the chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   708
     * @param yearOfEra  the chronology year-of-era
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   709
     * @return the proleptic-year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   710
     * @throws DateTimeException if unable to convert
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   711
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   712
    public abstract int prolepticYear(Era era, int yearOfEra);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   713
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   714
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   715
     * Creates the chronology era object from the numeric value.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   716
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   717
     * The era is, conceptually, the largest division of the time-line.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   718
     * Most calendar systems have a single epoch dividing the time-line into two eras.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   719
     * However, some have multiple eras, such as one for the reign of each leader.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   720
     * The exact meaning is determined by the chronology according to the following constraints.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   721
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   722
     * The era in use at 1970-01-01 must have the value 1.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   723
     * Later eras must have sequentially higher values.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   724
     * Earlier eras must have sequentially lower values.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   725
     * Each chronology must refer to an enum or similar singleton to provide the era values.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   726
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   727
     * This method returns the singleton era of the correct type for the specified era value.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   728
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   729
     * @param eraValue  the era value
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   730
     * @return the calendar system era, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   731
     * @throws DateTimeException if unable to create the era
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   732
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   733
    public abstract Era eraOf(int eraValue);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   734
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   735
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   736
     * Gets the list of eras for the chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   737
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   738
     * Most calendar systems have an era, within which the year has meaning.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   739
     * If the calendar system does not support the concept of eras, an empty
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   740
     * list must be returned.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   741
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   742
     * @return the list of eras for the chronology, may be immutable, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   743
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   744
    public abstract List<Era> eras();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   745
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   746
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   747
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   748
     * Gets the range of valid values for the specified field.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   749
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   750
     * All fields can be expressed as a {@code long} integer.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   751
     * This method returns an object that describes the valid range for that value.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   752
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   753
     * Note that the result only describes the minimum and maximum valid values
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   754
     * and it is important not to read too much into them. For example, there
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   755
     * could be values within the range that are invalid for the field.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   756
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   757
     * This method will return a result whether or not the chronology supports the field.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   758
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   759
     * @param field  the field to get the range for, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   760
     * @return the range of valid values for the field, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   761
     * @throws DateTimeException if the range for the field cannot be obtained
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   762
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   763
    public abstract ValueRange range(ChronoField field);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   764
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
     * Gets the textual representation of this chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   768
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   769
     * This returns the textual name used to identify the chronology,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   770
     * suitable for presentation to the user.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   771
     * The parameters control the style of the returned text and the locale.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   772
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   773
     * @param style  the style of the text required, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   774
     * @param locale  the locale to use, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   775
     * @return the text value of the chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   776
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   777
    public String getDisplayName(TextStyle style, Locale locale) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   778
        return new DateTimeFormatterBuilder().appendChronologyText(style).toFormatter(locale).format(new TemporalAccessor() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   779
            @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   780
            public boolean isSupported(TemporalField field) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   781
                return false;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   782
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   783
            @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   784
            public long getLong(TemporalField field) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   785
                throw new DateTimeException("Unsupported field: " + field);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   786
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   787
            @SuppressWarnings("unchecked")
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   788
            @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   789
            public <R> R query(TemporalQuery<R> query) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   790
                if (query == Queries.chronology()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   791
                    return (R) Chronology.this;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   792
                }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   793
                return TemporalAccessor.super.query(query);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   794
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   795
        });
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   796
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   797
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   798
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   799
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   800
     * Compares this chronology to another chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   801
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   802
     * The comparison order first by the chronology ID string, then by any
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   803
     * additional information specific to the subclass.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   804
     * It is "consistent with equals", as defined by {@link Comparable}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   805
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   806
     * The default implementation compares the chronology ID.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   807
     * Subclasses must compare any additional state that they store.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   808
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   809
     * @param other  the other chronology to compare to, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   810
     * @return the comparator value, negative if less, positive if greater
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   811
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   812
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   813
    public int compareTo(Chronology other) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   814
        return getId().compareTo(other.getId());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   815
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   816
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   817
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   818
     * Checks if this chronology is equal to another chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   819
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   820
     * The comparison is based on the entire state of the object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   821
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   822
     * The default implementation checks the type and calls {@link #compareTo(Chronology)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   823
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   824
     * @param obj  the object to check, null returns false
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   825
     * @return true if this is equal to the other chronology
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   826
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   827
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   828
    public boolean equals(Object obj) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   829
        if (this == obj) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   830
           return true;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   831
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   832
        if (obj instanceof Chronology) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   833
            return compareTo((Chronology) obj) == 0;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   834
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   835
        return false;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   836
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   837
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   838
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   839
     * A hash code for this chronology.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   840
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   841
     * The default implementation is based on the ID and class.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   842
     * Subclasses should add any additional state that they store.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   843
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   844
     * @return a suitable hash code
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   845
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   846
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   847
    public int hashCode() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   848
        return getClass().hashCode() ^ getId().hashCode();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   849
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   850
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   851
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   852
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   853
     * Outputs this chronology as a {@code String}, using the ID.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   854
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   855
     * @return a string representation of this chronology, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   856
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   857
    @Override
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   858
    public String toString() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   859
        return getId();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   860
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   861
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   862
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   863
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   864
     * Writes the object using a
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   865
     * <a href="../../../serialized-form.html#java.time.temporal.Ser">dedicated serialized form</a>.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   866
     * <pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   867
     *  out.writeByte(7);  // identifies this as a Chronology
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   868
     * out.writeUTF(chronoId);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   869
     * </pre>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   870
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   871
     * @return the instance of {@code Ser}, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   872
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   873
    private Object writeReplace() {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   874
        return new Ser(Ser.CHRONO_TYPE, this);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   875
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   876
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   877
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   878
     * Defend against malicious streams.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   879
     * @return never
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   880
     * @throws InvalidObjectException always
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   881
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   882
    private Object readResolve() throws ObjectStreamException {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   883
        throw new InvalidObjectException("Deserialization via serialization delegate");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   884
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   885
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   886
    void writeExternal(DataOutput out) throws IOException {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   887
        out.writeUTF(getId());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   888
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   889
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   890
    static Chronology readExternal(DataInput in) throws IOException {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   891
        String id = in.readUTF();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   892
        return Chronology.of(id);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   893
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   894
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   895
}