Path: blob/master/src/java.base/share/classes/java/time/chrono/ThaiBuddhistEra.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;6465import java.time.DateTimeException;66import java.time.format.DateTimeFormatterBuilder;67import java.time.format.TextStyle;68import java.util.Locale;6970/**71* An era in the Thai Buddhist calendar system.72* <p>73* The Thai Buddhist calendar system has two eras.74* The current era, for years from 1 onwards, is known as the 'Buddhist' era.75* All previous years, zero or earlier in the proleptic count or one and greater76* in the year-of-era count, are part of the 'Before Buddhist' era.77*78* <table class="striped" style="text-align:left">79* <caption style="display:none">Buddhist years and eras</caption>80* <thead>81* <tr>82* <th scope="col">year-of-era</th>83* <th scope="col">era</th>84* <th scope="col">proleptic-year</th>85* <th scope="col">ISO proleptic-year</th>86* </tr>87* </thead>88* <tbody>89* <tr>90* <td>2</td><td>BE</td><th scope="row">2</th><td>-542</td>91* </tr>92* <tr>93* <td>1</td><td>BE</td><th scope="row">1</th><td>-543</td>94* </tr>95* <tr>96* <td>1</td><td>BEFORE_BE</td><th scope="row">0</th><td>-544</td>97* </tr>98* <tr>99* <td>2</td><td>BEFORE_BE</td><th scope="row">-1</th><td>-545</td>100* </tr>101* </tbody>102* </table>103* <p>104* <b>Do not use {@code ordinal()} to obtain the numeric representation of {@code ThaiBuddhistEra}.105* Use {@code getValue()} instead.</b>106*107* @implSpec108* This is an immutable and thread-safe enum.109*110* @since 1.8111*/112public enum ThaiBuddhistEra implements Era {113114/**115* The singleton instance for the era before the current one, 'Before Buddhist Era',116* which has the numeric value 0.117*/118BEFORE_BE,119/**120* The singleton instance for the current era, 'Buddhist Era',121* which has the numeric value 1.122*/123BE;124125//-----------------------------------------------------------------------126/**127* Obtains an instance of {@code ThaiBuddhistEra} from an {@code int} value.128* <p>129* {@code ThaiBuddhistEra} is an enum representing the Thai Buddhist eras of BEFORE_BE/BE.130* This factory allows the enum to be obtained from the {@code int} value.131*132* @param thaiBuddhistEra the era to represent, from 0 to 1133* @return the BuddhistEra singleton, never null134* @throws DateTimeException if the era is invalid135*/136public static ThaiBuddhistEra of(int thaiBuddhistEra) {137switch (thaiBuddhistEra) {138case 0:139return BEFORE_BE;140case 1:141return BE;142default:143throw new DateTimeException("Invalid era: " + thaiBuddhistEra);144}145}146147//-----------------------------------------------------------------------148/**149* Gets the numeric era {@code int} value.150* <p>151* The era BEFORE_BE has the value 0, while the era BE has the value 1.152*153* @return the era value, from 0 (BEFORE_BE) to 1 (BE)154*/155@Override156public int getValue() {157return ordinal();158}159160/**161* {@inheritDoc}162*163* @param style {@inheritDoc}164* @param locale {@inheritDoc}165*/166@Override167public String getDisplayName(TextStyle style, Locale locale) {168return new DateTimeFormatterBuilder()169.appendText(ERA, style)170.toFormatter(locale)171.withChronology(ThaiBuddhistChronology.INSTANCE)172.format(this == BE ? ThaiBuddhistDate.of(1, 1, 1) : ThaiBuddhistDate.of(0, 1, 1));173}174175}176177178