15658
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
|
|
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
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
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 |
*
|
|
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.
|
|
24 |
*/
|
|
25 |
|
|
26 |
/*
|
16852
|
27 |
* This file is available under and governed by the GNU General Public
|
|
28 |
* License version 2 only, as published by the Free Software Foundation.
|
|
29 |
* However, the following notice accompanied the original version of this
|
|
30 |
* file:
|
|
31 |
*
|
15658
|
32 |
* Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
|
|
33 |
*
|
|
34 |
* All rights reserved.
|
|
35 |
*
|
|
36 |
* Redistribution and use in source and binary forms, with or without
|
|
37 |
* modification, are permitted provided that the following conditions are met:
|
|
38 |
*
|
|
39 |
* * Redistributions of source code must retain the above copyright notice,
|
|
40 |
* this list of conditions and the following disclaimer.
|
|
41 |
*
|
|
42 |
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
43 |
* this list of conditions and the following disclaimer in the documentation
|
|
44 |
* and/or other materials provided with the distribution.
|
|
45 |
*
|
|
46 |
* * Neither the name of JSR-310 nor the names of its contributors
|
|
47 |
* may be used to endorse or promote products derived from this software
|
|
48 |
* without specific prior written permission.
|
|
49 |
*
|
|
50 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
51 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
52 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
53 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
54 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
55 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
56 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
57 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
58 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
59 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
60 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
61 |
*/
|
|
62 |
package java.time.chrono;
|
|
63 |
|
16852
|
64 |
import static java.time.temporal.ChronoField.ERA;
|
|
65 |
|
15658
|
66 |
import java.io.DataInput;
|
|
67 |
import java.io.DataOutput;
|
|
68 |
import java.io.IOException;
|
|
69 |
import java.io.InvalidObjectException;
|
|
70 |
import java.io.ObjectStreamException;
|
|
71 |
import java.io.Serializable;
|
|
72 |
import java.time.DateTimeException;
|
|
73 |
import java.time.LocalDate;
|
16852
|
74 |
import java.time.temporal.ChronoField;
|
|
75 |
import java.time.temporal.TemporalField;
|
|
76 |
import java.time.temporal.UnsupportedTemporalTypeException;
|
|
77 |
import java.time.temporal.ValueRange;
|
15658
|
78 |
import java.util.Arrays;
|
16852
|
79 |
import java.util.Objects;
|
15658
|
80 |
|
|
81 |
import sun.util.calendar.CalendarDate;
|
|
82 |
|
|
83 |
/**
|
|
84 |
* An era in the Japanese Imperial calendar system.
|
|
85 |
* <p>
|
|
86 |
* This class defines the valid eras for the Japanese chronology.
|
|
87 |
* Only Meiji (1868-09-08 - 1912-07-29) and later eras are supported.
|
|
88 |
* Japan introduced the Gregorian calendar since Meiji 6. The dates
|
|
89 |
* between Meiji 1 - 5 are not historically correct.
|
|
90 |
* The older eras are recognized as Seireki (Western calendar) era,
|
|
91 |
* and the year of era of Seireki is proleptic Gregorian year.
|
|
92 |
* (The Julian to Gregorian transition is not supported.)
|
|
93 |
*
|
17474
|
94 |
* @implSpec
|
15658
|
95 |
* This class is immutable and thread-safe.
|
|
96 |
*
|
|
97 |
* @since 1.8
|
|
98 |
*/
|
16852
|
99 |
public final class JapaneseEra
|
15658
|
100 |
implements Era, Serializable {
|
|
101 |
|
|
102 |
// The offset value to 0-based index from the era value.
|
|
103 |
// i.e., getValue() + ERA_OFFSET == 0-based index; except that -999 is mapped to zero
|
|
104 |
static final int ERA_OFFSET = 2;
|
|
105 |
|
|
106 |
static final sun.util.calendar.Era[] ERA_CONFIG;
|
|
107 |
|
|
108 |
/**
|
|
109 |
* The singleton instance for the before Meiji era ( - 1868-09-07)
|
|
110 |
* which has the value -999.
|
|
111 |
*/
|
|
112 |
public static final JapaneseEra SEIREKI = new JapaneseEra(-999, LocalDate.MIN);
|
|
113 |
/**
|
|
114 |
* The singleton instance for the 'Meiji' era (1868-09-08 - 1912-07-29)
|
|
115 |
* which has the value -1.
|
|
116 |
*/
|
|
117 |
public static final JapaneseEra MEIJI = new JapaneseEra(-1, LocalDate.of(1868, 9, 8));
|
|
118 |
/**
|
|
119 |
* The singleton instance for the 'Taisho' era (1912-07-30 - 1926-12-24)
|
|
120 |
* which has the value 0.
|
|
121 |
*/
|
|
122 |
public static final JapaneseEra TAISHO = new JapaneseEra(0, LocalDate.of(1912, 7, 30));
|
|
123 |
/**
|
|
124 |
* The singleton instance for the 'Showa' era (1926-12-25 - 1989-01-07)
|
|
125 |
* which has the value 1.
|
|
126 |
*/
|
|
127 |
public static final JapaneseEra SHOWA = new JapaneseEra(1, LocalDate.of(1926, 12, 25));
|
|
128 |
/**
|
|
129 |
* The singleton instance for the 'Heisei' era (1989-01-08 - current)
|
|
130 |
* which has the value 2.
|
|
131 |
*/
|
|
132 |
public static final JapaneseEra HEISEI = new JapaneseEra(2, LocalDate.of(1989, 1, 8));
|
|
133 |
|
|
134 |
// the number of defined JapaneseEra constants.
|
|
135 |
// There could be an extra era defined in its configuration.
|
|
136 |
private static final int N_ERA_CONSTANTS = HEISEI.getValue() + ERA_OFFSET + 1;
|
|
137 |
|
|
138 |
/**
|
|
139 |
* Serialization version.
|
|
140 |
*/
|
|
141 |
private static final long serialVersionUID = 1466499369062886794L;
|
|
142 |
|
|
143 |
// array for the singleton JapaneseEra instances
|
|
144 |
private static final JapaneseEra[] KNOWN_ERAS;
|
|
145 |
|
|
146 |
static {
|
|
147 |
sun.util.calendar.Era[] sunEras = JapaneseChronology.JCAL.getEras();
|
|
148 |
ERA_CONFIG = new sun.util.calendar.Era[sunEras.length + 1];
|
|
149 |
for (int i = 1; i < ERA_CONFIG.length; i++) {
|
|
150 |
ERA_CONFIG[i] = sunEras[i - 1];
|
|
151 |
}
|
|
152 |
KNOWN_ERAS = new JapaneseEra[ERA_CONFIG.length];
|
|
153 |
KNOWN_ERAS[0] = SEIREKI;
|
|
154 |
KNOWN_ERAS[1] = MEIJI;
|
|
155 |
KNOWN_ERAS[2] = TAISHO;
|
|
156 |
KNOWN_ERAS[3] = SHOWA;
|
|
157 |
KNOWN_ERAS[4] = HEISEI;
|
|
158 |
for (int i = N_ERA_CONSTANTS; i < ERA_CONFIG.length; i++) {
|
|
159 |
CalendarDate date = ERA_CONFIG[i].getSinceDate();
|
|
160 |
LocalDate isoDate = LocalDate.of(date.getYear(), date.getMonth(), date.getDayOfMonth());
|
|
161 |
KNOWN_ERAS[i] = new JapaneseEra(i - ERA_OFFSET, isoDate);
|
|
162 |
}
|
|
163 |
};
|
|
164 |
|
|
165 |
/**
|
|
166 |
* The era value.
|
|
167 |
* @serial
|
|
168 |
*/
|
|
169 |
private final int eraValue;
|
|
170 |
|
|
171 |
// the first day of the era
|
|
172 |
private final transient LocalDate since;
|
|
173 |
|
|
174 |
/**
|
|
175 |
* Creates an instance.
|
|
176 |
*
|
|
177 |
* @param eraValue the era value, validated
|
|
178 |
* @param since the date representing the first date of the era, validated not null
|
|
179 |
*/
|
|
180 |
private JapaneseEra(int eraValue, LocalDate since) {
|
|
181 |
this.eraValue = eraValue;
|
|
182 |
this.since = since;
|
|
183 |
}
|
|
184 |
|
|
185 |
/**
|
|
186 |
* Returns the singleton {@code JapaneseEra} corresponding to this object.
|
|
187 |
* It's possible that this version of {@code JapaneseEra} doesn't support the latest era value.
|
|
188 |
* In that case, this method throws an {@code ObjectStreamException}.
|
|
189 |
*
|
|
190 |
* @return the singleton {@code JapaneseEra} for this object
|
|
191 |
* @throws ObjectStreamException if the deserialized object has any unknown numeric era value.
|
|
192 |
*/
|
|
193 |
private Object readResolve() throws ObjectStreamException {
|
|
194 |
try {
|
|
195 |
return of(eraValue);
|
|
196 |
} catch (DateTimeException e) {
|
|
197 |
InvalidObjectException ex = new InvalidObjectException("Invalid era");
|
|
198 |
ex.initCause(e);
|
|
199 |
throw ex;
|
|
200 |
}
|
|
201 |
}
|
|
202 |
|
|
203 |
//-----------------------------------------------------------------------
|
|
204 |
/**
|
|
205 |
* Returns the Sun private Era instance corresponding to this {@code JapaneseEra}.
|
|
206 |
* SEIREKI doesn't have its corresponding one.
|
|
207 |
*
|
|
208 |
* @return the Sun private Era instance for this {@code JapaneseEra},
|
|
209 |
* or null for SEIREKI.
|
|
210 |
*/
|
|
211 |
sun.util.calendar.Era getPrivateEra() {
|
|
212 |
return ERA_CONFIG[ordinal(eraValue)];
|
|
213 |
}
|
|
214 |
|
|
215 |
//-----------------------------------------------------------------------
|
|
216 |
/**
|
16852
|
217 |
* Obtains an instance of {@code JapaneseEra} from an {@code int} value.
|
15658
|
218 |
* <p>
|
|
219 |
* The {@link #SHOWA} era that contains 1970-01-01 (ISO calendar system) has the value 1
|
|
220 |
* Later era is numbered 2 ({@link #HEISEI}). Earlier eras are numbered 0 ({@link #TAISHO}),
|
|
221 |
* -1 ({@link #MEIJI}), only Meiji and later eras are supported. The prior to Meiji,
|
|
222 |
* {@link #SEIREKI} is used.
|
|
223 |
*
|
|
224 |
* @param japaneseEra the era to represent
|
16852
|
225 |
* @return the {@code JapaneseEra} singleton, not null
|
|
226 |
* @throws DateTimeException if the value is invalid
|
15658
|
227 |
*/
|
|
228 |
public static JapaneseEra of(int japaneseEra) {
|
|
229 |
if (japaneseEra != SEIREKI.eraValue &&
|
|
230 |
(japaneseEra < MEIJI.eraValue || japaneseEra > HEISEI.eraValue)) {
|
16852
|
231 |
throw new DateTimeException("Invalid era: " + japaneseEra);
|
15658
|
232 |
}
|
|
233 |
return KNOWN_ERAS[ordinal(japaneseEra)];
|
|
234 |
}
|
|
235 |
|
|
236 |
/**
|
16852
|
237 |
* Returns the {@code JapaneseEra} with the name.
|
|
238 |
* <p>
|
|
239 |
* The string must match exactly the name of the era.
|
|
240 |
* (Extraneous whitespace characters are not permitted.)
|
|
241 |
*
|
|
242 |
* @param japaneseEra the japaneseEra name; non-null
|
|
243 |
* @return the {@code JapaneseEra} singleton, never null
|
|
244 |
* @throws IllegalArgumentException if there is not JapaneseEra with the specified name
|
|
245 |
*/
|
|
246 |
public static JapaneseEra valueOf(String japaneseEra) {
|
|
247 |
Objects.requireNonNull(japaneseEra, "japaneseEra");
|
|
248 |
for (JapaneseEra era : KNOWN_ERAS) {
|
|
249 |
if (era.getName().equals(japaneseEra)) {
|
|
250 |
return era;
|
|
251 |
}
|
|
252 |
}
|
|
253 |
throw new IllegalArgumentException("japaneseEra is invalid");
|
|
254 |
}
|
|
255 |
|
|
256 |
/**
|
15658
|
257 |
* Returns an array of JapaneseEras.
|
16852
|
258 |
* <p>
|
|
259 |
* This method may be used to iterate over the JapaneseEras as follows:
|
|
260 |
* <pre>
|
|
261 |
* for (JapaneseEra c : JapaneseEra.values())
|
|
262 |
* System.out.println(c);
|
|
263 |
* </pre>
|
|
264 |
*
|
15658
|
265 |
* @return an array of JapaneseEras
|
|
266 |
*/
|
16852
|
267 |
public static JapaneseEra[] values() {
|
15658
|
268 |
return Arrays.copyOf(KNOWN_ERAS, KNOWN_ERAS.length);
|
|
269 |
}
|
|
270 |
|
|
271 |
//-----------------------------------------------------------------------
|
|
272 |
/**
|
|
273 |
* Obtains an instance of {@code JapaneseEra} from a date.
|
|
274 |
*
|
|
275 |
* @param date the date, not null
|
|
276 |
* @return the Era singleton, never null
|
|
277 |
*/
|
|
278 |
static JapaneseEra from(LocalDate date) {
|
|
279 |
for (int i = KNOWN_ERAS.length - 1; i > 0; i--) {
|
|
280 |
JapaneseEra era = KNOWN_ERAS[i];
|
|
281 |
if (date.compareTo(era.since) >= 0) {
|
|
282 |
return era;
|
|
283 |
}
|
|
284 |
}
|
|
285 |
return SEIREKI;
|
|
286 |
}
|
|
287 |
|
|
288 |
static JapaneseEra toJapaneseEra(sun.util.calendar.Era privateEra) {
|
|
289 |
for (int i = ERA_CONFIG.length - 1; i > 0; i--) {
|
|
290 |
if (ERA_CONFIG[i].equals(privateEra)) {
|
|
291 |
return KNOWN_ERAS[i];
|
|
292 |
}
|
|
293 |
}
|
|
294 |
return SEIREKI;
|
|
295 |
}
|
|
296 |
|
|
297 |
static sun.util.calendar.Era privateEraFrom(LocalDate isoDate) {
|
|
298 |
for (int i = KNOWN_ERAS.length - 1; i > 0; i--) {
|
|
299 |
JapaneseEra era = KNOWN_ERAS[i];
|
|
300 |
if (isoDate.compareTo(era.since) >= 0) {
|
|
301 |
return ERA_CONFIG[i];
|
|
302 |
}
|
|
303 |
}
|
|
304 |
return null;
|
|
305 |
}
|
|
306 |
|
|
307 |
/**
|
|
308 |
* Returns the index into the arrays from the Era value.
|
|
309 |
* the eraValue is a valid Era number, -999, -1..2.
|
16852
|
310 |
*
|
|
311 |
* @param eraValue the era value to convert to the index
|
15658
|
312 |
* @return the index of the current Era
|
|
313 |
*/
|
16852
|
314 |
private static int ordinal(int eraValue) {
|
|
315 |
return (eraValue == SEIREKI.eraValue) ? 0 : eraValue + ERA_OFFSET;
|
15658
|
316 |
}
|
|
317 |
|
|
318 |
//-----------------------------------------------------------------------
|
|
319 |
/**
|
16852
|
320 |
* Gets the numeric era {@code int} value.
|
15658
|
321 |
* <p>
|
|
322 |
* The {@link #SHOWA} era that contains 1970-01-01 (ISO calendar system) has the value 1.
|
|
323 |
* Later eras are numbered from 2 ({@link #HEISEI}).
|
|
324 |
* Earlier eras are numbered 0 ({@link #TAISHO}), -1 ({@link #MEIJI}), and -999 ({@link #SEIREKI}).
|
|
325 |
*
|
|
326 |
* @return the era value
|
|
327 |
*/
|
|
328 |
@Override
|
|
329 |
public int getValue() {
|
|
330 |
return eraValue;
|
|
331 |
}
|
|
332 |
|
16852
|
333 |
//-----------------------------------------------------------------------
|
|
334 |
/**
|
|
335 |
* Gets the range of valid values for the specified field.
|
|
336 |
* <p>
|
|
337 |
* The range object expresses the minimum and maximum valid values for a field.
|
|
338 |
* This era is used to enhance the accuracy of the returned range.
|
|
339 |
* If it is not possible to return the range, because the field is not supported
|
|
340 |
* or for some other reason, an exception is thrown.
|
|
341 |
* <p>
|
|
342 |
* If the field is a {@link ChronoField} then the query is implemented here.
|
|
343 |
* The {@code ERA} field returns the range.
|
|
344 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
|
|
345 |
* <p>
|
|
346 |
* If the field is not a {@code ChronoField}, then the result of this method
|
|
347 |
* is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
|
|
348 |
* passing {@code this} as the argument.
|
|
349 |
* Whether the range can be obtained is determined by the field.
|
|
350 |
* <p>
|
|
351 |
* The range of valid Japanese eras can change over time due to the nature
|
|
352 |
* of the Japanese calendar system.
|
|
353 |
*
|
|
354 |
* @param field the field to query the range for, not null
|
|
355 |
* @return the range of valid values for the field, not null
|
|
356 |
* @throws DateTimeException if the range for the field cannot be obtained
|
|
357 |
* @throws UnsupportedTemporalTypeException if the unit is not supported
|
|
358 |
*/
|
|
359 |
@Override // override as super would return range from 0 to 1
|
|
360 |
public ValueRange range(TemporalField field) {
|
|
361 |
if (field == ERA) {
|
|
362 |
return JapaneseChronology.INSTANCE.range(ERA);
|
|
363 |
}
|
|
364 |
return Era.super.range(field);
|
15658
|
365 |
}
|
|
366 |
|
|
367 |
//-----------------------------------------------------------------------
|
|
368 |
String getAbbreviation() {
|
|
369 |
int index = ordinal(getValue());
|
|
370 |
if (index == 0) {
|
|
371 |
return "";
|
|
372 |
}
|
|
373 |
return ERA_CONFIG[index].getAbbreviation();
|
|
374 |
}
|
|
375 |
|
|
376 |
String getName() {
|
|
377 |
int index = ordinal(getValue());
|
|
378 |
if (index == 0) {
|
|
379 |
return "Seireki";
|
|
380 |
}
|
|
381 |
return ERA_CONFIG[index].getName();
|
|
382 |
}
|
|
383 |
|
|
384 |
@Override
|
|
385 |
public String toString() {
|
|
386 |
return getName();
|
|
387 |
}
|
|
388 |
|
|
389 |
//-----------------------------------------------------------------------
|
|
390 |
private Object writeReplace() {
|
|
391 |
return new Ser(Ser.JAPANESE_ERA_TYPE, this);
|
|
392 |
}
|
|
393 |
|
|
394 |
void writeExternal(DataOutput out) throws IOException {
|
|
395 |
out.writeByte(this.getValue());
|
|
396 |
}
|
|
397 |
|
|
398 |
static JapaneseEra readExternal(DataInput in) throws IOException {
|
|
399 |
byte eraValue = in.readByte();
|
|
400 |
return JapaneseEra.of(eraValue);
|
|
401 |
}
|
|
402 |
|
|
403 |
}
|