diff options
author | rriggs <none@none> | 2013-09-14 22:46:49 +0100 |
---|---|---|
committer | rriggs <none@none> | 2013-09-14 22:46:49 +0100 |
commit | e55cd4b8be4e45c4fb72be658343ddbbc28f7eec (patch) | |
tree | 9bd90bf96723ee8d99b7873cecebe1cb9d8165da /src/share/classes/java/time | |
parent | 458e1139a6143d5a1bdbcf710e1be39be38d4e13 (diff) | |
download | jdk8u_jdk-e55cd4b8be4e45c4fb72be658343ddbbc28f7eec.tar.gz |
8024835: Change until() to accept any compatible temporal
Summary: Method until(Temporal,TemporalUnit) now uses from() to convert; Enhance from() methods where necessary
Reviewed-by: sherman
Contributed-by: scolebourne@joda.org
Diffstat (limited to 'src/share/classes/java/time')
22 files changed, 181 insertions, 184 deletions
diff --git a/src/share/classes/java/time/Duration.java b/src/share/classes/java/time/Duration.java index ce2ba7781b..896a2990ea 100644 --- a/src/share/classes/java/time/Duration.java +++ b/src/share/classes/java/time/Duration.java @@ -441,9 +441,13 @@ public final class Duration //----------------------------------------------------------------------- /** - * Obtains a {@code Duration} representing the duration between two instants. + * Obtains a {@code Duration} representing the duration between two temporal objects. + * <p> + * This calculates the duration between two temporal objects. If the objects + * are of different types, then the duration is calculated based on the type + * of the first object. For example, if the first argument is a {@code LocalTime} + * then the second argument is converted to a {@code LocalTime}. * <p> - * This calculates the duration between two temporal objects of the same type. * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit. * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported. diff --git a/src/share/classes/java/time/Instant.java b/src/share/classes/java/time/Instant.java index 9d74e29f91..a7bd6f9200 100644 --- a/src/share/classes/java/time/Instant.java +++ b/src/share/classes/java/time/Instant.java @@ -362,6 +362,10 @@ public final class Instant * @throws DateTimeException if unable to convert to an {@code Instant} */ public static Instant from(TemporalAccessor temporal) { + if (temporal instanceof Instant) { + return (Instant) temporal; + } + Objects.requireNonNull(temporal, "temporal"); long instantSecs = temporal.getLong(INSTANT_SECONDS); int nanoOfSecond = temporal.get(NANO_OF_SECOND); return Instant.ofEpochSecond(instantSecs, nanoOfSecond); @@ -1091,7 +1095,8 @@ public final class Instant * The result will be negative if the end is before the start. * The calculation returns a whole number, representing the number of * complete units between the two instants. - * The {@code Temporal} passed to this method must be an {@code Instant}. + * The {@code Temporal} passed to this method is converted to a + * {@code Instant} using {@link #from(TemporalAccessor)}. * For example, the amount in days between two dates can be calculated * using {@code startInstant.until(endInstant, SECONDS)}. * <p> @@ -1112,25 +1117,22 @@ public final class Instant * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} - * passing {@code this} as the first argument and the input temporal as - * the second argument. + * passing {@code this} as the first argument and the converted input temporal + * as the second argument. * <p> * This instance is immutable and unaffected by this method call. * - * @param endInstant the end date, which must be an {@code Instant}, not null + * @param endExclusive the end date, exclusive, which is converted to an {@code Instant}, not null * @param unit the unit to measure the amount in, not null * @return the amount of time between this instant and the end instant - * @throws DateTimeException if the amount cannot be calculated + * @throws DateTimeException if the amount cannot be calculated, or the end + * temporal cannot be converted to an {@code Instant} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ @Override - public long until(Temporal endInstant, TemporalUnit unit) { - if (endInstant instanceof Instant == false) { - Objects.requireNonNull(endInstant, "endInstant"); - throw new DateTimeException("Unable to calculate amount as objects are of two different types"); - } - Instant end = (Instant) endInstant; + public long until(Temporal endExclusive, TemporalUnit unit) { + Instant end = Instant.from(endExclusive); if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; switch (f) { @@ -1145,7 +1147,7 @@ public final class Instant } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } - return unit.between(this, endInstant); + return unit.between(this, end); } private long nanosUntil(Instant end) { diff --git a/src/share/classes/java/time/LocalDate.java b/src/share/classes/java/time/LocalDate.java index 110a80fc9c..f388959aa6 100644 --- a/src/share/classes/java/time/LocalDate.java +++ b/src/share/classes/java/time/LocalDate.java @@ -353,6 +353,7 @@ public final class LocalDate * @throws DateTimeException if unable to convert to a {@code LocalDate} */ public static LocalDate from(TemporalAccessor temporal) { + Objects.requireNonNull(temporal, "temporal"); LocalDate date = temporal.query(TemporalQuery.localDate()); if (date == null) { throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " + temporal.getClass()); @@ -1125,11 +1126,11 @@ public final class LocalDate */ @Override public LocalDate plus(TemporalAmount amountToAdd) { - Objects.requireNonNull(amountToAdd, "amountToAdd"); if (amountToAdd instanceof Period) { Period periodToAdd = (Period) amountToAdd; return plusMonths(periodToAdd.toTotalMonths()).plusDays(periodToAdd.getDays()); } + Objects.requireNonNull(amountToAdd, "amountToAdd"); return (LocalDate) amountToAdd.addTo(this); } @@ -1358,11 +1359,11 @@ public final class LocalDate */ @Override public LocalDate minus(TemporalAmount amountToSubtract) { - Objects.requireNonNull(amountToSubtract, "amountToSubtract"); if (amountToSubtract instanceof Period) { Period periodToSubtract = (Period) amountToSubtract; return minusMonths(periodToSubtract.toTotalMonths()).minusDays(periodToSubtract.getDays()); } + Objects.requireNonNull(amountToSubtract, "amountToSubtract"); return (LocalDate) amountToSubtract.subtractFrom(this); } @@ -1541,7 +1542,8 @@ public final class LocalDate * objects in terms of a single {@code TemporalUnit}. * The start and end points are {@code this} and the specified date. * The result will be negative if the end is before the start. - * The {@code Temporal} passed to this method must be a {@code LocalDate}. + * The {@code Temporal} passed to this method is converted to a + * {@code LocalDate} using {@link #from(TemporalAccessor)}. * For example, the amount in days between two dates can be calculated * using {@code startDate.until(endDate, DAYS)}. * <p> @@ -1567,26 +1569,22 @@ public final class LocalDate * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} - * passing {@code this} as the first argument and the input temporal as - * the second argument. + * passing {@code this} as the first argument and the converted input temporal + * as the second argument. * <p> * This instance is immutable and unaffected by this method call. * - * @param endDate the end date, which must be a {@code LocalDate}, not null + * @param endExclusive the end date, exclusive, which is converted to a {@code LocalDate}, not null * @param unit the unit to measure the amount in, not null * @return the amount of time between this date and the end date - * @throws DateTimeException if the amount cannot be calculated + * @throws DateTimeException if the amount cannot be calculated, or the end + * temporal cannot be converted to a {@code LocalDate} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ @Override - public long until(Temporal endDate, TemporalUnit unit) { - Objects.requireNonNull(unit, "unit"); - if (endDate instanceof LocalDate == false) { - Objects.requireNonNull(endDate, "endDate"); - throw new DateTimeException("Unable to calculate amount as objects are of two different types"); - } - LocalDate end = (LocalDate) endDate; + public long until(Temporal endExclusive, TemporalUnit unit) { + LocalDate end = LocalDate.from(endExclusive); if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { case DAYS: return daysUntil(end); @@ -1600,7 +1598,7 @@ public final class LocalDate } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } - return unit.between(this, endDate); + return unit.between(this, end); } long daysUntil(LocalDate end) { diff --git a/src/share/classes/java/time/LocalDateTime.java b/src/share/classes/java/time/LocalDateTime.java index d0b72b90f8..aed44dd4bf 100644 --- a/src/share/classes/java/time/LocalDateTime.java +++ b/src/share/classes/java/time/LocalDateTime.java @@ -1129,11 +1129,11 @@ public final class LocalDateTime */ @Override public LocalDateTime plus(TemporalAmount amountToAdd) { - Objects.requireNonNull(amountToAdd, "amountToAdd"); if (amountToAdd instanceof Period) { Period periodToAdd = (Period) amountToAdd; return with(date.plus(periodToAdd), time); } + Objects.requireNonNull(amountToAdd, "amountToAdd"); return (LocalDateTime) amountToAdd.addTo(this); } @@ -1348,11 +1348,11 @@ public final class LocalDateTime */ @Override public LocalDateTime minus(TemporalAmount amountToSubtract) { - Objects.requireNonNull(amountToSubtract, "amountToSubtract"); if (amountToSubtract instanceof Period) { Period periodToSubtract = (Period) amountToSubtract; return with(date.minus(periodToSubtract), time); } + Objects.requireNonNull(amountToSubtract, "amountToSubtract"); return (LocalDateTime) amountToSubtract.subtractFrom(this); } @@ -1621,7 +1621,8 @@ public final class LocalDateTime * objects in terms of a single {@code TemporalUnit}. * The start and end points are {@code this} and the specified date-time. * The result will be negative if the end is before the start. - * The {@code Temporal} passed to this method must be a {@code LocalDateTime}. + * The {@code Temporal} passed to this method is converted to a + * {@code LocalDateTime} using {@link #from(TemporalAccessor)}. * For example, the amount in days between two date-times can be calculated * using {@code startDateTime.until(endDateTime, DAYS)}. * <p> @@ -1649,25 +1650,22 @@ public final class LocalDateTime * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} - * passing {@code this} as the first argument and the input temporal as - * the second argument. + * passing {@code this} as the first argument and the converted input temporal + * as the second argument. * <p> * This instance is immutable and unaffected by this method call. * - * @param endDateTime the end date-time, which must be a {@code LocalDateTime}, not null + * @param endExclusive the end date, exclusive, which is converted to a {@code LocalDateTime}, not null * @param unit the unit to measure the amount in, not null * @return the amount of time between this date-time and the end date-time - * @throws DateTimeException if the amount cannot be calculated + * @throws DateTimeException if the amount cannot be calculated, or the end + * temporal cannot be converted to a {@code LocalDateTime} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ @Override - public long until(Temporal endDateTime, TemporalUnit unit) { - if (endDateTime instanceof LocalDateTime == false) { - Objects.requireNonNull(endDateTime, "endDateTime"); - throw new DateTimeException("Unable to calculate amount as objects are of two different types"); - } - LocalDateTime end = (LocalDateTime) endDateTime; + public long until(Temporal endExclusive, TemporalUnit unit) { + LocalDateTime end = LocalDateTime.from(endExclusive); if (unit instanceof ChronoUnit) { if (unit.isTimeBased()) { long amount = date.daysUntil(end.date); @@ -1721,7 +1719,7 @@ public final class LocalDateTime } return date.until(endDate, unit); } - return unit.between(this, endDateTime); + return unit.between(this, end); } /** diff --git a/src/share/classes/java/time/LocalTime.java b/src/share/classes/java/time/LocalTime.java index a6270db19c..77ab2c7f28 100644 --- a/src/share/classes/java/time/LocalTime.java +++ b/src/share/classes/java/time/LocalTime.java @@ -394,6 +394,7 @@ public final class LocalTime * @throws DateTimeException if unable to convert to a {@code LocalTime} */ public static LocalTime from(TemporalAccessor temporal) { + Objects.requireNonNull(temporal, "temporal"); LocalTime time = temporal.query(TemporalQuery.localTime()); if (time == null) { throw new DateTimeException("Unable to obtain LocalTime from TemporalAccessor: " + temporal.getClass()); @@ -1330,7 +1331,8 @@ public final class LocalTime * objects in terms of a single {@code TemporalUnit}. * The start and end points are {@code this} and the specified time. * The result will be negative if the end is before the start. - * The {@code Temporal} passed to this method must be a {@code LocalTime}. + * The {@code Temporal} passed to this method is converted to a + * {@code LocalTime} using {@link #from(TemporalAccessor)}. * For example, the amount in hours between two times can be calculated * using {@code startTime.until(endTime, HOURS)}. * <p> @@ -1356,25 +1358,22 @@ public final class LocalTime * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} - * passing {@code this} as the first argument and the input temporal as - * the second argument. + * passing {@code this} as the first argument and the converted input temporal + * as the second argument. * <p> * This instance is immutable and unaffected by this method call. * - * @param endTime the end time, which must be a {@code LocalTime}, not null + * @param endExclusive the end time, exclusive, which is converted to a {@code LocalTime}, not null * @param unit the unit to measure the amount in, not null * @return the amount of time between this time and the end time - * @throws DateTimeException if the amount cannot be calculated + * @throws DateTimeException if the amount cannot be calculated, or the end + * temporal cannot be converted to a {@code LocalTime} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ @Override - public long until(Temporal endTime, TemporalUnit unit) { - if (endTime instanceof LocalTime == false) { - Objects.requireNonNull(endTime, "endTime"); - throw new DateTimeException("Unable to calculate amount as objects are of two different types"); - } - LocalTime end = (LocalTime) endTime; + public long until(Temporal endExclusive, TemporalUnit unit) { + LocalTime end = LocalTime.from(endExclusive); if (unit instanceof ChronoUnit) { long nanosUntil = end.toNanoOfDay() - toNanoOfDay(); // no overflow switch ((ChronoUnit) unit) { @@ -1388,7 +1387,7 @@ public final class LocalTime } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } - return unit.between(this, endTime); + return unit.between(this, end); } /** diff --git a/src/share/classes/java/time/MonthDay.java b/src/share/classes/java/time/MonthDay.java index 22807822f2..67f5d4fa87 100644 --- a/src/share/classes/java/time/MonthDay.java +++ b/src/share/classes/java/time/MonthDay.java @@ -246,7 +246,8 @@ public final class MonthDay * <p> * The conversion extracts the {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} and * {@link ChronoField#DAY_OF_MONTH DAY_OF_MONTH} fields. - * The extraction is only permitted if the date-time has an ISO chronology. + * The extraction is only permitted if the temporal object has an ISO + * chronology, or can be converted to a {@code LocalDate}. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used in queries via method reference, {@code MonthDay::from}. diff --git a/src/share/classes/java/time/OffsetDateTime.java b/src/share/classes/java/time/OffsetDateTime.java index f894e53e46..410c7f3a74 100644 --- a/src/share/classes/java/time/OffsetDateTime.java +++ b/src/share/classes/java/time/OffsetDateTime.java @@ -1592,7 +1592,8 @@ public final class OffsetDateTime * For example, the period in days between two date-times can be calculated * using {@code startDateTime.until(endDateTime, DAYS)}. * <p> - * The {@code Temporal} passed to this method must be an {@code OffsetDateTime}. + * The {@code Temporal} passed to this method is converted to a + * {@code OffsetDateTime} using {@link #from(TemporalAccessor)}. * If the offset differs between the two date-times, the specified * end date-time is normalized to have the same offset as this date-time. * <p> @@ -1620,30 +1621,27 @@ public final class OffsetDateTime * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} - * passing {@code this} as the first argument and the input temporal as - * the second argument. + * passing {@code this} as the first argument and the converted input temporal + * as the second argument. * <p> * This instance is immutable and unaffected by this method call. * - * @param endDateTime the end date-time, which must be an {@code OffsetDateTime}, not null + * @param endExclusive the end date, exclusive, which is converted to an {@code OffsetDateTime}, not null * @param unit the unit to measure the amount in, not null * @return the amount of time between this date-time and the end date-time - * @throws DateTimeException if the amount cannot be calculated + * @throws DateTimeException if the amount cannot be calculated, or the end + * temporal cannot be converted to an {@code OffsetDateTime} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ @Override - public long until(Temporal endDateTime, TemporalUnit unit) { - if (endDateTime instanceof OffsetDateTime == false) { - Objects.requireNonNull(endDateTime, "endDateTime"); - throw new DateTimeException("Unable to calculate amount as objects are of two different types"); - } + public long until(Temporal endExclusive, TemporalUnit unit) { + OffsetDateTime end = OffsetDateTime.from(endExclusive); if (unit instanceof ChronoUnit) { - OffsetDateTime end = (OffsetDateTime) endDateTime; end = end.withOffsetSameInstant(offset); return dateTime.until(end.dateTime, unit); } - return unit.between(this, endDateTime); + return unit.between(this, end); } /** diff --git a/src/share/classes/java/time/OffsetTime.java b/src/share/classes/java/time/OffsetTime.java index 6c67ef82bb..a8dbf8a7a5 100644 --- a/src/share/classes/java/time/OffsetTime.java +++ b/src/share/classes/java/time/OffsetTime.java @@ -1124,7 +1124,8 @@ public final class OffsetTime * For example, the period in hours between two times can be calculated * using {@code startTime.until(endTime, HOURS)}. * <p> - * The {@code Temporal} passed to this method must be an {@code OffsetTime}. + * The {@code Temporal} passed to this method is converted to a + * {@code OffsetTime} using {@link #from(TemporalAccessor)}. * If the offset differs between the two times, then the specified * end time is normalized to have the same offset as this time. * <p> @@ -1150,26 +1151,23 @@ public final class OffsetTime * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} - * passing {@code this} as the first argument and the input temporal as - * the second argument. + * passing {@code this} as the first argument and the converted input temporal + * as the second argument. * <p> * This instance is immutable and unaffected by this method call. * - * @param endTime the end time, which must be an {@code OffsetTime}, not null + * @param endExclusive the end date, exclusive, which is converted to an {@code OffsetTime}, not null * @param unit the unit to measure the amount in, not null * @return the amount of time between this time and the end time - * @throws DateTimeException if the amount cannot be calculated + * @throws DateTimeException if the amount cannot be calculated, or the end + * temporal cannot be converted to an {@code OffsetTime} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ @Override - public long until(Temporal endTime, TemporalUnit unit) { - if (endTime instanceof OffsetTime == false) { - Objects.requireNonNull(endTime, "endTime"); - throw new DateTimeException("Unable to calculate amount as objects are of two different types"); - } + public long until(Temporal endExclusive, TemporalUnit unit) { + OffsetTime end = OffsetTime.from(endExclusive); if (unit instanceof ChronoUnit) { - OffsetTime end = (OffsetTime) endTime; long nanosUntil = end.toEpochNano() - toEpochNano(); // no overflow switch ((ChronoUnit) unit) { case NANOS: return nanosUntil; @@ -1182,7 +1180,7 @@ public final class OffsetTime } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } - return unit.between(this, endTime); + return unit.between(this, end); } /** diff --git a/src/share/classes/java/time/Year.java b/src/share/classes/java/time/Year.java index f51bda6966..377dfd5185 100644 --- a/src/share/classes/java/time/Year.java +++ b/src/share/classes/java/time/Year.java @@ -242,6 +242,7 @@ public final class Year if (temporal instanceof Year) { return (Year) temporal; } + Objects.requireNonNull(temporal, "temporal"); try { if (IsoChronology.INSTANCE.equals(Chronology.from(temporal)) == false) { temporal = LocalDate.from(temporal); @@ -859,7 +860,8 @@ public final class Year * objects in terms of a single {@code TemporalUnit}. * The start and end points are {@code this} and the specified year. * The result will be negative if the end is before the start. - * The {@code Temporal} passed to this method must be a {@code Year}. + * The {@code Temporal} passed to this method is converted to a + * {@code Year} using {@link #from(TemporalAccessor)}. * For example, the period in decades between two year can be calculated * using {@code startYear.until(endYear, DECADES)}. * <p> @@ -885,25 +887,22 @@ public final class Year * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} - * passing {@code this} as the first argument and the input temporal as - * the second argument. + * passing {@code this} as the first argument and the converted input temporal + * as the second argument. * <p> * This instance is immutable and unaffected by this method call. * - * @param endYear the end year, which must be a {@code Year}, not null + * @param endExclusive the end date, exclusive, which is converted to a {@code Year}, not null * @param unit the unit to measure the amount in, not null * @return the amount of time between this year and the end year - * @throws DateTimeException if the amount cannot be calculated + * @throws DateTimeException if the amount cannot be calculated, or the end + * temporal cannot be converted to a {@code Year} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ @Override - public long until(Temporal endYear, TemporalUnit unit) { - if (endYear instanceof Year == false) { - Objects.requireNonNull(endYear, "endYear"); - throw new DateTimeException("Unable to calculate amount as objects are of two different types"); - } - Year end = (Year) endYear; + public long until(Temporal endExclusive, TemporalUnit unit) { + Year end = Year.from(endExclusive); if (unit instanceof ChronoUnit) { long yearsUntil = ((long) end.year) - year; // no overflow switch ((ChronoUnit) unit) { @@ -915,7 +914,7 @@ public final class Year } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } - return unit.between(this, endYear); + return unit.between(this, end); } /** diff --git a/src/share/classes/java/time/YearMonth.java b/src/share/classes/java/time/YearMonth.java index 541676117e..223c90ab0f 100644 --- a/src/share/classes/java/time/YearMonth.java +++ b/src/share/classes/java/time/YearMonth.java @@ -245,6 +245,7 @@ public final class YearMonth if (temporal instanceof YearMonth) { return (YearMonth) temporal; } + Objects.requireNonNull(temporal, "temporal"); try { if (IsoChronology.INSTANCE.equals(Chronology.from(temporal)) == false) { temporal = LocalDate.from(temporal); @@ -992,7 +993,8 @@ public final class YearMonth * objects in terms of a single {@code TemporalUnit}. * The start and end points are {@code this} and the specified year-month. * The result will be negative if the end is before the start. - * The {@code Temporal} passed to this method must be a {@code YearMonth}. + * The {@code Temporal} passed to this method is converted to a + * {@code YearMonth} using {@link #from(TemporalAccessor)}. * For example, the period in years between two year-months can be calculated * using {@code startYearMonth.until(endYearMonth, YEARS)}. * <p> @@ -1018,25 +1020,22 @@ public final class YearMonth * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} - * passing {@code this} as the first argument and the input temporal as - * the second argument. + * passing {@code this} as the first argument and the converted input temporal + * as the second argument. * <p> * This instance is immutable and unaffected by this method call. * - * @param endYearMonth the end year-month, which must be a {@code YearMonth}, not null + * @param endExclusive the end date, exclusive, which is converted to a {@code YearMonth}, not null * @param unit the unit to measure the amount in, not null * @return the amount of time between this year-month and the end year-month - * @throws DateTimeException if the amount cannot be calculated + * @throws DateTimeException if the amount cannot be calculated, or the end + * temporal cannot be converted to a {@code YearMonth} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ @Override - public long until(Temporal endYearMonth, TemporalUnit unit) { - if (endYearMonth instanceof YearMonth == false) { - Objects.requireNonNull(endYearMonth, "endYearMonth"); - throw new DateTimeException("Unable to calculate amount as objects are of two different types"); - } - YearMonth end = (YearMonth) endYearMonth; + public long until(Temporal endExclusive, TemporalUnit unit) { + YearMonth end = YearMonth.from(endExclusive); if (unit instanceof ChronoUnit) { long monthsUntil = end.getProlepticMonth() - getProlepticMonth(); // no overflow switch ((ChronoUnit) unit) { @@ -1049,7 +1048,7 @@ public final class YearMonth } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } - return unit.between(this, endYearMonth); + return unit.between(this, end); } /** diff --git a/src/share/classes/java/time/ZoneOffset.java b/src/share/classes/java/time/ZoneOffset.java index 2d63a978d8..3475e5f4db 100644 --- a/src/share/classes/java/time/ZoneOffset.java +++ b/src/share/classes/java/time/ZoneOffset.java @@ -333,6 +333,7 @@ public final class ZoneOffset * @throws DateTimeException if unable to convert to an {@code ZoneOffset} */ public static ZoneOffset from(TemporalAccessor temporal) { + Objects.requireNonNull(temporal, "temporal"); ZoneOffset offset = temporal.query(TemporalQuery.offset()); if (offset == null) { throw new DateTimeException("Unable to obtain ZoneOffset from TemporalAccessor: " + temporal.getClass()); diff --git a/src/share/classes/java/time/ZonedDateTime.java b/src/share/classes/java/time/ZonedDateTime.java index 971ba6daf5..1114ab4edd 100644 --- a/src/share/classes/java/time/ZonedDateTime.java +++ b/src/share/classes/java/time/ZonedDateTime.java @@ -1540,11 +1540,11 @@ public final class ZonedDateTime */ @Override public ZonedDateTime plus(TemporalAmount amountToAdd) { - Objects.requireNonNull(amountToAdd, "amountToAdd"); if (amountToAdd instanceof Period) { Period periodToAdd = (Period) amountToAdd; return resolveLocal(dateTime.plus(periodToAdd)); } + Objects.requireNonNull(amountToAdd, "amountToAdd"); return (ZonedDateTime) amountToAdd.addTo(this); } @@ -1792,11 +1792,11 @@ public final class ZonedDateTime */ @Override public ZonedDateTime minus(TemporalAmount amountToSubtract) { - Objects.requireNonNull(amountToSubtract, "amountToSubtract"); if (amountToSubtract instanceof Period) { Period periodToSubtract = (Period) amountToSubtract; return resolveLocal(dateTime.minus(periodToSubtract)); } + Objects.requireNonNull(amountToSubtract, "amountToSubtract"); return (ZonedDateTime) amountToSubtract.subtractFrom(this); } @@ -2044,7 +2044,8 @@ public final class ZonedDateTime * For example, the period in days between two date-times can be calculated * using {@code startDateTime.until(endDateTime, DAYS)}. * <p> - * The {@code Temporal} passed to this method must be a {@code ZonedDateTime}. + * The {@code Temporal} passed to this method is converted to a + * {@code ZonedDateTime} using {@link #from(TemporalAccessor)}. * If the time-zone differs between the two zoned date-times, the specified * end date-time is normalized to have the same zone as this date-time. * <p> @@ -2086,26 +2087,23 @@ public final class ZonedDateTime * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} - * passing {@code this} as the first argument and the input temporal as - * the second argument. + * passing {@code this} as the first argument and the converted input temporal + * as the second argument. * <p> * This instance is immutable and unaffected by this method call. * - * @param endDateTime the end date-time, which must be a {@code ZonedDateTime}, not null + * @param endExclusive the end date, exclusive, which is converted to a {@code ZonedDateTime}, not null * @param unit the unit to measure the amount in, not null * @return the amount of time between this date-time and the end date-time - * @throws DateTimeException if the amount cannot be calculated + * @throws DateTimeException if the amount cannot be calculated, or the end + * temporal cannot be converted to a {@code ZonedDateTime} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ @Override - public long until(Temporal endDateTime, TemporalUnit unit) { - if (endDateTime instanceof ZonedDateTime == false) { - Objects.requireNonNull(endDateTime, "endDateTime"); - throw new DateTimeException("Unable to calculate amount as objects are of two different types"); - } + public long until(Temporal endExclusive, TemporalUnit unit) { + ZonedDateTime end = ZonedDateTime.from(endExclusive); if (unit instanceof ChronoUnit) { - ZonedDateTime end = (ZonedDateTime) endDateTime; end = end.withZoneSameInstant(zone); if (unit.isDateBased()) { return dateTime.until(end.dateTime, unit); @@ -2113,7 +2111,7 @@ public final class ZonedDateTime return toOffsetDateTime().until(end.toOffsetDateTime(), unit); } } - return unit.between(this, endDateTime); + return unit.between(this, end); } /** diff --git a/src/share/classes/java/time/chrono/ChronoLocalDate.java b/src/share/classes/java/time/chrono/ChronoLocalDate.java index 58ae3b8c1c..58e4f5d525 100644 --- a/src/share/classes/java/time/chrono/ChronoLocalDate.java +++ b/src/share/classes/java/time/chrono/ChronoLocalDate.java @@ -291,6 +291,7 @@ public interface ChronoLocalDate if (temporal instanceof ChronoLocalDate) { return (ChronoLocalDate) temporal; } + Objects.requireNonNull(temporal, "temporal"); Chronology chrono = temporal.query(TemporalQuery.chronology()); if (chrono == null) { throw new DateTimeException("Unable to obtain ChronoLocalDate from TemporalAccessor: " + temporal.getClass()); @@ -560,8 +561,8 @@ public interface ChronoLocalDate * objects in terms of a single {@code TemporalUnit}. * The start and end points are {@code this} and the specified date. * The result will be negative if the end is before the start. - * The {@code Temporal} passed to this method must be a - * {@code ChronoLocalDate} in the same chronology. + * The {@code Temporal} passed to this method is converted to a + * {@code ChronoLocalDate} using {@link Chronology#date(TemporalAccessor)}. * The calculation returns a whole number, representing the number of * complete units between the two dates. * For example, the amount in days between two dates can be calculated @@ -585,20 +586,22 @@ public interface ChronoLocalDate * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} - * passing {@code this} as the first argument and the input temporal as + * passing {@code this} as the first argument and the converted input temporal as * the second argument. * <p> * This instance is immutable and unaffected by this method call. * - * @param endDate the end date, which must be a {@code ChronoLocalDate} - * in the same chronology, not null + * @param endExclusive the end date, exclusive, which is converted to a + * {@code ChronoLocalDate} in the same chronology, not null * @param unit the unit to measure the amount in, not null * @return the amount of time between this date and the end date - * @throws DateTimeException if the amount cannot be calculated + * @throws DateTimeException if the amount cannot be calculated, or the end + * temporal cannot be converted to a {@code ChronoLocalDate} + * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ @Override // override for Javadoc - long until(Temporal endDate, TemporalUnit unit); + long until(Temporal endExclusive, TemporalUnit unit); /** * Calculates the period between this date and another date as a {@code ChronoPeriod}. diff --git a/src/share/classes/java/time/chrono/ChronoLocalDateImpl.java b/src/share/classes/java/time/chrono/ChronoLocalDateImpl.java index fd8c8f8866..6cb115d393 100644 --- a/src/share/classes/java/time/chrono/ChronoLocalDateImpl.java +++ b/src/share/classes/java/time/chrono/ChronoLocalDateImpl.java @@ -372,22 +372,10 @@ abstract class ChronoLocalDateImpl<D extends ChronoLocalDate> } //----------------------------------------------------------------------- - /** - * {@inheritDoc} - * @throws DateTimeException {@inheritDoc} - * @throws ArithmeticException {@inheritDoc} - */ @Override - public long until(Temporal endDateTime, TemporalUnit unit) { - Objects.requireNonNull(endDateTime, "endDateTime"); - Objects.requireNonNull(unit, "unit"); - if (endDateTime instanceof ChronoLocalDate == false) { - throw new DateTimeException("Unable to calculate amount as objects are of two different types"); - } - ChronoLocalDate end = (ChronoLocalDate) endDateTime; - if (getChronology().equals(end.getChronology()) == false) { - throw new DateTimeException("Unable to calculate amount as objects have different chronologies"); - } + public long until(Temporal endExclusive, TemporalUnit unit) { + Objects.requireNonNull(endExclusive, "endExclusive"); + ChronoLocalDate end = getChronology().date(endExclusive); if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { case DAYS: return daysUntil(end); @@ -401,7 +389,8 @@ abstract class ChronoLocalDateImpl<D extends ChronoLocalDate> } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } - return unit.between(this, endDateTime); + Objects.requireNonNull(unit, "unit"); + return unit.between(this, end); } private long daysUntil(ChronoLocalDate end) { diff --git a/src/share/classes/java/time/chrono/ChronoLocalDateTime.java b/src/share/classes/java/time/chrono/ChronoLocalDateTime.java index be93f0e036..cde8a982fb 100644 --- a/src/share/classes/java/time/chrono/ChronoLocalDateTime.java +++ b/src/share/classes/java/time/chrono/ChronoLocalDateTime.java @@ -165,6 +165,7 @@ public interface ChronoLocalDateTime<D extends ChronoLocalDate> if (temporal instanceof ChronoLocalDateTime) { return (ChronoLocalDateTime<?>) temporal; } + Objects.requireNonNull(temporal, "temporal"); Chronology chrono = temporal.query(TemporalQuery.chronology()); if (chrono == null) { throw new DateTimeException("Unable to obtain ChronoLocalDateTime from TemporalAccessor: " + temporal.getClass()); diff --git a/src/share/classes/java/time/chrono/ChronoLocalDateTimeImpl.java b/src/share/classes/java/time/chrono/ChronoLocalDateTimeImpl.java index 13b3a0e881..817ae85411 100644 --- a/src/share/classes/java/time/chrono/ChronoLocalDateTimeImpl.java +++ b/src/share/classes/java/time/chrono/ChronoLocalDateTimeImpl.java @@ -69,7 +69,6 @@ import java.io.ObjectInput; import java.io.ObjectOutput; import java.io.ObjectStreamException; import java.io.Serializable; -import java.time.DateTimeException; import java.time.LocalTime; import java.time.ZoneId; import java.time.temporal.ChronoField; @@ -369,15 +368,10 @@ final class ChronoLocalDateTimeImpl<D extends ChronoLocalDate> //----------------------------------------------------------------------- @Override - public long until(Temporal endDateTime, TemporalUnit unit) { - if (endDateTime instanceof ChronoLocalDateTime == false) { - throw new DateTimeException("Unable to calculate amount as objects are of two different types"); - } + public long until(Temporal endExclusive, TemporalUnit unit) { + Objects.requireNonNull(endExclusive, "endExclusive"); @SuppressWarnings("unchecked") - ChronoLocalDateTime<D> end = (ChronoLocalDateTime<D>) endDateTime; - if (toLocalDate().getChronology().equals(end.toLocalDate().getChronology()) == false) { - throw new DateTimeException("Unable to calculate amount as objects have different chronologies"); - } + ChronoLocalDateTime<D> end = (ChronoLocalDateTime<D>) toLocalDate().getChronology().localDateTime(endExclusive); if (unit instanceof ChronoUnit) { if (unit.isTimeBased()) { long amount = end.getLong(EPOCH_DAY) - date.getLong(EPOCH_DAY); @@ -398,7 +392,8 @@ final class ChronoLocalDateTimeImpl<D extends ChronoLocalDate> } return date.until(endDate, unit); } - return unit.between(this, endDateTime); + Objects.requireNonNull(unit, "unit"); + return unit.between(this, end); } //----------------------------------------------------------------------- diff --git a/src/share/classes/java/time/chrono/ChronoZonedDateTime.java b/src/share/classes/java/time/chrono/ChronoZonedDateTime.java index dd911fcd78..abe8479971 100644 --- a/src/share/classes/java/time/chrono/ChronoZonedDateTime.java +++ b/src/share/classes/java/time/chrono/ChronoZonedDateTime.java @@ -166,6 +166,7 @@ public interface ChronoZonedDateTime<D extends ChronoLocalDate> if (temporal instanceof ChronoZonedDateTime) { return (ChronoZonedDateTime<?>) temporal; } + Objects.requireNonNull(temporal, "temporal"); Chronology chrono = temporal.query(TemporalQuery.chronology()); if (chrono == null) { throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " + temporal.getClass()); diff --git a/src/share/classes/java/time/chrono/ChronoZonedDateTimeImpl.java b/src/share/classes/java/time/chrono/ChronoZonedDateTimeImpl.java index 1cc7b5b578..1fe8ccf019 100644 --- a/src/share/classes/java/time/chrono/ChronoZonedDateTimeImpl.java +++ b/src/share/classes/java/time/chrono/ChronoZonedDateTimeImpl.java @@ -69,7 +69,6 @@ import java.io.ObjectInput; import java.io.ObjectOutput; import java.io.ObjectStreamException; import java.io.Serializable; -import java.time.DateTimeException; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; @@ -234,7 +233,7 @@ final class ChronoZonedDateTimeImpl<D extends ChronoLocalDate> if (trans != null && trans.isOverlap()) { ZoneOffset earlierOffset = trans.getOffsetBefore(); if (earlierOffset.equals(offset) == false) { - return new ChronoZonedDateTimeImpl<D>(dateTime, earlierOffset, zone); + return new ChronoZonedDateTimeImpl<>(dateTime, earlierOffset, zone); } } return this; @@ -246,7 +245,7 @@ final class ChronoZonedDateTimeImpl<D extends ChronoLocalDate> if (trans != null) { ZoneOffset offset = trans.getOffsetAfter(); if (offset.equals(getOffset()) == false) { - return new ChronoZonedDateTimeImpl<D>(dateTime, offset, zone); + return new ChronoZonedDateTimeImpl<>(dateTime, offset, zone); } } return this; @@ -308,20 +307,16 @@ final class ChronoZonedDateTimeImpl<D extends ChronoLocalDate> //----------------------------------------------------------------------- @Override - public long until(Temporal endDateTime, TemporalUnit unit) { - if (endDateTime instanceof ChronoZonedDateTime == false) { - throw new DateTimeException("Unable to calculate amount as objects are of two different types"); - } + public long until(Temporal endExclusive, TemporalUnit unit) { + Objects.requireNonNull(endExclusive, "endExclusive"); @SuppressWarnings("unchecked") - ChronoZonedDateTime<D> end = (ChronoZonedDateTime<D>) endDateTime; - if (toLocalDate().getChronology().equals(end.toLocalDate().getChronology()) == false) { - throw new DateTimeException("Unable to calculate amount as objects have different chronologies"); - } + ChronoZonedDateTime<D> end = (ChronoZonedDateTime<D>) toLocalDate().getChronology().zonedDateTime(endExclusive); if (unit instanceof ChronoUnit) { end = end.withZoneSameInstant(offset); return dateTime.until(end.toLocalDateTime(), unit); } - return unit.between(this, endDateTime); + Objects.requireNonNull(unit, "unit"); + return unit.between(this, end); } //----------------------------------------------------------------------- diff --git a/src/share/classes/java/time/temporal/ChronoUnit.java b/src/share/classes/java/time/temporal/ChronoUnit.java index 19a37e46f4..c704327239 100644 --- a/src/share/classes/java/time/temporal/ChronoUnit.java +++ b/src/share/classes/java/time/temporal/ChronoUnit.java @@ -268,8 +268,8 @@ public enum ChronoUnit implements TemporalUnit { //----------------------------------------------------------------------- @Override - public long between(Temporal temporal1, Temporal temporal2) { - return temporal1.until(temporal2, this); + public long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive) { + return temporal1Inclusive.until(temporal2Exclusive, this); } //----------------------------------------------------------------------- diff --git a/src/share/classes/java/time/temporal/IsoFields.java b/src/share/classes/java/time/temporal/IsoFields.java index eae057afb2..bb19c29987 100644 --- a/src/share/classes/java/time/temporal/IsoFields.java +++ b/src/share/classes/java/time/temporal/IsoFields.java @@ -684,13 +684,16 @@ public final class IsoFields { } @Override - public long between(Temporal temporal1, Temporal temporal2) { + public long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive) { + if (temporal1Inclusive.getClass() != temporal2Exclusive.getClass()) { + return temporal1Inclusive.until(temporal2Exclusive, this); + } switch(this) { case WEEK_BASED_YEARS: - return Math.subtractExact(temporal2.getLong(WEEK_BASED_YEAR), - temporal1.getLong(WEEK_BASED_YEAR)); + return Math.subtractExact(temporal2Exclusive.getLong(WEEK_BASED_YEAR), + temporal1Inclusive.getLong(WEEK_BASED_YEAR)); case QUARTER_YEARS: - return temporal1.until(temporal2, MONTHS) / 3; + return temporal1Inclusive.until(temporal2Exclusive, MONTHS) / 3; default: throw new IllegalStateException("Unreachable"); } diff --git a/src/share/classes/java/time/temporal/Temporal.java b/src/share/classes/java/time/temporal/Temporal.java index 54110bed77..9931c46f4f 100644 --- a/src/share/classes/java/time/temporal/Temporal.java +++ b/src/share/classes/java/time/temporal/Temporal.java @@ -374,8 +374,9 @@ public interface Temporal extends TemporalAccessor { * Calculates the amount of time until another temporal in terms of the specified unit. * <p> * This calculates the amount of time between two temporal objects - * of the same type in terms of a single {@code TemporalUnit}. + * in terms of a single {@code TemporalUnit}. * The start and end points are {@code this} and the specified temporal. + * The end point is converted to be of the same type as the start point if different. * The result will be negative if the end is before the start. * For example, the period in hours between two temporal objects can be * calculated using {@code startTime.until(endTime, HOURS)}. @@ -412,31 +413,36 @@ public interface Temporal extends TemporalAccessor { * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} - * passing {@code this} as the first argument and the input temporal as + * passing {@code this} as the first argument and the converted input temporal as * the second argument. * <p> - * In summary, implementations must behave in a manner equivalent to this code: + * In summary, implementations must behave in a manner equivalent to this pseudo-code: * <pre> - * // check input temporal is the same type as this class + * // convert the end temporal to the same type as this class * if (unit instanceof ChronoUnit) { * // if unit is supported, then calculate and return result * // else throw UnsupportedTemporalTypeException for unsupported units * } - * return unit.between(this, endTemporal); + * return unit.between(this, convertedEndTemporal); * </pre> * <p> + * Note that the unit's {@code between} method must only be invoked if the + * two temporal objects have exactly the same type evaluated by {@code getClass()}. + * <p> * Implementations must ensure that no observable state is altered when this * read-only method is invoked. * - * @param endTemporal the end temporal, of the same type as this object, not null + * @param endExclusive the end temporal, exclusive, converted to be of the + * same type as this object, not null * @param unit the unit to measure the amount in, not null * @return the amount of time between this temporal object and the specified one * in terms of the unit; positive if the specified object is later than this one, * negative if it is earlier than this one - * @throws DateTimeException if the amount cannot be calculated + * @throws DateTimeException if the amount cannot be calculated, or the end + * temporal cannot be converted to the same type as this temporal * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ - long until(Temporal endTemporal, TemporalUnit unit); + long until(Temporal endExclusive, TemporalUnit unit); } diff --git a/src/share/classes/java/time/temporal/TemporalUnit.java b/src/share/classes/java/time/temporal/TemporalUnit.java index 05577d713f..1c41afa059 100644 --- a/src/share/classes/java/time/temporal/TemporalUnit.java +++ b/src/share/classes/java/time/temporal/TemporalUnit.java @@ -231,7 +231,9 @@ public interface TemporalUnit { * Calculates the amount of time between two temporal objects. * <p> * This calculates the amount in terms of this unit. The start and end - * points are supplied as temporal objects and must be of the same type. + * points are supplied as temporal objects and must be of compatible types. + * The implementation will convert the second type to be an instance of the + * first type before the calculating the amount. * The result will be negative if the end is before the start. * For example, the amount in hours between two temporal objects can be * calculated using {@code HOURS.between(startTime, endTime)}. @@ -264,15 +266,22 @@ public interface TemporalUnit { * If the unit is not supported an {@code UnsupportedTemporalTypeException} must be thrown. * Implementations must not alter the specified temporal objects. * - * @param temporal1 the base temporal object, not null - * @param temporal2 the other temporal object, not null - * @return the amount of time between temporal1 and temporal2 in terms of this unit; - * positive if temporal2 is later than temporal1, negative if earlier - * @throws DateTimeException if the amount cannot be calculated + * @implSpec + * Implementations must begin by checking to if the two temporals have the + * same type using {@code getClass()}. If they do not, then the result must be + * obtained by calling {@code temporal1Inclusive.until(temporal2Exclusive, this)}. + * + * @param temporal1Inclusive the base temporal object, not null + * @param temporal2Exclusive the other temporal object, exclusive, not null + * @return the amount of time between temporal1Inclusive and temporal2Exclusive + * in terms of this unit; positive if temporal2Exclusive is later than + * temporal1Inclusive, negative if earlier + * @throws DateTimeException if the amount cannot be calculated, or the end + * temporal cannot be converted to the same type as the start temporal * @throws UnsupportedTemporalTypeException if the unit is not supported by the temporal * @throws ArithmeticException if numeric overflow occurs */ - long between(Temporal temporal1, Temporal temporal2); + long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive); //----------------------------------------------------------------------- /** |