src/share/classes/java/time/chrono/ChronoLocalDateTimeImpl.java
Print this page
*** 57,67 ****
* 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 java.time.temporal;
import static java.time.temporal.ChronoField.EPOCH_DAY;
import java.io.IOException;
import java.io.InvalidObjectException;
--- 57,67 ----
* 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 java.time.chrono;
import static java.time.temporal.ChronoField.EPOCH_DAY;
import java.io.IOException;
import java.io.InvalidObjectException;
*** 70,79 ****
--- 70,86 ----
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;
+ import java.time.temporal.ChronoUnit;
+ import java.time.temporal.Temporal;
+ import java.time.temporal.TemporalAdjuster;
+ import java.time.temporal.TemporalField;
+ import java.time.temporal.TemporalUnit;
+ import java.time.temporal.ValueRange;
import java.util.Objects;
/**
* A date-time without a time-zone for the calendar neutral API.
* <p>
*** 86,100 ****
* "2nd October 2007 at 13:45.30.123456789" can be stored in an {@code ChronoLocalDateTime}.
*
* <h3>Specification for implementors</h3>
* This class is immutable and thread-safe.
*
! * @param <C> the chronology of this date
* @since 1.8
*/
! final class ChronoLocalDateTimeImpl<C extends Chrono<C>>
! implements ChronoLocalDateTime<C>, Temporal, TemporalAdjuster, Serializable {
/**
* Serialization version.
*/
private static final long serialVersionUID = 4556003607393004514L;
--- 93,107 ----
* "2nd October 2007 at 13:45.30.123456789" can be stored in an {@code ChronoLocalDateTime}.
*
* <h3>Specification for implementors</h3>
* 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.
*/
private static final long serialVersionUID = 4556003607393004514L;
*** 148,158 ****
static final long NANOS_PER_DAY = NANOS_PER_HOUR * HOURS_PER_DAY;
/**
* The date part.
*/
! private final ChronoLocalDate<C> date;
/**
* The time part.
*/
private final LocalTime time;
--- 155,165 ----
static final long NANOS_PER_DAY = NANOS_PER_HOUR * HOURS_PER_DAY;
/**
* The date part.
*/
! private final D date;
/**
* The time part.
*/
private final LocalTime time;
*** 162,182 ****
*
* @param date the local date, not null
* @param time the local time, not null
* @return the local date-time, not null
*/
! static <R extends Chrono<R>> ChronoLocalDateTimeImpl<R> of(ChronoLocalDate<R> date, LocalTime time) {
! return new ChronoLocalDateTimeImpl<>(date, time);
}
/**
* Constructor.
*
* @param date the date part of the date-time, not null
* @param time the time part of the date-time, not null
*/
! private ChronoLocalDateTimeImpl(ChronoLocalDate<C> date, LocalTime time) {
Objects.requireNonNull(date, "date");
Objects.requireNonNull(time, "time");
this.date = date;
this.time = time;
}
--- 169,189 ----
*
* @param date the local date, not null
* @param time the local time, not null
* @return the local date-time, not null
*/
! static ChronoLocalDateTimeImpl<?> of(ChronoLocalDate<?> date, LocalTime time) {
! return new ChronoLocalDateTimeImpl(date, time);
}
/**
* Constructor.
*
* @param date the date part of the date-time, not null
* @param time the time part of the date-time, not null
*/
! private ChronoLocalDateTimeImpl(D date, LocalTime time) {
Objects.requireNonNull(date, "date");
Objects.requireNonNull(time, "time");
this.date = date;
this.time = time;
}
*** 187,233 ****
*
* @param newDate the date of the new date-time, not null
* @param newTime the time of the new date-time, not null
* @return the date-time, not null
*/
! private ChronoLocalDateTimeImpl<C> with(Temporal newDate, LocalTime newTime) {
if (date == newDate && time == newTime) {
return this;
}
// Validate that the new Temporal is a ChronoLocalDate (and not something else)
! ChronoLocalDate<C> cd = date.getChrono().ensureChronoLocalDate(newDate);
! return new ChronoLocalDateTimeImpl<>(cd, newTime);
}
//-----------------------------------------------------------------------
@Override
! public ChronoLocalDate<C> getDate() {
return date;
}
@Override
! public LocalTime getTime() {
return time;
}
//-----------------------------------------------------------------------
@Override
public boolean isSupported(TemporalField field) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
return f.isDateField() || f.isTimeField();
}
! return field != null && field.doIsSupported(this);
}
@Override
public ValueRange range(TemporalField field) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
return (f.isTimeField() ? time.range(field) : date.range(field));
}
! return field.doRange(this);
}
@Override
public int get(TemporalField field) {
if (field instanceof ChronoField) {
--- 194,240 ----
*
* @param newDate the date of the new date-time, not null
* @param newTime the time of the new date-time, not null
* @return the date-time, not null
*/
! 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<>((D)cd, newTime);
}
//-----------------------------------------------------------------------
@Override
! public D toLocalDate() {
return date;
}
@Override
! public LocalTime toLocalTime() {
return time;
}
//-----------------------------------------------------------------------
@Override
public boolean isSupported(TemporalField field) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
return f.isDateField() || f.isTimeField();
}
! return field != null && field.isSupportedBy(this);
}
@Override
public ValueRange range(TemporalField field) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
return (f.isTimeField() ? time.range(field) : date.range(field));
}
! return field.rangeRefinedBy(this);
}
@Override
public int get(TemporalField field) {
if (field instanceof ChronoField) {
*** 241,284 ****
public long getLong(TemporalField field) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
return (f.isTimeField() ? time.getLong(field) : date.getLong(field));
}
! return field.doGet(this);
}
//-----------------------------------------------------------------------
@SuppressWarnings("unchecked")
@Override
! public ChronoLocalDateTimeImpl<C> with(TemporalAdjuster adjuster) {
if (adjuster instanceof ChronoLocalDate) {
! // The Chrono is checked in with(date,time)
! return with((ChronoLocalDate<C>) adjuster, time);
} else if (adjuster instanceof LocalTime) {
return with(date, (LocalTime) adjuster);
} else if (adjuster instanceof ChronoLocalDateTimeImpl) {
! return date.getChrono().ensureChronoLocalDateTime((ChronoLocalDateTimeImpl<?>) adjuster);
}
! return date.getChrono().ensureChronoLocalDateTime((ChronoLocalDateTimeImpl<?>) adjuster.adjustInto(this));
}
@Override
! public ChronoLocalDateTimeImpl<C> with(TemporalField field, long newValue) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
if (f.isTimeField()) {
return with(date, time.with(field, newValue));
} else {
return with(date.with(field, newValue), time);
}
}
! return date.getChrono().ensureChronoLocalDateTime(field.doWith(this, newValue));
}
//-----------------------------------------------------------------------
@Override
! public ChronoLocalDateTimeImpl<C> plus(long amountToAdd, TemporalUnit unit) {
if (unit instanceof ChronoUnit) {
ChronoUnit f = (ChronoUnit) unit;
switch (f) {
case NANOS: return plusNanos(amountToAdd);
case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
--- 248,291 ----
public long getLong(TemporalField field) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
return (f.isTimeField() ? time.getLong(field) : date.getLong(field));
}
! return field.getFrom(this);
}
//-----------------------------------------------------------------------
@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) {
ChronoField f = (ChronoField) field;
if (f.isTimeField()) {
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) {
if (unit instanceof ChronoUnit) {
ChronoUnit f = (ChronoUnit) unit;
switch (f) {
case NANOS: return plusNanos(amountToAdd);
case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
*** 288,322 ****
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 date.getChrono().ensureChronoLocalDateTime(unit.doPlus(this, amountToAdd));
}
! private ChronoLocalDateTimeImpl<C> plusDays(long days) {
return with(date.plus(days, ChronoUnit.DAYS), time);
}
! private ChronoLocalDateTimeImpl<C> plusHours(long hours) {
return plusWithOverflow(date, hours, 0, 0, 0);
}
! private ChronoLocalDateTimeImpl<C> plusMinutes(long minutes) {
return plusWithOverflow(date, 0, minutes, 0, 0);
}
! ChronoLocalDateTimeImpl<C> plusSeconds(long seconds) {
return plusWithOverflow(date, 0, 0, seconds, 0);
}
! private ChronoLocalDateTimeImpl<C> plusNanos(long nanos) {
return plusWithOverflow(date, 0, 0, 0, nanos);
}
//-----------------------------------------------------------------------
! private ChronoLocalDateTimeImpl<C> plusWithOverflow(ChronoLocalDate<C> 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
--- 295,329 ----
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);
}
! private ChronoLocalDateTimeImpl<D> plusHours(long hours) {
return plusWithOverflow(date, hours, 0, 0, 0);
}
! private ChronoLocalDateTimeImpl<D> plusMinutes(long minutes) {
return plusWithOverflow(date, 0, minutes, 0, 0);
}
! ChronoLocalDateTimeImpl<D> plusSeconds(long seconds) {
return plusWithOverflow(date, 0, 0, seconds, 0);
}
! 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
*** 335,357 ****
return with(newDate.plus(totDays, ChronoUnit.DAYS), newTime);
}
//-----------------------------------------------------------------------
@Override
! public ChronoZonedDateTime<C> atZone(ZoneId zone) {
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 period between objects of two different types");
}
@SuppressWarnings("unchecked")
! ChronoLocalDateTime<C> end = (ChronoLocalDateTime<C>) endDateTime;
! if (getDate().getChrono().equals(end.getDate().getChrono()) == false) {
throw new DateTimeException("Unable to calculate period between two different chronologies");
}
if (unit instanceof ChronoUnit) {
ChronoUnit f = (ChronoUnit) unit;
if (f.isTimeUnit()) {
--- 342,364 ----
return with(newDate.plus(totDays, ChronoUnit.DAYS), newTime);
}
//-----------------------------------------------------------------------
@Override
! public ChronoZonedDateTime<D> atZone(ZoneId zone) {
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 period between objects 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 period between two different chronologies");
}
if (unit instanceof ChronoUnit) {
ChronoUnit f = (ChronoUnit) unit;
if (f.isTimeUnit()) {
*** 363,381 ****
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.getTime(), unit));
}
! ChronoLocalDate<C> endDate = end.getDate();
! if (end.getTime().isBefore(time)) {
! endDate = endDate.minus(1, ChronoUnit.DAYS);
}
return date.periodUntil(endDate, unit);
}
! return unit.between(this, endDateTime).getAmount();
}
//-----------------------------------------------------------------------
private Object writeReplace() {
return new Ser(Ser.CHRONO_LOCAL_DATE_TIME_TYPE, this);
--- 370,388 ----
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 = (D)endDate.minus(1, ChronoUnit.DAYS);
}
return date.periodUntil(endDate, unit);
}
! return unit.between(this, endDateTime);
}
//-----------------------------------------------------------------------
private Object writeReplace() {
return new Ser(Ser.CHRONO_LOCAL_DATE_TIME_TYPE, this);
*** 394,404 ****
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);
}
//-----------------------------------------------------------------------
--- 401,411 ----
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);
}
//-----------------------------------------------------------------------
*** 413,426 ****
return false;
}
@Override
public int hashCode() {
! return getDate().hashCode() ^ getTime().hashCode();
}
@Override
public String toString() {
! return getDate().toString() + 'T' + getTime().toString();
}
}
--- 420,433 ----
return false;
}
@Override
public int hashCode() {
! return toLocalDate().hashCode() ^ toLocalTime().hashCode();
}
@Override
public String toString() {
! return toLocalDate().toString() + 'T' + toLocalTime().toString();
}
}