Path: blob/master/src/java.base/share/classes/java/time/chrono/MinguoChronology.java
41159 views
/*1* Copyright (c) 2012, 2019, 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* Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos27*28* All rights reserved.29*30* Redistribution and use in source and binary forms, with or without31* modification, are permitted provided that the following conditions are met:32*33* * Redistributions of source code must retain the above copyright notice,34* this list of conditions and the following disclaimer.35*36* * Redistributions in binary form must reproduce the above copyright notice,37* this list of conditions and the following disclaimer in the documentation38* and/or other materials provided with the distribution.39*40* * Neither the name of JSR-310 nor the names of its contributors41* may be used to endorse or promote products derived from this software42* without specific prior written permission.43*44* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS45* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT46* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR47* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR48* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,49* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,50* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR51* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF52* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING53* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS54* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.55*/56package java.time.chrono;5758import java.io.InvalidObjectException;59import static java.time.temporal.ChronoField.PROLEPTIC_MONTH;60import static java.time.temporal.ChronoField.YEAR;6162import java.io.ObjectInputStream;63import java.io.Serializable;64import java.time.Clock;65import java.time.DateTimeException;66import java.time.Instant;67import java.time.LocalDate;68import java.time.ZoneId;69import java.time.format.ResolverStyle;70import java.time.temporal.ChronoField;71import java.time.temporal.TemporalAccessor;72import java.time.temporal.TemporalField;73import java.time.temporal.ValueRange;74import java.util.List;75import java.util.Locale;76import java.util.Map;7778/**79* The Minguo calendar system.80* <p>81* This chronology defines the rules of the Minguo calendar system.82* This calendar system is primarily used in the Republic of China, often known as Taiwan.83* Dates are aligned such that {@code 0001-01-01 (Minguo)} is {@code 1912-01-01 (ISO)}.84* <p>85* The fields are defined as follows:86* <ul>87* <li>era - There are two eras, the current 'Republic' (ERA_ROC) and the previous era (ERA_BEFORE_ROC).88* <li>year-of-era - The year-of-era for the current era increases uniformly from the epoch at year one.89* For the previous era the year increases from one as time goes backwards.90* The value for the current era is equal to the ISO proleptic-year minus 1911.91* <li>proleptic-year - The proleptic year is the same as the year-of-era for the92* current era. For the previous era, years have zero, then negative values.93* The value is equal to the ISO proleptic-year minus 1911.94* <li>month-of-year - The Minguo month-of-year exactly matches ISO.95* <li>day-of-month - The Minguo day-of-month exactly matches ISO.96* <li>day-of-year - The Minguo day-of-year exactly matches ISO.97* <li>leap-year - The Minguo leap-year pattern exactly matches ISO, such that the two calendars98* are never out of step.99* </ul>100*101* @implSpec102* This class is immutable and thread-safe.103*104* @since 1.8105*/106public final class MinguoChronology extends AbstractChronology implements Serializable {107108/**109* Singleton instance for the Minguo chronology.110*/111public static final MinguoChronology INSTANCE = new MinguoChronology();112113/**114* Serialization version.115*/116@java.io.Serial117private static final long serialVersionUID = 1039765215346859963L;118/**119* The difference in years between ISO and Minguo.120*/121static final int YEARS_DIFFERENCE = 1911;122123/**124* Restricted constructor.125*/126private MinguoChronology() {127}128129//-----------------------------------------------------------------------130/**131* Gets the ID of the chronology - 'Minguo'.132* <p>133* The ID uniquely identifies the {@code Chronology}.134* It can be used to lookup the {@code Chronology} using {@link Chronology#of(String)}.135*136* @return the chronology ID - 'Minguo'137* @see #getCalendarType()138*/139@Override140public String getId() {141return "Minguo";142}143144/**145* Gets the calendar type of the underlying calendar system - 'roc'.146* <p>147* The calendar type is an identifier defined by the148* <em>Unicode Locale Data Markup Language (LDML)</em> specification.149* It can be used to lookup the {@code Chronology} using {@link Chronology#of(String)}.150* It can also be used as part of a locale, accessible via151* {@link Locale#getUnicodeLocaleType(String)} with the key 'ca'.152*153* @return the calendar system type - 'roc'154* @see #getId()155*/156@Override157public String getCalendarType() {158return "roc";159}160161//-----------------------------------------------------------------------162/**163* Obtains a local date in Minguo calendar system from the164* era, year-of-era, month-of-year and day-of-month fields.165*166* @param era the Minguo era, not null167* @param yearOfEra the year-of-era168* @param month the month-of-year169* @param dayOfMonth the day-of-month170* @return the Minguo local date, not null171* @throws DateTimeException if unable to create the date172* @throws ClassCastException if the {@code era} is not a {@code MinguoEra}173*/174@Override175public MinguoDate date(Era era, int yearOfEra, int month, int dayOfMonth) {176return date(prolepticYear(era, yearOfEra), month, dayOfMonth);177}178179/**180* Obtains a local date in Minguo calendar system from the181* proleptic-year, month-of-year and day-of-month fields.182*183* @param prolepticYear the proleptic-year184* @param month the month-of-year185* @param dayOfMonth the day-of-month186* @return the Minguo local date, not null187* @throws DateTimeException if unable to create the date188*/189@Override190public MinguoDate date(int prolepticYear, int month, int dayOfMonth) {191return new MinguoDate(LocalDate.of(prolepticYear + YEARS_DIFFERENCE, month, dayOfMonth));192}193194/**195* Obtains a local date in Minguo calendar system from the196* era, year-of-era and day-of-year fields.197*198* @param era the Minguo era, not null199* @param yearOfEra the year-of-era200* @param dayOfYear the day-of-year201* @return the Minguo local date, not null202* @throws DateTimeException if unable to create the date203* @throws ClassCastException if the {@code era} is not a {@code MinguoEra}204*/205@Override206public MinguoDate dateYearDay(Era era, int yearOfEra, int dayOfYear) {207return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear);208}209210/**211* Obtains a local date in Minguo calendar system from the212* proleptic-year and day-of-year fields.213*214* @param prolepticYear the proleptic-year215* @param dayOfYear the day-of-year216* @return the Minguo local date, not null217* @throws DateTimeException if unable to create the date218*/219@Override220public MinguoDate dateYearDay(int prolepticYear, int dayOfYear) {221return new MinguoDate(LocalDate.ofYearDay(prolepticYear + YEARS_DIFFERENCE, dayOfYear));222}223224/**225* Obtains a local date in the Minguo calendar system from the epoch-day.226*227* @param epochDay the epoch day228* @return the Minguo local date, not null229* @throws DateTimeException if unable to create the date230*/231@Override // override with covariant return type232public MinguoDate dateEpochDay(long epochDay) {233return new MinguoDate(LocalDate.ofEpochDay(epochDay));234}235236@Override237public MinguoDate dateNow() {238return dateNow(Clock.systemDefaultZone());239}240241@Override242public MinguoDate dateNow(ZoneId zone) {243return dateNow(Clock.system(zone));244}245246@Override247public MinguoDate dateNow(Clock clock) {248return date(LocalDate.now(clock));249}250251@Override252public MinguoDate date(TemporalAccessor temporal) {253if (temporal instanceof MinguoDate) {254return (MinguoDate) temporal;255}256return new MinguoDate(LocalDate.from(temporal));257}258259@Override260@SuppressWarnings("unchecked")261public ChronoLocalDateTime<MinguoDate> localDateTime(TemporalAccessor temporal) {262return (ChronoLocalDateTime<MinguoDate>)super.localDateTime(temporal);263}264265@Override266@SuppressWarnings("unchecked")267public ChronoZonedDateTime<MinguoDate> zonedDateTime(TemporalAccessor temporal) {268return (ChronoZonedDateTime<MinguoDate>)super.zonedDateTime(temporal);269}270271@Override272@SuppressWarnings("unchecked")273public ChronoZonedDateTime<MinguoDate> zonedDateTime(Instant instant, ZoneId zone) {274return (ChronoZonedDateTime<MinguoDate>)super.zonedDateTime(instant, zone);275}276277//-----------------------------------------------------------------------278/**279* Checks if the specified year is a leap year.280* <p>281* Minguo leap years occur exactly in line with ISO leap years.282* This method does not validate the year passed in, and only has a283* well-defined result for years in the supported range.284*285* @param prolepticYear the proleptic-year to check, not validated for range286* @return true if the year is a leap year287*/288@Override289public boolean isLeapYear(long prolepticYear) {290return IsoChronology.INSTANCE.isLeapYear(prolepticYear + YEARS_DIFFERENCE);291}292293@Override294public int prolepticYear(Era era, int yearOfEra) {295if (!(era instanceof MinguoEra)) {296throw new ClassCastException("Era must be MinguoEra");297}298return (era == MinguoEra.ROC ? yearOfEra : 1 - yearOfEra);299}300301@Override302public MinguoEra eraOf(int eraValue) {303return MinguoEra.of(eraValue);304}305306@Override307public List<Era> eras() {308return List.of(MinguoEra.values());309}310311//-----------------------------------------------------------------------312@Override313public ValueRange range(ChronoField field) {314switch (field) {315case PROLEPTIC_MONTH: {316ValueRange range = PROLEPTIC_MONTH.range();317return ValueRange.of(range.getMinimum() - YEARS_DIFFERENCE * 12L, range.getMaximum() - YEARS_DIFFERENCE * 12L);318}319case YEAR_OF_ERA: {320ValueRange range = YEAR.range();321return ValueRange.of(1, range.getMaximum() - YEARS_DIFFERENCE, -range.getMinimum() + 1 + YEARS_DIFFERENCE);322}323case YEAR: {324ValueRange range = YEAR.range();325return ValueRange.of(range.getMinimum() - YEARS_DIFFERENCE, range.getMaximum() - YEARS_DIFFERENCE);326}327}328return field.range();329}330331//-----------------------------------------------------------------------332@Override // override for return type333public MinguoDate resolveDate(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {334return (MinguoDate) super.resolveDate(fieldValues, resolverStyle);335}336337//-----------------------------------------------------------------------338/**339* Writes the Chronology using a340* <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.341* @serialData342* <pre>343* out.writeByte(1); // identifies a Chronology344* out.writeUTF(getId());345* </pre>346*347* @return the instance of {@code Ser}, not null348*/349@Override350@java.io.Serial351Object writeReplace() {352return super.writeReplace();353}354355/**356* Defend against malicious streams.357*358* @param s the stream to read359* @throws InvalidObjectException always360*/361@java.io.Serial362private void readObject(ObjectInputStream s) throws InvalidObjectException {363throw new InvalidObjectException("Deserialization via serialization delegate");364}365}366367368