--- /dev/null 2013-01-18 16:17:08.886776012 -0800 +++ new/src/share/classes/java/time/temporal/Era.java 2013-01-22 16:58:10.000000000 -0800 @@ -0,0 +1,359 @@ +/* + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * 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 + * 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 java.time.temporal; + +import static java.time.temporal.ChronoField.ERA; +import static java.time.temporal.ChronoUnit.ERAS; + +import java.time.DateTimeException; +import java.time.format.DateTimeFormatterBuilder; +import java.time.format.TextStyle; +import java.util.Locale; + +/** + * An era of the time-line. + *
+ * Most calendar systems have a single epoch dividing the time-line into two eras. + * However, some calendar systems, have multiple eras, such as one for the reign + * of each leader. + * In all cases, the era is conceptually the largest division of the time-line. + * Each chronology defines the Era's that are known Eras and a + * {@link Chrono#eras Chrono.eras} to get the valid eras. + *
+ * For example, the Thai Buddhist calendar system divides time into two eras, + * before and after a single date. By contrast, the Japanese calendar system + * has one era for the reign of each Emperor. + *
+ * Instances of {@code Era} may be compared using the {@code ==} operator. + * + *
+ * All fields, including eras, have an associated numeric value. + * The meaning of the numeric value for era is determined by the chronology + * according to these principles: + *
+ * + * @return the numeric era value + */ + int getValue(); + + /** + * Gets the chronology of this era. + *
+ * The {@code Chrono} represents the calendar system in use. + * This always returns the standard form of the chronology. + * + * @return the chronology, not null + */ + C getChrono(); + + //----------------------------------------------------------------------- + /** + * Obtains a date in this era given the year-of-era, month, and day. + *
+ * This era is combined with the given date fields to form a date. + * The year specified must be the year-of-era. + * Methods to create a date from the proleptic-year are on {@code Chrono}. + * This always uses the standard form of the chronology. + *
+ * This default implementation invokes the factory method on {@link Chrono}.
+ *
+ * @param yearOfEra the calendar system year-of-era
+ * @param month the calendar system month-of-year
+ * @param day the calendar system day-of-month
+ * @return a local date based on this era and the specified year-of-era, month and day
+ */
+ public default ChronoLocalDate
+ * This era is combined with the given date fields to form a date.
+ * The year specified must be the year-of-era.
+ * Methods to create a date from the proleptic-year are on {@code Chrono}.
+ * This always uses the standard form of the chronology.
+ *
+ * This default implementation invokes the factory method on {@link Chrono}.
+ *
+ * @param yearOfEra the calendar system year-of-era
+ * @param dayOfYear the calendar system day-of-year
+ * @return a local date based on this era and the specified year-of-era and day-of-year
+ */
+ public default ChronoLocalDate
+ * This checks if this era can be queried for the specified field.
+ * If false, then calling the {@link #range(TemporalField) range} and
+ * {@link #get(TemporalField) get} methods will throw an exception.
+ *
+ * If the field is a {@link ChronoField} then the query is implemented here.
+ * The {@code ERA} field returns true.
+ * All other {@code ChronoField} instances will return false.
+ *
+ * If the field is not a {@code ChronoField}, then the result of this method
+ * is obtained by invoking {@code TemporalField.doIsSupported(TemporalAccessor)}
+ * passing {@code this} as the argument.
+ * Whether the field is supported is determined by the field.
+ *
+ * @param field the field to check, null returns false
+ * @return true if the field is supported on this era, false if not
+ */
+ @Override
+ public default boolean isSupported(TemporalField field) {
+ if (field instanceof ChronoField) {
+ return field == ERA;
+ }
+ return field != null && field.doIsSupported(this);
+ }
+
+ /**
+ * Gets the range of valid values for the specified field.
+ *
+ * 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.
+ *
+ * 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 a {@code DateTimeException}.
+ *
+ * If the field is not a {@code ChronoField}, then the result of this method
+ * is obtained by invoking {@code TemporalField.doRange(TemporalAccessor)}
+ * passing {@code this} as the argument.
+ * Whether the range can be obtained is determined by the field.
+ *
+ * @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
+ */
+ @Override // override for Javadoc
+ public default ValueRange range(TemporalField field) {
+ return TemporalAccessor.super.range(field);
+ }
+
+ /**
+ * Gets the value of the specified field from this era as an {@code int}.
+ *
+ * This queries this era for the value for the specified field.
+ * The returned value will always be within the valid range of values for the field.
+ * If it is not possible to return the value, because the field is not supported
+ * or for some other reason, an exception is thrown.
+ *
+ * If the field is a {@link ChronoField} then the query is implemented here.
+ * The {@code ERA} field returns the value of the era.
+ * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
+ *
+ * If the field is not a {@code ChronoField}, then the result of this method
+ * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
+ * passing {@code this} as the argument. Whether the value can be obtained,
+ * and what the value represents, is determined by the field.
+ *
+ * @param field the field to get, not null
+ * @return the value for the field
+ * @throws DateTimeException if a value for the field cannot be obtained
+ * @throws ArithmeticException if numeric overflow occurs
+ */
+ @Override // override for Javadoc and performance
+ public default int get(TemporalField field) {
+ if (field == ERA) {
+ return getValue();
+ }
+ return range(field).checkValidIntValue(getLong(field), field);
+ }
+
+ /**
+ * Gets the value of the specified field from this era as a {@code long}.
+ *
+ * This queries this era for the value for the specified field.
+ * If it is not possible to return the value, because the field is not supported
+ * or for some other reason, an exception is thrown.
+ *
+ * If the field is a {@link ChronoField} then the query is implemented here.
+ * The {@code ERA} field returns the value of the era.
+ * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
+ *
+ * If the field is not a {@code ChronoField}, then the result of this method
+ * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
+ * passing {@code this} as the argument. Whether the value can be obtained,
+ * and what the value represents, is determined by the field.
+ *
+ * @param field the field to get, not null
+ * @return the value for the field
+ * @throws DateTimeException if a value for the field cannot be obtained
+ * @throws ArithmeticException if numeric overflow occurs
+ */
+ @Override
+ public default long getLong(TemporalField field) {
+ if (field == ERA) {
+ return getValue();
+ } else if (field instanceof ChronoField) {
+ throw new DateTimeException("Unsupported field: " + field.getName());
+ }
+ return field.doGet(this);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Queries this era using the specified query.
+ *
+ * This queries this era using the specified query strategy object.
+ * The {@code TemporalQuery} object defines the logic to be used to
+ * obtain the result. Read the documentation of the query to understand
+ * what the result of this method will be.
+ *
+ * The result of this method is obtained by invoking the
+ * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
+ * specified query passing {@code this} as the argument.
+ *
+ * @param
+ * This returns a temporal object of the same observable type as the input
+ * with the era changed to be the same as this.
+ *
+ * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
+ * passing {@link ChronoField#ERA} as the field.
+ *
+ * In most cases, it is clearer to reverse the calling pattern by using
+ * {@link Temporal#with(TemporalAdjuster)}:
+ *
+ * This instance is immutable and unaffected by this method call.
+ *
+ * @param temporal the target object to be adjusted, not null
+ * @return the adjusted object, not null
+ * @throws DateTimeException if unable to make the adjustment
+ * @throws ArithmeticException if numeric overflow occurs
+ */
+ @Override
+ public default Temporal adjustInto(Temporal temporal) {
+ return temporal.with(ERA, getValue());
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets the textual representation of this era.
+ *
+ * This returns the textual name used to identify the era.
+ * The parameters control the style of the returned text and the locale.
+ *
+ * If no textual mapping is found then the {@link #getValue() numeric value} is returned.
+ *
+ * This default implementation is suitable for all implementations.
+ *
+ * @param style the style of the text required, not null
+ * @param locale the locale to use, not null
+ * @return the text value of the era, not null
+ */
+ public default String getText(TextStyle style, Locale locale) {
+ return new DateTimeFormatterBuilder().appendText(ERA, style).toFormatter(locale).print(this);
+ }
+
+ // NOTE: methods to convert year-of-era/proleptic-year cannot be here as they may depend on month/day (Japanese)
+}
+ * // these two lines are equivalent, but the second approach is recommended
+ * temporal = thisEra.adjustInto(temporal);
+ * temporal = temporal.with(thisEra);
+ *
+ *