Path: blob/master/src/java.base/share/classes/java/time/chrono/ThaiBuddhistChronology.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.HashMap;75import java.util.List;76import java.util.Locale;77import java.util.Map;7879/**80* The Thai Buddhist calendar system.81* <p>82* This chronology defines the rules of the Thai Buddhist calendar system.83* This calendar system is primarily used in Thailand.84* Dates are aligned such that {@code 2484-01-01 (Buddhist)} is {@code 1941-01-01 (ISO)}.85* <p>86* The fields are defined as follows:87* <ul>88* <li>era - There are two eras, the current 'Buddhist' (ERA_BE) and the previous era (ERA_BEFORE_BE).89* <li>year-of-era - The year-of-era for the current era increases uniformly from the epoch at year one.90* For the previous era the year increases from one as time goes backwards.91* The value for the current era is equal to the ISO proleptic-year plus 543.92* <li>proleptic-year - The proleptic year is the same as the year-of-era for the93* current era. For the previous era, years have zero, then negative values.94* The value is equal to the ISO proleptic-year plus 543.95* <li>month-of-year - The ThaiBuddhist month-of-year exactly matches ISO.96* <li>day-of-month - The ThaiBuddhist day-of-month exactly matches ISO.97* <li>day-of-year - The ThaiBuddhist day-of-year exactly matches ISO.98* <li>leap-year - The ThaiBuddhist leap-year pattern exactly matches ISO, such that the two calendars99* are never out of step.100* </ul>101*102* @implSpec103* This class is immutable and thread-safe.104*105* @since 1.8106*/107public final class ThaiBuddhistChronology extends AbstractChronology implements Serializable {108109/**110* Singleton instance of the Buddhist chronology.111*/112public static final ThaiBuddhistChronology INSTANCE = new ThaiBuddhistChronology();113114/**115* Serialization version.116*/117@java.io.Serial118private static final long serialVersionUID = 2775954514031616474L;119/**120* Containing the offset to add to the ISO year.121*/122static final int YEARS_DIFFERENCE = 543;123/**124* Narrow names for eras.125*/126private static final HashMap<String, String[]> ERA_NARROW_NAMES = new HashMap<>();127/**128* Short names for eras.129*/130private static final HashMap<String, String[]> ERA_SHORT_NAMES = new HashMap<>();131/**132* Full names for eras.133*/134private static final HashMap<String, String[]> ERA_FULL_NAMES = new HashMap<>();135/**136* Fallback language for the era names.137*/138private static final String FALLBACK_LANGUAGE = "en";139/**140* Language that has the era names.141*/142private static final String TARGET_LANGUAGE = "th";143/**144* Name data.145*/146static {147ERA_NARROW_NAMES.put(FALLBACK_LANGUAGE, new String[]{"BB", "BE"});148ERA_NARROW_NAMES.put(TARGET_LANGUAGE, new String[]{"BB", "BE"});149ERA_SHORT_NAMES.put(FALLBACK_LANGUAGE, new String[]{"B.B.", "B.E."});150ERA_SHORT_NAMES.put(TARGET_LANGUAGE,151new String[]{"\u0e1e.\u0e28.",152"\u0e1b\u0e35\u0e01\u0e48\u0e2d\u0e19\u0e04\u0e23\u0e34\u0e2a\u0e15\u0e4c\u0e01\u0e32\u0e25\u0e17\u0e35\u0e48"});153ERA_FULL_NAMES.put(FALLBACK_LANGUAGE, new String[]{"Before Buddhist", "Budhhist Era"});154ERA_FULL_NAMES.put(TARGET_LANGUAGE,155new String[]{"\u0e1e\u0e38\u0e17\u0e18\u0e28\u0e31\u0e01\u0e23\u0e32\u0e0a",156"\u0e1b\u0e35\u0e01\u0e48\u0e2d\u0e19\u0e04\u0e23\u0e34\u0e2a\u0e15\u0e4c\u0e01\u0e32\u0e25\u0e17\u0e35\u0e48"});157}158159/**160* Restricted constructor.161*/162private ThaiBuddhistChronology() {163}164165//-----------------------------------------------------------------------166/**167* Gets the ID of the chronology - 'ThaiBuddhist'.168* <p>169* The ID uniquely identifies the {@code Chronology}.170* It can be used to lookup the {@code Chronology} using {@link Chronology#of(String)}.171*172* @return the chronology ID - 'ThaiBuddhist'173* @see #getCalendarType()174*/175@Override176public String getId() {177return "ThaiBuddhist";178}179180/**181* Gets the calendar type of the underlying calendar system - 'buddhist'.182* <p>183* The calendar type is an identifier defined by the184* <em>Unicode Locale Data Markup Language (LDML)</em> specification.185* It can be used to lookup the {@code Chronology} using {@link Chronology#of(String)}.186* It can also be used as part of a locale, accessible via187* {@link Locale#getUnicodeLocaleType(String)} with the key 'ca'.188*189* @return the calendar system type - 'buddhist'190* @see #getId()191*/192@Override193public String getCalendarType() {194return "buddhist";195}196197//-----------------------------------------------------------------------198/**199* Obtains a local date in Thai Buddhist calendar system from the200* era, year-of-era, month-of-year and day-of-month fields.201*202* @param era the Thai Buddhist era, not null203* @param yearOfEra the year-of-era204* @param month the month-of-year205* @param dayOfMonth the day-of-month206* @return the Thai Buddhist local date, not null207* @throws DateTimeException if unable to create the date208* @throws ClassCastException if the {@code era} is not a {@code ThaiBuddhistEra}209*/210@Override211public ThaiBuddhistDate date(Era era, int yearOfEra, int month, int dayOfMonth) {212return date(prolepticYear(era, yearOfEra), month, dayOfMonth);213}214215/**216* Obtains a local date in Thai Buddhist calendar system from the217* proleptic-year, month-of-year and day-of-month fields.218*219* @param prolepticYear the proleptic-year220* @param month the month-of-year221* @param dayOfMonth the day-of-month222* @return the Thai Buddhist local date, not null223* @throws DateTimeException if unable to create the date224*/225@Override226public ThaiBuddhistDate date(int prolepticYear, int month, int dayOfMonth) {227return new ThaiBuddhistDate(LocalDate.of(prolepticYear - YEARS_DIFFERENCE, month, dayOfMonth));228}229230/**231* Obtains a local date in Thai Buddhist calendar system from the232* era, year-of-era and day-of-year fields.233*234* @param era the Thai Buddhist era, not null235* @param yearOfEra the year-of-era236* @param dayOfYear the day-of-year237* @return the Thai Buddhist local date, not null238* @throws DateTimeException if unable to create the date239* @throws ClassCastException if the {@code era} is not a {@code ThaiBuddhistEra}240*/241@Override242public ThaiBuddhistDate dateYearDay(Era era, int yearOfEra, int dayOfYear) {243return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear);244}245246/**247* Obtains a local date in Thai Buddhist calendar system from the248* proleptic-year and day-of-year fields.249*250* @param prolepticYear the proleptic-year251* @param dayOfYear the day-of-year252* @return the Thai Buddhist local date, not null253* @throws DateTimeException if unable to create the date254*/255@Override256public ThaiBuddhistDate dateYearDay(int prolepticYear, int dayOfYear) {257return new ThaiBuddhistDate(LocalDate.ofYearDay(prolepticYear - YEARS_DIFFERENCE, dayOfYear));258}259260/**261* Obtains a local date in the Thai Buddhist calendar system from the epoch-day.262*263* @param epochDay the epoch day264* @return the Thai Buddhist local date, not null265* @throws DateTimeException if unable to create the date266*/267@Override // override with covariant return type268public ThaiBuddhistDate dateEpochDay(long epochDay) {269return new ThaiBuddhistDate(LocalDate.ofEpochDay(epochDay));270}271272@Override273public ThaiBuddhistDate dateNow() {274return dateNow(Clock.systemDefaultZone());275}276277@Override278public ThaiBuddhistDate dateNow(ZoneId zone) {279return dateNow(Clock.system(zone));280}281282@Override283public ThaiBuddhistDate dateNow(Clock clock) {284return date(LocalDate.now(clock));285}286287@Override288public ThaiBuddhistDate date(TemporalAccessor temporal) {289if (temporal instanceof ThaiBuddhistDate) {290return (ThaiBuddhistDate) temporal;291}292return new ThaiBuddhistDate(LocalDate.from(temporal));293}294295@Override296@SuppressWarnings("unchecked")297public ChronoLocalDateTime<ThaiBuddhistDate> localDateTime(TemporalAccessor temporal) {298return (ChronoLocalDateTime<ThaiBuddhistDate>)super.localDateTime(temporal);299}300301@Override302@SuppressWarnings("unchecked")303public ChronoZonedDateTime<ThaiBuddhistDate> zonedDateTime(TemporalAccessor temporal) {304return (ChronoZonedDateTime<ThaiBuddhistDate>)super.zonedDateTime(temporal);305}306307@Override308@SuppressWarnings("unchecked")309public ChronoZonedDateTime<ThaiBuddhistDate> zonedDateTime(Instant instant, ZoneId zone) {310return (ChronoZonedDateTime<ThaiBuddhistDate>)super.zonedDateTime(instant, zone);311}312313//-----------------------------------------------------------------------314/**315* Checks if the specified year is a leap year.316* <p>317* Thai Buddhist leap years occur exactly in line with ISO leap years.318* This method does not validate the year passed in, and only has a319* well-defined result for years in the supported range.320*321* @param prolepticYear the proleptic-year to check, not validated for range322* @return true if the year is a leap year323*/324@Override325public boolean isLeapYear(long prolepticYear) {326return IsoChronology.INSTANCE.isLeapYear(prolepticYear - YEARS_DIFFERENCE);327}328329@Override330public int prolepticYear(Era era, int yearOfEra) {331if (!(era instanceof ThaiBuddhistEra)) {332throw new ClassCastException("Era must be BuddhistEra");333}334return (era == ThaiBuddhistEra.BE ? yearOfEra : 1 - yearOfEra);335}336337@Override338public ThaiBuddhistEra eraOf(int eraValue) {339return ThaiBuddhistEra.of(eraValue);340}341342@Override343public List<Era> eras() {344return List.of(ThaiBuddhistEra.values());345}346347//-----------------------------------------------------------------------348@Override349public ValueRange range(ChronoField field) {350switch (field) {351case PROLEPTIC_MONTH: {352ValueRange range = PROLEPTIC_MONTH.range();353return ValueRange.of(range.getMinimum() + YEARS_DIFFERENCE * 12L, range.getMaximum() + YEARS_DIFFERENCE * 12L);354}355case YEAR_OF_ERA: {356ValueRange range = YEAR.range();357return ValueRange.of(1, -(range.getMinimum() + YEARS_DIFFERENCE) + 1, range.getMaximum() + YEARS_DIFFERENCE);358}359case YEAR: {360ValueRange range = YEAR.range();361return ValueRange.of(range.getMinimum() + YEARS_DIFFERENCE, range.getMaximum() + YEARS_DIFFERENCE);362}363}364return field.range();365}366367//-----------------------------------------------------------------------368@Override // override for return type369public ThaiBuddhistDate resolveDate(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {370return (ThaiBuddhistDate) super.resolveDate(fieldValues, resolverStyle);371}372373//-----------------------------------------------------------------------374/**375* Writes the Chronology using a376* <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.377* @serialData378* <pre>379* out.writeByte(1); // identifies a Chronology380* out.writeUTF(getId());381* </pre>382*383* @return the instance of {@code Ser}, not null384*/385@Override386@java.io.Serial387Object writeReplace() {388return super.writeReplace();389}390391/**392* Defend against malicious streams.393*394* @param s the stream to read395* @throws InvalidObjectException always396*/397@java.io.Serial398private void readObject(ObjectInputStream s) throws InvalidObjectException {399throw new InvalidObjectException("Deserialization via serialization delegate");400}401}402403404