jdk/src/share/classes/sun/util/BuddhistCalendar.java
author okutsu
Wed, 08 Dec 2010 13:09:29 +0900
changeset 7504 e490ef909bf5
parent 5506 202f599c92aa
child 7668 d4a77089c587
permissions -rw-r--r--
6653944: (cal) BuddhistCalendar yearOffset erased when deserialized Reviewed-by: peytoia
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
7504
e490ef909bf5 6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents: 5506
diff changeset
    28
import java.io.IOException;
e490ef909bf5 6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents: 5506
diff changeset
    29
import java.io.ObjectInputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Calendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.GregorianCalendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.ResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.TimeZone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.util.resources.LocaleData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class BuddhistCalendar extends GregorianCalendar {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
//////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
// Class Variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
//////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final long serialVersionUID = -8527488697350388578L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
7504
e490ef909bf5 6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents: 5506
diff changeset
    47
    private static final int BUDDHIST_YEAR_OFFSET = 543;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
///////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
// Constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
///////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Constructs a default BuddhistCalendar using the current time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * in the default time zone with the default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public BuddhistCalendar() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Constructs a BuddhistCalendar based on the current time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * in the given time zone with the default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @param zone the given time zone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public BuddhistCalendar(TimeZone zone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        super(zone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Constructs a BuddhistCalendar based on the current time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * in the default time zone with the given locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param aLocale the given locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public BuddhistCalendar(Locale aLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        super(aLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Constructs a BuddhistCalendar based on the current time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * in the given time zone with the given locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param zone the given time zone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param aLocale the given locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public BuddhistCalendar(TimeZone zone, Locale aLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        super(zone, aLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
// Public methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Compares this BuddhistCalendar to an object reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param obj the object reference with which to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @return true if this object is equal to <code>obj</code>; false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return obj instanceof BuddhistCalendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            && super.equals(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Override hashCode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Generates the hash code for the BuddhistCalendar object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public int hashCode() {
7504
e490ef909bf5 6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents: 5506
diff changeset
   108
        return super.hashCode() ^ BUDDHIST_YEAR_OFFSET;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Gets the value for a given time field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param field the given time field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @return the value for the given time field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public int get(int field)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (field == YEAR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return super.get(field) + yearOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return super.get(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Sets the time field with the given value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param field the given time field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param value the value to be set for the given time field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public void set(int field, int value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (field == YEAR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            super.set(field, value - yearOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            super.set(field, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Adds the specified (signed) amount of time to the given time field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param field the time field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param amount the amount of date or time to be added to the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public void add(int field, int amount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int savedYearOffset = yearOffset;
7504
e490ef909bf5 6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents: 5506
diff changeset
   146
        // To let the superclass calculate date-time values correctly,
e490ef909bf5 6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents: 5506
diff changeset
   147
        // temporarily make this GregorianCalendar.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        yearOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            super.add(field, amount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            yearOffset = savedYearOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Add to field a signed amount without changing larger fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * A negative roll amount means to subtract from field without changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * larger fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param field the time field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param amount the signed amount to add to <code>field</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public void roll(int field, int amount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        int savedYearOffset = yearOffset;
7504
e490ef909bf5 6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents: 5506
diff changeset
   166
        // To let the superclass calculate date-time values correctly,
e490ef909bf5 6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents: 5506
diff changeset
   167
        // temporarily make this GregorianCalendar.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        yearOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            super.roll(field, amount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            yearOffset = savedYearOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public String getDisplayName(int field, int style, Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (field != ERA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return super.getDisplayName(field, style, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // Handle Thai BuddhistCalendar specific era names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (field < 0 || field >= fields.length ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            style < SHORT || style > LONG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (locale == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        ResourceBundle rb = LocaleData.getDateFormatData(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        String[] eras = rb.getStringArray(getKey(style));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return eras[get(field)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (field != ERA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            return super.getDisplayNames(field, style, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // Handle Thai BuddhistCalendar specific era names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (field < 0 || field >= fields.length ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            style < ALL_STYLES || style > LONG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (locale == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // ALL_STYLES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (style == ALL_STYLES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            Map<String,Integer> shortNames = getDisplayNamesImpl(field, SHORT, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            Map<String,Integer> longNames = getDisplayNamesImpl(field, LONG, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (shortNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                return longNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            if (longNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                shortNames.putAll(longNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            return shortNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // SHORT or LONG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return getDisplayNamesImpl(field, style, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private Map<String,Integer> getDisplayNamesImpl(int field, int style, Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        ResourceBundle rb = LocaleData.getDateFormatData(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        String[] eras = rb.getStringArray(getKey(style));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        Map<String,Integer> map = new HashMap<String,Integer>(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        for (int i = 0; i < eras.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            map.put(eras[i], i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private String getKey(int style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        StringBuilder key = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        key.append(BuddhistCalendar.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (style == SHORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            key.append(".short");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        key.append(".Eras");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return key.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Returns the maximum value that this field could have, given the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * current date.  For example, with the date "Feb 3, 2540" and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <code>DAY_OF_MONTH</code> field, the actual maximum is 28; for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * "Feb 3, 2539" it is 29.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param field the field to determine the maximum of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @return the maximum of the given field for the current date of this Calendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public int getActualMaximum(int field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        int savedYearOffset = yearOffset;
7504
e490ef909bf5 6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents: 5506
diff changeset
   255
        // To let the superclass calculate date-time values correctly,
e490ef909bf5 6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents: 5506
diff changeset
   256
        // temporarily make this GregorianCalendar.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        yearOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            return super.getActualMaximum(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            yearOffset = savedYearOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        // The super class produces a String with the Gregorian year
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // value (or '?')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        String s = super.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // If the YEAR field is UNSET, then return the Gregorian string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (!isSet(YEAR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        final String yearField = "YEAR=";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        int p = s.indexOf(yearField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        // If the string doesn't include the year value for some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // reason, then return the Gregorian string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (p == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        p += yearField.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        StringBuilder sb = new StringBuilder(s.substring(0, p));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // Skip the year number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        while (Character.isDigit(s.charAt(p++)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            ;
7504
e490ef909bf5 6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents: 5506
diff changeset
   286
        int year = internalGet(YEAR) + BUDDHIST_YEAR_OFFSET;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        sb.append(year).append(s.substring(p - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
7504
e490ef909bf5 6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents: 5506
diff changeset
   291
    private transient int yearOffset = BUDDHIST_YEAR_OFFSET;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
7504
e490ef909bf5 6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents: 5506
diff changeset
   293
    private void readObject(ObjectInputStream stream)
e490ef909bf5 6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents: 5506
diff changeset
   294
        throws IOException, ClassNotFoundException {
e490ef909bf5 6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents: 5506
diff changeset
   295
        stream.defaultReadObject();
e490ef909bf5 6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents: 5506
diff changeset
   296
        yearOffset = BUDDHIST_YEAR_OFFSET;
e490ef909bf5 6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents: 5506
diff changeset
   297
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
}