jdk/src/share/classes/java/time/chrono/AbstractChronology.java
changeset 20794 ec823009c5f7
child 20795 8ec9e5b79828
equal deleted inserted replaced
20793:5acd4b8d70a1 20794:ec823009c5f7
       
     1 /*
       
     2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 /*
       
    27  * This file is available under and governed by the GNU General Public
       
    28  * License version 2 only, as published by the Free Software Foundation.
       
    29  * However, the following notice accompanied the original version of this
       
    30  * file:
       
    31  *
       
    32  * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
       
    33  *
       
    34  * All rights reserved.
       
    35  *
       
    36  * Redistribution and use in source and binary forms, with or without
       
    37  * modification, are permitted provided that the following conditions are met:
       
    38  *
       
    39  *  * Redistributions of source code must retain the above copyright notice,
       
    40  *    this list of conditions and the following disclaimer.
       
    41  *
       
    42  *  * Redistributions in binary form must reproduce the above copyright notice,
       
    43  *    this list of conditions and the following disclaimer in the documentation
       
    44  *    and/or other materials provided with the distribution.
       
    45  *
       
    46  *  * Neither the name of JSR-310 nor the names of its contributors
       
    47  *    may be used to endorse or promote products derived from this software
       
    48  *    without specific prior written permission.
       
    49  *
       
    50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
       
    54  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    55  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    56  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    57  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
       
    58  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
       
    59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
       
    60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    61  */
       
    62 package java.time.chrono;
       
    63 
       
    64 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH;
       
    65 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR;
       
    66 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH;
       
    67 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
       
    68 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
       
    69 import static java.time.temporal.ChronoField.DAY_OF_WEEK;
       
    70 import static java.time.temporal.ChronoField.DAY_OF_YEAR;
       
    71 import static java.time.temporal.ChronoField.EPOCH_DAY;
       
    72 import static java.time.temporal.ChronoField.ERA;
       
    73 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
       
    74 import static java.time.temporal.ChronoField.PROLEPTIC_MONTH;
       
    75 import static java.time.temporal.ChronoField.YEAR;
       
    76 import static java.time.temporal.ChronoField.YEAR_OF_ERA;
       
    77 import static java.time.temporal.ChronoUnit.DAYS;
       
    78 import static java.time.temporal.ChronoUnit.MONTHS;
       
    79 import static java.time.temporal.ChronoUnit.WEEKS;
       
    80 import static java.time.temporal.TemporalAdjuster.nextOrSame;
       
    81 
       
    82 import java.io.DataInput;
       
    83 import java.io.DataOutput;
       
    84 import java.io.IOException;
       
    85 import java.io.InvalidObjectException;
       
    86 import java.io.ObjectStreamException;
       
    87 import java.io.Serializable;
       
    88 import java.time.DateTimeException;
       
    89 import java.time.DayOfWeek;
       
    90 import java.time.format.ResolverStyle;
       
    91 import java.time.temporal.ChronoField;
       
    92 import java.time.temporal.TemporalAdjuster;
       
    93 import java.time.temporal.TemporalField;
       
    94 import java.time.temporal.ValueRange;
       
    95 import java.util.Comparator;
       
    96 import java.util.HashSet;
       
    97 import java.util.List;
       
    98 import java.util.Locale;
       
    99 import java.util.Map;
       
   100 import java.util.Objects;
       
   101 import java.util.ServiceLoader;
       
   102 import java.util.Set;
       
   103 import java.util.concurrent.ConcurrentHashMap;
       
   104 
       
   105 import sun.util.logging.PlatformLogger;
       
   106 
       
   107 /**
       
   108  * An abstract implementation of a calendar system, used to organize and identify dates.
       
   109  * <p>
       
   110  * The main date and time API is built on the ISO calendar system.
       
   111  * The chronology operates behind the scenes to represent the general concept of a calendar system.
       
   112  * <p>
       
   113  * See {@link Chronology} for more details.
       
   114  *
       
   115  * @implSpec
       
   116  * This class is separated from the {@code Chronology} interface so that the static methods
       
   117  * are not inherited. While {@code Chronology} can be implemented directly, it is strongly
       
   118  * recommended to extend this abstract class instead.
       
   119  * <p>
       
   120  * This class must be implemented with care to ensure other classes operate correctly.
       
   121  * All implementations that can be instantiated must be final, immutable and thread-safe.
       
   122  * Subclasses should be Serializable wherever possible.
       
   123  *
       
   124  * @since 1.8
       
   125  */
       
   126 public abstract class AbstractChronology implements Chronology {
       
   127 
       
   128     /**
       
   129      * ChronoLocalDate order constant.
       
   130      */
       
   131     static final Comparator<ChronoLocalDate> DATE_ORDER =
       
   132         (Comparator<ChronoLocalDate> & Serializable) (date1, date2) -> {
       
   133             return Long.compare(date1.toEpochDay(), date2.toEpochDay());
       
   134         };
       
   135     /**
       
   136      * ChronoLocalDateTime order constant.
       
   137      */
       
   138     static final Comparator<ChronoLocalDateTime<? extends ChronoLocalDate>> DATE_TIME_ORDER =
       
   139         (Comparator<ChronoLocalDateTime<? extends ChronoLocalDate>> & Serializable) (dateTime1, dateTime2) -> {
       
   140             int cmp = Long.compare(dateTime1.toLocalDate().toEpochDay(), dateTime2.toLocalDate().toEpochDay());
       
   141             if (cmp == 0) {
       
   142                 cmp = Long.compare(dateTime1.toLocalTime().toNanoOfDay(), dateTime2.toLocalTime().toNanoOfDay());
       
   143             }
       
   144             return cmp;
       
   145         };
       
   146     /**
       
   147      * ChronoZonedDateTime order constant.
       
   148      */
       
   149     static final Comparator<ChronoZonedDateTime<?>> INSTANT_ORDER =
       
   150             (Comparator<ChronoZonedDateTime<?>> & Serializable) (dateTime1, dateTime2) -> {
       
   151                 int cmp = Long.compare(dateTime1.toEpochSecond(), dateTime2.toEpochSecond());
       
   152                 if (cmp == 0) {
       
   153                     cmp = Long.compare(dateTime1.toLocalTime().getNano(), dateTime2.toLocalTime().getNano());
       
   154                 }
       
   155                 return cmp;
       
   156             };
       
   157 
       
   158     /**
       
   159      * Map of available calendars by ID.
       
   160      */
       
   161     private static final ConcurrentHashMap<String, Chronology> CHRONOS_BY_ID = new ConcurrentHashMap<>();
       
   162     /**
       
   163      * Map of available calendars by calendar type.
       
   164      */
       
   165     private static final ConcurrentHashMap<String, Chronology> CHRONOS_BY_TYPE = new ConcurrentHashMap<>();
       
   166 
       
   167     /**
       
   168      * Register a Chronology by its ID and type for lookup by {@link #of(String)}.
       
   169      * Chronologies must not be registered until they are completely constructed.
       
   170      * Specifically, not in the constructor of Chronology.
       
   171      *
       
   172      * @param chrono the chronology to register; not null
       
   173      * @return the already registered Chronology if any, may be null
       
   174      */
       
   175     static Chronology registerChrono(Chronology chrono) {
       
   176         return registerChrono(chrono, chrono.getId());
       
   177     }
       
   178 
       
   179     /**
       
   180      * Register a Chronology by ID and type for lookup by {@link #of(String)}.
       
   181      * Chronos must not be registered until they are completely constructed.
       
   182      * Specifically, not in the constructor of Chronology.
       
   183      *
       
   184      * @param chrono the chronology to register; not null
       
   185      * @param id the ID to register the chronology; not null
       
   186      * @return the already registered Chronology if any, may be null
       
   187      */
       
   188     static Chronology registerChrono(Chronology chrono, String id) {
       
   189         Chronology prev = CHRONOS_BY_ID.putIfAbsent(id, chrono);
       
   190         if (prev == null) {
       
   191             String type = chrono.getCalendarType();
       
   192             if (type != null) {
       
   193                 CHRONOS_BY_TYPE.putIfAbsent(type, chrono);
       
   194             }
       
   195         }
       
   196         return prev;
       
   197     }
       
   198 
       
   199     /**
       
   200      * Initialization of the maps from id and type to Chronology.
       
   201      * The ServiceLoader is used to find and register any implementations
       
   202      * of {@link java.time.chrono.AbstractChronology} found in the bootclass loader.
       
   203      * The built-in chronologies are registered explicitly.
       
   204      * Calendars configured via the Thread's context classloader are local
       
   205      * to that thread and are ignored.
       
   206      * <p>
       
   207      * The initialization is done only once using the registration
       
   208      * of the IsoChronology as the test and the final step.
       
   209      * Multiple threads may perform the initialization concurrently.
       
   210      * Only the first registration of each Chronology is retained by the
       
   211      * ConcurrentHashMap.
       
   212      * @return true if the cache was initialized
       
   213      */
       
   214     private static boolean initCache() {
       
   215         if (CHRONOS_BY_ID.get("ISO") == null) {
       
   216             // Initialization is incomplete
       
   217 
       
   218             // Register built-in Chronologies
       
   219             registerChrono(HijrahChronology.INSTANCE);
       
   220             registerChrono(JapaneseChronology.INSTANCE);
       
   221             registerChrono(MinguoChronology.INSTANCE);
       
   222             registerChrono(ThaiBuddhistChronology.INSTANCE);
       
   223 
       
   224             // Register Chronologies from the ServiceLoader
       
   225             @SuppressWarnings("rawtypes")
       
   226             ServiceLoader<AbstractChronology> loader =  ServiceLoader.load(AbstractChronology.class, null);
       
   227             for (AbstractChronology chrono : loader) {
       
   228                 String id = chrono.getId();
       
   229                 if (id.equals("ISO") || registerChrono(chrono) != null) {
       
   230                     // Log the attempt to replace an existing Chronology
       
   231                     PlatformLogger logger = PlatformLogger.getLogger("java.time.chrono");
       
   232                     logger.warning("Ignoring duplicate Chronology, from ServiceLoader configuration "  + id);
       
   233                 }
       
   234             }
       
   235 
       
   236             // finally, register IsoChronology to mark initialization is complete
       
   237             registerChrono(IsoChronology.INSTANCE);
       
   238             return true;
       
   239         }
       
   240         return false;
       
   241     }
       
   242 
       
   243     //-----------------------------------------------------------------------
       
   244     /**
       
   245      * Obtains an instance of {@code Chronology} from a locale.
       
   246      * <p>
       
   247      * See {@link Chronology#ofLocale(Locale)}.
       
   248      *
       
   249      * @param locale  the locale to use to obtain the calendar system, not null
       
   250      * @return the calendar system associated with the locale, not null
       
   251      * @throws java.time.DateTimeException if the locale-specified calendar cannot be found
       
   252      */
       
   253     static Chronology ofLocale(Locale locale) {
       
   254         Objects.requireNonNull(locale, "locale");
       
   255         String type = locale.getUnicodeLocaleType("ca");
       
   256         if (type == null || "iso".equals(type) || "iso8601".equals(type)) {
       
   257             return IsoChronology.INSTANCE;
       
   258         }
       
   259         // Not pre-defined; lookup by the type
       
   260         do {
       
   261             Chronology chrono = CHRONOS_BY_TYPE.get(type);
       
   262             if (chrono != null) {
       
   263                 return chrono;
       
   264             }
       
   265             // If not found, do the initialization (once) and repeat the lookup
       
   266         } while (initCache());
       
   267 
       
   268         // Look for a Chronology using ServiceLoader of the Thread's ContextClassLoader
       
   269         // Application provided Chronologies must not be cached
       
   270         @SuppressWarnings("rawtypes")
       
   271         ServiceLoader<Chronology> loader = ServiceLoader.load(Chronology.class);
       
   272         for (Chronology chrono : loader) {
       
   273             if (type.equals(chrono.getCalendarType())) {
       
   274                 return chrono;
       
   275             }
       
   276         }
       
   277         throw new DateTimeException("Unknown calendar system: " + type);
       
   278     }
       
   279 
       
   280     //-----------------------------------------------------------------------
       
   281     /**
       
   282      * Obtains an instance of {@code Chronology} from a chronology ID or
       
   283      * calendar system type.
       
   284      * <p>
       
   285      * See {@link Chronology#of(String)}.
       
   286      *
       
   287      * @param id  the chronology ID or calendar system type, not null
       
   288      * @return the chronology with the identifier requested, not null
       
   289      * @throws java.time.DateTimeException if the chronology cannot be found
       
   290      */
       
   291     static Chronology of(String id) {
       
   292         Objects.requireNonNull(id, "id");
       
   293         do {
       
   294             Chronology chrono = of0(id);
       
   295             if (chrono != null) {
       
   296                 return chrono;
       
   297             }
       
   298             // If not found, do the initialization (once) and repeat the lookup
       
   299         } while (initCache());
       
   300 
       
   301         // Look for a Chronology using ServiceLoader of the Thread's ContextClassLoader
       
   302         // Application provided Chronologies must not be cached
       
   303         @SuppressWarnings("rawtypes")
       
   304         ServiceLoader<Chronology> loader = ServiceLoader.load(Chronology.class);
       
   305         for (Chronology chrono : loader) {
       
   306             if (id.equals(chrono.getId()) || id.equals(chrono.getCalendarType())) {
       
   307                 return chrono;
       
   308             }
       
   309         }
       
   310         throw new DateTimeException("Unknown chronology: " + id);
       
   311     }
       
   312 
       
   313     /**
       
   314      * Obtains an instance of {@code Chronology} from a chronology ID or
       
   315      * calendar system type.
       
   316      *
       
   317      * @param id  the chronology ID or calendar system type, not null
       
   318      * @return the chronology with the identifier requested, or {@code null} if not found
       
   319      */
       
   320     private static Chronology of0(String id) {
       
   321         Chronology chrono = CHRONOS_BY_ID.get(id);
       
   322         if (chrono == null) {
       
   323             chrono = CHRONOS_BY_TYPE.get(id);
       
   324         }
       
   325         return chrono;
       
   326     }
       
   327 
       
   328     /**
       
   329      * Returns the available chronologies.
       
   330      * <p>
       
   331      * Each returned {@code Chronology} is available for use in the system.
       
   332      * The set of chronologies includes the system chronologies and
       
   333      * any chronologies provided by the application via ServiceLoader
       
   334      * configuration.
       
   335      *
       
   336      * @return the independent, modifiable set of the available chronology IDs, not null
       
   337      */
       
   338     static Set<Chronology> getAvailableChronologies() {
       
   339         initCache();       // force initialization
       
   340         HashSet<Chronology> chronos = new HashSet<>(CHRONOS_BY_ID.values());
       
   341 
       
   342         /// Add in Chronologies from the ServiceLoader configuration
       
   343         @SuppressWarnings("rawtypes")
       
   344         ServiceLoader<Chronology> loader = ServiceLoader.load(Chronology.class);
       
   345         for (Chronology chrono : loader) {
       
   346             chronos.add(chrono);
       
   347         }
       
   348         return chronos;
       
   349     }
       
   350 
       
   351     //-----------------------------------------------------------------------
       
   352     /**
       
   353      * Creates an instance.
       
   354      */
       
   355     protected AbstractChronology() {
       
   356     }
       
   357 
       
   358     //-----------------------------------------------------------------------
       
   359     /**
       
   360      * Resolves parsed {@code ChronoField} values into a date during parsing.
       
   361      * <p>
       
   362      * Most {@code TemporalField} implementations are resolved using the
       
   363      * resolve method on the field. By contrast, the {@code ChronoField} class
       
   364      * defines fields that only have meaning relative to the chronology.
       
   365      * As such, {@code ChronoField} date fields are resolved here in the
       
   366      * context of a specific chronology.
       
   367      * <p>
       
   368      * {@code ChronoField} instances are resolved by this method, which may
       
   369      * be overridden in subclasses.
       
   370      * <ul>
       
   371      * <li>{@code EPOCH_DAY} - If present, this is converted to a date and
       
   372      *  all other date fields are then cross-checked against the date.
       
   373      * <li>{@code PROLEPTIC_MONTH} - If present, then it is split into the
       
   374      *  {@code YEAR} and {@code MONTH_OF_YEAR}. If the mode is strict or smart
       
   375      *  then the field is validated.
       
   376      * <li>{@code YEAR_OF_ERA} and {@code ERA} - If both are present, then they
       
   377      *  are combined to form a {@code YEAR}. In lenient mode, the {@code YEAR_OF_ERA}
       
   378      *  range is not validated, in smart and strict mode it is. The {@code ERA} is
       
   379      *  validated for range in all three modes. If only the {@code YEAR_OF_ERA} is
       
   380      *  present, and the mode is smart or lenient, then the last available era
       
   381      *  is assumed. In strict mode, no era is assumed and the {@code YEAR_OF_ERA} is
       
   382      *  left untouched. If only the {@code ERA} is present, then it is left untouched.
       
   383      * <li>{@code YEAR}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} -
       
   384      *  If all three are present, then they are combined to form a date.
       
   385      *  In all three modes, the {@code YEAR} is validated.
       
   386      *  If the mode is smart or strict, then the month and day are validated.
       
   387      *  If the mode is lenient, then the date is combined in a manner equivalent to
       
   388      *  creating a date on the first day of the first month in the requested year,
       
   389      *  then adding the difference in months, then the difference in days.
       
   390      *  If the mode is smart, and the day-of-month is greater than the maximum for
       
   391      *  the year-month, then the day-of-month is adjusted to the last day-of-month.
       
   392      *  If the mode is strict, then the three fields must form a valid date.
       
   393      * <li>{@code YEAR} and {@code DAY_OF_YEAR} -
       
   394      *  If both are present, then they are combined to form a date.
       
   395      *  In all three modes, the {@code YEAR} is validated.
       
   396      *  If the mode is lenient, then the date is combined in a manner equivalent to
       
   397      *  creating a date on the first day of the requested year, then adding
       
   398      *  the difference in days.
       
   399      *  If the mode is smart or strict, then the two fields must form a valid date.
       
   400      * <li>{@code YEAR}, {@code MONTH_OF_YEAR}, {@code ALIGNED_WEEK_OF_MONTH} and
       
   401      *  {@code ALIGNED_DAY_OF_WEEK_IN_MONTH} -
       
   402      *  If all four are present, then they are combined to form a date.
       
   403      *  In all three modes, the {@code YEAR} is validated.
       
   404      *  If the mode is lenient, then the date is combined in a manner equivalent to
       
   405      *  creating a date on the first day of the first month in the requested year, then adding
       
   406      *  the difference in months, then the difference in weeks, then in days.
       
   407      *  If the mode is smart or strict, then the all four fields are validated to
       
   408      *  their outer ranges. The date is then combined in a manner equivalent to
       
   409      *  creating a date on the first day of the requested year and month, then adding
       
   410      *  the amount in weeks and days to reach their values. If the mode is strict,
       
   411      *  the date is additionally validated to check that the day and week adjustment
       
   412      *  did not change the month.
       
   413      * <li>{@code YEAR}, {@code MONTH_OF_YEAR}, {@code ALIGNED_WEEK_OF_MONTH} and
       
   414      *  {@code DAY_OF_WEEK} - If all four are present, then they are combined to
       
   415      *  form a date. The approach is the same as described above for
       
   416      *  years, months and weeks in {@code ALIGNED_DAY_OF_WEEK_IN_MONTH}.
       
   417      *  The day-of-week is adjusted as the next or same matching day-of-week once
       
   418      *  the years, months and weeks have been handled.
       
   419      * <li>{@code YEAR}, {@code ALIGNED_WEEK_OF_YEAR} and {@code ALIGNED_DAY_OF_WEEK_IN_YEAR} -
       
   420      *  If all three are present, then they are combined to form a date.
       
   421      *  In all three modes, the {@code YEAR} is validated.
       
   422      *  If the mode is lenient, then the date is combined in a manner equivalent to
       
   423      *  creating a date on the first day of the requested year, then adding
       
   424      *  the difference in weeks, then in days.
       
   425      *  If the mode is smart or strict, then the all three fields are validated to
       
   426      *  their outer ranges. The date is then combined in a manner equivalent to
       
   427      *  creating a date on the first day of the requested year, then adding
       
   428      *  the amount in weeks and days to reach their values. If the mode is strict,
       
   429      *  the date is additionally validated to check that the day and week adjustment
       
   430      *  did not change the year.
       
   431      * <li>{@code YEAR}, {@code ALIGNED_WEEK_OF_YEAR} and {@code DAY_OF_WEEK} -
       
   432      *  If all three are present, then they are combined to form a date.
       
   433      *  The approach is the same as described above for years and weeks in
       
   434      *  {@code ALIGNED_DAY_OF_WEEK_IN_YEAR}. The day-of-week is adjusted as the
       
   435      *  next or same matching day-of-week once the years and weeks have been handled.
       
   436      * </ul>
       
   437      * <p>
       
   438      * The default implementation is suitable for most calendar systems.
       
   439      * If {@link java.time.temporal.ChronoField#YEAR_OF_ERA} is found without an {@link java.time.temporal.ChronoField#ERA}
       
   440      * then the last era in {@link #eras()} is used.
       
   441      * The implementation assumes a 7 day week, that the first day-of-month
       
   442      * has the value 1, that first day-of-year has the value 1, and that the
       
   443      * first of the month and year always exists.
       
   444      *
       
   445      * @param fieldValues  the map of fields to values, which can be updated, not null
       
   446      * @param resolverStyle  the requested type of resolve, not null
       
   447      * @return the resolved date, null if insufficient information to create a date
       
   448      * @throws java.time.DateTimeException if the date cannot be resolved, typically
       
   449      *  because of a conflict in the input data
       
   450      */
       
   451     @Override
       
   452     public ChronoLocalDate resolveDate(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
       
   453         // check epoch-day before inventing era
       
   454         if (fieldValues.containsKey(EPOCH_DAY)) {
       
   455             return dateEpochDay(fieldValues.remove(EPOCH_DAY));
       
   456         }
       
   457 
       
   458         // fix proleptic month before inventing era
       
   459         resolveProlepticMonth(fieldValues, resolverStyle);
       
   460 
       
   461         // invent era if necessary to resolve year-of-era
       
   462         ChronoLocalDate resolved = resolveYearOfEra(fieldValues, resolverStyle);
       
   463         if (resolved != null) {
       
   464             return resolved;
       
   465         }
       
   466 
       
   467         // build date
       
   468         if (fieldValues.containsKey(YEAR)) {
       
   469             if (fieldValues.containsKey(MONTH_OF_YEAR)) {
       
   470                 if (fieldValues.containsKey(DAY_OF_MONTH)) {
       
   471                     return resolveYMD(fieldValues, resolverStyle);
       
   472                 }
       
   473                 if (fieldValues.containsKey(ALIGNED_WEEK_OF_MONTH)) {
       
   474                     if (fieldValues.containsKey(ALIGNED_DAY_OF_WEEK_IN_MONTH)) {
       
   475                         return resolveYMAA(fieldValues, resolverStyle);
       
   476                     }
       
   477                     if (fieldValues.containsKey(DAY_OF_WEEK)) {
       
   478                         return resolveYMAD(fieldValues, resolverStyle);
       
   479                     }
       
   480                 }
       
   481             }
       
   482             if (fieldValues.containsKey(DAY_OF_YEAR)) {
       
   483                 return resolveYD(fieldValues, resolverStyle);
       
   484             }
       
   485             if (fieldValues.containsKey(ALIGNED_WEEK_OF_YEAR)) {
       
   486                 if (fieldValues.containsKey(ALIGNED_DAY_OF_WEEK_IN_YEAR)) {
       
   487                     return resolveYAA(fieldValues, resolverStyle);
       
   488                 }
       
   489                 if (fieldValues.containsKey(DAY_OF_WEEK)) {
       
   490                     return resolveYAD(fieldValues, resolverStyle);
       
   491                 }
       
   492             }
       
   493         }
       
   494         return null;
       
   495     }
       
   496 
       
   497     void resolveProlepticMonth(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
       
   498         Long pMonth = fieldValues.remove(PROLEPTIC_MONTH);
       
   499         if (pMonth != null) {
       
   500             if (resolverStyle != ResolverStyle.LENIENT) {
       
   501                 PROLEPTIC_MONTH.checkValidValue(pMonth);
       
   502             }
       
   503             // first day-of-month is likely to be safest for setting proleptic-month
       
   504             // cannot add to year zero, as not all chronologies have a year zero
       
   505             ChronoLocalDate chronoDate = dateNow()
       
   506                     .with(DAY_OF_MONTH, 1).with(PROLEPTIC_MONTH, pMonth);
       
   507             addFieldValue(fieldValues, MONTH_OF_YEAR, chronoDate.get(MONTH_OF_YEAR));
       
   508             addFieldValue(fieldValues, YEAR, chronoDate.get(YEAR));
       
   509         }
       
   510     }
       
   511 
       
   512     ChronoLocalDate resolveYearOfEra(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
       
   513         Long yoeLong = fieldValues.remove(YEAR_OF_ERA);
       
   514         if (yoeLong != null) {
       
   515             Long eraLong = fieldValues.remove(ERA);
       
   516             int yoe;
       
   517             if (resolverStyle != ResolverStyle.LENIENT) {
       
   518                 yoe = range(YEAR_OF_ERA).checkValidIntValue(yoeLong, YEAR_OF_ERA);
       
   519             } else {
       
   520                 yoe = Math.toIntExact(yoeLong);
       
   521             }
       
   522             if (eraLong != null) {
       
   523                 Era eraObj = eraOf(range(ERA).checkValidIntValue(eraLong, ERA));
       
   524                 addFieldValue(fieldValues, YEAR, prolepticYear(eraObj, yoe));
       
   525             } else {
       
   526                 if (fieldValues.containsKey(YEAR)) {
       
   527                     int year = range(YEAR).checkValidIntValue(fieldValues.get(YEAR), YEAR);
       
   528                     ChronoLocalDate chronoDate = dateYearDay(year, 1);
       
   529                     addFieldValue(fieldValues, YEAR, prolepticYear(chronoDate.getEra(), yoe));
       
   530                 } else if (resolverStyle == ResolverStyle.STRICT) {
       
   531                     // do not invent era if strict
       
   532                     // reinstate the field removed earlier, no cross-check issues
       
   533                     fieldValues.put(YEAR_OF_ERA, yoeLong);
       
   534                 } else {
       
   535                     List<Era> eras = eras();
       
   536                     if (eras.isEmpty()) {
       
   537                         addFieldValue(fieldValues, YEAR, yoe);
       
   538                     } else {
       
   539                         Era eraObj = eras.get(eras.size() - 1);
       
   540                         addFieldValue(fieldValues, YEAR, prolepticYear(eraObj, yoe));
       
   541                     }
       
   542                 }
       
   543             }
       
   544         } else if (fieldValues.containsKey(ERA)) {
       
   545             range(ERA).checkValidValue(fieldValues.get(ERA), ERA);  // always validated
       
   546         }
       
   547         return null;
       
   548     }
       
   549 
       
   550     ChronoLocalDate resolveYMD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
       
   551         int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);
       
   552         if (resolverStyle == ResolverStyle.LENIENT) {
       
   553             long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1);
       
   554             long days = Math.subtractExact(fieldValues.remove(DAY_OF_MONTH), 1);
       
   555             return date(y, 1, 1).plus(months, MONTHS).plus(days, DAYS);
       
   556         }
       
   557         int moy = range(MONTH_OF_YEAR).checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR), MONTH_OF_YEAR);
       
   558         ValueRange domRange = range(DAY_OF_MONTH);
       
   559         int dom = domRange.checkValidIntValue(fieldValues.remove(DAY_OF_MONTH), DAY_OF_MONTH);
       
   560         if (resolverStyle == ResolverStyle.SMART) {  // previous valid
       
   561             try {
       
   562                 return date(y, moy, dom);
       
   563             } catch (DateTimeException ex) {
       
   564                 return date(y, moy, 1).with(TemporalAdjuster.lastDayOfMonth());
       
   565             }
       
   566         }
       
   567         return date(y, moy, dom);
       
   568     }
       
   569 
       
   570     ChronoLocalDate resolveYD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
       
   571         int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);
       
   572         if (resolverStyle == ResolverStyle.LENIENT) {
       
   573             long days = Math.subtractExact(fieldValues.remove(DAY_OF_YEAR), 1);
       
   574             return dateYearDay(y, 1).plus(days, DAYS);
       
   575         }
       
   576         int doy = range(DAY_OF_YEAR).checkValidIntValue(fieldValues.remove(DAY_OF_YEAR), DAY_OF_YEAR);
       
   577         return dateYearDay(y, doy);  // smart is same as strict
       
   578     }
       
   579 
       
   580     ChronoLocalDate resolveYMAA(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
       
   581         int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);
       
   582         if (resolverStyle == ResolverStyle.LENIENT) {
       
   583             long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1);
       
   584             long weeks = Math.subtractExact(fieldValues.remove(ALIGNED_WEEK_OF_MONTH), 1);
       
   585             long days = Math.subtractExact(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_MONTH), 1);
       
   586             return date(y, 1, 1).plus(months, MONTHS).plus(weeks, WEEKS).plus(days, DAYS);
       
   587         }
       
   588         int moy = range(MONTH_OF_YEAR).checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR), MONTH_OF_YEAR);
       
   589         int aw = range(ALIGNED_WEEK_OF_MONTH).checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_MONTH), ALIGNED_WEEK_OF_MONTH);
       
   590         int ad = range(ALIGNED_DAY_OF_WEEK_IN_MONTH).checkValidIntValue(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_MONTH), ALIGNED_DAY_OF_WEEK_IN_MONTH);
       
   591         ChronoLocalDate date = date(y, moy, 1).plus((aw - 1) * 7 + (ad - 1), DAYS);
       
   592         if (resolverStyle == ResolverStyle.STRICT && date.get(MONTH_OF_YEAR) != moy) {
       
   593             throw new DateTimeException("Strict mode rejected resolved date as it is in a different month");
       
   594         }
       
   595         return date;
       
   596     }
       
   597 
       
   598     ChronoLocalDate resolveYMAD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
       
   599         int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);
       
   600         if (resolverStyle == ResolverStyle.LENIENT) {
       
   601             long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1);
       
   602             long weeks = Math.subtractExact(fieldValues.remove(ALIGNED_WEEK_OF_MONTH), 1);
       
   603             long dow = Math.subtractExact(fieldValues.remove(DAY_OF_WEEK), 1);
       
   604             return resolveAligned(date(y, 1, 1), months, weeks, dow);
       
   605         }
       
   606         int moy = range(MONTH_OF_YEAR).checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR), MONTH_OF_YEAR);
       
   607         int aw = range(ALIGNED_WEEK_OF_MONTH).checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_MONTH), ALIGNED_WEEK_OF_MONTH);
       
   608         int dow = range(DAY_OF_WEEK).checkValidIntValue(fieldValues.remove(DAY_OF_WEEK), DAY_OF_WEEK);
       
   609         ChronoLocalDate date = date(y, moy, 1).plus((aw - 1) * 7, DAYS).with(nextOrSame(DayOfWeek.of(dow)));
       
   610         if (resolverStyle == ResolverStyle.STRICT && date.get(MONTH_OF_YEAR) != moy) {
       
   611             throw new DateTimeException("Strict mode rejected resolved date as it is in a different month");
       
   612         }
       
   613         return date;
       
   614     }
       
   615 
       
   616     ChronoLocalDate resolveYAA(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
       
   617         int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);
       
   618         if (resolverStyle == ResolverStyle.LENIENT) {
       
   619             long weeks = Math.subtractExact(fieldValues.remove(ALIGNED_WEEK_OF_YEAR), 1);
       
   620             long days = Math.subtractExact(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_YEAR), 1);
       
   621             return dateYearDay(y, 1).plus(weeks, WEEKS).plus(days, DAYS);
       
   622         }
       
   623         int aw = range(ALIGNED_WEEK_OF_YEAR).checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_YEAR), ALIGNED_WEEK_OF_YEAR);
       
   624         int ad = range(ALIGNED_DAY_OF_WEEK_IN_YEAR).checkValidIntValue(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_YEAR), ALIGNED_DAY_OF_WEEK_IN_YEAR);
       
   625         ChronoLocalDate date = dateYearDay(y, 1).plus((aw - 1) * 7 + (ad - 1), DAYS);
       
   626         if (resolverStyle == ResolverStyle.STRICT && date.get(YEAR) != y) {
       
   627             throw new DateTimeException("Strict mode rejected resolved date as it is in a different year");
       
   628         }
       
   629         return date;
       
   630     }
       
   631 
       
   632     ChronoLocalDate resolveYAD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
       
   633         int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);
       
   634         if (resolverStyle == ResolverStyle.LENIENT) {
       
   635             long weeks = Math.subtractExact(fieldValues.remove(ALIGNED_WEEK_OF_YEAR), 1);
       
   636             long dow = Math.subtractExact(fieldValues.remove(DAY_OF_WEEK), 1);
       
   637             return resolveAligned(dateYearDay(y, 1), 0, weeks, dow);
       
   638         }
       
   639         int aw = range(ALIGNED_WEEK_OF_YEAR).checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_YEAR), ALIGNED_WEEK_OF_YEAR);
       
   640         int dow = range(DAY_OF_WEEK).checkValidIntValue(fieldValues.remove(DAY_OF_WEEK), DAY_OF_WEEK);
       
   641         ChronoLocalDate date = dateYearDay(y, 1).plus((aw - 1) * 7, DAYS).with(nextOrSame(DayOfWeek.of(dow)));
       
   642         if (resolverStyle == ResolverStyle.STRICT && date.get(YEAR) != y) {
       
   643             throw new DateTimeException("Strict mode rejected resolved date as it is in a different year");
       
   644         }
       
   645         return date;
       
   646     }
       
   647 
       
   648     ChronoLocalDate resolveAligned(ChronoLocalDate base, long months, long weeks, long dow) {
       
   649         ChronoLocalDate date = base.plus(months, MONTHS).plus(weeks, WEEKS);
       
   650         if (dow > 7) {
       
   651             date = date.plus((dow - 1) / 7, WEEKS);
       
   652             dow = ((dow - 1) % 7) + 1;
       
   653         } else if (dow < 1) {
       
   654             date = date.plus(Math.subtractExact(dow,  7) / 7, WEEKS);
       
   655             dow = ((dow + 6) % 7) + 1;
       
   656         }
       
   657         return date.with(nextOrSame(DayOfWeek.of((int) dow)));
       
   658     }
       
   659 
       
   660     /**
       
   661      * Adds a field-value pair to the map, checking for conflicts.
       
   662      * <p>
       
   663      * If the field is not already present, then the field-value pair is added to the map.
       
   664      * If the field is already present and it has the same value as that specified, no action occurs.
       
   665      * If the field is already present and it has a different value to that specified, then
       
   666      * an exception is thrown.
       
   667      *
       
   668      * @param field  the field to add, not null
       
   669      * @param value  the value to add, not null
       
   670      * @throws java.time.DateTimeException if the field is already present with a different value
       
   671      */
       
   672     void addFieldValue(Map<TemporalField, Long> fieldValues, ChronoField field, long value) {
       
   673         Long old = fieldValues.get(field);  // check first for better error message
       
   674         if (old != null && old.longValue() != value) {
       
   675             throw new DateTimeException("Conflict found: " + field + " " + old + " differs from " + field + " " + value);
       
   676         }
       
   677         fieldValues.put(field, value);
       
   678     }
       
   679 
       
   680     //-----------------------------------------------------------------------
       
   681     /**
       
   682      * Compares this chronology to another chronology.
       
   683      * <p>
       
   684      * The comparison order first by the chronology ID string, then by any
       
   685      * additional information specific to the subclass.
       
   686      * It is "consistent with equals", as defined by {@link Comparable}.
       
   687      *
       
   688      * @implSpec
       
   689      * This implementation compares the chronology ID.
       
   690      * Subclasses must compare any additional state that they store.
       
   691      *
       
   692      * @param other  the other chronology to compare to, not null
       
   693      * @return the comparator value, negative if less, positive if greater
       
   694      */
       
   695     @Override
       
   696     public int compareTo(Chronology other) {
       
   697         return getId().compareTo(other.getId());
       
   698     }
       
   699 
       
   700     /**
       
   701      * Checks if this chronology is equal to another chronology.
       
   702      * <p>
       
   703      * The comparison is based on the entire state of the object.
       
   704      *
       
   705      * @implSpec
       
   706      * This implementation checks the type and calls
       
   707      * {@link #compareTo(java.time.chrono.Chronology)}.
       
   708      *
       
   709      * @param obj  the object to check, null returns false
       
   710      * @return true if this is equal to the other chronology
       
   711      */
       
   712     @Override
       
   713     public boolean equals(Object obj) {
       
   714         if (this == obj) {
       
   715            return true;
       
   716         }
       
   717         if (obj instanceof AbstractChronology) {
       
   718             return compareTo((AbstractChronology) obj) == 0;
       
   719         }
       
   720         return false;
       
   721     }
       
   722 
       
   723     /**
       
   724      * A hash code for this chronology.
       
   725      * <p>
       
   726      * The hash code should be based on the entire state of the object.
       
   727      *
       
   728      * @implSpec
       
   729      * This implementation is based on the chronology ID and class.
       
   730      * Subclasses should add any additional state that they store.
       
   731      *
       
   732      * @return a suitable hash code
       
   733      */
       
   734     @Override
       
   735     public int hashCode() {
       
   736         return getClass().hashCode() ^ getId().hashCode();
       
   737     }
       
   738 
       
   739     //-----------------------------------------------------------------------
       
   740     /**
       
   741      * Outputs this chronology as a {@code String}, using the chronology ID.
       
   742      *
       
   743      * @return a string representation of this chronology, not null
       
   744      */
       
   745     @Override
       
   746     public String toString() {
       
   747         return getId();
       
   748     }
       
   749 
       
   750     //-----------------------------------------------------------------------
       
   751     /**
       
   752      * Writes the Chronology using a
       
   753      * <a href="../../../serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
       
   754      * <pre>
       
   755      *  out.writeByte(1);  // identifies this as a Chronology
       
   756      *  out.writeUTF(getId());
       
   757      * </pre>
       
   758      *
       
   759      * @return the instance of {@code Ser}, not null
       
   760      */
       
   761     Object writeReplace() {
       
   762         return new Ser(Ser.CHRONO_TYPE, this);
       
   763     }
       
   764 
       
   765     /**
       
   766      * Defend against malicious streams.
       
   767      * @return never
       
   768      * @throws java.io.InvalidObjectException always
       
   769      */
       
   770     private Object readResolve() throws ObjectStreamException {
       
   771         throw new InvalidObjectException("Deserialization via serialization delegate");
       
   772     }
       
   773 
       
   774     void writeExternal(DataOutput out) throws IOException {
       
   775         out.writeUTF(getId());
       
   776     }
       
   777 
       
   778     static Chronology readExternal(DataInput in) throws IOException {
       
   779         String id = in.readUTF();
       
   780         return Chronology.of(id);
       
   781     }
       
   782 
       
   783 }