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 tck.java.time.chrono; 58 59 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH; 60 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR; 61 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH; 62 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; 63 import static java.time.temporal.ChronoField.DAY_OF_MONTH; 64 import static java.time.temporal.ChronoField.MONTH_OF_YEAR; 65 import static java.time.temporal.ChronoField.YEAR_OF_ERA; 66 67 import java.io.Serializable; 68 69 import java.time.DateTimeException; 70 import java.time.LocalDate; 71 import java.time.Period; 72 import java.time.Year; 73 import java.time.chrono.ChronoLocalDate; 74 import java.time.temporal.ChronoField; 75 import java.time.temporal.ChronoUnit; 76 import java.time.temporal.Temporal; 77 import java.time.temporal.TemporalField; 78 import java.time.temporal.TemporalUnit; 79 import java.time.temporal.ValueRange; 80 import java.time.temporal.UnsupportedTemporalTypeException; 81 82 /** 83 * A date in the Coptic calendar system. 84 * <p> 85 * This implements {@code ChronoLocalDate} for the {@link CopticChronology Coptic calendar}. 86 * 87 * <h4>Implementation notes</h4> 88 * This class is immutable and thread-safe. 89 */ 90 public final class CopticDate 91 implements ChronoLocalDate, Serializable { 92 93 /** 94 * Serialization version. 95 */ 96 private static final long serialVersionUID = -7920528871688876868L; 97 /** 98 * The difference between the Coptic and Coptic epoch day count. 99 */ 100 private static final int EPOCH_DAY_DIFFERENCE = 574971 + 40587; 101 102 /** 103 * The proleptic year. 104 */ 105 private final int prolepticYear; 106 /** 107 * The month. 108 */ 109 private final short month; 110 /** 111 * The day. 112 */ 113 private final short day; 114 115 //----------------------------------------------------------------------- 116 /** 117 * Creates an instance. 118 * 119 * @param epochDay the epoch day to convert based on 1970-01-01 (ISO) 120 * @return the Coptic date, not null 121 * @throws DateTimeException if the date is invalid 122 */ ofEpochDay(long epochDay)123 static CopticDate ofEpochDay(long epochDay) { 124 epochDay += EPOCH_DAY_DIFFERENCE; 125 int prolepticYear = (int) (((epochDay * 4) + 1463) / 1461); 126 int startYearEpochDay = (prolepticYear - 1) * 365 + (prolepticYear / 4); 127 int doy0 = (int) (epochDay - startYearEpochDay); 128 int month = doy0 / 30 + 1; 129 int dom = doy0 % 30 + 1; 130 return new CopticDate(prolepticYear, month, dom); 131 } 132 resolvePreviousValid(int prolepticYear, int month, int day)133 private static CopticDate resolvePreviousValid(int prolepticYear, int month, int day) { 134 if (month == 13 && day > 5) { 135 day = CopticChronology.INSTANCE.isLeapYear(prolepticYear) ? 6 : 5; 136 } 137 return new CopticDate(prolepticYear, month, day); 138 } 139 140 //----------------------------------------------------------------------- 141 /** 142 * Creates an instance. 143 * 144 * @param prolepticYear the Coptic proleptic-year 145 * @param month the Coptic month, from 1 to 13 146 * @param dayOfMonth the Coptic day-of-month, from 1 to 30 147 * @throws DateTimeException if the date is invalid 148 */ CopticDate(int prolepticYear, int month, int dayOfMonth)149 CopticDate(int prolepticYear, int month, int dayOfMonth) { 150 CopticChronology.MOY_RANGE.checkValidValue(month, MONTH_OF_YEAR); 151 ValueRange range; 152 if (month == 13) { 153 range = CopticChronology.INSTANCE.isLeapYear(prolepticYear) ? CopticChronology.DOM_RANGE_LEAP : CopticChronology.DOM_RANGE_NONLEAP; 154 } else { 155 range = CopticChronology.DOM_RANGE; 156 } 157 range.checkValidValue(dayOfMonth, DAY_OF_MONTH); 158 159 this.prolepticYear = prolepticYear; 160 this.month = (short) month; 161 this.day = (short) dayOfMonth; 162 } 163 164 /** 165 * Validates the object. 166 * 167 * @return the resolved date, not null 168 */ readResolve()169 private Object readResolve() { 170 // TODO: validate 171 return this; 172 } 173 174 //----------------------------------------------------------------------- 175 @Override getChronology()176 public CopticChronology getChronology() { 177 return CopticChronology.INSTANCE; 178 } 179 180 //----------------------------------------------------------------------- 181 @Override lengthOfMonth()182 public int lengthOfMonth() { 183 switch (month) { 184 case 13: 185 return (isLeapYear() ? 6 : 5); 186 default: 187 return 30; 188 } 189 } 190 191 @Override range(TemporalField field)192 public ValueRange range(TemporalField field) { 193 if (field instanceof ChronoField) { 194 if (isSupported(field)) { 195 ChronoField f = (ChronoField) field; 196 switch (f) { 197 case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth()); 198 case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear()); 199 case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, month == 13 ? 1 : 5); 200 case YEAR: 201 case YEAR_OF_ERA: return (prolepticYear <= 0 ? 202 ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE)); // TODO 203 } 204 return getChronology().range(f); 205 } 206 throw new UnsupportedTemporalTypeException("Unsupported field: " + field); 207 } 208 return field.rangeRefinedBy(this); 209 } 210 211 @Override getLong(TemporalField field)212 public long getLong(TemporalField field) { 213 if (field instanceof ChronoField) { 214 switch ((ChronoField) field) { 215 case DAY_OF_WEEK: return Math.floorMod(toEpochDay() + 3, 7) + 1; 216 case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1; 217 case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((get(ChronoField.DAY_OF_YEAR) - 1) % 7) + 1; 218 case DAY_OF_MONTH: return day; 219 case DAY_OF_YEAR: return (month - 1) * 30 + day; 220 case EPOCH_DAY: return toEpochDay(); 221 case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1; 222 case ALIGNED_WEEK_OF_YEAR: return ((get(ChronoField.DAY_OF_YEAR) - 1) / 7) + 1; 223 case MONTH_OF_YEAR: return month; 224 case YEAR_OF_ERA: return (prolepticYear >= 1 ? prolepticYear : 1 - prolepticYear); 225 case YEAR: return prolepticYear; 226 case ERA: return (prolepticYear >= 1 ? 1 : 0); 227 } 228 throw new UnsupportedTemporalTypeException("Unsupported field: " + field); 229 } 230 return field.getFrom(this); 231 } 232 233 @Override with(TemporalField field, long newValue)234 public CopticDate with(TemporalField field, long newValue) { 235 if (field instanceof ChronoField) { 236 ChronoField f = (ChronoField) field; 237 f.checkValidValue(newValue); // TODO: validate value 238 int nvalue = (int) newValue; 239 switch (f) { 240 case DAY_OF_WEEK: return plusDays(newValue - get(ChronoField.DAY_OF_WEEK)); 241 case ALIGNED_DAY_OF_WEEK_IN_MONTH: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH)); 242 case ALIGNED_DAY_OF_WEEK_IN_YEAR: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR)); 243 case DAY_OF_MONTH: return resolvePreviousValid(prolepticYear, month, nvalue); 244 case DAY_OF_YEAR: return resolvePreviousValid(prolepticYear, ((nvalue - 1) / 30) + 1, ((nvalue - 1) % 30) + 1); 245 case EPOCH_DAY: return ofEpochDay(nvalue); 246 case ALIGNED_WEEK_OF_MONTH: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_MONTH)) * 7); 247 case ALIGNED_WEEK_OF_YEAR: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_YEAR)) * 7); 248 case MONTH_OF_YEAR: return resolvePreviousValid(prolepticYear, nvalue, day); 249 case YEAR_OF_ERA: return resolvePreviousValid(prolepticYear >= 1 ? nvalue : 1 - nvalue, month, day); 250 case YEAR: return resolvePreviousValid(nvalue, month, day); 251 case ERA: return resolvePreviousValid(1 - prolepticYear, month, day); 252 } 253 throw new UnsupportedTemporalTypeException("Unsupported field: " + field); 254 } 255 return field.adjustInto(this, newValue); 256 } 257 258 //----------------------------------------------------------------------- 259 @Override plus(long amountToAdd, TemporalUnit unit)260 public CopticDate plus(long amountToAdd, TemporalUnit unit) { 261 if (unit instanceof ChronoUnit) { 262 ChronoUnit f = (ChronoUnit) unit; 263 switch (f) { 264 case DAYS: return plusDays(amountToAdd); 265 case WEEKS: return plusDays(Math.multiplyExact(amountToAdd, 7)); 266 case MONTHS: return plusMonths(amountToAdd); 267 case YEARS: return plusYears(amountToAdd); 268 case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10)); 269 case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100)); 270 case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000)); 271 } 272 throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); 273 } 274 return unit.addTo(this, amountToAdd); 275 } 276 277 //----------------------------------------------------------------------- plusYears(long years)278 private CopticDate plusYears(long years) { 279 return plusMonths(Math.multiplyExact(years, 13)); 280 } 281 plusMonths(long months)282 private CopticDate plusMonths(long months) { 283 if (months == 0) { 284 return this; 285 } 286 long curEm = prolepticYear * 13L + (month - 1); 287 long calcEm = Math.addExact(curEm, months); 288 int newYear = Math.toIntExact(Math.floorDiv(calcEm, 13)); 289 int newMonth = (int)Math.floorMod(calcEm, 13) + 1; 290 return resolvePreviousValid(newYear, newMonth, day); 291 } 292 plusDays(long days)293 private CopticDate plusDays(long days) { 294 if (days == 0) { 295 return this; 296 } 297 return CopticDate.ofEpochDay(Math.addExact(toEpochDay(), days)); 298 } 299 300 @Override until(Temporal endExclusive, TemporalUnit unit)301 public long until(Temporal endExclusive, TemporalUnit unit) { 302 CopticDate end = getChronology().date(endExclusive); 303 if (unit instanceof ChronoUnit) { 304 return LocalDate.from(this).until(end, unit); // TODO: this is wrong 305 } 306 return unit.between(this, end); 307 } 308 309 @Override until(ChronoLocalDate endDate)310 public Period until(ChronoLocalDate endDate) { 311 // TODO: untested 312 CopticDate end = getChronology().date(endDate); 313 long totalMonths = (end.prolepticYear - this.prolepticYear) * 13 + (end.month - this.month); // safe 314 int days = end.day - this.day; 315 if (totalMonths > 0 && days < 0) { 316 totalMonths--; 317 CopticDate calcDate = this.plusMonths(totalMonths); 318 days = (int) (end.toEpochDay() - calcDate.toEpochDay()); // safe 319 } else if (totalMonths < 0 && days > 0) { 320 totalMonths++; 321 days -= end.lengthOfMonth(); 322 } 323 long years = totalMonths / 13; // safe 324 int months = (int) (totalMonths % 13); // safe 325 return Period.of(Math.toIntExact(years), months, days); 326 } 327 328 //----------------------------------------------------------------------- 329 @Override toEpochDay()330 public long toEpochDay() { 331 long year = (long) prolepticYear; 332 long copticEpochDay = ((year - 1) * 365) + Math.floorDiv(year, 4) + (get(ChronoField.DAY_OF_YEAR) - 1); 333 return copticEpochDay - EPOCH_DAY_DIFFERENCE; 334 } 335 336 @Override toString()337 public String toString() { 338 // getLong() reduces chances of exceptions in toString() 339 long yoe = getLong(YEAR_OF_ERA); 340 long moy = getLong(MONTH_OF_YEAR); 341 long dom = getLong(DAY_OF_MONTH); 342 StringBuilder buf = new StringBuilder(30); 343 buf.append(getChronology().toString()) 344 .append(" ") 345 .append(getEra()) 346 .append(" ") 347 .append(yoe) 348 .append(moy < 10 ? "-0" : "-").append(moy) 349 .append(dom < 10 ? "-0" : "-").append(dom); 350 return buf.toString(); 351 } 352 353 @Override 354 public boolean equals(Object obj) { 355 if (this == obj) { 356 return true; 357 } 358 if (obj instanceof CopticDate) { 359 CopticDate cd = (CopticDate)obj; 360 if (this.prolepticYear == cd.prolepticYear && 361 this.month == cd.month && 362 this.day == cd.day) { 363 return true; 364 } 365 } 366 return false; 367 } 368 369 @Override 370 public int hashCode() { 371 long epDay = toEpochDay(); 372 return getChronology().hashCode() ^ ((int) (epDay ^ (epDay >>> 32))); 373 } 374 375 } 376