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 58 package tck.java.time.chrono; 59 60 import static java.time.temporal.ChronoField.EPOCH_DAY; 61 62 import java.io.Serializable; 63 import java.time.chrono.AbstractChronology; 64 import java.time.chrono.Era; 65 import java.time.temporal.ChronoField; 66 import java.time.temporal.TemporalAccessor; 67 import java.time.temporal.ValueRange; 68 import java.util.Arrays; 69 import java.util.List; 70 import java.util.Locale; 71 72 /** 73 * The Coptic calendar system. 74 * <p> 75 * This chronology defines the rules of the Coptic calendar system. 76 * This calendar system is primarily used in Christian Egypt. 77 * Dates are aligned such that {@code 0001AM-01-01 (Coptic)} is {@code 0284-08-29 (ISO)}. 78 * <p> 79 * The fields are defined as follows: 80 * <p><ul> 81 * <li>era - There are two eras, the current 'Era of the Martyrs' (AM) and the previous era (ERA_ERA_BEFORE_AM). 82 * <li>year-of-era - The year-of-era for the current era increases uniformly from the epoch at year one. 83 * For the previous era the year increases from one as time goes backwards. 84 * <li>proleptic-year - The proleptic year is the same as the year-of-era for the 85 * current era. For the previous era, years have zero, then negative values. 86 * <li>month-of-year - There are 13 months in a Coptic year, numbered from 1 to 13. 87 * <li>day-of-month - There are 30 days in each of the first 12 Coptic months, numbered 1 to 30. 88 * The 13th month has 5 days, or 6 in a leap year, numbered 1 to 5 or 1 to 6. 89 * <li>day-of-year - There are 365 days in a standard Coptic year and 366 in a leap year. 90 * The days are numbered from 1 to 365 or 1 to 366. 91 * <li>leap-year - Leap years occur every 4 years. 92 * </ul><p> 93 * 94 * <h4>Implementation notes</h4> 95 * This class is immutable and thread-safe. 96 */ 97 public final class CopticChronology extends AbstractChronology implements Serializable { 98 99 /** 100 * Singleton instance of the Coptic chronology. 101 */ 102 public static final CopticChronology INSTANCE = new CopticChronology(); 103 104 /** 105 * Serialization version. 106 */ 107 private static final long serialVersionUID = 7291205177830286973L; 108 /** 109 * Range of months. 110 */ 111 static final ValueRange MOY_RANGE = ValueRange.of(1, 13); 112 /** 113 * Range of days. 114 */ 115 static final ValueRange DOM_RANGE = ValueRange.of(1, 5, 30); 116 /** 117 * Range of days. 118 */ 119 static final ValueRange DOM_RANGE_NONLEAP = ValueRange.of(1, 5); 120 /** 121 * Range of days. 122 */ 123 static final ValueRange DOM_RANGE_LEAP = ValueRange.of(1, 6); 124 125 /** 126 * Public Constructor to be instantiated by the ServiceLoader 127 */ CopticChronology()128 public CopticChronology() { 129 } 130 131 /** 132 * Resolve singleton. 133 * 134 * @return the singleton instance, not null 135 */ readResolve()136 private Object readResolve() { 137 return INSTANCE; 138 } 139 140 //----------------------------------------------------------------------- 141 /** 142 * Gets the ID of the chronology - 'Coptic'. 143 * <p> 144 * The ID uniquely identifies the {@code Chronology}. 145 * It can be used to lookup the {@code Chronology} using {@link #of(String)}. 146 * 147 * @return the chronology ID - 'Coptic' 148 * @see #getCalendarType() 149 */ 150 @Override getId()151 public String getId() { 152 return "Coptic"; 153 } 154 155 /** 156 * Gets the calendar type of the underlying calendar system - 'coptic'. 157 * <p> 158 * The calendar type is an identifier defined by the 159 * <em>Unicode Locale Data Markup Language (LDML)</em> specification. 160 * It can be used to lookup the {@code Chronology} using {@link #of(String)}. 161 * It can also be used as part of a locale, accessible via 162 * {@link Locale#getUnicodeLocaleType(String)} with the key 'ca'. 163 * 164 * @return the calendar system type - 'coptic' 165 * @see #getId() 166 */ 167 @Override getCalendarType()168 public String getCalendarType() { 169 return "coptic"; 170 } 171 172 //----------------------------------------------------------------------- 173 @Override date(int prolepticYear, int month, int dayOfMonth)174 public CopticDate date(int prolepticYear, int month, int dayOfMonth) { 175 return new CopticDate(prolepticYear, month, dayOfMonth); 176 } 177 178 @Override dateYearDay(int prolepticYear, int dayOfYear)179 public CopticDate dateYearDay(int prolepticYear, int dayOfYear) { 180 return new CopticDate(prolepticYear, (dayOfYear - 1) / 30 + 1, (dayOfYear - 1) % 30 + 1); 181 } 182 183 @Override dateEpochDay(long epochDay)184 public CopticDate dateEpochDay(long epochDay) { 185 return CopticDate.ofEpochDay(epochDay); 186 } 187 188 @Override date(TemporalAccessor dateTime)189 public CopticDate date(TemporalAccessor dateTime) { 190 if (dateTime instanceof CopticDate) { 191 return (CopticDate) dateTime; 192 } 193 return CopticDate.ofEpochDay(dateTime.getLong(EPOCH_DAY)); 194 } 195 196 //----------------------------------------------------------------------- 197 /** 198 * Checks if the specified year is a leap year. 199 * <p> 200 * A Coptic proleptic-year is leap if the remainder after division by four equals three. 201 * This method does not validate the year passed in, and only has a 202 * well-defined result for years in the supported range. 203 * 204 * @param prolepticYear the proleptic-year to check, not validated for range 205 * @return true if the year is a leap year 206 */ 207 @Override isLeapYear(long prolepticYear)208 public boolean isLeapYear(long prolepticYear) { 209 return Math.floorMod(prolepticYear, 4) == 3; 210 } 211 212 @Override prolepticYear(Era era, int yearOfEra)213 public int prolepticYear(Era era, int yearOfEra) { 214 if (era instanceof CopticEra == false) { 215 throw new ClassCastException("Era must be CopticEra"); 216 } 217 return (era == CopticEra.AM ? yearOfEra : 1 - yearOfEra); 218 } 219 220 @Override eraOf(int eraValue)221 public Era eraOf(int eraValue) { 222 return CopticEra.of(eraValue); 223 } 224 225 @Override eras()226 public List<Era> eras() { 227 return Arrays.<Era>asList(CopticEra.values()); 228 } 229 230 //----------------------------------------------------------------------- 231 @Override range(ChronoField field)232 public ValueRange range(ChronoField field) { 233 switch (field) { 234 case DAY_OF_MONTH: return ValueRange.of(1, 5, 30); 235 case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, 1, 5); 236 case MONTH_OF_YEAR: return ValueRange.of(1, 13); 237 case PROLEPTIC_MONTH: return ValueRange.of(-1000, 1000); // TODO 238 case YEAR_OF_ERA: return ValueRange.of(1, 999, 1000); // TODO 239 case YEAR: return ValueRange.of(-1000, 1000); // TODO 240 } 241 return field.range(); 242 } 243 244 } 245