Path: blob/master/src/java.base/share/classes/sun/util/BuddhistCalendar.java
41152 views
/*1* Copyright (c) 2000, 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*/2425package sun.util;2627import java.io.IOException;28import java.io.ObjectInputStream;29import java.util.GregorianCalendar;30import java.util.Locale;31import java.util.Map;32import java.util.TimeZone;33import sun.util.locale.provider.CalendarDataUtility;3435public class BuddhistCalendar extends GregorianCalendar {3637//////////////////38// Class Variables39//////////////////4041@java.io.Serial42private static final long serialVersionUID = -8527488697350388578L;4344private static final int BUDDHIST_YEAR_OFFSET = 543;4546///////////////47// Constructors48///////////////4950/**51* Constructs a default BuddhistCalendar using the current time52* in the default time zone with the default locale.53*/54public BuddhistCalendar() {55super();56}5758/**59* Constructs a BuddhistCalendar based on the current time60* in the given time zone with the default locale.61* @param zone the given time zone.62*/63public BuddhistCalendar(TimeZone zone) {64super(zone);65}6667/**68* Constructs a BuddhistCalendar based on the current time69* in the default time zone with the given locale.70* @param aLocale the given locale.71*/72public BuddhistCalendar(Locale aLocale) {73super(aLocale);74}7576/**77* Constructs a BuddhistCalendar based on the current time78* in the given time zone with the given locale.79* @param zone the given time zone.80* @param aLocale the given locale.81*/82public BuddhistCalendar(TimeZone zone, Locale aLocale) {83super(zone, aLocale);84}8586/////////////////87// Public methods88/////////////////8990/**91* Returns {@code "buddhist"} as the calendar type of this Calendar.92*/93@Override94public String getCalendarType() {95return "buddhist";96}9798/**99* Compares this BuddhistCalendar to an object reference.100* @param obj the object reference with which to compare101* @return true if this object is equal to <code>obj</code>; false otherwise102*/103@Override104public boolean equals(Object obj) {105return obj instanceof BuddhistCalendar106&& super.equals(obj);107}108109/**110* Override hashCode.111* Generates the hash code for the BuddhistCalendar object112*/113@Override114public int hashCode() {115return super.hashCode() ^ BUDDHIST_YEAR_OFFSET;116}117118/**119* Gets the value for a given time field.120* @param field the given time field.121* @return the value for the given time field.122*/123@Override124public int get(int field)125{126if (field == YEAR) {127return super.get(field) + yearOffset;128}129return super.get(field);130}131132/**133* Sets the time field with the given value.134* @param field the given time field.135* @param value the value to be set for the given time field.136*/137@Override138public void set(int field, int value)139{140if (field == YEAR) {141super.set(field, value - yearOffset);142} else {143super.set(field, value);144}145}146147/**148* Adds the specified (signed) amount of time to the given time field.149* @param field the time field.150* @param amount the amount of date or time to be added to the field.151*/152@Override153public void add(int field, int amount)154{155int savedYearOffset = yearOffset;156// To let the superclass calculate date-time values correctly,157// temporarily make this GregorianCalendar.158yearOffset = 0;159try {160super.add(field, amount);161} finally {162yearOffset = savedYearOffset;163}164}165166/**167* Add to field a signed amount without changing larger fields.168* A negative roll amount means to subtract from field without changing169* larger fields.170* @param field the time field.171* @param amount the signed amount to add to <code>field</code>.172*/173@Override174public void roll(int field, int amount)175{176int savedYearOffset = yearOffset;177// To let the superclass calculate date-time values correctly,178// temporarily make this GregorianCalendar.179yearOffset = 0;180try {181super.roll(field, amount);182} finally {183yearOffset = savedYearOffset;184}185}186187@Override188public String getDisplayName(int field, int style, Locale locale) {189if (field != ERA) {190return super.getDisplayName(field, style, locale);191}192193return CalendarDataUtility.retrieveFieldValueName("buddhist", field, get(field), style, locale);194}195196@Override197public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) {198if (field != ERA) {199return super.getDisplayNames(field, style, locale);200}201return CalendarDataUtility.retrieveFieldValueNames("buddhist", field, style, locale);202}203204/**205* Returns the maximum value that this field could have, given the206* current date. For example, with the date "Feb 3, 2540" and the207* <code>DAY_OF_MONTH</code> field, the actual maximum is 28; for208* "Feb 3, 2539" it is 29.209*210* @param field the field to determine the maximum of211* @return the maximum of the given field for the current date of this Calendar212*/213@Override214public int getActualMaximum(int field) {215int savedYearOffset = yearOffset;216// To let the superclass calculate date-time values correctly,217// temporarily make this GregorianCalendar.218yearOffset = 0;219try {220return super.getActualMaximum(field);221} finally {222yearOffset = savedYearOffset;223}224}225226@Override227@SuppressWarnings("empty-statement")228public String toString() {229// The super class produces a String with the Gregorian year230// value (or '?')231String s = super.toString();232// If the YEAR field is UNSET, then return the Gregorian string.233if (!isSet(YEAR)) {234return s;235}236237final String yearField = "YEAR=";238int p = s.indexOf(yearField);239// If the string doesn't include the year value for some240// reason, then return the Gregorian string.241if (p == -1) {242return s;243}244p += yearField.length();245StringBuilder sb = new StringBuilder(s.length() + 10);246sb.append(s, 0, p);247// Skip the year number248while (Character.isDigit(s.charAt(p++)))249;250int year = internalGet(YEAR) + BUDDHIST_YEAR_OFFSET;251sb.append(year).append(s, p - 1, s.length());252return sb.toString();253}254255private transient int yearOffset = BUDDHIST_YEAR_OFFSET;256257@java.io.Serial258private void readObject(ObjectInputStream stream)259throws IOException, ClassNotFoundException {260stream.defaultReadObject();261yearOffset = BUDDHIST_YEAR_OFFSET;262}263}264265266