• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  *  * Neither the name of JSR-310 nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.threeten.bp.chrono;
33 
34 import static org.threeten.bp.temporal.ChronoField.ERA;
35 
36 import java.io.DataInput;
37 import java.io.DataOutput;
38 import java.io.IOException;
39 import java.util.Locale;
40 
41 import org.threeten.bp.DateTimeException;
42 import org.threeten.bp.format.DateTimeFormatterBuilder;
43 import org.threeten.bp.format.TextStyle;
44 import org.threeten.bp.temporal.ChronoField;
45 import org.threeten.bp.temporal.ChronoUnit;
46 import org.threeten.bp.temporal.Temporal;
47 import org.threeten.bp.temporal.TemporalField;
48 import org.threeten.bp.temporal.TemporalQueries;
49 import org.threeten.bp.temporal.TemporalQuery;
50 import org.threeten.bp.temporal.UnsupportedTemporalTypeException;
51 import org.threeten.bp.temporal.ValueRange;
52 
53 /**
54  * An era in the Thai Buddhist calendar system.
55  * <p>
56  * The Thai Buddhist calendar system has two eras.
57  * <p>
58  * <b>Do not use ordinal() to obtain the numeric representation of a ThaiBuddhistEra
59  * instance. Use getValue() instead.</b>
60  *
61  * <h3>Specification for implementors</h3>
62  * This is an immutable and thread-safe enum.
63  */
64 public enum ThaiBuddhistEra implements Era {
65 
66     /**
67      * The singleton instance for the era before the current one, 'Before Buddhist Era',
68      * which has the value 0.
69      */
70     BEFORE_BE,
71     /**
72      * The singleton instance for the current era, 'Buddhist Era', which has the value 1.
73      */
74     BE;
75 
76     //-----------------------------------------------------------------------
77     /**
78      * Obtains an instance of {@code ThaiBuddhistEra} from a value.
79      * <p>
80      * The current era (from ISO year -543 onwards) has the value 1
81      * The previous era has the value 0.
82      *
83      * @param thaiBuddhistEra  the era to represent, from 0 to 1
84      * @return the BuddhistEra singleton, never null
85      * @throws IllegalCalendarFieldValueException if the era is invalid
86      */
of(int thaiBuddhistEra)87     public static ThaiBuddhistEra of(int thaiBuddhistEra) {
88         switch (thaiBuddhistEra) {
89             case 0:
90                 return BEFORE_BE;
91             case 1:
92                 return BE;
93             default:
94                 throw new DateTimeException("Era is not valid for ThaiBuddhistEra");
95         }
96     }
97 
98     //-----------------------------------------------------------------------
99     /**
100      * Gets the era numeric value.
101      * <p>
102      * The current era (from ISO year -543 onwards) has the value 1
103      * The previous era has the value 0.
104      *
105      * @return the era value, from 0 (BEFORE_BE) to 1 (BE)
106      */
107     @Override
getValue()108     public int getValue() {
109         return ordinal();
110     }
111 
112     //-----------------------------------------------------------------------
113     @Override
isSupported(TemporalField field)114     public boolean isSupported(TemporalField field) {
115         if (field instanceof ChronoField) {
116             return field == ERA;
117         }
118         return field != null && field.isSupportedBy(this);
119     }
120 
121     @Override
range(TemporalField field)122     public ValueRange range(TemporalField field) {
123         if (field == ERA) {
124             return field.range();
125         } else if (field instanceof ChronoField) {
126             throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
127         }
128         return field.rangeRefinedBy(this);
129     }
130 
131     @Override
get(TemporalField field)132     public int get(TemporalField field) {
133         if (field == ERA) {
134             return getValue();
135         }
136         return range(field).checkValidIntValue(getLong(field), field);
137     }
138 
139     @Override
getLong(TemporalField field)140     public long getLong(TemporalField field) {
141         if (field == ERA) {
142             return getValue();
143         } else if (field instanceof ChronoField) {
144             throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
145         }
146         return field.getFrom(this);
147     }
148 
149     //-------------------------------------------------------------------------
150     @Override
adjustInto(Temporal temporal)151     public Temporal adjustInto(Temporal temporal) {
152         return temporal.with(ERA, getValue());
153     }
154 
155     @SuppressWarnings("unchecked")
156     @Override
query(TemporalQuery<R> query)157     public <R> R query(TemporalQuery<R> query) {
158         if (query == TemporalQueries.precision()) {
159             return (R) ChronoUnit.ERAS;
160         }
161         if (query == TemporalQueries.chronology() || query == TemporalQueries.zone() ||
162                 query == TemporalQueries.zoneId() || query == TemporalQueries.offset() ||
163                 query == TemporalQueries.localDate() || query == TemporalQueries.localTime()) {
164             return null;
165         }
166         return query.queryFrom(this);
167     }
168 
169     //-----------------------------------------------------------------------
170     @Override
getDisplayName(TextStyle style, Locale locale)171     public String getDisplayName(TextStyle style, Locale locale) {
172         return new DateTimeFormatterBuilder().appendText(ERA, style).toFormatter(locale).format(this);
173     }
174 
175     //-----------------------------------------------------------------------
writeReplace()176     private Object writeReplace() {
177         return new Ser(Ser.THAIBUDDHIST_ERA_TYPE, this);
178     }
179 
writeExternal(DataOutput out)180     void writeExternal(DataOutput out) throws IOException {
181         out.writeByte(this.getValue());
182     }
183 
readExternal(DataInput in)184     static ThaiBuddhistEra readExternal(DataInput in) throws IOException {
185         byte eraValue = in.readByte();
186         return ThaiBuddhistEra.of(eraValue);
187     }
188 
189 }
190