1 /* 2 * Copyright (c) 2012, 2013, 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 static java.time.temporal.ChronoField.PROLEPTIC_MONTH; 60 import static java.time.temporal.ChronoField.YEAR; 61 62 import java.io.Serializable; 63 import java.time.Clock; 64 import java.time.DateTimeException; 65 import java.time.Instant; 66 import java.time.LocalDate; 67 import java.time.ZoneId; 68 import java.time.format.ResolverStyle; 69 import java.time.temporal.ChronoField; 70 import java.time.temporal.TemporalAccessor; 71 import java.time.temporal.TemporalField; 72 import java.time.temporal.ValueRange; 73 import java.util.Arrays; 74 import java.util.List; 75 import java.util.Locale; 76 import java.util.Map; 77 78 /** 79 * The Minguo calendar system. 80 * <p> 81 * This chronology defines the rules of the Minguo calendar system. 82 * This calendar system is primarily used in the Republic of China, often known as Taiwan. 83 * Dates are aligned such that {@code 0001-01-01 (Minguo)} is {@code 1912-01-01 (ISO)}. 84 * <p> 85 * The fields are defined as follows: 86 * <p><ul> 87 * <li>era - There are two eras, the current 'Republic' (ERA_ROC) and the previous era (ERA_BEFORE_ROC). 88 * <li>year-of-era - The year-of-era for the current era increases uniformly from the epoch at year one. 89 * For the previous era the year increases from one as time goes backwards. 90 * The value for the current era is equal to the ISO proleptic-year minus 1911. 91 * <li>proleptic-year - The proleptic year is the same as the year-of-era for the 92 * current era. For the previous era, years have zero, then negative values. 93 * The value is equal to the ISO proleptic-year minus 1911. 94 * <li>month-of-year - The Minguo month-of-year exactly matches ISO. 95 * <li>day-of-month - The Minguo day-of-month exactly matches ISO. 96 * <li>day-of-year - The Minguo day-of-year exactly matches ISO. 97 * <li>leap-year - The Minguo leap-year pattern exactly matches ISO, such that the two calendars 98 * are never out of step. 99 * </ul><p> 100 * 101 * @implSpec 102 * This class is immutable and thread-safe. 103 * 104 * @since 1.8 105 */ 106 public final class MinguoChronology extends Chronology implements Serializable { 107 108 /** 109 * Singleton instance for the Minguo chronology. 110 */ 111 public static final MinguoChronology INSTANCE = new MinguoChronology(); 112 113 /** 114 * Serialization version. 115 */ 116 private static final long serialVersionUID = 1039765215346859963L; 117 /** 118 * The difference in years between ISO and Minguo. 119 */ 120 static final int YEARS_DIFFERENCE = 1911; 121 122 /** 123 * Restricted constructor. 124 */ 125 private MinguoChronology() { 126 } 127 128 //----------------------------------------------------------------------- 129 /** 130 * Gets the ID of the chronology - 'Minguo'. 131 * <p> 132 * The ID uniquely identifies the {@code Chronology}. 133 * It can be used to lookup the {@code Chronology} using {@link #of(String)}. 134 * 135 * @return the chronology ID - 'Minguo' 136 * @see #getCalendarType() 137 */ 138 @Override 139 public String getId() { 140 return "Minguo"; 141 } 142 143 /** 144 * Gets the calendar type of the underlying calendar system - 'roc'. 145 * <p> 146 * The calendar type is an identifier defined by the 147 * <em>Unicode Locale Data Markup Language (LDML)</em> specification. 148 * It can be used to lookup the {@code Chronology} using {@link #of(String)}. 149 * It can also be used as part of a locale, accessible via 150 * {@link Locale#getUnicodeLocaleType(String)} with the key 'ca'. 151 * 152 * @return the calendar system type - 'roc' 153 * @see #getId() 154 */ 155 @Override 156 public String getCalendarType() { 157 return "roc"; 158 } 159 160 //----------------------------------------------------------------------- 161 /** 162 * Obtains a local date in Minguo calendar system from the 163 * era, year-of-era, month-of-year and day-of-month fields. 164 * 165 * @param era the Minguo era, not null 166 * @param yearOfEra the year-of-era 167 * @param month the month-of-year 168 * @param dayOfMonth the day-of-month 169 * @return the Minguo local date, not null 170 * @throws DateTimeException if unable to create the date 171 * @throws ClassCastException if the {@code era} is not a {@code MinguoEra} 172 */ 173 @Override 174 public MinguoDate date(Era era, int yearOfEra, int month, int dayOfMonth) { 175 return date(prolepticYear(era, yearOfEra), month, dayOfMonth); 176 } 177 178 /** 179 * Obtains a local date in Minguo calendar system from the 180 * proleptic-year, month-of-year and day-of-month fields. 181 * 182 * @param prolepticYear the proleptic-year 183 * @param month the month-of-year 184 * @param dayOfMonth the day-of-month 185 * @return the Minguo local date, not null 186 * @throws DateTimeException if unable to create the date 187 */ 188 @Override 189 public MinguoDate date(int prolepticYear, int month, int dayOfMonth) { 190 return new MinguoDate(LocalDate.of(prolepticYear + YEARS_DIFFERENCE, month, dayOfMonth)); 191 } 192 193 /** 194 * Obtains a local date in Minguo calendar system from the 195 * era, year-of-era and day-of-year fields. 196 * 197 * @param era the Minguo era, not null 198 * @param yearOfEra the year-of-era 199 * @param dayOfYear the day-of-year 200 * @return the Minguo local date, not null 201 * @throws DateTimeException if unable to create the date 202 * @throws ClassCastException if the {@code era} is not a {@code MinguoEra} 203 */ 204 @Override 205 public MinguoDate dateYearDay(Era era, int yearOfEra, int dayOfYear) { 206 return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear); 207 } 208 209 /** 210 * Obtains a local date in Minguo calendar system from the 211 * proleptic-year and day-of-year fields. 212 * 213 * @param prolepticYear the proleptic-year 214 * @param dayOfYear the day-of-year 215 * @return the Minguo local date, not null 216 * @throws DateTimeException if unable to create the date 217 */ 218 @Override 219 public MinguoDate dateYearDay(int prolepticYear, int dayOfYear) { 220 return new MinguoDate(LocalDate.ofYearDay(prolepticYear + YEARS_DIFFERENCE, dayOfYear)); 221 } 222 223 /** 224 * Obtains a local date in the Minguo calendar system from the epoch-day. 225 * 226 * @param epochDay the epoch day 227 * @return the Minguo local date, not null 228 * @throws DateTimeException if unable to create the date 229 */ 230 @Override // override with covariant return type 231 public MinguoDate dateEpochDay(long epochDay) { 232 return new MinguoDate(LocalDate.ofEpochDay(epochDay)); 233 } 234 235 @Override 236 public MinguoDate dateNow() { 237 return dateNow(Clock.systemDefaultZone()); 238 } 239 240 @Override 241 public MinguoDate dateNow(ZoneId zone) { 242 return dateNow(Clock.system(zone)); 243 } 244 245 @Override 246 public MinguoDate dateNow(Clock clock) { 247 return date(LocalDate.now(clock)); 248 } 249 250 @Override 251 public MinguoDate date(TemporalAccessor temporal) { 252 if (temporal instanceof MinguoDate) { 253 return (MinguoDate) temporal; 254 } 255 return new MinguoDate(LocalDate.from(temporal)); 256 } 257 258 @Override 259 @SuppressWarnings("unchecked") 260 public ChronoLocalDateTime<MinguoDate> localDateTime(TemporalAccessor temporal) { 261 return (ChronoLocalDateTime<MinguoDate>)super.localDateTime(temporal); 262 } 263 264 @Override 265 @SuppressWarnings("unchecked") 266 public ChronoZonedDateTime<MinguoDate> zonedDateTime(TemporalAccessor temporal) { 267 return (ChronoZonedDateTime<MinguoDate>)super.zonedDateTime(temporal); 268 } 269 270 @Override 271 @SuppressWarnings("unchecked") 272 public ChronoZonedDateTime<MinguoDate> zonedDateTime(Instant instant, ZoneId zone) { 273 return (ChronoZonedDateTime<MinguoDate>)super.zonedDateTime(instant, zone); 274 } 275 276 //----------------------------------------------------------------------- 277 /** 278 * Checks if the specified year is a leap year. 279 * <p> 280 * Minguo leap years occur exactly in line with ISO leap years. 281 * This method does not validate the year passed in, and only has a 282 * well-defined result for years in the supported range. 283 * 284 * @param prolepticYear the proleptic-year to check, not validated for range 285 * @return true if the year is a leap year 286 */ 287 @Override 288 public boolean isLeapYear(long prolepticYear) { 289 return IsoChronology.INSTANCE.isLeapYear(prolepticYear + YEARS_DIFFERENCE); 290 } 291 292 @Override 293 public int prolepticYear(Era era, int yearOfEra) { 294 if (era instanceof MinguoEra == false) { 295 throw new ClassCastException("Era must be MinguoEra"); 296 } 297 return (era == MinguoEra.ROC ? yearOfEra : 1 - yearOfEra); 298 } 299 300 @Override 301 public MinguoEra eraOf(int eraValue) { 302 return MinguoEra.of(eraValue); 303 } 304 305 @Override 306 public List<Era> eras() { 307 return Arrays.<Era>asList(MinguoEra.values()); 308 } 309 310 //----------------------------------------------------------------------- 311 @Override 312 public ValueRange range(ChronoField field) { 313 switch (field) { 314 case PROLEPTIC_MONTH: { 315 ValueRange range = PROLEPTIC_MONTH.range(); 316 return ValueRange.of(range.getMinimum() - YEARS_DIFFERENCE * 12L, range.getMaximum() - YEARS_DIFFERENCE * 12L); 317 } 318 case YEAR_OF_ERA: { 319 ValueRange range = YEAR.range(); 320 return ValueRange.of(1, range.getMaximum() - YEARS_DIFFERENCE, -range.getMinimum() + 1 + YEARS_DIFFERENCE); 321 } 322 case YEAR: { 323 ValueRange range = YEAR.range(); 324 return ValueRange.of(range.getMinimum() - YEARS_DIFFERENCE, range.getMaximum() - YEARS_DIFFERENCE); 325 } 326 } 327 return field.range(); 328 } 329 330 //----------------------------------------------------------------------- 331 @Override // override for return type 332 public MinguoDate resolveDate(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { 333 return (MinguoDate) super.resolveDate(fieldValues, resolverStyle); 334 } 335 336 }