jdk/src/share/classes/java/time/temporal/Era.java
changeset 15658 55b829ca2334
parent 15657 c588664d547e
child 15659 e575dab44ff5
--- a/jdk/src/share/classes/java/time/temporal/Era.java	Tue Feb 12 16:02:14 2013 +0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,359 +0,0 @@
-/*
- * 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.
- * <p>
- * 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.
- * <p>
- * 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.
- * <p>
- * Instances of {@code Era} may be compared using the {@code ==} operator.
- *
- * <h3>Specification for implementors</h3>
- * This interface must be implemented with care to ensure other classes operate correctly.
- * All implementations must be singletons - final, immutable and thread-safe.
- * It is recommended to use an enum whenever possible.
- *
- * @param <C> the chronology of the era
- * @since 1.8
- */
-public interface Era<C extends Chrono<C>> extends TemporalAccessor, TemporalAdjuster {
-
-    /**
-     * Gets the numeric value associated with the era as defined by the chronology.
-     * Each chronology defines the predefined Eras and methods to list the Eras
-     * of the chronology.
-     * <p>
-     * 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:
-     * <p><ul>
-     * <li>The era in use at the epoch 1970-01-01 (ISO) has the value 1.
-     * <li>Later eras have sequentially higher values.
-     * <li>Earlier eras have sequentially lower values, which may be negative.
-     * </ul><p>
-     *
-     * @return the numeric era value
-     */
-    int getValue();
-
-    /**
-     * Gets the chronology of this era.
-     * <p>
-     * 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.
-     * <p>
-     * 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.
-     * <p>
-     * 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<C> date(int yearOfEra, int month, int day) {
-        return getChrono().date(this, yearOfEra, month, day);
-    }
-
-
-    /**
-     * Obtains a date in this era given year-of-era and day-of-year fields.
-     * <p>
-     * 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.
-     * <p>
-     * 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<C> dateYearDay(int yearOfEra, int dayOfYear) {
-        return getChrono().dateYearDay(this, yearOfEra, dayOfYear);
-    }
-
-    //-----------------------------------------------------------------------
-    /**
-     * Checks if the specified field is supported.
-     * <p>
-     * 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.
-     * <p>
-     * 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.
-     * <p>
-     * 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.
-     * <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 a {@code DateTimeException}.
-     * <p>
-     * 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}.
-     * <p>
-     * 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.
-     * <p>
-     * 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}.
-     * <p>
-     * 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}.
-     * <p>
-     * 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.
-     * <p>
-     * 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}.
-     * <p>
-     * 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.
-     * <p>
-     * 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.
-     * <p>
-     * 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 <R> the type of the result
-     * @param query  the query to invoke, not null
-     * @return the query result, null may be returned (defined by the query)
-     * @throws DateTimeException if unable to query (defined by the query)
-     * @throws ArithmeticException if numeric overflow occurs (defined by the query)
-     */
-    @SuppressWarnings("unchecked")
-    @Override
-    public default <R> R query(TemporalQuery<R> query) {
-        if (query == Queries.chrono()) {
-            return (R) getChrono();
-        } else if (query == Queries.precision()) {
-            return (R) ERAS;
-        }
-        return TemporalAccessor.super.query(query);
-    }
-
-    /**
-     * Adjusts the specified temporal object to have the same era as this object.
-     * <p>
-     * This returns a temporal object of the same observable type as the input
-     * with the era changed to be the same as this.
-     * <p>
-     * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
-     * passing {@link ChronoField#ERA} as the field.
-     * <p>
-     * In most cases, it is clearer to reverse the calling pattern by using
-     * {@link Temporal#with(TemporalAdjuster)}:
-     * <pre>
-     *   // these two lines are equivalent, but the second approach is recommended
-     *   temporal = thisEra.adjustInto(temporal);
-     *   temporal = temporal.with(thisEra);
-     * </pre>
-     * <p>
-     * 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.
-     * <p>
-     * This returns the textual name used to identify the era.
-     * The parameters control the style of the returned text and the locale.
-     * <p>
-     * If no textual mapping is found then the {@link #getValue() numeric value} is returned.
-     * <p>
-     * 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)
-}