0N/A/*
3261N/A * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.util;
0N/A
3189N/Aimport java.io.IOException;
3189N/Aimport java.io.ObjectInputStream;
0N/Aimport java.util.Calendar;
0N/Aimport java.util.GregorianCalendar;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Locale;
0N/Aimport java.util.Map;
0N/Aimport java.util.ResourceBundle;
0N/Aimport java.util.TimeZone;
0N/Aimport sun.util.resources.LocaleData;
0N/A
0N/Apublic class BuddhistCalendar extends GregorianCalendar {
0N/A
0N/A//////////////////
0N/A// Class Variables
0N/A//////////////////
0N/A
0N/A private static final long serialVersionUID = -8527488697350388578L;
0N/A
3189N/A private static final int BUDDHIST_YEAR_OFFSET = 543;
0N/A
0N/A///////////////
0N/A// Constructors
0N/A///////////////
0N/A
0N/A /**
0N/A * Constructs a default BuddhistCalendar using the current time
0N/A * in the default time zone with the default locale.
0N/A */
0N/A public BuddhistCalendar() {
0N/A super();
0N/A }
0N/A
0N/A /**
0N/A * Constructs a BuddhistCalendar based on the current time
0N/A * in the given time zone with the default locale.
0N/A * @param zone the given time zone.
0N/A */
0N/A public BuddhistCalendar(TimeZone zone) {
0N/A super(zone);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a BuddhistCalendar based on the current time
0N/A * in the default time zone with the given locale.
0N/A * @param aLocale the given locale.
0N/A */
0N/A public BuddhistCalendar(Locale aLocale) {
0N/A super(aLocale);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a BuddhistCalendar based on the current time
0N/A * in the given time zone with the given locale.
0N/A * @param zone the given time zone.
0N/A * @param aLocale the given locale.
0N/A */
0N/A public BuddhistCalendar(TimeZone zone, Locale aLocale) {
0N/A super(zone, aLocale);
0N/A }
0N/A
0N/A/////////////////
0N/A// Public methods
0N/A/////////////////
0N/A
0N/A /**
0N/A * Compares this BuddhistCalendar to an object reference.
0N/A * @param obj the object reference with which to compare
0N/A * @return true if this object is equal to <code>obj</code>; false otherwise
0N/A */
0N/A public boolean equals(Object obj) {
0N/A return obj instanceof BuddhistCalendar
0N/A && super.equals(obj);
0N/A }
0N/A
0N/A /**
0N/A * Override hashCode.
0N/A * Generates the hash code for the BuddhistCalendar object
0N/A */
0N/A public int hashCode() {
3189N/A return super.hashCode() ^ BUDDHIST_YEAR_OFFSET;
0N/A }
0N/A
0N/A /**
0N/A * Gets the value for a given time field.
0N/A * @param field the given time field.
0N/A * @return the value for the given time field.
0N/A */
0N/A public int get(int field)
0N/A {
0N/A if (field == YEAR) {
0N/A return super.get(field) + yearOffset;
0N/A }
0N/A return super.get(field);
0N/A }
0N/A
0N/A /**
0N/A * Sets the time field with the given value.
0N/A * @param field the given time field.
0N/A * @param value the value to be set for the given time field.
0N/A */
0N/A public void set(int field, int value)
0N/A {
0N/A if (field == YEAR) {
0N/A super.set(field, value - yearOffset);
0N/A } else {
0N/A super.set(field, value);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Adds the specified (signed) amount of time to the given time field.
0N/A * @param field the time field.
0N/A * @param amount the amount of date or time to be added to the field.
0N/A */
0N/A public void add(int field, int amount)
0N/A {
0N/A int savedYearOffset = yearOffset;
3189N/A // To let the superclass calculate date-time values correctly,
3189N/A // temporarily make this GregorianCalendar.
0N/A yearOffset = 0;
0N/A try {
0N/A super.add(field, amount);
0N/A } finally {
0N/A yearOffset = savedYearOffset;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Add to field a signed amount without changing larger fields.
0N/A * A negative roll amount means to subtract from field without changing
0N/A * larger fields.
0N/A * @param field the time field.
0N/A * @param amount the signed amount to add to <code>field</code>.
0N/A */
0N/A public void roll(int field, int amount)
0N/A {
0N/A int savedYearOffset = yearOffset;
3189N/A // To let the superclass calculate date-time values correctly,
3189N/A // temporarily make this GregorianCalendar.
0N/A yearOffset = 0;
0N/A try {
0N/A super.roll(field, amount);
0N/A } finally {
0N/A yearOffset = savedYearOffset;
0N/A }
0N/A }
0N/A
0N/A public String getDisplayName(int field, int style, Locale locale) {
0N/A if (field != ERA) {
0N/A return super.getDisplayName(field, style, locale);
0N/A }
0N/A
0N/A // Handle Thai BuddhistCalendar specific era names
0N/A if (field < 0 || field >= fields.length ||
0N/A style < SHORT || style > LONG) {
0N/A throw new IllegalArgumentException();
0N/A }
0N/A if (locale == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A ResourceBundle rb = LocaleData.getDateFormatData(locale);
0N/A String[] eras = rb.getStringArray(getKey(style));
0N/A return eras[get(field)];
0N/A }
0N/A
0N/A public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) {
0N/A if (field != ERA) {
0N/A return super.getDisplayNames(field, style, locale);
0N/A }
0N/A
0N/A // Handle Thai BuddhistCalendar specific era names
0N/A if (field < 0 || field >= fields.length ||
0N/A style < ALL_STYLES || style > LONG) {
0N/A throw new IllegalArgumentException();
0N/A }
0N/A if (locale == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A // ALL_STYLES
0N/A if (style == ALL_STYLES) {
0N/A Map<String,Integer> shortNames = getDisplayNamesImpl(field, SHORT, locale);
0N/A Map<String,Integer> longNames = getDisplayNamesImpl(field, LONG, locale);
0N/A if (shortNames == null) {
0N/A return longNames;
0N/A }
0N/A if (longNames != null) {
0N/A shortNames.putAll(longNames);
0N/A }
0N/A return shortNames;
0N/A }
0N/A
0N/A // SHORT or LONG
0N/A return getDisplayNamesImpl(field, style, locale);
0N/A }
0N/A
0N/A private Map<String,Integer> getDisplayNamesImpl(int field, int style, Locale locale) {
0N/A ResourceBundle rb = LocaleData.getDateFormatData(locale);
0N/A String[] eras = rb.getStringArray(getKey(style));
0N/A Map<String,Integer> map = new HashMap<String,Integer>(4);
0N/A for (int i = 0; i < eras.length; i++) {
0N/A map.put(eras[i], i);
0N/A }
0N/A return map;
0N/A }
0N/A
0N/A private String getKey(int style) {
0N/A StringBuilder key = new StringBuilder();
0N/A key.append(BuddhistCalendar.class.getName());
0N/A if (style == SHORT) {
0N/A key.append(".short");
0N/A }
0N/A key.append(".Eras");
0N/A return key.toString();
0N/A }
0N/A
0N/A /**
0N/A * Returns the maximum value that this field could have, given the
0N/A * current date. For example, with the date "Feb 3, 2540" and the
0N/A * <code>DAY_OF_MONTH</code> field, the actual maximum is 28; for
0N/A * "Feb 3, 2539" it is 29.
0N/A *
0N/A * @param field the field to determine the maximum of
0N/A * @return the maximum of the given field for the current date of this Calendar
0N/A */
0N/A public int getActualMaximum(int field) {
0N/A int savedYearOffset = yearOffset;
3189N/A // To let the superclass calculate date-time values correctly,
3189N/A // temporarily make this GregorianCalendar.
0N/A yearOffset = 0;
0N/A try {
0N/A return super.getActualMaximum(field);
0N/A } finally {
0N/A yearOffset = savedYearOffset;
0N/A }
0N/A }
0N/A
0N/A public String toString() {
0N/A // The super class produces a String with the Gregorian year
0N/A // value (or '?')
0N/A String s = super.toString();
0N/A // If the YEAR field is UNSET, then return the Gregorian string.
0N/A if (!isSet(YEAR)) {
0N/A return s;
0N/A }
0N/A
0N/A final String yearField = "YEAR=";
0N/A int p = s.indexOf(yearField);
0N/A // If the string doesn't include the year value for some
0N/A // reason, then return the Gregorian string.
0N/A if (p == -1) {
0N/A return s;
0N/A }
0N/A p += yearField.length();
0N/A StringBuilder sb = new StringBuilder(s.substring(0, p));
0N/A // Skip the year number
0N/A while (Character.isDigit(s.charAt(p++)))
0N/A ;
3189N/A int year = internalGet(YEAR) + BUDDHIST_YEAR_OFFSET;
0N/A sb.append(year).append(s.substring(p - 1));
0N/A return sb.toString();
0N/A }
0N/A
3189N/A private transient int yearOffset = BUDDHIST_YEAR_OFFSET;
0N/A
3189N/A private void readObject(ObjectInputStream stream)
3189N/A throws IOException, ClassNotFoundException {
3189N/A stream.defaultReadObject();
3189N/A yearOffset = BUDDHIST_YEAR_OFFSET;
3189N/A }
0N/A}