test/java/time/tck/java/time/chrono/CopticDate.java

Print this page

        

*** 51,61 **** * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ ! package tck.java.time.calendar; import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH; import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR; import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH; import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; --- 51,61 ---- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ ! package tck.java.time.chrono; import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH; import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR; import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH; import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
*** 65,97 **** import java.io.Serializable; import java.time.DateTimeException; import java.time.LocalDate; import java.time.temporal.ChronoField; - import java.time.temporal.ChronoLocalDate; import java.time.temporal.ChronoUnit; - import java.time.temporal.Era; import java.time.temporal.Temporal; - import java.time.temporal.TemporalAdjuster; - import java.time.temporal.TemporalAdder; - import java.time.temporal.TemporalSubtractor; import java.time.temporal.TemporalField; import java.time.temporal.TemporalUnit; import java.time.temporal.ValueRange; ! import java.time.temporal.Year; /** * A date in the Coptic calendar system. * <p> ! * This implements {@code ChronoLocalDate} for the {@link CopticChrono Coptic calendar}. * * <h4>Implementation notes</h4> * This class is immutable and thread-safe. */ ! final class CopticDate ! implements ChronoLocalDate<CopticChrono>, Serializable { /** * Serialization version. */ private static final long serialVersionUID = -7920528871688876868L; --- 65,94 ---- import java.io.Serializable; import java.time.DateTimeException; import java.time.LocalDate; + import java.time.Period; + import java.time.chrono.ChronoLocalDate; import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; import java.time.temporal.Temporal; import java.time.temporal.TemporalField; import java.time.temporal.TemporalUnit; import java.time.temporal.ValueRange; ! import java.time.Year; /** * A date in the Coptic calendar system. * <p> ! * This implements {@code ChronoLocalDate} for the {@link CopticChronology Coptic calendar}. * * <h4>Implementation notes</h4> * This class is immutable and thread-safe. */ ! public final class CopticDate ! implements ChronoLocalDate<CopticDate>, Serializable { /** * Serialization version. */ private static final long serialVersionUID = -7920528871688876868L;
*** 131,141 **** return new CopticDate(prolepticYear, month, dom); } private static CopticDate resolvePreviousValid(int prolepticYear, int month, int day) { if (month == 13 && day > 5) { ! day = CopticChrono.INSTANCE.isLeapYear(prolepticYear) ? 6 : 5; } return new CopticDate(prolepticYear, month, day); } //----------------------------------------------------------------------- --- 128,138 ---- return new CopticDate(prolepticYear, month, dom); } private static CopticDate resolvePreviousValid(int prolepticYear, int month, int day) { if (month == 13 && day > 5) { ! day = CopticChronology.INSTANCE.isLeapYear(prolepticYear) ? 6 : 5; } return new CopticDate(prolepticYear, month, day); } //-----------------------------------------------------------------------
*** 146,161 **** * @param month the Coptic month, from 1 to 13 * @param dayOfMonth the Coptic day-of-month, from 1 to 30 * @throws DateTimeException if the date is invalid */ CopticDate(int prolepticYear, int month, int dayOfMonth) { ! CopticChrono.MOY_RANGE.checkValidValue(month, MONTH_OF_YEAR); ValueRange range; if (month == 13) { ! range = CopticChrono.INSTANCE.isLeapYear(prolepticYear) ? CopticChrono.DOM_RANGE_LEAP : CopticChrono.DOM_RANGE_NONLEAP; } else { ! range = CopticChrono.DOM_RANGE; } range.checkValidValue(dayOfMonth, DAY_OF_MONTH); this.prolepticYear = prolepticYear; this.month = (short) month; --- 143,158 ---- * @param month the Coptic month, from 1 to 13 * @param dayOfMonth the Coptic day-of-month, from 1 to 30 * @throws DateTimeException if the date is invalid */ CopticDate(int prolepticYear, int month, int dayOfMonth) { ! CopticChronology.MOY_RANGE.checkValidValue(month, MONTH_OF_YEAR); ValueRange range; if (month == 13) { ! range = CopticChronology.INSTANCE.isLeapYear(prolepticYear) ? CopticChronology.DOM_RANGE_LEAP : CopticChronology.DOM_RANGE_NONLEAP; } else { ! range = CopticChronology.DOM_RANGE; } range.checkValidValue(dayOfMonth, DAY_OF_MONTH); this.prolepticYear = prolepticYear; this.month = (short) month;
*** 172,183 **** return this; } //----------------------------------------------------------------------- @Override ! public CopticChrono getChrono() { ! return CopticChrono.INSTANCE; } //----------------------------------------------------------------------- @Override public int lengthOfMonth() { --- 169,180 ---- return this; } //----------------------------------------------------------------------- @Override ! public CopticChronology getChronology() { ! return CopticChronology.INSTANCE; } //----------------------------------------------------------------------- @Override public int lengthOfMonth() {
*** 200,214 **** case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, month == 13 ? 1 : 5); case YEAR: case YEAR_OF_ERA: return (prolepticYear <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE)); // TODO } ! return getChrono().range(f); } throw new DateTimeException("Unsupported field: " + field.getName()); } ! return field.doRange(this); } @Override public long getLong(TemporalField field) { if (field instanceof ChronoField) { --- 197,211 ---- case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, month == 13 ? 1 : 5); case YEAR: case YEAR_OF_ERA: return (prolepticYear <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE)); // TODO } ! return getChronology().range(f); } throw new DateTimeException("Unsupported field: " + field.getName()); } ! return field.rangeRefinedBy(this); } @Override public long getLong(TemporalField field) { if (field instanceof ChronoField) {
*** 226,236 **** case YEAR: return prolepticYear; case ERA: return (prolepticYear >= 1 ? 1 : 0); } throw new DateTimeException("Unsupported field: " + field.getName()); } ! return field.doGet(this); } @Override public CopticDate with(TemporalField field, long newValue) { if (field instanceof ChronoField) { --- 223,233 ---- case YEAR: return prolepticYear; case ERA: return (prolepticYear >= 1 ? 1 : 0); } throw new DateTimeException("Unsupported field: " + field.getName()); } ! return field.getFrom(this); } @Override public CopticDate with(TemporalField field, long newValue) { if (field instanceof ChronoField) {
*** 251,261 **** case YEAR: return resolvePreviousValid(nvalue, month, day); case ERA: return resolvePreviousValid(1 - prolepticYear, month, day); } throw new DateTimeException("Unsupported field: " + field.getName()); } ! return field.doWith(this, newValue); } //----------------------------------------------------------------------- @Override public CopticDate plus(long amountToAdd, TemporalUnit unit) { --- 248,258 ---- case YEAR: return resolvePreviousValid(nvalue, month, day); case ERA: return resolvePreviousValid(1 - prolepticYear, month, day); } throw new DateTimeException("Unsupported field: " + field.getName()); } ! return field.adjustInto(this, newValue); } //----------------------------------------------------------------------- @Override public CopticDate plus(long amountToAdd, TemporalUnit unit) {
*** 270,280 **** case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100)); case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000)); } throw new DateTimeException(unit.getName() + " not valid for CopticDate"); } ! return unit.doPlus(this, amountToAdd); } //----------------------------------------------------------------------- private CopticDate plusYears(long years) { return plusMonths(Math.multiplyExact(years, 13)); --- 267,277 ---- case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100)); case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000)); } throw new DateTimeException(unit.getName() + " not valid for CopticDate"); } ! return unit.addTo(this, amountToAdd); } //----------------------------------------------------------------------- private CopticDate plusYears(long years) { return plusMonths(Math.multiplyExact(years, 13));
*** 302,318 **** public long periodUntil(Temporal endDateTime, TemporalUnit unit) { if (endDateTime instanceof ChronoLocalDate == false) { throw new DateTimeException("Unable to calculate period between objects of two different types"); } ChronoLocalDate<?> end = (ChronoLocalDate<?>) endDateTime; ! if (getChrono().equals(end.getChrono()) == false) { throw new DateTimeException("Unable to calculate period between two different chronologies"); } if (unit instanceof ChronoUnit) { return LocalDate.from(this).periodUntil(end, unit); // TODO: this is wrong } ! return unit.between(this, endDateTime).getAmount(); } //----------------------------------------------------------------------- @Override public long toEpochDay() { --- 299,334 ---- public long periodUntil(Temporal endDateTime, TemporalUnit unit) { if (endDateTime instanceof ChronoLocalDate == false) { throw new DateTimeException("Unable to calculate period between objects of two different types"); } ChronoLocalDate<?> end = (ChronoLocalDate<?>) endDateTime; ! if (getChronology().equals(end.getChronology()) == false) { throw new DateTimeException("Unable to calculate period between two different chronologies"); } if (unit instanceof ChronoUnit) { return LocalDate.from(this).periodUntil(end, unit); // TODO: this is wrong } ! return unit.between(this, endDateTime); ! } ! ! @Override ! public Period periodUntil(ChronoLocalDate<?> endDate) { ! // TODO: untested ! CopticDate end = (CopticDate) getChronology().date(endDate); ! long totalMonths = (end.prolepticYear - this.prolepticYear) * 13 + (end.month - this.month); // safe ! int days = end.day - this.day; ! if (totalMonths > 0 && days < 0) { ! totalMonths--; ! CopticDate calcDate = this.plusMonths(totalMonths); ! days = (int) (end.toEpochDay() - calcDate.toEpochDay()); // safe ! } else if (totalMonths < 0 && days > 0) { ! totalMonths++; ! days -= end.lengthOfMonth(); ! } ! long years = totalMonths / 13; // safe ! int months = (int) (totalMonths % 13); // safe ! return Period.of(Math.toIntExact(years), months, days); } //----------------------------------------------------------------------- @Override public long toEpochDay() {
*** 326,336 **** // getLong() reduces chances of exceptions in toString() long yoe = getLong(YEAR_OF_ERA); long moy = getLong(MONTH_OF_YEAR); long dom = getLong(DAY_OF_MONTH); StringBuilder buf = new StringBuilder(30); ! buf.append(getChrono().toString()) .append(" ") .append(getEra()) .append(" ") .append(yoe) .append(moy < 10 ? "-0" : "-").append(moy) --- 342,352 ---- // getLong() reduces chances of exceptions in toString() long yoe = getLong(YEAR_OF_ERA); long moy = getLong(MONTH_OF_YEAR); long dom = getLong(DAY_OF_MONTH); StringBuilder buf = new StringBuilder(30); ! buf.append(getChronology().toString()) .append(" ") .append(getEra()) .append(" ") .append(yoe) .append(moy < 10 ? "-0" : "-").append(moy)