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.calendar; 58 59 import static java.time.temporal.ChronoField.YEAR; 60 61 import java.io.Serializable; 62 import java.time.DateTimeException; 63 import java.time.LocalDate; 64 import java.time.temporal.Chrono; 65 import java.time.temporal.ChronoField; 66 import java.time.temporal.ChronoLocalDate; 67 import java.time.temporal.Era; 68 import java.time.temporal.ISOChrono; 69 import java.time.temporal.TemporalAccessor; 70 import java.time.temporal.ValueRange; 71 import java.util.Arrays; 72 import java.util.HashMap; 73 import java.util.List; 74 import java.util.Locale; 75 76 /** 77 * The Thai Buddhist calendar system. 78 * <p> 79 * This chronology defines the rules of the Thai Buddhist calendar system. 80 * This calendar system is primarily used in Thailand. 81 * Dates are aligned such that {@code 2484-01-01 (Buddhist)} is {@code 1941-01-01 (ISO)}. 82 * <p> 83 * The fields are defined as follows: 84 * <p><ul> 85 * <li>era - There are two eras, the current 'Buddhist' (ERA_BE) and the previous era (ERA_BEFORE_BE). 86 * <li>year-of-era - The year-of-era for the current era increases uniformly from the epoch at year one. 87 * For the previous era the year increases from one as time goes backwards. 88 * The value for the current era is equal to the ISO proleptic-year plus 543. 89 * <li>proleptic-year - The proleptic year is the same as the year-of-era for the 90 * current era. For the previous era, years have zero, then negative values. 91 * The value is equal to the ISO proleptic-year plus 543. 92 * <li>month-of-year - The ThaiBuddhist month-of-year exactly matches ISO. 93 * <li>day-of-month - The ThaiBuddhist day-of-month exactly matches ISO. 94 * <li>day-of-year - The ThaiBuddhist day-of-year exactly matches ISO. 95 * <li>leap-year - The ThaiBuddhist leap-year pattern exactly matches ISO, such that the two calendars 96 * are never out of step. 97 * </ul><p> 98 * 99 * <h3>Specification for implementors</h3> 100 * This class is immutable and thread-safe. 101 * 102 * @since 1.8 103 */ 104 public final class ThaiBuddhistChrono extends Chrono<ThaiBuddhistChrono> implements Serializable { 105 106 /** 107 * Singleton instance of the Buddhist chronology. 108 */ 109 public static final ThaiBuddhistChrono INSTANCE = new ThaiBuddhistChrono(); 110 /** 111 * The singleton instance for the era before the current one - Before Buddhist - 112 * which has the value 0. 113 */ 114 public static final Era<ThaiBuddhistChrono> ERA_BEFORE_BE = ThaiBuddhistEra.BEFORE_BE; 115 /** 116 * The singleton instance for the current era - Buddhist - which has the value 1. 117 */ 118 public static final Era<ThaiBuddhistChrono> ERA_BE = ThaiBuddhistEra.BE; 119 120 /** 121 * Serialization version. 122 */ 123 private static final long serialVersionUID = 2775954514031616474L; 124 /** 125 * Containing the offset to add to the ISO year. 126 */ 127 static final int YEARS_DIFFERENCE = 543; 128 /** 129 * Narrow names for eras. 130 */ 131 private static final HashMap<String, String[]> ERA_NARROW_NAMES = new HashMap<>(); 132 /** 133 * Short names for eras. 134 */ 135 private static final HashMap<String, String[]> ERA_SHORT_NAMES = new HashMap<>(); 136 /** 137 * Full names for eras. 138 */ 139 private static final HashMap<String, String[]> ERA_FULL_NAMES = new HashMap<>(); 140 /** 141 * Fallback language for the era names. 142 */ 143 private static final String FALLBACK_LANGUAGE = "en"; 144 /** 145 * Language that has the era names. 146 */ 147 private static final String TARGET_LANGUAGE = "th"; 148 /** 149 * Name data. 150 */ 151 static { 152 ERA_NARROW_NAMES.put(FALLBACK_LANGUAGE, new String[]{"BB", "BE"}); 153 ERA_NARROW_NAMES.put(TARGET_LANGUAGE, new String[]{"BB", "BE"}); 154 ERA_SHORT_NAMES.put(FALLBACK_LANGUAGE, new String[]{"B.B.", "B.E."}); 155 ERA_SHORT_NAMES.put(TARGET_LANGUAGE, 156 new String[]{"\u0e1e.\u0e28.", 157 "\u0e1b\u0e35\u0e01\u0e48\u0e2d\u0e19\u0e04\u0e23\u0e34\u0e2a\u0e15\u0e4c\u0e01\u0e32\u0e25\u0e17\u0e35\u0e48"}); 158 ERA_FULL_NAMES.put(FALLBACK_LANGUAGE, new String[]{"Before Buddhist", "Budhhist Era"}); 159 ERA_FULL_NAMES.put(TARGET_LANGUAGE, 160 new String[]{"\u0e1e\u0e38\u0e17\u0e18\u0e28\u0e31\u0e01\u0e23\u0e32\u0e0a", 161 "\u0e1b\u0e35\u0e01\u0e48\u0e2d\u0e19\u0e04\u0e23\u0e34\u0e2a\u0e15\u0e4c\u0e01\u0e32\u0e25\u0e17\u0e35\u0e48"}); 162 } 163 164 /** 165 * Restricted constructor. 166 */ 167 private ThaiBuddhistChrono() { 168 } 169 170 /** 171 * Resolve singleton. 172 * 173 * @return the singleton instance, not null 174 */ 175 private Object readResolve() { 176 return INSTANCE; 177 } 178 179 //----------------------------------------------------------------------- 180 /** 181 * Gets the ID of the chronology - 'ThaiBuddhist'. 182 * <p> 183 * The ID uniquely identifies the {@code Chrono}. 184 * It can be used to lookup the {@code Chrono} using {@link #of(String)}. 185 * 186 * @return the chronology ID - 'ThaiBuddhist' 187 * @see #getCalendarType() 188 */ 189 @Override 190 public String getId() { 191 return "ThaiBuddhist"; 192 } 193 194 /** 195 * Gets the calendar type of the underlying calendar system - 'buddhist'. 196 * <p> 197 * The calendar type is an identifier defined by the 198 * <em>Unicode Locale Data Markup Language (LDML)</em> specification. 199 * It can be used to lookup the {@code Chrono} using {@link #of(String)}. 200 * It can also be used as part of a locale, accessible via 201 * {@link Locale#getUnicodeLocaleType(String)} with the key 'ca'. 202 * 203 * @return the calendar system type - 'buddhist' 204 * @see #getId() 205 */ 206 @Override 207 public String getCalendarType() { 208 return "buddhist"; 209 } 210 211 //----------------------------------------------------------------------- 212 @Override 213 public ChronoLocalDate<ThaiBuddhistChrono> date(int prolepticYear, int month, int dayOfMonth) { 214 return new ThaiBuddhistDate(LocalDate.of(prolepticYear - YEARS_DIFFERENCE, month, dayOfMonth)); 215 } 216 217 @Override 218 public ChronoLocalDate<ThaiBuddhistChrono> dateYearDay(int prolepticYear, int dayOfYear) { 219 return new ThaiBuddhistDate(LocalDate.ofYearDay(prolepticYear - YEARS_DIFFERENCE, dayOfYear)); 220 } 221 222 @Override 223 public ChronoLocalDate<ThaiBuddhistChrono> date(TemporalAccessor temporal) { 224 if (temporal instanceof ThaiBuddhistDate) { 225 return (ThaiBuddhistDate) temporal; 226 } 227 return new ThaiBuddhistDate(LocalDate.from(temporal)); 228 } 229 230 //----------------------------------------------------------------------- 231 /** 232 * Checks if the specified year is a leap year. 233 * <p> 234 * Thai Buddhist leap years occur exactly in line with ISO leap years. 235 * This method does not validate the year passed in, and only has a 236 * well-defined result for years in the supported range. 237 * 238 * @param prolepticYear the proleptic-year to check, not validated for range 239 * @return true if the year is a leap year 240 */ 241 @Override 242 public boolean isLeapYear(long prolepticYear) { 243 return ISOChrono.INSTANCE.isLeapYear(prolepticYear - YEARS_DIFFERENCE); 244 } 245 246 @Override 247 public int prolepticYear(Era<ThaiBuddhistChrono> era, int yearOfEra) { 248 if (era instanceof ThaiBuddhistEra == false) { 249 throw new DateTimeException("Era must be BuddhistEra"); 250 } 251 return (era == ThaiBuddhistEra.BE ? yearOfEra : 1 - yearOfEra); 252 } 253 254 @Override 255 public Era<ThaiBuddhistChrono> eraOf(int eraValue) { 256 return ThaiBuddhistEra.of(eraValue); 257 } 258 259 @Override 260 public List<Era<ThaiBuddhistChrono>> eras() { 261 return Arrays.<Era<ThaiBuddhistChrono>>asList(ThaiBuddhistEra.values()); 262 } 263 264 //----------------------------------------------------------------------- 265 @Override 266 public ValueRange range(ChronoField field) { 267 switch (field) { 268 case YEAR_OF_ERA: { 269 ValueRange range = YEAR.range(); 270 return ValueRange.of(1, -(range.getMinimum() + YEARS_DIFFERENCE) + 1, range.getMaximum() + YEARS_DIFFERENCE); 271 } 272 case YEAR: { 273 ValueRange range = YEAR.range(); 274 return ValueRange.of(range.getMinimum() + YEARS_DIFFERENCE, range.getMaximum() + YEARS_DIFFERENCE); 275 } 276 } 277 return field.range(); 278 } 279 280 }