src/share/classes/java/time/chrono/JapaneseEra.java
Print this page
*** 22,31 ****
--- 22,36 ----
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
+ * This file is available under and governed by the GNU General Public
+ * License version 2 only, as published by the Free Software Foundation.
+ * However, the following notice accompanied the original version of this
+ * file:
+ *
* Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
*** 54,72 ****
--- 59,84 ----
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package java.time.chrono;
+ import static java.time.temporal.ChronoField.ERA;
+
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.time.DateTimeException;
import java.time.LocalDate;
+ import java.time.temporal.ChronoField;
+ import java.time.temporal.TemporalField;
+ import java.time.temporal.UnsupportedTemporalTypeException;
+ import java.time.temporal.ValueRange;
import java.util.Arrays;
+ import java.util.Objects;
import sun.util.calendar.CalendarDate;
/**
* An era in the Japanese Imperial calendar system.
*** 82,92 ****
* <h3>Specification for implementors</h3>
* This class is immutable and thread-safe.
*
* @since 1.8
*/
! final class JapaneseEra
implements Era, Serializable {
// The offset value to 0-based index from the era value.
// i.e., getValue() + ERA_OFFSET == 0-based index; except that -999 is mapped to zero
static final int ERA_OFFSET = 2;
--- 94,104 ----
* <h3>Specification for implementors</h3>
* This class is immutable and thread-safe.
*
* @since 1.8
*/
! public final class JapaneseEra
implements Era, Serializable {
// The offset value to 0-based index from the era value.
// i.e., getValue() + ERA_OFFSET == 0-based index; except that -999 is mapped to zero
static final int ERA_OFFSET = 2;
*** 200,233 ****
return ERA_CONFIG[ordinal(eraValue)];
}
//-----------------------------------------------------------------------
/**
! * Obtains an instance of {@code JapaneseEra} from a value.
* <p>
* 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. The prior to Meiji,
* {@link #SEIREKI} is used.
*
* @param japaneseEra the era to represent
! * @return the {@code JapaneseEra} singleton, never null
! * @throws DateTimeException if {@code japaneseEra} is invalid
*/
public static JapaneseEra of(int japaneseEra) {
if (japaneseEra != SEIREKI.eraValue &&
(japaneseEra < MEIJI.eraValue || japaneseEra > HEISEI.eraValue)) {
! throw new DateTimeException("japaneseEra is invalid");
}
return KNOWN_ERAS[ordinal(japaneseEra)];
}
/**
* Returns an array of JapaneseEras.
* @return an array of JapaneseEras
*/
! static JapaneseEra[] values() {
return Arrays.copyOf(KNOWN_ERAS, KNOWN_ERAS.length);
}
//-----------------------------------------------------------------------
/**
--- 212,272 ----
return ERA_CONFIG[ordinal(eraValue)];
}
//-----------------------------------------------------------------------
/**
! * Obtains an instance of {@code JapaneseEra} from an {@code int} value.
* <p>
* 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. The prior to Meiji,
* {@link #SEIREKI} is used.
*
* @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) {
if (japaneseEra != SEIREKI.eraValue &&
(japaneseEra < MEIJI.eraValue || japaneseEra > HEISEI.eraValue)) {
! throw new DateTimeException("Invalid era: " + japaneseEra);
}
return KNOWN_ERAS[ordinal(japaneseEra)];
}
/**
+ * Returns the {@code JapaneseEra} with the name.
+ * <p>
+ * 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) {
+ Objects.requireNonNull(japaneseEra, "japaneseEra");
+ for (JapaneseEra era : KNOWN_ERAS) {
+ if (era.getName().equals(japaneseEra)) {
+ return era;
+ }
+ }
+ throw new IllegalArgumentException("japaneseEra is invalid");
+ }
+
+ /**
* Returns an array of JapaneseEras.
+ * <p>
+ * This method may be used to iterate over the JapaneseEras as follows:
+ * <pre>
+ * for (JapaneseEra c : JapaneseEra.values())
+ * System.out.println(c);
+ * </pre>
+ *
* @return an array of JapaneseEras
*/
! public static JapaneseEra[] values() {
return Arrays.copyOf(KNOWN_ERAS, KNOWN_ERAS.length);
}
//-----------------------------------------------------------------------
/**
*** 266,285 ****
}
/**
* 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 == SEIREKI.eraValue) ? 0 : eravalue + ERA_OFFSET;
}
//-----------------------------------------------------------------------
/**
! * Returns the numeric value of this {@code JapaneseEra}.
* <p>
* 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}), -1 ({@link #MEIJI}), and -999 ({@link #SEIREKI}).
*
--- 305,325 ----
}
/**
* 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 == SEIREKI.eraValue) ? 0 : eraValue + ERA_OFFSET;
}
//-----------------------------------------------------------------------
/**
! * Gets the numeric era {@code int} value.
* <p>
* 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}), -1 ({@link #MEIJI}), and -999 ({@link #SEIREKI}).
*
*** 288,300 ****
@Override
public int getValue() {
return eraValue;
}
! @Override
! public JapaneseChronology getChronology() {
! return JapaneseChronology.INSTANCE;
}
//-----------------------------------------------------------------------
String getAbbreviation() {
int index = ordinal(getValue());
--- 328,369 ----
@Override
public int getValue() {
return eraValue;
}
! //-----------------------------------------------------------------------
! /**
! * Gets the range of valid values for the specified field.
! * <p>
! * The range object expresses the minimum and maximum valid values for a field.
! * This era is used to enhance the accuracy of the returned range.
! * If it is not possible to return the range, because the field is not supported
! * or for some other reason, an exception is thrown.
! * <p>
! * If the field is a {@link ChronoField} then the query is implemented here.
! * The {@code ERA} field returns the range.
! * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
! * <p>
! * If the field is not a {@code ChronoField}, then the result of this method
! * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
! * passing {@code this} as the argument.
! * Whether the range can be obtained is determined by the field.
! * <p>
! * The range of valid Japanese eras can change over time due to the nature
! * of the Japanese calendar system.
! *
! * @param field the field to query the range for, not null
! * @return the range of valid values for the field, not null
! * @throws DateTimeException if the range for the field cannot be obtained
! * @throws UnsupportedTemporalTypeException if the unit is not supported
! */
! @Override // override as super would return range from 0 to 1
! public ValueRange range(TemporalField field) {
! if (field == ERA) {
! return JapaneseChronology.INSTANCE.range(ERA);
! }
! return Era.super.range(field);
}
//-----------------------------------------------------------------------
String getAbbreviation() {
int index = ordinal(getValue());