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