src/share/classes/java/time/chrono/ChronoLocalDateTimeImpl.java
Print this page
*** 96,106 ****
* This class is immutable and thread-safe.
*
* @param <D> the concrete type for the date of this date-time
* @since 1.8
*/
! final class ChronoLocalDateTimeImpl<D extends ChronoLocalDate<D>>
implements ChronoLocalDateTime<D>, Temporal, TemporalAdjuster, Serializable {
/**
* Serialization version.
*/
--- 96,106 ----
* This class is immutable and thread-safe.
*
* @param <D> the concrete type for the date of this date-time
* @since 1.8
*/
! final class ChronoLocalDateTimeImpl<D extends ChronoLocalDate>
implements ChronoLocalDateTime<D>, Temporal, TemporalAdjuster, Serializable {
/**
* Serialization version.
*/
*** 169,181 ****
*
* @param date the local date, not null
* @param time the local time, not null
* @return the local date-time, not null
*/
! @SuppressWarnings("rawtypes")
! static ChronoLocalDateTimeImpl<?> of(ChronoLocalDate<?> date, LocalTime time) {
! return new ChronoLocalDateTimeImpl(date, time);
}
/**
* Constructor.
*
--- 169,199 ----
*
* @param date the local date, not null
* @param time the local time, not null
* @return the local date-time, not null
*/
! static <R extends ChronoLocalDate> ChronoLocalDateTimeImpl<R> of(R date, LocalTime time) {
! return new ChronoLocalDateTimeImpl<>(date, time);
! }
!
! /**
! * Casts the {@code Temporal} to {@code ChronoLocalDateTime} 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 ChronoLocalDateTime}, not null
! * @throws ClassCastException if the date-time cannot be cast to ChronoLocalDateTimeImpl
! * or the chronology is not equal this Chronology
! */
! static <R extends ChronoLocalDate> ChronoLocalDateTimeImpl<R> ensureValid(Chronology chrono, Temporal temporal) {
! @SuppressWarnings("unchecked")
! ChronoLocalDateTimeImpl<R> other = (ChronoLocalDateTimeImpl<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.
*
*** 200,210 ****
private ChronoLocalDateTimeImpl<D> with(Temporal newDate, LocalTime newTime) {
if (date == newDate && time == newTime) {
return this;
}
// Validate that the new Temporal is a ChronoLocalDate (and not something else)
! D cd = (D) date.getChronology().ensureChronoLocalDate(newDate);
return new ChronoLocalDateTimeImpl<>(cd, newTime);
}
//-----------------------------------------------------------------------
@Override
--- 218,228 ----
private ChronoLocalDateTimeImpl<D> with(Temporal newDate, LocalTime newTime) {
if (date == newDate && time == newTime) {
return this;
}
// Validate that the new Temporal is a ChronoLocalDate (and not something else)
! D cd = ChronoDateImpl.ensureValid(date.getChronology(), newDate);
return new ChronoLocalDateTimeImpl<>(cd, newTime);
}
//-----------------------------------------------------------------------
@Override
*** 258,274 ****
@SuppressWarnings("unchecked")
@Override
public ChronoLocalDateTimeImpl<D> with(TemporalAdjuster adjuster) {
if (adjuster instanceof ChronoLocalDate) {
// The Chronology is checked in with(date,time)
! return with((ChronoLocalDate<D>) adjuster, time);
} else if (adjuster instanceof LocalTime) {
return with(date, (LocalTime) adjuster);
} else if (adjuster instanceof ChronoLocalDateTimeImpl) {
! return (ChronoLocalDateTimeImpl<D>)(date.getChronology().ensureChronoLocalDateTime((ChronoLocalDateTimeImpl<?>) adjuster));
}
! return (ChronoLocalDateTimeImpl<D>)(date.getChronology().ensureChronoLocalDateTime((ChronoLocalDateTimeImpl<?>) adjuster.adjustInto(this)));
}
@Override
public ChronoLocalDateTimeImpl<D> with(TemporalField field, long newValue) {
if (field instanceof ChronoField) {
--- 276,292 ----
@SuppressWarnings("unchecked")
@Override
public ChronoLocalDateTimeImpl<D> with(TemporalAdjuster adjuster) {
if (adjuster instanceof ChronoLocalDate) {
// The Chronology is checked in with(date,time)
! return with((ChronoLocalDate) adjuster, time);
} else if (adjuster instanceof LocalTime) {
return with(date, (LocalTime) adjuster);
} else if (adjuster instanceof ChronoLocalDateTimeImpl) {
! return ChronoLocalDateTimeImpl.ensureValid(date.getChronology(), (ChronoLocalDateTimeImpl<?>) adjuster);
}
! return ChronoLocalDateTimeImpl.ensureValid(date.getChronology(), (ChronoLocalDateTimeImpl<?>) adjuster.adjustInto(this));
}
@Override
public ChronoLocalDateTimeImpl<D> with(TemporalField field, long newValue) {
if (field instanceof ChronoField) {
*** 277,287 ****
return with(date, time.with(field, newValue));
} else {
return with(date.with(field, newValue), time);
}
}
! return (ChronoLocalDateTimeImpl<D>)(date.getChronology().ensureChronoLocalDateTime(field.adjustInto(this, newValue)));
}
//-----------------------------------------------------------------------
@Override
public ChronoLocalDateTimeImpl<D> plus(long amountToAdd, TemporalUnit unit) {
--- 295,305 ----
return with(date, time.with(field, newValue));
} else {
return with(date.with(field, newValue), time);
}
}
! return ChronoLocalDateTimeImpl.ensureValid(date.getChronology(), field.adjustInto(this, newValue));
}
//-----------------------------------------------------------------------
@Override
public ChronoLocalDateTimeImpl<D> plus(long amountToAdd, TemporalUnit unit) {
*** 296,306 ****
case HOURS: return plusHours(amountToAdd);
case HALF_DAYS: return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12); // no overflow (256 is multiple of 2)
}
return with(date.plus(amountToAdd, unit), time);
}
! return (ChronoLocalDateTimeImpl<D>)(date.getChronology().ensureChronoLocalDateTime(unit.addTo(this, amountToAdd)));
}
private ChronoLocalDateTimeImpl<D> plusDays(long days) {
return with(date.plus(days, ChronoUnit.DAYS), time);
}
--- 314,324 ----
case HOURS: return plusHours(amountToAdd);
case HALF_DAYS: return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12); // no overflow (256 is multiple of 2)
}
return with(date.plus(amountToAdd, unit), time);
}
! return ChronoLocalDateTimeImpl.ensureValid(date.getChronology(), unit.addTo(this, amountToAdd));
}
private ChronoLocalDateTimeImpl<D> plusDays(long days) {
return with(date.plus(days, ChronoUnit.DAYS), time);
}
*** 320,330 ****
private ChronoLocalDateTimeImpl<D> plusNanos(long nanos) {
return plusWithOverflow(date, 0, 0, 0, nanos);
}
//-----------------------------------------------------------------------
! private ChronoLocalDateTimeImpl<D> plusWithOverflow(ChronoLocalDate<?> newDate, long hours, long minutes, long seconds, long nanos) {
// 9223372036854775808 long, 2147483648 int
if ((hours | minutes | seconds | nanos) == 0) {
return with(newDate, time);
}
long totDays = nanos / NANOS_PER_DAY + // max/24*60*60*1B
--- 338,348 ----
private ChronoLocalDateTimeImpl<D> plusNanos(long nanos) {
return plusWithOverflow(date, 0, 0, 0, nanos);
}
//-----------------------------------------------------------------------
! private ChronoLocalDateTimeImpl<D> plusWithOverflow(D newDate, long hours, long minutes, long seconds, long nanos) {
// 9223372036854775808 long, 2147483648 int
if ((hours | minutes | seconds | nanos) == 0) {
return with(newDate, time);
}
long totDays = nanos / NANOS_PER_DAY + // max/24*60*60*1B
*** 349,387 ****
return ChronoZonedDateTimeImpl.ofBest(this, zone, null);
}
//-----------------------------------------------------------------------
@Override
! public long periodUntil(Temporal endDateTime, TemporalUnit unit) {
if (endDateTime instanceof ChronoLocalDateTime == false) {
throw new DateTimeException("Unable to calculate amount as objects are of two different types");
}
@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");
}
if (unit instanceof ChronoUnit) {
! ChronoUnit f = (ChronoUnit) unit;
! if (f.isTimeUnit()) {
long amount = end.getLong(EPOCH_DAY) - date.getLong(EPOCH_DAY);
! switch (f) {
case NANOS: amount = Math.multiplyExact(amount, NANOS_PER_DAY); break;
case MICROS: amount = Math.multiplyExact(amount, MICROS_PER_DAY); break;
case MILLIS: amount = Math.multiplyExact(amount, MILLIS_PER_DAY); break;
case SECONDS: amount = Math.multiplyExact(amount, SECONDS_PER_DAY); break;
case MINUTES: amount = Math.multiplyExact(amount, MINUTES_PER_DAY); break;
case HOURS: amount = Math.multiplyExact(amount, HOURS_PER_DAY); break;
case HALF_DAYS: amount = Math.multiplyExact(amount, 2); break;
}
! return Math.addExact(amount, time.periodUntil(end.toLocalTime(), unit));
}
! D endDate = end.toLocalDate();
if (end.toLocalTime().isBefore(time)) {
endDate = endDate.minus(1, ChronoUnit.DAYS);
}
! return date.periodUntil(endDate, unit);
}
return unit.between(this, endDateTime);
}
//-----------------------------------------------------------------------
--- 367,404 ----
return ChronoZonedDateTimeImpl.ofBest(this, zone, null);
}
//-----------------------------------------------------------------------
@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");
}
@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");
}
if (unit instanceof ChronoUnit) {
! if (unit.isTimeBased()) {
long amount = end.getLong(EPOCH_DAY) - date.getLong(EPOCH_DAY);
! switch ((ChronoUnit) unit) {
case NANOS: amount = Math.multiplyExact(amount, NANOS_PER_DAY); break;
case MICROS: amount = Math.multiplyExact(amount, MICROS_PER_DAY); break;
case MILLIS: amount = Math.multiplyExact(amount, MILLIS_PER_DAY); break;
case SECONDS: amount = Math.multiplyExact(amount, SECONDS_PER_DAY); break;
case MINUTES: amount = Math.multiplyExact(amount, MINUTES_PER_DAY); break;
case HOURS: amount = Math.multiplyExact(amount, HOURS_PER_DAY); break;
case HALF_DAYS: amount = Math.multiplyExact(amount, 2); break;
}
! return Math.addExact(amount, time.until(end.toLocalTime(), unit));
}
! ChronoLocalDate endDate = end.toLocalDate();
if (end.toLocalTime().isBefore(time)) {
endDate = endDate.minus(1, ChronoUnit.DAYS);
}
! return date.until(endDate, unit);
}
return unit.between(this, endDateTime);
}
//-----------------------------------------------------------------------
*** 402,412 ****
out.writeObject(date);
out.writeObject(time);
}
static ChronoLocalDateTime<?> readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
! ChronoLocalDate<?> date = (ChronoLocalDate<?>) in.readObject();
LocalTime time = (LocalTime) in.readObject();
return date.atTime(time);
}
//-----------------------------------------------------------------------
--- 419,429 ----
out.writeObject(date);
out.writeObject(time);
}
static ChronoLocalDateTime<?> readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
! ChronoLocalDate date = (ChronoLocalDate) in.readObject();
LocalTime time = (LocalTime) in.readObject();
return date.atTime(time);
}
//-----------------------------------------------------------------------