src/share/classes/java/time/chrono/ChronoLocalDateTimeImpl.java
Print this page
@@ -57,11 +57,11 @@
* 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;
+package java.time.chrono;
import static java.time.temporal.ChronoField.EPOCH_DAY;
import java.io.IOException;
import java.io.InvalidObjectException;
@@ -70,10 +70,17 @@
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,15 +93,15 @@
* "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
+ * @param <D> the concrete type for the date of this date-time
* @since 1.8
*/
-final class ChronoLocalDateTimeImpl<C extends Chrono<C>>
- implements ChronoLocalDateTime<C>, Temporal, TemporalAdjuster, Serializable {
+final class ChronoLocalDateTimeImpl<D extends ChronoLocalDate<D>>
+ implements ChronoLocalDateTime<D>, Temporal, TemporalAdjuster, Serializable {
/**
* Serialization version.
*/
private static final long serialVersionUID = 4556003607393004514L;
@@ -148,11 +155,11 @@
static final long NANOS_PER_DAY = NANOS_PER_HOUR * HOURS_PER_DAY;
/**
* The date part.
*/
- private final ChronoLocalDate<C> date;
+ private final D date;
/**
* The time part.
*/
private final LocalTime time;
@@ -162,21 +169,21 @@
*
* @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);
+ 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(ChronoLocalDate<C> date, LocalTime time) {
+ private ChronoLocalDateTimeImpl(D date, LocalTime time) {
Objects.requireNonNull(date, "date");
Objects.requireNonNull(time, "time");
this.date = date;
this.time = time;
}
@@ -187,47 +194,47 @@
*
* @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) {
+ 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)
- ChronoLocalDate<C> cd = date.getChrono().ensureChronoLocalDate(newDate);
- return new ChronoLocalDateTimeImpl<>(cd, newTime);
+ D cd = (D)date.getChronology().ensureChronoLocalDate(newDate);
+ return new ChronoLocalDateTimeImpl<>((D)cd, newTime);
}
//-----------------------------------------------------------------------
@Override
- public ChronoLocalDate<C> getDate() {
+ public D toLocalDate() {
return date;
}
@Override
- public LocalTime getTime() {
+ 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.doIsSupported(this);
+ 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.doRange(this);
+ return field.rangeRefinedBy(this);
}
@Override
public int get(TemporalField field) {
if (field instanceof ChronoField) {
@@ -241,44 +248,44 @@
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);
+ return field.getFrom(this);
}
//-----------------------------------------------------------------------
@SuppressWarnings("unchecked")
@Override
- public ChronoLocalDateTimeImpl<C> with(TemporalAdjuster adjuster) {
+ public ChronoLocalDateTimeImpl<D> with(TemporalAdjuster adjuster) {
if (adjuster instanceof ChronoLocalDate) {
- // The Chrono is checked in with(date,time)
- return with((ChronoLocalDate<C>) adjuster, time);
+ // 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 date.getChrono().ensureChronoLocalDateTime((ChronoLocalDateTimeImpl<?>) adjuster);
+ return (ChronoLocalDateTimeImpl<D>)(date.getChronology().ensureChronoLocalDateTime((ChronoLocalDateTimeImpl<?>) adjuster));
}
- return date.getChrono().ensureChronoLocalDateTime((ChronoLocalDateTimeImpl<?>) adjuster.adjustInto(this));
+ return (ChronoLocalDateTimeImpl<D>)(date.getChronology().ensureChronoLocalDateTime((ChronoLocalDateTimeImpl<?>) adjuster.adjustInto(this)));
}
@Override
- public ChronoLocalDateTimeImpl<C> with(TemporalField field, long newValue) {
+ 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 date.getChrono().ensureChronoLocalDateTime(field.doWith(this, newValue));
+ return (ChronoLocalDateTimeImpl<D>)(date.getChronology().ensureChronoLocalDateTime(field.adjustInto(this, newValue)));
}
//-----------------------------------------------------------------------
@Override
- public ChronoLocalDateTimeImpl<C> plus(long amountToAdd, TemporalUnit unit) {
+ 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,35 +295,35 @@
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));
+ return (ChronoLocalDateTimeImpl<D>)(date.getChronology().ensureChronoLocalDateTime(unit.addTo(this, amountToAdd)));
}
- private ChronoLocalDateTimeImpl<C> plusDays(long days) {
+ private ChronoLocalDateTimeImpl<D> plusDays(long days) {
return with(date.plus(days, ChronoUnit.DAYS), time);
}
- private ChronoLocalDateTimeImpl<C> plusHours(long hours) {
+ private ChronoLocalDateTimeImpl<D> plusHours(long hours) {
return plusWithOverflow(date, hours, 0, 0, 0);
}
- private ChronoLocalDateTimeImpl<C> plusMinutes(long minutes) {
+ private ChronoLocalDateTimeImpl<D> plusMinutes(long minutes) {
return plusWithOverflow(date, 0, minutes, 0, 0);
}
- ChronoLocalDateTimeImpl<C> plusSeconds(long seconds) {
+ ChronoLocalDateTimeImpl<D> plusSeconds(long seconds) {
return plusWithOverflow(date, 0, 0, seconds, 0);
}
- private ChronoLocalDateTimeImpl<C> plusNanos(long nanos) {
+ private ChronoLocalDateTimeImpl<D> 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) {
+ 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,23 +342,23 @@
return with(newDate.plus(totDays, ChronoUnit.DAYS), newTime);
}
//-----------------------------------------------------------------------
@Override
- public ChronoZonedDateTime<C> atZone(ZoneId zone) {
+ 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<C> end = (ChronoLocalDateTime<C>) endDateTime;
- if (getDate().getChrono().equals(end.getDate().getChrono()) == false) {
+ 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,19 +370,19 @@
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));
+ return Math.addExact(amount, time.periodUntil(end.toLocalTime(), unit));
}
- ChronoLocalDate<C> endDate = end.getDate();
- if (end.getTime().isBefore(time)) {
- endDate = endDate.minus(1, ChronoUnit.DAYS);
+ 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).getAmount();
+ return unit.between(this, endDateTime);
}
//-----------------------------------------------------------------------
private Object writeReplace() {
return new Ser(Ser.CHRONO_LOCAL_DATE_TIME_TYPE, this);
@@ -394,11 +401,11 @@
out.writeObject(date);
out.writeObject(time);
}
static ChronoLocalDateTime<?> readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
- ChronoLocalDate<?> date = (ChronoLocalDate<?>) in.readObject();
+ ChronoLocalDate date = (ChronoLocalDate) in.readObject();
LocalTime time = (LocalTime) in.readObject();
return date.atTime(time);
}
//-----------------------------------------------------------------------
@@ -413,14 +420,14 @@
return false;
}
@Override
public int hashCode() {
- return getDate().hashCode() ^ getTime().hashCode();
+ return toLocalDate().hashCode() ^ toLocalTime().hashCode();
}
@Override
public String toString() {
- return getDate().toString() + 'T' + getTime().toString();
+ return toLocalDate().toString() + 'T' + toLocalTime().toString();
}
}