Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/util/BuddhistCalendar.java
38829 views
/*1* Copyright (c) 2000, 2012, 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//////////////////4041private static final long serialVersionUID = -8527488697350388578L;4243private static final int BUDDHIST_YEAR_OFFSET = 543;4445///////////////46// Constructors47///////////////4849/**50* Constructs a default BuddhistCalendar using the current time51* in the default time zone with the default locale.52*/53public BuddhistCalendar() {54super();55}5657/**58* Constructs a BuddhistCalendar based on the current time59* in the given time zone with the default locale.60* @param zone the given time zone.61*/62public BuddhistCalendar(TimeZone zone) {63super(zone);64}6566/**67* Constructs a BuddhistCalendar based on the current time68* in the default time zone with the given locale.69* @param aLocale the given locale.70*/71public BuddhistCalendar(Locale aLocale) {72super(aLocale);73}7475/**76* Constructs a BuddhistCalendar based on the current time77* in the given time zone with the given locale.78* @param zone the given time zone.79* @param aLocale the given locale.80*/81public BuddhistCalendar(TimeZone zone, Locale aLocale) {82super(zone, aLocale);83}8485/////////////////86// Public methods87/////////////////8889/**90* Returns {@code "buddhist"} as the calendar type of this Calendar.91*/92@Override93public String getCalendarType() {94return "buddhist";95}9697/**98* Compares this BuddhistCalendar to an object reference.99* @param obj the object reference with which to compare100* @return true if this object is equal to <code>obj</code>; false otherwise101*/102@Override103public boolean equals(Object obj) {104return obj instanceof BuddhistCalendar105&& super.equals(obj);106}107108/**109* Override hashCode.110* Generates the hash code for the BuddhistCalendar object111*/112@Override113public int hashCode() {114return super.hashCode() ^ BUDDHIST_YEAR_OFFSET;115}116117/**118* Gets the value for a given time field.119* @param field the given time field.120* @return the value for the given time field.121*/122@Override123public int get(int field)124{125if (field == YEAR) {126return super.get(field) + yearOffset;127}128return super.get(field);129}130131/**132* Sets the time field with the given value.133* @param field the given time field.134* @param value the value to be set for the given time field.135*/136@Override137public void set(int field, int value)138{139if (field == YEAR) {140super.set(field, value - yearOffset);141} else {142super.set(field, value);143}144}145146/**147* Adds the specified (signed) amount of time to the given time field.148* @param field the time field.149* @param amount the amount of date or time to be added to the field.150*/151@Override152public void add(int field, int amount)153{154int savedYearOffset = yearOffset;155// To let the superclass calculate date-time values correctly,156// temporarily make this GregorianCalendar.157yearOffset = 0;158try {159super.add(field, amount);160} finally {161yearOffset = savedYearOffset;162}163}164165/**166* Add to field a signed amount without changing larger fields.167* A negative roll amount means to subtract from field without changing168* larger fields.169* @param field the time field.170* @param amount the signed amount to add to <code>field</code>.171*/172@Override173public void roll(int field, int amount)174{175int savedYearOffset = yearOffset;176// To let the superclass calculate date-time values correctly,177// temporarily make this GregorianCalendar.178yearOffset = 0;179try {180super.roll(field, amount);181} finally {182yearOffset = savedYearOffset;183}184}185186@Override187public String getDisplayName(int field, int style, Locale locale) {188if (field != ERA) {189return super.getDisplayName(field, style, locale);190}191192return CalendarDataUtility.retrieveFieldValueName("buddhist", field, get(field), style, locale);193}194195@Override196public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) {197if (field != ERA) {198return super.getDisplayNames(field, style, locale);199}200return CalendarDataUtility.retrieveFieldValueNames("buddhist", field, style, locale);201}202203/**204* Returns the maximum value that this field could have, given the205* current date. For example, with the date "Feb 3, 2540" and the206* <code>DAY_OF_MONTH</code> field, the actual maximum is 28; for207* "Feb 3, 2539" it is 29.208*209* @param field the field to determine the maximum of210* @return the maximum of the given field for the current date of this Calendar211*/212@Override213public int getActualMaximum(int field) {214int savedYearOffset = yearOffset;215// To let the superclass calculate date-time values correctly,216// temporarily make this GregorianCalendar.217yearOffset = 0;218try {219return super.getActualMaximum(field);220} finally {221yearOffset = savedYearOffset;222}223}224225@Override226@SuppressWarnings("empty-statement")227public String toString() {228// The super class produces a String with the Gregorian year229// value (or '?')230String s = super.toString();231// If the YEAR field is UNSET, then return the Gregorian string.232if (!isSet(YEAR)) {233return s;234}235236final String yearField = "YEAR=";237int p = s.indexOf(yearField);238// If the string doesn't include the year value for some239// reason, then return the Gregorian string.240if (p == -1) {241return s;242}243p += yearField.length();244StringBuilder sb = new StringBuilder(s.substring(0, p));245// Skip the year number246while (Character.isDigit(s.charAt(p++)))247;248int year = internalGet(YEAR) + BUDDHIST_YEAR_OFFSET;249sb.append(year).append(s.substring(p - 1));250return sb.toString();251}252253private transient int yearOffset = BUDDHIST_YEAR_OFFSET;254255private void readObject(ObjectInputStream stream)256throws IOException, ClassNotFoundException {257stream.defaultReadObject();258yearOffset = BUDDHIST_YEAR_OFFSET;259}260}261262263