1 /*
2  * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /*
27  * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
28  *
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions are met:
33  *
34  *  * Redistributions of source code must retain the above copyright notice,
35  *    this list of conditions and the following disclaimer.
36  *
37  *  * Redistributions in binary form must reproduce the above copyright notice,
38  *    this list of conditions and the following disclaimer in the documentation
39  *    and/or other materials provided with the distribution.
40  *
41  *  * Neither the name of JSR-310 nor the names of its contributors
42  *    may be used to endorse or promote products derived from this software
43  *    without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56  */
57 package java.time.chrono;
58 
59 import java.io.InvalidObjectException;
60 import static java.time.temporal.ChronoField.PROLEPTIC_MONTH;
61 import static java.time.temporal.ChronoField.YEAR;
62 
63 import java.io.ObjectInputStream;
64 import java.io.Serializable;
65 import java.time.Clock;
66 import java.time.DateTimeException;
67 import java.time.Instant;
68 import java.time.LocalDate;
69 import java.time.ZoneId;
70 import java.time.format.ResolverStyle;
71 import java.time.temporal.ChronoField;
72 import java.time.temporal.TemporalAccessor;
73 import java.time.temporal.TemporalField;
74 import java.time.temporal.ValueRange;
75 import java.util.Arrays;
76 import java.util.List;
77 import java.util.Locale;
78 import java.util.Map;
79 
80 /**
81  * The Minguo calendar system.
82  * <p>
83  * This chronology defines the rules of the Minguo calendar system.
84  * This calendar system is primarily used in the Republic of China, often known as Taiwan.
85  * Dates are aligned such that {@code 0001-01-01 (Minguo)} is {@code 1912-01-01 (ISO)}.
86  * <p>
87  * The fields are defined as follows:
88  * <ul>
89  * <li>era - There are two eras, the current 'Republic' (ERA_ROC) and the previous era (ERA_BEFORE_ROC).
90  * <li>year-of-era - The year-of-era for the current era increases uniformly from the epoch at year one.
91  *  For the previous era the year increases from one as time goes backwards.
92  *  The value for the current era is equal to the ISO proleptic-year minus 1911.
93  * <li>proleptic-year - The proleptic year is the same as the year-of-era for the
94  *  current era. For the previous era, years have zero, then negative values.
95  *  The value is equal to the ISO proleptic-year minus 1911.
96  * <li>month-of-year - The Minguo month-of-year exactly matches ISO.
97  * <li>day-of-month - The Minguo day-of-month exactly matches ISO.
98  * <li>day-of-year - The Minguo day-of-year exactly matches ISO.
99  * <li>leap-year - The Minguo leap-year pattern exactly matches ISO, such that the two calendars
100  *  are never out of step.
101  * </ul>
102  *
103  * @implSpec
104  * This class is immutable and thread-safe.
105  *
106  * @since 1.8
107  */
108 public final class MinguoChronology extends AbstractChronology implements Serializable {
109 
110     /**
111      * Singleton instance for the Minguo chronology.
112      */
113     public static final MinguoChronology INSTANCE = new MinguoChronology();
114 
115     /**
116      * Serialization version.
117      */
118     private static final long serialVersionUID = 1039765215346859963L;
119     /**
120      * The difference in years between ISO and Minguo.
121      */
122     static final int YEARS_DIFFERENCE = 1911;
123 
124     /**
125      * Restricted constructor.
126      */
MinguoChronology()127     private MinguoChronology() {
128     }
129 
130     //-----------------------------------------------------------------------
131     /**
132      * Gets the ID of the chronology - 'Minguo'.
133      * <p>
134      * The ID uniquely identifies the {@code Chronology}.
135      * It can be used to lookup the {@code Chronology} using {@link Chronology#of(String)}.
136      *
137      * @return the chronology ID - 'Minguo'
138      * @see #getCalendarType()
139      */
140     @Override
getId()141     public String getId() {
142         return "Minguo";
143     }
144 
145     /**
146      * Gets the calendar type of the underlying calendar system - 'roc'.
147      * <p>
148      * The calendar type is an identifier defined by the
149      * <em>Unicode Locale Data Markup Language (LDML)</em> specification.
150      * It can be used to lookup the {@code Chronology} using {@link Chronology#of(String)}.
151      * It can also be used as part of a locale, accessible via
152      * {@link Locale#getUnicodeLocaleType(String)} with the key 'ca'.
153      *
154      * @return the calendar system type - 'roc'
155      * @see #getId()
156      */
157     @Override
getCalendarType()158     public String getCalendarType() {
159         return "roc";
160     }
161 
162     //-----------------------------------------------------------------------
163     /**
164      * Obtains a local date in Minguo calendar system from the
165      * era, year-of-era, month-of-year and day-of-month fields.
166      *
167      * @param era  the Minguo era, not null
168      * @param yearOfEra  the year-of-era
169      * @param month  the month-of-year
170      * @param dayOfMonth  the day-of-month
171      * @return the Minguo local date, not null
172      * @throws DateTimeException if unable to create the date
173      * @throws ClassCastException if the {@code era} is not a {@code MinguoEra}
174      */
175     @Override
date(Era era, int yearOfEra, int month, int dayOfMonth)176     public MinguoDate date(Era era, int yearOfEra, int month, int dayOfMonth) {
177         return date(prolepticYear(era, yearOfEra), month, dayOfMonth);
178     }
179 
180     /**
181      * Obtains a local date in Minguo calendar system from the
182      * proleptic-year, month-of-year and day-of-month fields.
183      *
184      * @param prolepticYear  the proleptic-year
185      * @param month  the month-of-year
186      * @param dayOfMonth  the day-of-month
187      * @return the Minguo local date, not null
188      * @throws DateTimeException if unable to create the date
189      */
190     @Override
date(int prolepticYear, int month, int dayOfMonth)191     public MinguoDate date(int prolepticYear, int month, int dayOfMonth) {
192         return new MinguoDate(LocalDate.of(prolepticYear + YEARS_DIFFERENCE, month, dayOfMonth));
193     }
194 
195     /**
196      * Obtains a local date in Minguo calendar system from the
197      * era, year-of-era and day-of-year fields.
198      *
199      * @param era  the Minguo era, not null
200      * @param yearOfEra  the year-of-era
201      * @param dayOfYear  the day-of-year
202      * @return the Minguo local date, not null
203      * @throws DateTimeException if unable to create the date
204      * @throws ClassCastException if the {@code era} is not a {@code MinguoEra}
205      */
206     @Override
dateYearDay(Era era, int yearOfEra, int dayOfYear)207     public MinguoDate dateYearDay(Era era, int yearOfEra, int dayOfYear) {
208         return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear);
209     }
210 
211     /**
212      * Obtains a local date in Minguo calendar system from the
213      * proleptic-year and day-of-year fields.
214      *
215      * @param prolepticYear  the proleptic-year
216      * @param dayOfYear  the day-of-year
217      * @return the Minguo local date, not null
218      * @throws DateTimeException if unable to create the date
219      */
220     @Override
dateYearDay(int prolepticYear, int dayOfYear)221     public MinguoDate dateYearDay(int prolepticYear, int dayOfYear) {
222         return new MinguoDate(LocalDate.ofYearDay(prolepticYear + YEARS_DIFFERENCE, dayOfYear));
223     }
224 
225     /**
226      * Obtains a local date in the Minguo calendar system from the epoch-day.
227      *
228      * @param epochDay  the epoch day
229      * @return the Minguo local date, not null
230      * @throws DateTimeException if unable to create the date
231      */
232     @Override  // override with covariant return type
dateEpochDay(long epochDay)233     public MinguoDate dateEpochDay(long epochDay) {
234         return new MinguoDate(LocalDate.ofEpochDay(epochDay));
235     }
236 
237     @Override
dateNow()238     public MinguoDate dateNow() {
239         return dateNow(Clock.systemDefaultZone());
240     }
241 
242     @Override
dateNow(ZoneId zone)243     public MinguoDate dateNow(ZoneId zone) {
244         return dateNow(Clock.system(zone));
245     }
246 
247     @Override
dateNow(Clock clock)248     public MinguoDate dateNow(Clock clock) {
249         return date(LocalDate.now(clock));
250     }
251 
252     @Override
date(TemporalAccessor temporal)253     public MinguoDate date(TemporalAccessor temporal) {
254         if (temporal instanceof MinguoDate) {
255             return (MinguoDate) temporal;
256         }
257         return new MinguoDate(LocalDate.from(temporal));
258     }
259 
260     @Override
261     @SuppressWarnings("unchecked")
localDateTime(TemporalAccessor temporal)262     public ChronoLocalDateTime<MinguoDate> localDateTime(TemporalAccessor temporal) {
263         return (ChronoLocalDateTime<MinguoDate>)super.localDateTime(temporal);
264     }
265 
266     @Override
267     @SuppressWarnings("unchecked")
zonedDateTime(TemporalAccessor temporal)268     public ChronoZonedDateTime<MinguoDate> zonedDateTime(TemporalAccessor temporal) {
269         return (ChronoZonedDateTime<MinguoDate>)super.zonedDateTime(temporal);
270     }
271 
272     @Override
273     @SuppressWarnings("unchecked")
zonedDateTime(Instant instant, ZoneId zone)274     public ChronoZonedDateTime<MinguoDate> zonedDateTime(Instant instant, ZoneId zone) {
275         return (ChronoZonedDateTime<MinguoDate>)super.zonedDateTime(instant, zone);
276     }
277 
278     //-----------------------------------------------------------------------
279     /**
280      * Checks if the specified year is a leap year.
281      * <p>
282      * Minguo leap years occur exactly in line with ISO leap years.
283      * This method does not validate the year passed in, and only has a
284      * well-defined result for years in the supported range.
285      *
286      * @param prolepticYear  the proleptic-year to check, not validated for range
287      * @return true if the year is a leap year
288      */
289     @Override
isLeapYear(long prolepticYear)290     public boolean isLeapYear(long prolepticYear) {
291         return IsoChronology.INSTANCE.isLeapYear(prolepticYear + YEARS_DIFFERENCE);
292     }
293 
294     @Override
prolepticYear(Era era, int yearOfEra)295     public int prolepticYear(Era era, int yearOfEra) {
296         if (era instanceof MinguoEra == false) {
297             throw new ClassCastException("Era must be MinguoEra");
298         }
299         return (era == MinguoEra.ROC ? yearOfEra : 1 - yearOfEra);
300     }
301 
302     @Override
eraOf(int eraValue)303     public MinguoEra eraOf(int eraValue) {
304         return MinguoEra.of(eraValue);
305     }
306 
307     @Override
eras()308     public List<Era> eras() {
309         return Arrays.<Era>asList(MinguoEra.values());
310     }
311 
312     //-----------------------------------------------------------------------
313     @Override
range(ChronoField field)314     public ValueRange range(ChronoField field) {
315         switch (field) {
316             case PROLEPTIC_MONTH: {
317                 ValueRange range = PROLEPTIC_MONTH.range();
318                 return ValueRange.of(range.getMinimum() - YEARS_DIFFERENCE * 12L, range.getMaximum() - YEARS_DIFFERENCE * 12L);
319             }
320             case YEAR_OF_ERA: {
321                 ValueRange range = YEAR.range();
322                 return ValueRange.of(1, range.getMaximum() - YEARS_DIFFERENCE, -range.getMinimum() + 1 + YEARS_DIFFERENCE);
323             }
324             case YEAR: {
325                 ValueRange range = YEAR.range();
326                 return ValueRange.of(range.getMinimum() - YEARS_DIFFERENCE, range.getMaximum() - YEARS_DIFFERENCE);
327             }
328         }
329         return field.range();
330     }
331 
332     //-----------------------------------------------------------------------
333     @Override  // override for return type
resolveDate(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle)334     public MinguoDate resolveDate(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
335         return (MinguoDate) super.resolveDate(fieldValues, resolverStyle);
336     }
337 
338     //-----------------------------------------------------------------------
339     /**
340      * Writes the Chronology using a
341      * <a href="../../../serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
342      * @serialData
343      * <pre>
344      *  out.writeByte(1);     // identifies a Chronology
345      *  out.writeUTF(getId());
346      * </pre>
347      *
348      * @return the instance of {@code Ser}, not null
349      */
350     @Override
writeReplace()351     Object writeReplace() {
352         return super.writeReplace();
353     }
354 
355     /**
356      * Defend against malicious streams.
357      *
358      * @param s the stream to read
359      * @throws InvalidObjectException always
360      */
readObject(ObjectInputStream s)361     private void readObject(ObjectInputStream s) throws InvalidObjectException {
362         throw new InvalidObjectException("Deserialization via serialization delegate");
363     }
364 }
365