/* * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * * Neither the name of JSR-310 nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.threeten.bp.chrono; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import java.io.InvalidObjectException; import java.io.ObjectStreamException; import java.io.Serializable; import java.util.Arrays; import java.util.concurrent.atomic.AtomicReference; import org.threeten.bp.DateTimeException; import org.threeten.bp.LocalDate; import org.threeten.bp.jdk8.DefaultInterfaceEra; import org.threeten.bp.jdk8.Jdk8Methods; import org.threeten.bp.temporal.ChronoField; import org.threeten.bp.temporal.TemporalField; import org.threeten.bp.temporal.ValueRange; /** * An era in the Japanese Imperial calendar system. *
* This class defines the valid eras for the Japanese chronology. * Japan introduced the Gregorian calendar starting with Meiji 6. * Only Meiji and later eras are supported; * dates before Meiji 6, January 1 are not supported. *
* The four supported eras are hard-coded. * A single additional era may be registered using {@link #registerEra(LocalDate, String)}. * *
* A new Japanese era can begin at any time. * This method allows one new era to be registered without the need for a new library version. * If needed, callers should assign the result to a static variable accessible * across the application. This must be done once, in early startup code. *
* NOTE: This method does not exist in Java SE 8. * * @param since the date representing the first date of the era, validated not null * @param name the name * @return the {@code JapaneseEra} singleton, not null * @throws DateTimeException if an additional era has already been registered */ public static JapaneseEra registerEra(LocalDate since, String name) { JapaneseEra[] known = KNOWN_ERAS.get(); if (known.length > 5) { throw new DateTimeException("Only one additional Japanese era can be added"); } Jdk8Methods.requireNonNull(since, "since"); Jdk8Methods.requireNonNull(name, "name"); if (!since.isAfter(REIWA.since)) { throw new DateTimeException("Invalid since date for additional Japanese era, must be after Reiwa"); } JapaneseEra era = new JapaneseEra(ADDITIONAL_VALUE, since, name); JapaneseEra[] newArray = Arrays.copyOf(known, 6); newArray[5] = era; if (!KNOWN_ERAS.compareAndSet(known, newArray)) { throw new DateTimeException("Only one additional Japanese era can be added"); } return era; } /** * Obtains an instance of {@code JapaneseEra} from an {@code int} value. *
* The {@link #SHOWA} era that contains 1970-01-01 (ISO calendar system) has the value 1 * Later era is numbered 2 ({@link #HEISEI}). Earlier eras are numbered 0 ({@link #TAISHO}), * -1 ({@link #MEIJI}), only Meiji and later eras are supported. * * @param japaneseEra the era to represent * @return the {@code JapaneseEra} singleton, not null * @throws DateTimeException if the value is invalid */ public static JapaneseEra of(int japaneseEra) { JapaneseEra[] known = KNOWN_ERAS.get(); if (japaneseEra < MEIJI.eraValue || japaneseEra > known[known.length - 1].eraValue) { throw new DateTimeException("japaneseEra is invalid"); } return known[ordinal(japaneseEra)]; } /** * Returns the {@code JapaneseEra} with the name. *
* The string must match exactly the name of the era. * (Extraneous whitespace characters are not permitted.) * * @param japaneseEra the japaneseEra name; non-null * @return the {@code JapaneseEra} singleton, never null * @throws IllegalArgumentException if there is not JapaneseEra with the specified name */ public static JapaneseEra valueOf(String japaneseEra) { Jdk8Methods.requireNonNull(japaneseEra, "japaneseEra"); JapaneseEra[] known = KNOWN_ERAS.get(); for (JapaneseEra era : known) { if (japaneseEra.equals(era.name)) { return era; } } throw new IllegalArgumentException("Era not found: " + japaneseEra); } /** * Returns an array of JapaneseEras. *
* This method may be used to iterate over the JapaneseEras as follows: *
* for (JapaneseEra c : JapaneseEra.values()) * System.out.println(c); ** * @return an array of JapaneseEras */ public static JapaneseEra[] values() { JapaneseEra[] known = KNOWN_ERAS.get(); return Arrays.copyOf(known, known.length); } //----------------------------------------------------------------------- /** * Obtains an instance of {@code JapaneseEra} from a date. * * @param date the date, not null * @return the Era singleton, never null */ static JapaneseEra from(LocalDate date) { if (date.isBefore(MEIJI.since)) { throw new DateTimeException("Date too early: " + date); } JapaneseEra[] known = KNOWN_ERAS.get(); for (int i = known.length - 1; i >= 0; i--) { JapaneseEra era = known[i]; if (date.compareTo(era.since) >= 0) { return era; } } return null; } /** * Returns the index into the arrays from the Era value. * the eraValue is a valid Era number, -999, -1..2. * @param eraValue the era value to convert to the index * @return the index of the current Era */ private static int ordinal(int eraValue) { return eraValue + 1; } /** * Returns the start date of the era. * @return the start date */ LocalDate startDate() { return since; } /** * Returns the end date of the era. * @return the end date */ LocalDate endDate() { int ordinal = ordinal(eraValue); JapaneseEra[] eras = values(); if (ordinal >= eras.length - 1) { return LocalDate.MAX; } return eras[ordinal + 1].startDate().minusDays(1); } //----------------------------------------------------------------------- /** * Returns the numeric value of this {@code JapaneseEra}. *
* The {@link #SHOWA} era that contains 1970-01-01 (ISO calendar system) has the value 1. * Later eras are numbered from 2 ({@link #HEISEI}). * Earlier eras are numbered 0 ({@link #TAISHO}) and -1 ({@link #MEIJI}). * * @return the era value */ @Override public int getValue() { return eraValue; } @Override public ValueRange range(TemporalField field) { if (field == ChronoField.ERA) { return JapaneseChronology.INSTANCE.range(ChronoField.ERA); } return super.range(field); } //----------------------------------------------------------------------- @Override public String toString() { return name; } //----------------------------------------------------------------------- private Object writeReplace() { return new Ser(Ser.JAPANESE_ERA_TYPE, this); } void writeExternal(DataOutput out) throws IOException { out.writeByte(this.getValue()); } static JapaneseEra readExternal(DataInput in) throws IOException { byte eraValue = in.readByte(); return JapaneseEra.of(eraValue); } }