Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/time/chrono/AbstractChronology.java
38918 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file:30*31* Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos32*33* All rights reserved.34*35* Redistribution and use in source and binary forms, with or without36* modification, are permitted provided that the following conditions are met:37*38* * Redistributions of source code must retain the above copyright notice,39* this list of conditions and the following disclaimer.40*41* * Redistributions in binary form must reproduce the above copyright notice,42* this list of conditions and the following disclaimer in the documentation43* and/or other materials provided with the distribution.44*45* * Neither the name of JSR-310 nor the names of its contributors46* may be used to endorse or promote products derived from this software47* without specific prior written permission.48*49* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS50* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT51* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR52* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR53* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,54* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,55* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR56* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF57* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING58* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS59* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.60*/61package java.time.chrono;6263import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH;64import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR;65import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH;66import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;67import static java.time.temporal.ChronoField.DAY_OF_MONTH;68import static java.time.temporal.ChronoField.DAY_OF_WEEK;69import static java.time.temporal.ChronoField.DAY_OF_YEAR;70import static java.time.temporal.ChronoField.EPOCH_DAY;71import static java.time.temporal.ChronoField.ERA;72import static java.time.temporal.ChronoField.MONTH_OF_YEAR;73import static java.time.temporal.ChronoField.PROLEPTIC_MONTH;74import static java.time.temporal.ChronoField.YEAR;75import static java.time.temporal.ChronoField.YEAR_OF_ERA;76import static java.time.temporal.ChronoUnit.DAYS;77import static java.time.temporal.ChronoUnit.MONTHS;78import static java.time.temporal.ChronoUnit.WEEKS;79import static java.time.temporal.TemporalAdjusters.nextOrSame;8081import java.io.DataInput;82import java.io.DataOutput;83import java.io.IOException;84import java.io.InvalidObjectException;85import java.io.ObjectInputStream;86import java.io.ObjectStreamException;87import java.io.Serializable;88import java.time.DateTimeException;89import java.time.DayOfWeek;90import java.time.format.ResolverStyle;91import java.time.temporal.ChronoField;92import java.time.temporal.TemporalAdjusters;93import java.time.temporal.TemporalField;94import java.time.temporal.ValueRange;95import java.util.Comparator;96import java.util.HashSet;97import java.util.List;98import java.util.Locale;99import java.util.Map;100import java.util.Objects;101import java.util.ServiceLoader;102import java.util.Set;103import java.util.concurrent.ConcurrentHashMap;104105import sun.util.logging.PlatformLogger;106107/**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* @implSpec116* This class is separated from the {@code Chronology} interface so that the static methods117* are not inherited. While {@code Chronology} can be implemented directly, it is strongly118* 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.8125*/126public abstract class AbstractChronology implements Chronology {127128/**129* ChronoLocalDate order constant.130*/131static final Comparator<ChronoLocalDate> DATE_ORDER =132(Comparator<ChronoLocalDate> & Serializable) (date1, date2) -> {133return Long.compare(date1.toEpochDay(), date2.toEpochDay());134};135/**136* ChronoLocalDateTime order constant.137*/138static final Comparator<ChronoLocalDateTime<? extends ChronoLocalDate>> DATE_TIME_ORDER =139(Comparator<ChronoLocalDateTime<? extends ChronoLocalDate>> & Serializable) (dateTime1, dateTime2) -> {140int cmp = Long.compare(dateTime1.toLocalDate().toEpochDay(), dateTime2.toLocalDate().toEpochDay());141if (cmp == 0) {142cmp = Long.compare(dateTime1.toLocalTime().toNanoOfDay(), dateTime2.toLocalTime().toNanoOfDay());143}144return cmp;145};146/**147* ChronoZonedDateTime order constant.148*/149static final Comparator<ChronoZonedDateTime<?>> INSTANT_ORDER =150(Comparator<ChronoZonedDateTime<?>> & Serializable) (dateTime1, dateTime2) -> {151int cmp = Long.compare(dateTime1.toEpochSecond(), dateTime2.toEpochSecond());152if (cmp == 0) {153cmp = Long.compare(dateTime1.toLocalTime().getNano(), dateTime2.toLocalTime().getNano());154}155return cmp;156};157158/**159* Map of available calendars by ID.160*/161private static final ConcurrentHashMap<String, Chronology> CHRONOS_BY_ID = new ConcurrentHashMap<>();162/**163* Map of available calendars by calendar type.164*/165private static final ConcurrentHashMap<String, Chronology> CHRONOS_BY_TYPE = new ConcurrentHashMap<>();166167/**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 null173* @return the already registered Chronology if any, may be null174*/175static Chronology registerChrono(Chronology chrono) {176return registerChrono(chrono, chrono.getId());177}178179/**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 null185* @param id the ID to register the chronology; not null186* @return the already registered Chronology if any, may be null187*/188static Chronology registerChrono(Chronology chrono, String id) {189Chronology prev = CHRONOS_BY_ID.putIfAbsent(id, chrono);190if (prev == null) {191String type = chrono.getCalendarType();192if (type != null) {193CHRONOS_BY_TYPE.putIfAbsent(type, chrono);194}195}196return prev;197}198199/**200* Initialization of the maps from id and type to Chronology.201* The ServiceLoader is used to find and register any implementations202* 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 local205* to that thread and are ignored.206* <p>207* The initialization is done only once using the registration208* 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 the211* ConcurrentHashMap.212* @return true if the cache was initialized213*/214private static boolean initCache() {215if (CHRONOS_BY_ID.get("ISO") == null) {216// Initialization is incomplete217218// Register built-in Chronologies219registerChrono(HijrahChronology.INSTANCE);220registerChrono(JapaneseChronology.INSTANCE);221registerChrono(MinguoChronology.INSTANCE);222registerChrono(ThaiBuddhistChronology.INSTANCE);223224// Register Chronologies from the ServiceLoader225@SuppressWarnings("rawtypes")226ServiceLoader<AbstractChronology> loader = ServiceLoader.load(AbstractChronology.class, null);227for (AbstractChronology chrono : loader) {228String id = chrono.getId();229if (id.equals("ISO") || registerChrono(chrono) != null) {230// Log the attempt to replace an existing Chronology231PlatformLogger logger = PlatformLogger.getLogger("java.time.chrono");232logger.warning("Ignoring duplicate Chronology, from ServiceLoader configuration " + id);233}234}235236// finally, register IsoChronology to mark initialization is complete237registerChrono(IsoChronology.INSTANCE);238return true;239}240return false;241}242243//-----------------------------------------------------------------------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 null250* @return the calendar system associated with the locale, not null251* @throws java.time.DateTimeException if the locale-specified calendar cannot be found252*/253static Chronology ofLocale(Locale locale) {254Objects.requireNonNull(locale, "locale");255String type = locale.getUnicodeLocaleType("ca");256if (type == null || "iso".equals(type) || "iso8601".equals(type)) {257return IsoChronology.INSTANCE;258}259// Not pre-defined; lookup by the type260do {261Chronology chrono = CHRONOS_BY_TYPE.get(type);262if (chrono != null) {263return chrono;264}265// If not found, do the initialization (once) and repeat the lookup266} while (initCache());267268// Look for a Chronology using ServiceLoader of the Thread's ContextClassLoader269// Application provided Chronologies must not be cached270@SuppressWarnings("rawtypes")271ServiceLoader<Chronology> loader = ServiceLoader.load(Chronology.class);272for (Chronology chrono : loader) {273if (type.equals(chrono.getCalendarType())) {274return chrono;275}276}277throw new DateTimeException("Unknown calendar system: " + type);278}279280//-----------------------------------------------------------------------281/**282* Obtains an instance of {@code Chronology} from a chronology ID or283* calendar system type.284* <p>285* See {@link Chronology#of(String)}.286*287* @param id the chronology ID or calendar system type, not null288* @return the chronology with the identifier requested, not null289* @throws java.time.DateTimeException if the chronology cannot be found290*/291static Chronology of(String id) {292Objects.requireNonNull(id, "id");293do {294Chronology chrono = of0(id);295if (chrono != null) {296return chrono;297}298// If not found, do the initialization (once) and repeat the lookup299} while (initCache());300301// Look for a Chronology using ServiceLoader of the Thread's ContextClassLoader302// Application provided Chronologies must not be cached303@SuppressWarnings("rawtypes")304ServiceLoader<Chronology> loader = ServiceLoader.load(Chronology.class);305for (Chronology chrono : loader) {306if (id.equals(chrono.getId()) || id.equals(chrono.getCalendarType())) {307return chrono;308}309}310throw new DateTimeException("Unknown chronology: " + id);311}312313/**314* Obtains an instance of {@code Chronology} from a chronology ID or315* calendar system type.316*317* @param id the chronology ID or calendar system type, not null318* @return the chronology with the identifier requested, or {@code null} if not found319*/320private static Chronology of0(String id) {321Chronology chrono = CHRONOS_BY_ID.get(id);322if (chrono == null) {323chrono = CHRONOS_BY_TYPE.get(id);324}325return chrono;326}327328/**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 and333* any chronologies provided by the application via ServiceLoader334* configuration.335*336* @return the independent, modifiable set of the available chronology IDs, not null337*/338static Set<Chronology> getAvailableChronologies() {339initCache(); // force initialization340HashSet<Chronology> chronos = new HashSet<>(CHRONOS_BY_ID.values());341342/// Add in Chronologies from the ServiceLoader configuration343@SuppressWarnings("rawtypes")344ServiceLoader<Chronology> loader = ServiceLoader.load(Chronology.class);345for (Chronology chrono : loader) {346chronos.add(chrono);347}348return chronos;349}350351//-----------------------------------------------------------------------352/**353* Creates an instance.354*/355protected AbstractChronology() {356}357358//-----------------------------------------------------------------------359/**360* Resolves parsed {@code ChronoField} values into a date during parsing.361* <p>362* Most {@code TemporalField} implementations are resolved using the363* resolve method on the field. By contrast, the {@code ChronoField} class364* defines fields that only have meaning relative to the chronology.365* As such, {@code ChronoField} date fields are resolved here in the366* context of a specific chronology.367* <p>368* {@code ChronoField} instances are resolved by this method, which may369* be overridden in subclasses.370* <ul>371* <li>{@code EPOCH_DAY} - If present, this is converted to a date and372* all other date fields are then cross-checked against the date.373* <li>{@code PROLEPTIC_MONTH} - If present, then it is split into the374* {@code YEAR} and {@code MONTH_OF_YEAR}. If the mode is strict or smart375* then the field is validated.376* <li>{@code YEAR_OF_ERA} and {@code ERA} - If both are present, then they377* 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} is379* validated for range in all three modes. If only the {@code YEAR_OF_ERA} is380* present, and the mode is smart or lenient, then the last available era381* is assumed. In strict mode, no era is assumed and the {@code YEAR_OF_ERA} is382* 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 to388* 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 for391* 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 to397* creating a date on the first day of the requested year, then adding398* 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} and401* {@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 to405* creating a date on the first day of the first month in the requested year, then adding406* 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 to408* their outer ranges. The date is then combined in a manner equivalent to409* creating a date on the first day of the requested year and month, then adding410* 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 adjustment412* did not change the month.413* <li>{@code YEAR}, {@code MONTH_OF_YEAR}, {@code ALIGNED_WEEK_OF_MONTH} and414* {@code DAY_OF_WEEK} - If all four are present, then they are combined to415* form a date. The approach is the same as described above for416* 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 once418* 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 to423* creating a date on the first day of the requested year, then adding424* the difference in weeks, then in days.425* If the mode is smart or strict, then the all three fields are validated to426* their outer ranges. The date is then combined in a manner equivalent to427* creating a date on the first day of the requested year, then adding428* 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 adjustment430* 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 in434* {@code ALIGNED_DAY_OF_WEEK_IN_YEAR}. The day-of-week is adjusted as the435* 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-month442* has the value 1, that first day-of-year has the value 1, and that the443* first of the month and year always exists.444*445* @param fieldValues the map of fields to values, which can be updated, not null446* @param resolverStyle the requested type of resolve, not null447* @return the resolved date, null if insufficient information to create a date448* @throws java.time.DateTimeException if the date cannot be resolved, typically449* because of a conflict in the input data450*/451@Override452public ChronoLocalDate resolveDate(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {453// check epoch-day before inventing era454if (fieldValues.containsKey(EPOCH_DAY)) {455return dateEpochDay(fieldValues.remove(EPOCH_DAY));456}457458// fix proleptic month before inventing era459resolveProlepticMonth(fieldValues, resolverStyle);460461// invent era if necessary to resolve year-of-era462ChronoLocalDate resolved = resolveYearOfEra(fieldValues, resolverStyle);463if (resolved != null) {464return resolved;465}466467// build date468if (fieldValues.containsKey(YEAR)) {469if (fieldValues.containsKey(MONTH_OF_YEAR)) {470if (fieldValues.containsKey(DAY_OF_MONTH)) {471return resolveYMD(fieldValues, resolverStyle);472}473if (fieldValues.containsKey(ALIGNED_WEEK_OF_MONTH)) {474if (fieldValues.containsKey(ALIGNED_DAY_OF_WEEK_IN_MONTH)) {475return resolveYMAA(fieldValues, resolverStyle);476}477if (fieldValues.containsKey(DAY_OF_WEEK)) {478return resolveYMAD(fieldValues, resolverStyle);479}480}481}482if (fieldValues.containsKey(DAY_OF_YEAR)) {483return resolveYD(fieldValues, resolverStyle);484}485if (fieldValues.containsKey(ALIGNED_WEEK_OF_YEAR)) {486if (fieldValues.containsKey(ALIGNED_DAY_OF_WEEK_IN_YEAR)) {487return resolveYAA(fieldValues, resolverStyle);488}489if (fieldValues.containsKey(DAY_OF_WEEK)) {490return resolveYAD(fieldValues, resolverStyle);491}492}493}494return null;495}496497void resolveProlepticMonth(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {498Long pMonth = fieldValues.remove(PROLEPTIC_MONTH);499if (pMonth != null) {500if (resolverStyle != ResolverStyle.LENIENT) {501PROLEPTIC_MONTH.checkValidValue(pMonth);502}503// first day-of-month is likely to be safest for setting proleptic-month504// cannot add to year zero, as not all chronologies have a year zero505ChronoLocalDate chronoDate = dateNow()506.with(DAY_OF_MONTH, 1).with(PROLEPTIC_MONTH, pMonth);507addFieldValue(fieldValues, MONTH_OF_YEAR, chronoDate.get(MONTH_OF_YEAR));508addFieldValue(fieldValues, YEAR, chronoDate.get(YEAR));509}510}511512ChronoLocalDate resolveYearOfEra(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {513Long yoeLong = fieldValues.remove(YEAR_OF_ERA);514if (yoeLong != null) {515Long eraLong = fieldValues.remove(ERA);516int yoe;517if (resolverStyle != ResolverStyle.LENIENT) {518yoe = range(YEAR_OF_ERA).checkValidIntValue(yoeLong, YEAR_OF_ERA);519} else {520yoe = Math.toIntExact(yoeLong);521}522if (eraLong != null) {523Era eraObj = eraOf(range(ERA).checkValidIntValue(eraLong, ERA));524addFieldValue(fieldValues, YEAR, prolepticYear(eraObj, yoe));525} else {526if (fieldValues.containsKey(YEAR)) {527int year = range(YEAR).checkValidIntValue(fieldValues.get(YEAR), YEAR);528ChronoLocalDate chronoDate = dateYearDay(year, 1);529addFieldValue(fieldValues, YEAR, prolepticYear(chronoDate.getEra(), yoe));530} else if (resolverStyle == ResolverStyle.STRICT) {531// do not invent era if strict532// reinstate the field removed earlier, no cross-check issues533fieldValues.put(YEAR_OF_ERA, yoeLong);534} else {535List<Era> eras = eras();536if (eras.isEmpty()) {537addFieldValue(fieldValues, YEAR, yoe);538} else {539Era eraObj = eras.get(eras.size() - 1);540addFieldValue(fieldValues, YEAR, prolepticYear(eraObj, yoe));541}542}543}544} else if (fieldValues.containsKey(ERA)) {545range(ERA).checkValidValue(fieldValues.get(ERA), ERA); // always validated546}547return null;548}549550ChronoLocalDate resolveYMD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {551int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);552if (resolverStyle == ResolverStyle.LENIENT) {553long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1);554long days = Math.subtractExact(fieldValues.remove(DAY_OF_MONTH), 1);555return date(y, 1, 1).plus(months, MONTHS).plus(days, DAYS);556}557int moy = range(MONTH_OF_YEAR).checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR), MONTH_OF_YEAR);558ValueRange domRange = range(DAY_OF_MONTH);559int dom = domRange.checkValidIntValue(fieldValues.remove(DAY_OF_MONTH), DAY_OF_MONTH);560if (resolverStyle == ResolverStyle.SMART) { // previous valid561try {562return date(y, moy, dom);563} catch (DateTimeException ex) {564return date(y, moy, 1).with(TemporalAdjusters.lastDayOfMonth());565}566}567return date(y, moy, dom);568}569570ChronoLocalDate resolveYD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {571int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);572if (resolverStyle == ResolverStyle.LENIENT) {573long days = Math.subtractExact(fieldValues.remove(DAY_OF_YEAR), 1);574return dateYearDay(y, 1).plus(days, DAYS);575}576int doy = range(DAY_OF_YEAR).checkValidIntValue(fieldValues.remove(DAY_OF_YEAR), DAY_OF_YEAR);577return dateYearDay(y, doy); // smart is same as strict578}579580ChronoLocalDate resolveYMAA(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {581int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);582if (resolverStyle == ResolverStyle.LENIENT) {583long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1);584long weeks = Math.subtractExact(fieldValues.remove(ALIGNED_WEEK_OF_MONTH), 1);585long days = Math.subtractExact(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_MONTH), 1);586return date(y, 1, 1).plus(months, MONTHS).plus(weeks, WEEKS).plus(days, DAYS);587}588int moy = range(MONTH_OF_YEAR).checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR), MONTH_OF_YEAR);589int aw = range(ALIGNED_WEEK_OF_MONTH).checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_MONTH), ALIGNED_WEEK_OF_MONTH);590int ad = range(ALIGNED_DAY_OF_WEEK_IN_MONTH).checkValidIntValue(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_MONTH), ALIGNED_DAY_OF_WEEK_IN_MONTH);591ChronoLocalDate date = date(y, moy, 1).plus((aw - 1) * 7 + (ad - 1), DAYS);592if (resolverStyle == ResolverStyle.STRICT && date.get(MONTH_OF_YEAR) != moy) {593throw new DateTimeException("Strict mode rejected resolved date as it is in a different month");594}595return date;596}597598ChronoLocalDate resolveYMAD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {599int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);600if (resolverStyle == ResolverStyle.LENIENT) {601long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1);602long weeks = Math.subtractExact(fieldValues.remove(ALIGNED_WEEK_OF_MONTH), 1);603long dow = Math.subtractExact(fieldValues.remove(DAY_OF_WEEK), 1);604return resolveAligned(date(y, 1, 1), months, weeks, dow);605}606int moy = range(MONTH_OF_YEAR).checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR), MONTH_OF_YEAR);607int aw = range(ALIGNED_WEEK_OF_MONTH).checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_MONTH), ALIGNED_WEEK_OF_MONTH);608int dow = range(DAY_OF_WEEK).checkValidIntValue(fieldValues.remove(DAY_OF_WEEK), DAY_OF_WEEK);609ChronoLocalDate date = date(y, moy, 1).plus((aw - 1) * 7, DAYS).with(nextOrSame(DayOfWeek.of(dow)));610if (resolverStyle == ResolverStyle.STRICT && date.get(MONTH_OF_YEAR) != moy) {611throw new DateTimeException("Strict mode rejected resolved date as it is in a different month");612}613return date;614}615616ChronoLocalDate resolveYAA(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {617int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);618if (resolverStyle == ResolverStyle.LENIENT) {619long weeks = Math.subtractExact(fieldValues.remove(ALIGNED_WEEK_OF_YEAR), 1);620long days = Math.subtractExact(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_YEAR), 1);621return dateYearDay(y, 1).plus(weeks, WEEKS).plus(days, DAYS);622}623int aw = range(ALIGNED_WEEK_OF_YEAR).checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_YEAR), ALIGNED_WEEK_OF_YEAR);624int ad = range(ALIGNED_DAY_OF_WEEK_IN_YEAR).checkValidIntValue(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_YEAR), ALIGNED_DAY_OF_WEEK_IN_YEAR);625ChronoLocalDate date = dateYearDay(y, 1).plus((aw - 1) * 7 + (ad - 1), DAYS);626if (resolverStyle == ResolverStyle.STRICT && date.get(YEAR) != y) {627throw new DateTimeException("Strict mode rejected resolved date as it is in a different year");628}629return date;630}631632ChronoLocalDate resolveYAD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {633int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);634if (resolverStyle == ResolverStyle.LENIENT) {635long weeks = Math.subtractExact(fieldValues.remove(ALIGNED_WEEK_OF_YEAR), 1);636long dow = Math.subtractExact(fieldValues.remove(DAY_OF_WEEK), 1);637return resolveAligned(dateYearDay(y, 1), 0, weeks, dow);638}639int aw = range(ALIGNED_WEEK_OF_YEAR).checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_YEAR), ALIGNED_WEEK_OF_YEAR);640int dow = range(DAY_OF_WEEK).checkValidIntValue(fieldValues.remove(DAY_OF_WEEK), DAY_OF_WEEK);641ChronoLocalDate date = dateYearDay(y, 1).plus((aw - 1) * 7, DAYS).with(nextOrSame(DayOfWeek.of(dow)));642if (resolverStyle == ResolverStyle.STRICT && date.get(YEAR) != y) {643throw new DateTimeException("Strict mode rejected resolved date as it is in a different year");644}645return date;646}647648ChronoLocalDate resolveAligned(ChronoLocalDate base, long months, long weeks, long dow) {649ChronoLocalDate date = base.plus(months, MONTHS).plus(weeks, WEEKS);650if (dow > 7) {651date = date.plus((dow - 1) / 7, WEEKS);652dow = ((dow - 1) % 7) + 1;653} else if (dow < 1) {654date = date.plus(Math.subtractExact(dow, 7) / 7, WEEKS);655dow = ((dow + 6) % 7) + 1;656}657return date.with(nextOrSame(DayOfWeek.of((int) dow)));658}659660/**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, then666* an exception is thrown.667*668* @param field the field to add, not null669* @param value the value to add, not null670* @throws java.time.DateTimeException if the field is already present with a different value671*/672void addFieldValue(Map<TemporalField, Long> fieldValues, ChronoField field, long value) {673Long old = fieldValues.get(field); // check first for better error message674if (old != null && old.longValue() != value) {675throw new DateTimeException("Conflict found: " + field + " " + old + " differs from " + field + " " + value);676}677fieldValues.put(field, value);678}679680//-----------------------------------------------------------------------681/**682* Compares this chronology to another chronology.683* <p>684* The comparison order first by the chronology ID string, then by any685* additional information specific to the subclass.686* It is "consistent with equals", as defined by {@link Comparable}.687*688* @implSpec689* 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 null693* @return the comparator value, negative if less, positive if greater694*/695@Override696public int compareTo(Chronology other) {697return getId().compareTo(other.getId());698}699700/**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* @implSpec706* This implementation checks the type and calls707* {@link #compareTo(java.time.chrono.Chronology)}.708*709* @param obj the object to check, null returns false710* @return true if this is equal to the other chronology711*/712@Override713public boolean equals(Object obj) {714if (this == obj) {715return true;716}717if (obj instanceof AbstractChronology) {718return compareTo((AbstractChronology) obj) == 0;719}720return false;721}722723/**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* @implSpec729* 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 code733*/734@Override735public int hashCode() {736return getClass().hashCode() ^ getId().hashCode();737}738739//-----------------------------------------------------------------------740/**741* Outputs this chronology as a {@code String}, using the chronology ID.742*743* @return a string representation of this chronology, not null744*/745@Override746public String toString() {747return getId();748}749750//-----------------------------------------------------------------------751/**752* Writes the Chronology using a753* <a href="../../../serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.754* <pre>755* out.writeByte(1); // identifies this as a Chronology756* out.writeUTF(getId());757* </pre>758*759* @return the instance of {@code Ser}, not null760*/761Object writeReplace() {762return new Ser(Ser.CHRONO_TYPE, this);763}764765/**766* Defend against malicious streams.767*768* @param s the stream to read769* @throws java.io.InvalidObjectException always770*/771private void readObject(ObjectInputStream s) throws ObjectStreamException {772throw new InvalidObjectException("Deserialization via serialization delegate");773}774775void writeExternal(DataOutput out) throws IOException {776out.writeUTF(getId());777}778779static Chronology readExternal(DataInput in) throws IOException {780String id = in.readUTF();781return Chronology.of(id);782}783784}785786787