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.HashMap;
  75 import java.util.List;
  76 import java.util.Locale;
  77 import java.util.Map;
  78 
  79 /**
  80  * The Thai Buddhist calendar system.
  81  * <p>
  82  * This chronology defines the rules of the Thai Buddhist calendar system.
  83  * This calendar system is primarily used in Thailand.
  84  * Dates are aligned such that {@code 2484-01-01 (Buddhist)} is {@code 1941-01-01 (ISO)}.
  85  * <p>
  86  * The fields are defined as follows:
  87  * <p><ul>
  88  * <li>era - There are two eras, the current 'Buddhist' (ERA_BE) and the previous era (ERA_BEFORE_BE).
  89  * <li>year-of-era - The year-of-era for the current era increases uniformly from the epoch at year one.
  90  *  For the previous era the year increases from one as time goes backwards.
  91  *  The value for the current era is equal to the ISO proleptic-year plus 543.
  92  * <li>proleptic-year - The proleptic year is the same as the year-of-era for the
  93  *  current era. For the previous era, years have zero, then negative values.
  94  *  The value is equal to the ISO proleptic-year plus 543.
  95  * <li>month-of-year - The ThaiBuddhist month-of-year exactly matches ISO.
  96  * <li>day-of-month - The ThaiBuddhist day-of-month exactly matches ISO.
  97  * <li>day-of-year - The ThaiBuddhist day-of-year exactly matches ISO.
  98  * <li>leap-year - The ThaiBuddhist leap-year pattern exactly matches ISO, such that the two calendars
  99  *  are never out of step.
 100  * </ul><p>
 101  *
 102  * @implSpec
 103  * This class is immutable and thread-safe.
 104  *
 105  * @since 1.8
 106  */
 107 public final class ThaiBuddhistChronology extends Chronology implements Serializable {
 108 
 109     /**
 110      * Singleton instance of the Buddhist chronology.
 111      */
 112     public static final ThaiBuddhistChronology INSTANCE = new ThaiBuddhistChronology();
 113 
 114     /**
 115      * Serialization version.
 116      */
 117     private static final long serialVersionUID = 2775954514031616474L;
 118     /**
 119      * Containing the offset to add to the ISO year.
 120      */
 121     static final int YEARS_DIFFERENCE = 543;
 122     /**
 123      * Narrow names for eras.
 124      */
 125     private static final HashMap<String, String[]> ERA_NARROW_NAMES = new HashMap<>();
 126     /**
 127      * Short names for eras.
 128      */
 129     private static final HashMap<String, String[]> ERA_SHORT_NAMES = new HashMap<>();
 130     /**
 131      * Full names for eras.
 132      */
 133     private static final HashMap<String, String[]> ERA_FULL_NAMES = new HashMap<>();
 134     /**
 135      * Fallback language for the era names.
 136      */
 137     private static final String FALLBACK_LANGUAGE = "en";
 138     /**
 139      * Language that has the era names.
 140      */
 141     private static final String TARGET_LANGUAGE = "th";
 142     /**
 143      * Name data.
 144      */
 145     static {
 146         ERA_NARROW_NAMES.put(FALLBACK_LANGUAGE, new String[]{"BB", "BE"});
 147         ERA_NARROW_NAMES.put(TARGET_LANGUAGE, new String[]{"BB", "BE"});
 148         ERA_SHORT_NAMES.put(FALLBACK_LANGUAGE, new String[]{"B.B.", "B.E."});
 149         ERA_SHORT_NAMES.put(TARGET_LANGUAGE,
 150                 new String[]{"\u0e1e.\u0e28.",
 151                 "\u0e1b\u0e35\u0e01\u0e48\u0e2d\u0e19\u0e04\u0e23\u0e34\u0e2a\u0e15\u0e4c\u0e01\u0e32\u0e25\u0e17\u0e35\u0e48"});
 152         ERA_FULL_NAMES.put(FALLBACK_LANGUAGE, new String[]{"Before Buddhist", "Budhhist Era"});
 153         ERA_FULL_NAMES.put(TARGET_LANGUAGE,
 154                 new String[]{"\u0e1e\u0e38\u0e17\u0e18\u0e28\u0e31\u0e01\u0e23\u0e32\u0e0a",
 155                 "\u0e1b\u0e35\u0e01\u0e48\u0e2d\u0e19\u0e04\u0e23\u0e34\u0e2a\u0e15\u0e4c\u0e01\u0e32\u0e25\u0e17\u0e35\u0e48"});
 156     }
 157 
 158     /**
 159      * Restricted constructor.
 160      */
 161     private ThaiBuddhistChronology() {
 162     }
 163 
 164     //-----------------------------------------------------------------------
 165     /**
 166      * Gets the ID of the chronology - 'ThaiBuddhist'.
 167      * <p>
 168      * The ID uniquely identifies the {@code Chronology}.
 169      * It can be used to lookup the {@code Chronology} using {@link #of(String)}.
 170      *
 171      * @return the chronology ID - 'ThaiBuddhist'
 172      * @see #getCalendarType()
 173      */
 174     @Override
 175     public String getId() {
 176         return "ThaiBuddhist";
 177     }
 178 
 179     /**
 180      * Gets the calendar type of the underlying calendar system - 'buddhist'.
 181      * <p>
 182      * The calendar type is an identifier defined by the
 183      * <em>Unicode Locale Data Markup Language (LDML)</em> specification.
 184      * It can be used to lookup the {@code Chronology} using {@link #of(String)}.
 185      * It can also be used as part of a locale, accessible via
 186      * {@link Locale#getUnicodeLocaleType(String)} with the key 'ca'.
 187      *
 188      * @return the calendar system type - 'buddhist'
 189      * @see #getId()
 190      */
 191     @Override
 192     public String getCalendarType() {
 193         return "buddhist";
 194     }
 195 
 196     //-----------------------------------------------------------------------
 197     /**
 198      * Obtains a local date in Thai Buddhist calendar system from the
 199      * era, year-of-era, month-of-year and day-of-month fields.
 200      *
 201      * @param era  the Thai Buddhist era, not null
 202      * @param yearOfEra  the year-of-era
 203      * @param month  the month-of-year
 204      * @param dayOfMonth  the day-of-month
 205      * @return the Thai Buddhist local date, not null
 206      * @throws DateTimeException if unable to create the date
 207      * @throws ClassCastException if the {@code era} is not a {@code ThaiBuddhistEra}
 208      */
 209     @Override
 210     public ThaiBuddhistDate date(Era era, int yearOfEra, int month, int dayOfMonth) {
 211         return date(prolepticYear(era, yearOfEra), month, dayOfMonth);
 212     }
 213 
 214     /**
 215      * Obtains a local date in Thai Buddhist calendar system from the
 216      * proleptic-year, month-of-year and day-of-month fields.
 217      *
 218      * @param prolepticYear  the proleptic-year
 219      * @param month  the month-of-year
 220      * @param dayOfMonth  the day-of-month
 221      * @return the Thai Buddhist local date, not null
 222      * @throws DateTimeException if unable to create the date
 223      */
 224     @Override
 225     public ThaiBuddhistDate date(int prolepticYear, int month, int dayOfMonth) {
 226         return new ThaiBuddhistDate(LocalDate.of(prolepticYear - YEARS_DIFFERENCE, month, dayOfMonth));
 227     }
 228 
 229     /**
 230      * Obtains a local date in Thai Buddhist calendar system from the
 231      * era, year-of-era and day-of-year fields.
 232      *
 233      * @param era  the Thai Buddhist era, not null
 234      * @param yearOfEra  the year-of-era
 235      * @param dayOfYear  the day-of-year
 236      * @return the Thai Buddhist local date, not null
 237      * @throws DateTimeException if unable to create the date
 238      * @throws ClassCastException if the {@code era} is not a {@code ThaiBuddhistEra}
 239      */
 240     @Override
 241     public ThaiBuddhistDate dateYearDay(Era era, int yearOfEra, int dayOfYear) {
 242         return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear);
 243     }
 244 
 245     /**
 246      * Obtains a local date in Thai Buddhist calendar system from the
 247      * proleptic-year and day-of-year fields.
 248      *
 249      * @param prolepticYear  the proleptic-year
 250      * @param dayOfYear  the day-of-year
 251      * @return the Thai Buddhist local date, not null
 252      * @throws DateTimeException if unable to create the date
 253      */
 254     @Override
 255     public ThaiBuddhistDate dateYearDay(int prolepticYear, int dayOfYear) {
 256         return new ThaiBuddhistDate(LocalDate.ofYearDay(prolepticYear - YEARS_DIFFERENCE, dayOfYear));
 257     }
 258 
 259     /**
 260      * Obtains a local date in the Thai Buddhist calendar system from the epoch-day.
 261      *
 262      * @param epochDay  the epoch day
 263      * @return the Thai Buddhist local date, not null
 264      * @throws DateTimeException if unable to create the date
 265      */
 266     @Override  // override with covariant return type
 267     public ThaiBuddhistDate dateEpochDay(long epochDay) {
 268         return new ThaiBuddhistDate(LocalDate.ofEpochDay(epochDay));
 269     }
 270 
 271     @Override
 272     public ThaiBuddhistDate dateNow() {
 273         return dateNow(Clock.systemDefaultZone());
 274     }
 275 
 276     @Override
 277     public ThaiBuddhistDate dateNow(ZoneId zone) {
 278         return dateNow(Clock.system(zone));
 279     }
 280 
 281     @Override
 282     public ThaiBuddhistDate dateNow(Clock clock) {
 283         return date(LocalDate.now(clock));
 284     }
 285 
 286     @Override
 287     public ThaiBuddhistDate date(TemporalAccessor temporal) {
 288         if (temporal instanceof ThaiBuddhistDate) {
 289             return (ThaiBuddhistDate) temporal;
 290         }
 291         return new ThaiBuddhistDate(LocalDate.from(temporal));
 292     }
 293 
 294     @Override
 295     @SuppressWarnings("unchecked")
 296     public ChronoLocalDateTime<ThaiBuddhistDate> localDateTime(TemporalAccessor temporal) {
 297         return (ChronoLocalDateTime<ThaiBuddhistDate>)super.localDateTime(temporal);
 298     }
 299 
 300     @Override
 301     @SuppressWarnings("unchecked")
 302     public ChronoZonedDateTime<ThaiBuddhistDate> zonedDateTime(TemporalAccessor temporal) {
 303         return (ChronoZonedDateTime<ThaiBuddhistDate>)super.zonedDateTime(temporal);
 304     }
 305 
 306     @Override
 307     @SuppressWarnings("unchecked")
 308     public ChronoZonedDateTime<ThaiBuddhistDate> zonedDateTime(Instant instant, ZoneId zone) {
 309         return (ChronoZonedDateTime<ThaiBuddhistDate>)super.zonedDateTime(instant, zone);
 310     }
 311 
 312     //-----------------------------------------------------------------------
 313     /**
 314      * Checks if the specified year is a leap year.
 315      * <p>
 316      * Thai Buddhist leap years occur exactly in line with ISO leap years.
 317      * This method does not validate the year passed in, and only has a
 318      * well-defined result for years in the supported range.
 319      *
 320      * @param prolepticYear  the proleptic-year to check, not validated for range
 321      * @return true if the year is a leap year
 322      */
 323     @Override
 324     public boolean isLeapYear(long prolepticYear) {
 325         return IsoChronology.INSTANCE.isLeapYear(prolepticYear - YEARS_DIFFERENCE);
 326     }
 327 
 328     @Override
 329     public int prolepticYear(Era era, int yearOfEra) {
 330         if (era instanceof ThaiBuddhistEra == false) {
 331             throw new ClassCastException("Era must be BuddhistEra");
 332         }
 333         return (era == ThaiBuddhistEra.BE ? yearOfEra : 1 - yearOfEra);
 334     }
 335 
 336     @Override
 337     public ThaiBuddhistEra eraOf(int eraValue) {
 338         return ThaiBuddhistEra.of(eraValue);
 339     }
 340 
 341     @Override
 342     public List<Era> eras() {
 343         return Arrays.<Era>asList(ThaiBuddhistEra.values());
 344     }
 345 
 346     //-----------------------------------------------------------------------
 347     @Override
 348     public ValueRange range(ChronoField field) {
 349         switch (field) {
 350             case PROLEPTIC_MONTH: {
 351                 ValueRange range = PROLEPTIC_MONTH.range();
 352                 return ValueRange.of(range.getMinimum() + YEARS_DIFFERENCE * 12L, range.getMaximum() + YEARS_DIFFERENCE * 12L);
 353             }
 354             case YEAR_OF_ERA: {
 355                 ValueRange range = YEAR.range();
 356                 return ValueRange.of(1, -(range.getMinimum() + YEARS_DIFFERENCE) + 1, range.getMaximum() + YEARS_DIFFERENCE);
 357             }
 358             case YEAR: {
 359                 ValueRange range = YEAR.range();
 360                 return ValueRange.of(range.getMinimum() + YEARS_DIFFERENCE, range.getMaximum() + YEARS_DIFFERENCE);
 361             }
 362         }
 363         return field.range();
 364     }
 365 
 366     //-----------------------------------------------------------------------
 367     @Override  // override for return type
 368     public ThaiBuddhistDate resolveDate(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
 369         return (ThaiBuddhistDate) super.resolveDate(fieldValues, resolverStyle);
 370     }
 371 
 372 }