src/share/classes/java/time/chrono/ChronoZonedDateTimeImpl.java
Print this page
@@ -99,11 +99,11 @@
* This class is immutable and thread-safe.
*
* @param <D> the concrete type for the date of this date-time
* @since 1.8
*/
-final class ChronoZonedDateTimeImpl<D extends ChronoLocalDate<D>>
+final class ChronoZonedDateTimeImpl<D extends ChronoLocalDate>
implements ChronoZonedDateTime<D>, Serializable {
/**
* Serialization version.
*/
@@ -129,11 +129,11 @@
* @param localDateTime the local date-time, not null
* @param zone the zone identifier, not null
* @param preferredOffset the zone offset, null if no preference
* @return the zoned date-time, not null
*/
- static <R extends ChronoLocalDate<R>> ChronoZonedDateTime<R> ofBest(
+ static <R extends ChronoLocalDate> ChronoZonedDateTime<R> ofBest(
ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
Objects.requireNonNull(localDateTime, "localDateTime");
Objects.requireNonNull(zone, "zone");
if (zone instanceof ZoneOffset) {
return new ChronoZonedDateTimeImpl<>(localDateTime, (ZoneOffset) zone, zone);
@@ -165,31 +165,50 @@
* @param chrono the chronology, not null
* @param instant the instant, not null
* @param zone the zone identifier, not null
* @return the zoned date-time, not null
*/
- @SuppressWarnings("rawtypes")
static ChronoZonedDateTimeImpl<?> ofInstant(Chronology chrono, Instant instant, ZoneId zone) {
ZoneRules rules = zone.getRules();
ZoneOffset offset = rules.getOffset(instant);
Objects.requireNonNull(offset, "offset"); // protect against bad ZoneRules
LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
- ChronoLocalDateTimeImpl<?> cldt = (ChronoLocalDateTimeImpl<?>) chrono.localDateTime(ldt);
- return new ChronoZonedDateTimeImpl(cldt, offset, zone);
+ ChronoLocalDateTimeImpl<?> cldt = (ChronoLocalDateTimeImpl<?>)chrono.localDateTime(ldt);
+ return new ChronoZonedDateTimeImpl<>(cldt, offset, zone);
}
/**
* Obtains an instance from an {@code Instant}.
*
* @param instant the instant to create the date-time from, not null
* @param zone the time-zone to use, validated not null
* @return the zoned date-time, validated not null
*/
+ @SuppressWarnings("unchecked")
private ChronoZonedDateTimeImpl<D> create(Instant instant, ZoneId zone) {
return (ChronoZonedDateTimeImpl<D>)ofInstant(toLocalDate().getChronology(), instant, zone);
}
+ /**
+ * Casts the {@code Temporal} to {@code ChronoZonedDateTimeImpl} ensuring it bas the specified chronology.
+ *
+ * @param chrono the chronology to check for, not null
+ * @param temporal a date-time to cast, not null
+ * @return the date-time checked and cast to {@code ChronoZonedDateTimeImpl}, not null
+ * @throws ClassCastException if the date-time cannot be cast to ChronoZonedDateTimeImpl
+ * or the chronology is not equal this Chronology
+ */
+ static <R extends ChronoLocalDate> ChronoZonedDateTimeImpl<R> ensureValid(Chronology chrono, Temporal temporal) {
+ @SuppressWarnings("unchecked")
+ ChronoZonedDateTimeImpl<R> other = (ChronoZonedDateTimeImpl<R>) temporal;
+ if (chrono.equals(other.toLocalDate().getChronology()) == false) {
+ throw new ClassCastException("Chronology mismatch, required: " + chrono.getId()
+ + ", actual: " + other.toLocalDate().getChronology().getId());
+ }
+ return other;
+ }
+
//-----------------------------------------------------------------------
/**
* Constructor.
*
* @param dateTime the date-time, not null
@@ -269,36 +288,36 @@
return create(dateTime.toInstant(offset), zone);
}
}
return ofBest(dateTime.with(field, newValue), zone, offset);
}
- return (ChronoZonedDateTime<D>)(toLocalDate().getChronology().ensureChronoZonedDateTime(field.adjustInto(this, newValue)));
+ return ChronoZonedDateTimeImpl.ensureValid(toLocalDate().getChronology(), field.adjustInto(this, newValue));
}
//-----------------------------------------------------------------------
@Override
public ChronoZonedDateTime<D> plus(long amountToAdd, TemporalUnit unit) {
if (unit instanceof ChronoUnit) {
return with(dateTime.plus(amountToAdd, unit));
}
- return (ChronoZonedDateTime<D>)(toLocalDate().getChronology().ensureChronoZonedDateTime(unit.addTo(this, amountToAdd))); /// TODO: Generics replacement Risk!
+ return ChronoZonedDateTimeImpl.ensureValid(toLocalDate().getChronology(), unit.addTo(this, amountToAdd)); /// TODO: Generics replacement Risk!
}
//-----------------------------------------------------------------------
@Override
- public long periodUntil(Temporal endDateTime, TemporalUnit unit) {
+ 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");
}
@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");
}
if (unit instanceof ChronoUnit) {
end = end.withZoneSameInstant(offset);
- return dateTime.periodUntil(end.toLocalDateTime(), unit);
+ return dateTime.until(end.toLocalDateTime(), unit);
}
return unit.between(this, endDateTime);
}
//-----------------------------------------------------------------------