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 /* 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 * 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 64 import static java.time.chrono.JapaneseDate.MEIJI_6_ISODATE; 65 import static java.time.temporal.ChronoField.ERA; 66 67 import java.io.DataInput; 68 import java.io.DataOutput; 69 import java.io.IOException; 70 import java.io.InvalidObjectException; 71 import java.io.ObjectStreamException; 72 import java.io.Serializable; 73 import java.time.DateTimeException; 74 import java.time.LocalDate; 75 import java.time.temporal.ChronoField; 76 import java.time.temporal.TemporalField; 77 import java.time.temporal.UnsupportedTemporalTypeException; 78 import java.time.temporal.ValueRange; 79 import java.util.Arrays; 80 import java.util.Objects; 81 82 import sun.util.calendar.CalendarDate; 83 84 /** 85 * An era in the Japanese Imperial calendar system. 86 * <p> 87 * This class defines the valid eras for the Japanese chronology. 88 * Japan introduced the Gregorian calendar starting with Meiji 6. 89 * Only Meiji and later eras are supported; 90 * dates before Meiji 6, January 1 are not supported. 91 * 92 * @implSpec 93 * This class is immutable and thread-safe. 94 * 95 * @since 1.8 96 */ 97 public final class JapaneseEra 98 implements Era, Serializable { 99 100 // The offset value to 0-based index from the era value. 101 // i.e., getValue() + ERA_OFFSET == 0-based index 102 static final int ERA_OFFSET = 2; 103 104 static final sun.util.calendar.Era[] ERA_CONFIG; 105 106 /** 107 * The singleton instance for the 'Meiji' era (1868-09-08 - 1912-07-29) 108 * which has the value -1. 109 */ 110 public static final JapaneseEra MEIJI = new JapaneseEra(-1, LocalDate.of(1868, 9, 8)); 111 /** 112 * The singleton instance for the 'Taisho' era (1912-07-30 - 1926-12-24) 113 * which has the value 0. 114 */ 115 public static final JapaneseEra TAISHO = new JapaneseEra(0, LocalDate.of(1912, 7, 30)); 116 /** 117 * The singleton instance for the 'Showa' era (1926-12-25 - 1989-01-07) 118 * which has the value 1. 119 */ 120 public static final JapaneseEra SHOWA = new JapaneseEra(1, LocalDate.of(1926, 12, 25)); 121 /** 122 * The singleton instance for the 'Heisei' era (1989-01-08 - current) 123 * which has the value 2. 124 */ 125 public static final JapaneseEra HEISEI = new JapaneseEra(2, LocalDate.of(1989, 1, 8)); 126 127 // the number of defined JapaneseEra constants. 128 // There could be an extra era defined in its configuration. 129 private static final int N_ERA_CONSTANTS = HEISEI.getValue() + ERA_OFFSET + 1; 130 131 /** 132 * Serialization version. 133 */ 134 private static final long serialVersionUID = 1466499369062886794L; 135 136 // array for the singleton JapaneseEra instances 137 private static final JapaneseEra[] KNOWN_ERAS; 138 139 static { 140 ERA_CONFIG = JapaneseChronology.JCAL.getEras(); 141 142 KNOWN_ERAS = new JapaneseEra[ERA_CONFIG.length]; 143 KNOWN_ERAS[0] = MEIJI; 144 KNOWN_ERAS[1] = TAISHO; 145 KNOWN_ERAS[2] = SHOWA; 146 KNOWN_ERAS[3] = HEISEI; 147 for (int i = N_ERA_CONSTANTS; i < ERA_CONFIG.length; i++) { 148 CalendarDate date = ERA_CONFIG[i].getSinceDate(); 149 LocalDate isoDate = LocalDate.of(date.getYear(), date.getMonth(), date.getDayOfMonth()); 150 KNOWN_ERAS[i] = new JapaneseEra(i - ERA_OFFSET, isoDate); 151 } 152 }; 153 154 /** 155 * The era value. 156 * @serial 157 */ 158 private final int eraValue; 159 160 // the first day of the era 161 private final transient LocalDate since; 162 163 /** 164 * Creates an instance. 165 * 166 * @param eraValue the era value, validated 167 * @param since the date representing the first date of the era, validated not null 168 */ 169 private JapaneseEra(int eraValue, LocalDate since) { 170 this.eraValue = eraValue; 171 this.since = since; 172 } 173 174 /** 175 * Returns the singleton {@code JapaneseEra} corresponding to this object. 176 * It's possible that this version of {@code JapaneseEra} doesn't support the latest era value. 177 * In that case, this method throws an {@code ObjectStreamException}. 178 * 179 * @return the singleton {@code JapaneseEra} for this object 180 * @throws ObjectStreamException if the deserialized object has any unknown numeric era value. 181 */ 182 private Object readResolve() throws ObjectStreamException { 183 try { 184 return of(eraValue); 185 } catch (DateTimeException e) { 186 InvalidObjectException ex = new InvalidObjectException("Invalid era"); 187 ex.initCause(e); 188 throw ex; 189 } 190 } 191 192 //----------------------------------------------------------------------- 193 /** 194 * Returns the Sun private Era instance corresponding to this {@code JapaneseEra}. 195 * 196 * @return the Sun private Era instance for this {@code JapaneseEra}. 197 */ 198 sun.util.calendar.Era getPrivateEra() { 199 return ERA_CONFIG[ordinal(eraValue)]; 200 } 201 202 //----------------------------------------------------------------------- 203 /** 204 * Obtains an instance of {@code JapaneseEra} from an {@code int} value. 205 * <p> 206 * The {@link #SHOWA} era that contains 1970-01-01 (ISO calendar system) has the value 1 207 * Later era is numbered 2 ({@link #HEISEI}). Earlier eras are numbered 0 ({@link #TAISHO}), 208 * -1 ({@link #MEIJI}), only Meiji and later eras are supported. 209 * 210 * @param japaneseEra the era to represent 211 * @return the {@code JapaneseEra} singleton, not null 212 * @throws DateTimeException if the value is invalid 213 */ 214 public static JapaneseEra of(int japaneseEra) { 215 if (japaneseEra < MEIJI.eraValue || japaneseEra > HEISEI.eraValue) { 216 throw new DateTimeException("Invalid era: " + japaneseEra); 217 } 218 return KNOWN_ERAS[ordinal(japaneseEra)]; 219 } 220 221 /** 222 * Returns the {@code JapaneseEra} with the name. 223 * <p> 224 * The string must match exactly the name of the era. 225 * (Extraneous whitespace characters are not permitted.) 226 * 227 * @param japaneseEra the japaneseEra name; non-null 228 * @return the {@code JapaneseEra} singleton, never null 229 * @throws IllegalArgumentException if there is not JapaneseEra with the specified name 230 */ 231 public static JapaneseEra valueOf(String japaneseEra) { 232 Objects.requireNonNull(japaneseEra, "japaneseEra"); 233 for (JapaneseEra era : KNOWN_ERAS) { 234 if (era.getName().equals(japaneseEra)) { 235 return era; 236 } 237 } 238 throw new IllegalArgumentException("japaneseEra is invalid"); 239 } 240 241 /** 242 * Returns an array of JapaneseEras. 243 * <p> 244 * This method may be used to iterate over the JapaneseEras as follows: 245 * <pre> 246 * for (JapaneseEra c : JapaneseEra.values()) 247 * System.out.println(c); 248 * </pre> 249 * 250 * @return an array of JapaneseEras 251 */ 252 public static JapaneseEra[] values() { 253 return Arrays.copyOf(KNOWN_ERAS, KNOWN_ERAS.length); 254 } 255 256 //----------------------------------------------------------------------- 257 /** 258 * Obtains an instance of {@code JapaneseEra} from a date. 259 * 260 * @param date the date, not null 261 * @return the Era singleton, never null 262 */ 263 static JapaneseEra from(LocalDate date) { 264 if (date.isBefore(MEIJI_6_ISODATE)) { 265 throw new DateTimeException("JapaneseDate before Meiji 6 are not supported"); 266 } 267 for (int i = KNOWN_ERAS.length - 1; i > 0; i--) { 268 JapaneseEra era = KNOWN_ERAS[i]; 269 if (date.compareTo(era.since) >= 0) { 270 return era; 271 } 272 } 273 return null; 274 } 275 276 static JapaneseEra toJapaneseEra(sun.util.calendar.Era privateEra) { 277 for (int i = ERA_CONFIG.length - 1; i >= 0; i--) { 278 if (ERA_CONFIG[i].equals(privateEra)) { 279 return KNOWN_ERAS[i]; 280 } 281 } 282 return null; 283 } 284 285 static sun.util.calendar.Era privateEraFrom(LocalDate isoDate) { 286 for (int i = KNOWN_ERAS.length - 1; i > 0; i--) { 287 JapaneseEra era = KNOWN_ERAS[i]; 288 if (isoDate.compareTo(era.since) >= 0) { 289 return ERA_CONFIG[i]; 290 } 291 } 292 return null; 293 } 294 295 /** 296 * Returns the index into the arrays from the Era value. 297 * the eraValue is a valid Era number, -1..2. 298 * 299 * @param eraValue the era value to convert to the index 300 * @return the index of the current Era 301 */ 302 private static int ordinal(int eraValue) { 303 return eraValue + ERA_OFFSET - 1; 304 } 305 306 //----------------------------------------------------------------------- 307 /** 308 * Gets the numeric era {@code int} value. 309 * <p> 310 * The {@link #SHOWA} era that contains 1970-01-01 (ISO calendar system) has the value 1. 311 * Later eras are numbered from 2 ({@link #HEISEI}). 312 * Earlier eras are numbered 0 ({@link #TAISHO}), -1 ({@link #MEIJI})). 313 * 314 * @return the era value 315 */ 316 @Override 317 public int getValue() { 318 return eraValue; 319 } 320 321 //----------------------------------------------------------------------- 322 /** 323 * Gets the range of valid values for the specified field. 324 * <p> 325 * The range object expresses the minimum and maximum valid values for a field. 326 * This era is used to enhance the accuracy of the returned range. 327 * If it is not possible to return the range, because the field is not supported 328 * or for some other reason, an exception is thrown. 329 * <p> 330 * If the field is a {@link ChronoField} then the query is implemented here. 331 * The {@code ERA} field returns the range. 332 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. 333 * <p> 334 * If the field is not a {@code ChronoField}, then the result of this method 335 * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)} 336 * passing {@code this} as the argument. 337 * Whether the range can be obtained is determined by the field. 338 * <p> 339 * The range of valid Japanese eras can change over time due to the nature 340 * of the Japanese calendar system. 341 * 342 * @param field the field to query the range for, not null 343 * @return the range of valid values for the field, not null 344 * @throws DateTimeException if the range for the field cannot be obtained 345 * @throws UnsupportedTemporalTypeException if the unit is not supported 346 */ 347 @Override // override as super would return range from 0 to 1 348 public ValueRange range(TemporalField field) { 349 if (field == ERA) { 350 return JapaneseChronology.INSTANCE.range(ERA); 351 } 352 return Era.super.range(field); 353 } 354 355 //----------------------------------------------------------------------- 356 String getAbbreviation() { 357 int index = ordinal(getValue()); 358 if (index == 0) { 359 return ""; 360 } 361 return ERA_CONFIG[index].getAbbreviation(); 362 } 363 364 String getName() { 365 return ERA_CONFIG[ordinal(getValue())].getName(); 366 } 367 368 @Override 369 public String toString() { 370 return getName(); 371 } 372 373 //----------------------------------------------------------------------- 374 private Object writeReplace() { 375 return new Ser(Ser.JAPANESE_ERA_TYPE, this); 376 } 377 378 void writeExternal(DataOutput out) throws IOException { 379 out.writeByte(this.getValue()); 380 } 381 382 static JapaneseEra readExternal(DataInput in) throws IOException { 383 byte eraValue = in.readByte(); 384 return JapaneseEra.of(eraValue); 385 } 386 387 }