author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 31471 | jdk/src/java.base/share/classes/sun/util/BuddhistCalendar.java@ae27c6f1d8bf |
child 57956 | e0b8b019d2f5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
13583 | 2 |
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.util; |
|
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 | 30 |
import java.util.GregorianCalendar; |
31 |
import java.util.Locale; |
|
32 |
import java.util.Map; |
|
33 |
import java.util.TimeZone; |
|
13583 | 34 |
import sun.util.locale.provider.CalendarDataUtility; |
2 | 35 |
|
36 |
public class BuddhistCalendar extends GregorianCalendar { |
|
37 |
||
38 |
////////////////// |
|
39 |
// Class Variables |
|
40 |
////////////////// |
|
41 |
||
42 |
private static final long serialVersionUID = -8527488697350388578L; |
|
43 |
||
7504
e490ef909bf5
6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents:
5506
diff
changeset
|
44 |
private static final int BUDDHIST_YEAR_OFFSET = 543; |
2 | 45 |
|
46 |
/////////////// |
|
47 |
// Constructors |
|
48 |
/////////////// |
|
49 |
||
50 |
/** |
|
51 |
* Constructs a default BuddhistCalendar using the current time |
|
52 |
* in the default time zone with the default locale. |
|
53 |
*/ |
|
54 |
public BuddhistCalendar() { |
|
55 |
super(); |
|
56 |
} |
|
57 |
||
58 |
/** |
|
59 |
* Constructs a BuddhistCalendar based on the current time |
|
60 |
* in the given time zone with the default locale. |
|
61 |
* @param zone the given time zone. |
|
62 |
*/ |
|
63 |
public BuddhistCalendar(TimeZone zone) { |
|
64 |
super(zone); |
|
65 |
} |
|
66 |
||
67 |
/** |
|
68 |
* Constructs a BuddhistCalendar based on the current time |
|
69 |
* in the default time zone with the given locale. |
|
70 |
* @param aLocale the given locale. |
|
71 |
*/ |
|
72 |
public BuddhistCalendar(Locale aLocale) { |
|
73 |
super(aLocale); |
|
74 |
} |
|
75 |
||
76 |
/** |
|
77 |
* Constructs a BuddhistCalendar based on the current time |
|
78 |
* in the given time zone with the given locale. |
|
79 |
* @param zone the given time zone. |
|
80 |
* @param aLocale the given locale. |
|
81 |
*/ |
|
82 |
public BuddhistCalendar(TimeZone zone, Locale aLocale) { |
|
83 |
super(zone, aLocale); |
|
84 |
} |
|
85 |
||
86 |
///////////////// |
|
87 |
// Public methods |
|
88 |
///////////////// |
|
89 |
||
90 |
/** |
|
13583 | 91 |
* Returns {@code "buddhist"} as the calendar type of this Calendar. |
92 |
*/ |
|
93 |
@Override |
|
94 |
public String getCalendarType() { |
|
95 |
return "buddhist"; |
|
96 |
} |
|
97 |
||
98 |
/** |
|
2 | 99 |
* Compares this BuddhistCalendar to an object reference. |
100 |
* @param obj the object reference with which to compare |
|
101 |
* @return true if this object is equal to <code>obj</code>; false otherwise |
|
102 |
*/ |
|
13583 | 103 |
@Override |
2 | 104 |
public boolean equals(Object obj) { |
105 |
return obj instanceof BuddhistCalendar |
|
106 |
&& super.equals(obj); |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Override hashCode. |
|
111 |
* Generates the hash code for the BuddhistCalendar object |
|
112 |
*/ |
|
13583 | 113 |
@Override |
2 | 114 |
public int hashCode() { |
7504
e490ef909bf5
6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents:
5506
diff
changeset
|
115 |
return super.hashCode() ^ BUDDHIST_YEAR_OFFSET; |
2 | 116 |
} |
117 |
||
118 |
/** |
|
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 |
*/ |
|
13583 | 123 |
@Override |
2 | 124 |
public int get(int field) |
125 |
{ |
|
126 |
if (field == YEAR) { |
|
127 |
return super.get(field) + yearOffset; |
|
128 |
} |
|
129 |
return super.get(field); |
|
130 |
} |
|
131 |
||
132 |
/** |
|
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 |
*/ |
|
13583 | 137 |
@Override |
2 | 138 |
public void set(int field, int value) |
139 |
{ |
|
140 |
if (field == YEAR) { |
|
141 |
super.set(field, value - yearOffset); |
|
142 |
} else { |
|
143 |
super.set(field, value); |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
/** |
|
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 |
*/ |
|
13583 | 152 |
@Override |
2 | 153 |
public void add(int field, int amount) |
154 |
{ |
|
155 |
int savedYearOffset = yearOffset; |
|
7504
e490ef909bf5
6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents:
5506
diff
changeset
|
156 |
// To let the superclass calculate date-time values correctly, |
e490ef909bf5
6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents:
5506
diff
changeset
|
157 |
// temporarily make this GregorianCalendar. |
2 | 158 |
yearOffset = 0; |
159 |
try { |
|
160 |
super.add(field, amount); |
|
161 |
} finally { |
|
162 |
yearOffset = savedYearOffset; |
|
163 |
} |
|
164 |
} |
|
165 |
||
166 |
/** |
|
167 |
* Add to field a signed amount without changing larger fields. |
|
168 |
* A negative roll amount means to subtract from field without changing |
|
169 |
* larger fields. |
|
170 |
* @param field the time field. |
|
171 |
* @param amount the signed amount to add to <code>field</code>. |
|
172 |
*/ |
|
13583 | 173 |
@Override |
2 | 174 |
public void roll(int field, int amount) |
175 |
{ |
|
176 |
int savedYearOffset = yearOffset; |
|
7504
e490ef909bf5
6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents:
5506
diff
changeset
|
177 |
// To let the superclass calculate date-time values correctly, |
e490ef909bf5
6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents:
5506
diff
changeset
|
178 |
// temporarily make this GregorianCalendar. |
2 | 179 |
yearOffset = 0; |
180 |
try { |
|
181 |
super.roll(field, amount); |
|
182 |
} finally { |
|
183 |
yearOffset = savedYearOffset; |
|
184 |
} |
|
185 |
} |
|
186 |
||
13583 | 187 |
@Override |
2 | 188 |
public String getDisplayName(int field, int style, Locale locale) { |
189 |
if (field != ERA) { |
|
190 |
return super.getDisplayName(field, style, locale); |
|
191 |
} |
|
192 |
||
13583 | 193 |
return CalendarDataUtility.retrieveFieldValueName("buddhist", field, get(field), style, locale); |
2 | 194 |
} |
195 |
||
13583 | 196 |
@Override |
2 | 197 |
public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) { |
198 |
if (field != ERA) { |
|
199 |
return super.getDisplayNames(field, style, locale); |
|
200 |
} |
|
13583 | 201 |
return CalendarDataUtility.retrieveFieldValueNames("buddhist", field, style, locale); |
2 | 202 |
} |
203 |
||
204 |
/** |
|
205 |
* Returns the maximum value that this field could have, given the |
|
206 |
* current date. For example, with the date "Feb 3, 2540" and the |
|
207 |
* <code>DAY_OF_MONTH</code> field, the actual maximum is 28; for |
|
208 |
* "Feb 3, 2539" it is 29. |
|
209 |
* |
|
210 |
* @param field the field to determine the maximum of |
|
211 |
* @return the maximum of the given field for the current date of this Calendar |
|
212 |
*/ |
|
13583 | 213 |
@Override |
2 | 214 |
public int getActualMaximum(int field) { |
215 |
int savedYearOffset = yearOffset; |
|
7504
e490ef909bf5
6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents:
5506
diff
changeset
|
216 |
// To let the superclass calculate date-time values correctly, |
e490ef909bf5
6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents:
5506
diff
changeset
|
217 |
// temporarily make this GregorianCalendar. |
2 | 218 |
yearOffset = 0; |
219 |
try { |
|
220 |
return super.getActualMaximum(field); |
|
221 |
} finally { |
|
222 |
yearOffset = savedYearOffset; |
|
223 |
} |
|
224 |
} |
|
225 |
||
13583 | 226 |
@Override |
227 |
@SuppressWarnings("empty-statement") |
|
2 | 228 |
public String toString() { |
229 |
// The super class produces a String with the Gregorian year |
|
230 |
// value (or '?') |
|
231 |
String s = super.toString(); |
|
232 |
// If the YEAR field is UNSET, then return the Gregorian string. |
|
233 |
if (!isSet(YEAR)) { |
|
234 |
return s; |
|
235 |
} |
|
236 |
||
237 |
final String yearField = "YEAR="; |
|
238 |
int p = s.indexOf(yearField); |
|
239 |
// If the string doesn't include the year value for some |
|
240 |
// reason, then return the Gregorian string. |
|
241 |
if (p == -1) { |
|
242 |
return s; |
|
243 |
} |
|
244 |
p += yearField.length(); |
|
31471
ae27c6f1d8bf
8077242: (str) Optimize AbstractStringBuilder.append(CharSequence, int, int) for String argument
igerasim
parents:
25859
diff
changeset
|
245 |
StringBuilder sb = new StringBuilder(s.length() + 10); |
ae27c6f1d8bf
8077242: (str) Optimize AbstractStringBuilder.append(CharSequence, int, int) for String argument
igerasim
parents:
25859
diff
changeset
|
246 |
sb.append(s, 0, p); |
2 | 247 |
// Skip the year number |
248 |
while (Character.isDigit(s.charAt(p++))) |
|
249 |
; |
|
7504
e490ef909bf5
6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents:
5506
diff
changeset
|
250 |
int year = internalGet(YEAR) + BUDDHIST_YEAR_OFFSET; |
31471
ae27c6f1d8bf
8077242: (str) Optimize AbstractStringBuilder.append(CharSequence, int, int) for String argument
igerasim
parents:
25859
diff
changeset
|
251 |
sb.append(year).append(s, p - 1, s.length()); |
2 | 252 |
return sb.toString(); |
253 |
} |
|
254 |
||
7504
e490ef909bf5
6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents:
5506
diff
changeset
|
255 |
private transient int yearOffset = BUDDHIST_YEAR_OFFSET; |
2 | 256 |
|
7504
e490ef909bf5
6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents:
5506
diff
changeset
|
257 |
private void readObject(ObjectInputStream stream) |
e490ef909bf5
6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents:
5506
diff
changeset
|
258 |
throws IOException, ClassNotFoundException { |
e490ef909bf5
6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents:
5506
diff
changeset
|
259 |
stream.defaultReadObject(); |
e490ef909bf5
6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents:
5506
diff
changeset
|
260 |
yearOffset = BUDDHIST_YEAR_OFFSET; |
e490ef909bf5
6653944: (cal) BuddhistCalendar yearOffset erased when deserialized
okutsu
parents:
5506
diff
changeset
|
261 |
} |
2 | 262 |
} |