Path: blob/master/src/java.base/share/classes/java/time/chrono/Era.java
41159 views
/*1* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file:30*31* Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos32*33* All rights reserved.34*35* Redistribution and use in source and binary forms, with or without36* modification, are permitted provided that the following conditions are met:37*38* * Redistributions of source code must retain the above copyright notice,39* this list of conditions and the following disclaimer.40*41* * Redistributions in binary form must reproduce the above copyright notice,42* this list of conditions and the following disclaimer in the documentation43* and/or other materials provided with the distribution.44*45* * Neither the name of JSR-310 nor the names of its contributors46* may be used to endorse or promote products derived from this software47* without specific prior written permission.48*49* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS50* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT51* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR52* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR53* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,54* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,55* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR56* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF57* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING58* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS59* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.60*/61package java.time.chrono;6263import static java.time.temporal.ChronoField.ERA;64import static java.time.temporal.ChronoUnit.ERAS;6566import java.time.DateTimeException;67import java.time.temporal.UnsupportedTemporalTypeException;68import java.time.format.DateTimeFormatterBuilder;69import java.time.format.TextStyle;70import java.time.temporal.ChronoField;71import java.time.temporal.Temporal;72import java.time.temporal.TemporalAccessor;73import java.time.temporal.TemporalAdjuster;74import java.time.temporal.TemporalField;75import java.time.temporal.TemporalQueries;76import java.time.temporal.TemporalQuery;77import java.time.temporal.ValueRange;78import java.util.Locale;7980/**81* An era of the time-line.82* <p>83* Most calendar systems have a single epoch dividing the time-line into two eras.84* However, some calendar systems, have multiple eras, such as one for the reign85* of each leader.86* In all cases, the era is conceptually the largest division of the time-line.87* Each chronology defines the Era's that are known Eras and a88* {@link Chronology#eras Chronology.eras} to get the valid eras.89* <p>90* For example, the Thai Buddhist calendar system divides time into two eras,91* before and after a single date. By contrast, the Japanese calendar system92* has one era for the reign of each Emperor.93* <p>94* Instances of {@code Era} may be compared using the {@code ==} operator.95*96* @implSpec97* This interface must be implemented with care to ensure other classes operate correctly.98* All implementations must be singletons - final, immutable and thread-safe.99* It is recommended to use an enum whenever possible.100*101* @since 1.8102*/103public interface Era extends TemporalAccessor, TemporalAdjuster {104105/**106* Gets the numeric value associated with the era as defined by the chronology.107* Each chronology defines the predefined Eras and methods to list the Eras108* of the chronology.109* <p>110* All fields, including eras, have an associated numeric value.111* The meaning of the numeric value for era is determined by the chronology112* according to these principles:113* <ul>114* <li>The era in use at the epoch 1970-01-01 (ISO) has the value 1.115* <li>Later eras have sequentially higher values.116* <li>Earlier eras have sequentially lower values, which may be negative.117* </ul>118*119* @return the numeric era value120*/121int getValue();122123//-----------------------------------------------------------------------124/**125* Checks if the specified field is supported.126* <p>127* This checks if this era can be queried for the specified field.128* If false, then calling the {@link #range(TemporalField) range} and129* {@link #get(TemporalField) get} methods will throw an exception.130* <p>131* If the field is a {@link ChronoField} then the query is implemented here.132* The {@code ERA} field returns true.133* All other {@code ChronoField} instances will return false.134* <p>135* If the field is not a {@code ChronoField}, then the result of this method136* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}137* passing {@code this} as the argument.138* Whether the field is supported is determined by the field.139*140* @param field the field to check, null returns false141* @return true if the field is supported on this era, false if not142*/143@Override144default boolean isSupported(TemporalField field) {145if (field instanceof ChronoField) {146return field == ERA;147}148return field != null && field.isSupportedBy(this);149}150151/**152* Gets the range of valid values for the specified field.153* <p>154* The range object expresses the minimum and maximum valid values for a field.155* This era is used to enhance the accuracy of the returned range.156* If it is not possible to return the range, because the field is not supported157* or for some other reason, an exception is thrown.158* <p>159* If the field is a {@link ChronoField} then the query is implemented here.160* The {@code ERA} field returns the range.161* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.162* <p>163* If the field is not a {@code ChronoField}, then the result of this method164* is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}165* passing {@code this} as the argument.166* Whether the range can be obtained is determined by the field.167* <p>168* The default implementation must return a range for {@code ERA} from169* zero to one, suitable for two era calendar systems such as ISO.170*171* @param field the field to query the range for, not null172* @return the range of valid values for the field, not null173* @throws DateTimeException if the range for the field cannot be obtained174* @throws UnsupportedTemporalTypeException if the unit is not supported175*/176@Override // override for Javadoc177default ValueRange range(TemporalField field) {178return TemporalAccessor.super.range(field);179}180181/**182* Gets the value of the specified field from this era as an {@code int}.183* <p>184* This queries this era for the value of the specified field.185* The returned value will always be within the valid range of values for the field.186* If it is not possible to return the value, because the field is not supported187* or for some other reason, an exception is thrown.188* <p>189* If the field is a {@link ChronoField} then the query is implemented here.190* The {@code ERA} field returns the value of the era.191* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.192* <p>193* If the field is not a {@code ChronoField}, then the result of this method194* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}195* passing {@code this} as the argument. Whether the value can be obtained,196* and what the value represents, is determined by the field.197*198* @param field the field to get, not null199* @return the value for the field200* @throws DateTimeException if a value for the field cannot be obtained or201* the value is outside the range of valid values for the field202* @throws UnsupportedTemporalTypeException if the field is not supported or203* the range of values exceeds an {@code int}204* @throws ArithmeticException if numeric overflow occurs205*/206@Override // override for Javadoc and performance207default int get(TemporalField field) {208if (field == ERA) {209return getValue();210}211return TemporalAccessor.super.get(field);212}213214/**215* Gets the value of the specified field from this era as a {@code long}.216* <p>217* This queries this era for the value of the specified field.218* If it is not possible to return the value, because the field is not supported219* or for some other reason, an exception is thrown.220* <p>221* If the field is a {@link ChronoField} then the query is implemented here.222* The {@code ERA} field returns the value of the era.223* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.224* <p>225* If the field is not a {@code ChronoField}, then the result of this method226* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}227* passing {@code this} as the argument. Whether the value can be obtained,228* and what the value represents, is determined by the field.229*230* @param field the field to get, not null231* @return the value for the field232* @throws DateTimeException if a value for the field cannot be obtained233* @throws UnsupportedTemporalTypeException if the field is not supported234* @throws ArithmeticException if numeric overflow occurs235*/236@Override237default long getLong(TemporalField field) {238if (field == ERA) {239return getValue();240} else if (field instanceof ChronoField) {241throw new UnsupportedTemporalTypeException("Unsupported field: " + field);242}243return field.getFrom(this);244}245246//-----------------------------------------------------------------------247/**248* Queries this era using the specified query.249* <p>250* This queries this era using the specified query strategy object.251* The {@code TemporalQuery} object defines the logic to be used to252* obtain the result. Read the documentation of the query to understand253* what the result of this method will be.254* <p>255* The result of this method is obtained by invoking the256* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the257* specified query passing {@code this} as the argument.258*259* @param <R> the type of the result260* @param query the query to invoke, not null261* @return the query result, null may be returned (defined by the query)262* @throws DateTimeException if unable to query (defined by the query)263* @throws ArithmeticException if numeric overflow occurs (defined by the query)264*/265@SuppressWarnings("unchecked")266@Override267default <R> R query(TemporalQuery<R> query) {268if (query == TemporalQueries.precision()) {269return (R) ERAS;270}271return TemporalAccessor.super.query(query);272}273274/**275* Adjusts the specified temporal object to have the same era as this object.276* <p>277* This returns a temporal object of the same observable type as the input278* with the era changed to be the same as this.279* <p>280* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}281* passing {@link ChronoField#ERA} as the field.282* <p>283* In most cases, it is clearer to reverse the calling pattern by using284* {@link Temporal#with(TemporalAdjuster)}:285* <pre>286* // these two lines are equivalent, but the second approach is recommended287* temporal = thisEra.adjustInto(temporal);288* temporal = temporal.with(thisEra);289* </pre>290* <p>291* This instance is immutable and unaffected by this method call.292*293* @param temporal the target object to be adjusted, not null294* @return the adjusted object, not null295* @throws DateTimeException if unable to make the adjustment296* @throws ArithmeticException if numeric overflow occurs297*/298@Override299default Temporal adjustInto(Temporal temporal) {300return temporal.with(ERA, getValue());301}302303//-----------------------------------------------------------------------304/**305* Gets the textual representation of this era.306* <p>307* This returns the textual name used to identify the era,308* suitable for presentation to the user.309* The parameters control the style of the returned text and the locale.310* <p>311* If no textual mapping is found then the {@link #getValue() numeric value} is returned.312*313* @apiNote This default implementation is suitable for most implementations.314*315* @param style the style of the text required, not null316* @param locale the locale to use, not null317* @return the text value of the era, not null318*/319default String getDisplayName(TextStyle style, Locale locale) {320return new DateTimeFormatterBuilder().appendText(ERA, style).toFormatter(locale).format(this);321}322323// NOTE: methods to convert year-of-era/proleptic-year cannot be here as they may depend on month/day (Japanese)324}325326327