mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8263668: Update java.time to use instanceof pattern variable
Reviewed-by: lancea, ryadav, naoto, rriggs, dfuchs, scolebourne, chegar
This commit is contained in:
parent
a93d911954
commit
28af31db34
34 changed files with 129 additions and 176 deletions
|
@ -377,8 +377,8 @@ abstract class ChronoLocalDateImpl<D extends ChronoLocalDate>
|
|||
public long until(Temporal endExclusive, TemporalUnit unit) {
|
||||
Objects.requireNonNull(endExclusive, "endExclusive");
|
||||
ChronoLocalDate end = getChronology().date(endExclusive);
|
||||
if (unit instanceof ChronoUnit) {
|
||||
switch ((ChronoUnit) unit) {
|
||||
if (unit instanceof ChronoUnit chronoUnit) {
|
||||
switch (chronoUnit) {
|
||||
case DAYS: return daysUntil(end);
|
||||
case WEEKS: return daysUntil(end) / 7;
|
||||
case MONTHS: return monthsUntil(end);
|
||||
|
|
|
@ -373,10 +373,10 @@ final class ChronoLocalDateTimeImpl<D extends ChronoLocalDate>
|
|||
Objects.requireNonNull(endExclusive, "endExclusive");
|
||||
@SuppressWarnings("unchecked")
|
||||
ChronoLocalDateTime<D> end = (ChronoLocalDateTime<D>) getChronology().localDateTime(endExclusive);
|
||||
if (unit instanceof ChronoUnit) {
|
||||
if (unit instanceof ChronoUnit chronoUnit) {
|
||||
if (unit.isTimeBased()) {
|
||||
long amount = end.getLong(EPOCH_DAY) - date.getLong(EPOCH_DAY);
|
||||
switch ((ChronoUnit) unit) {
|
||||
switch (chronoUnit) {
|
||||
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;
|
||||
|
|
|
@ -199,11 +199,11 @@ final class ChronoPeriodImpl
|
|||
*/
|
||||
private ChronoPeriodImpl validateAmount(TemporalAmount amount) {
|
||||
Objects.requireNonNull(amount, "amount");
|
||||
if (amount instanceof ChronoPeriodImpl == false) {
|
||||
if (!(amount instanceof ChronoPeriodImpl)) {
|
||||
throw new DateTimeException("Unable to obtain ChronoPeriod from TemporalAmount: " + amount.getClass());
|
||||
}
|
||||
ChronoPeriodImpl period = (ChronoPeriodImpl) amount;
|
||||
if (chrono.equals(period.getChronology()) == false) {
|
||||
if (!(chrono.equals(period.getChronology()))) {
|
||||
throw new ClassCastException("Chronology mismatch, expected: " + chrono.getId() + ", actual: " + period.getChronology().getId());
|
||||
}
|
||||
return period;
|
||||
|
@ -320,12 +320,9 @@ final class ChronoPeriodImpl
|
|||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof ChronoPeriodImpl) {
|
||||
ChronoPeriodImpl other = (ChronoPeriodImpl) obj;
|
||||
return years == other.years && months == other.months &&
|
||||
days == other.days && chrono.equals(other.chrono);
|
||||
}
|
||||
return false;
|
||||
return (obj instanceof ChronoPeriodImpl other)
|
||||
&& years == other.years && months == other.months
|
||||
&& days == other.days && chrono.equals(other.chrono);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -195,8 +195,8 @@ public interface ChronoZonedDateTime<D extends ChronoLocalDate>
|
|||
|
||||
@Override
|
||||
default int get(TemporalField field) {
|
||||
if (field instanceof ChronoField) {
|
||||
switch ((ChronoField) field) {
|
||||
if (field instanceof ChronoField chronoField) {
|
||||
switch (chronoField) {
|
||||
case INSTANT_SECONDS:
|
||||
throw new UnsupportedTemporalTypeException("Invalid field 'InstantSeconds' for get() method, use getLong() instead");
|
||||
case OFFSET_SECONDS:
|
||||
|
@ -209,8 +209,8 @@ public interface ChronoZonedDateTime<D extends ChronoLocalDate>
|
|||
|
||||
@Override
|
||||
default long getLong(TemporalField field) {
|
||||
if (field instanceof ChronoField) {
|
||||
switch ((ChronoField) field) {
|
||||
if (field instanceof ChronoField chronoField) {
|
||||
switch (chronoField) {
|
||||
case INSTANT_SECONDS: return toEpochSecond();
|
||||
case OFFSET_SECONDS: return getOffset().getTotalSeconds();
|
||||
}
|
||||
|
|
|
@ -498,7 +498,7 @@ public final class HijrahChronology extends AbstractChronology implements Serial
|
|||
|
||||
@Override
|
||||
public int prolepticYear(Era era, int yearOfEra) {
|
||||
if (era instanceof HijrahEra == false) {
|
||||
if (!(era instanceof HijrahEra)) {
|
||||
throw new ClassCastException("Era must be HijrahEra");
|
||||
}
|
||||
return yearOfEra;
|
||||
|
|
|
@ -628,14 +628,11 @@ public final class HijrahDate
|
|||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof HijrahDate) {
|
||||
HijrahDate otherDate = (HijrahDate) obj;
|
||||
return prolepticYear == otherDate.prolepticYear
|
||||
return (obj instanceof HijrahDate otherDate)
|
||||
&& prolepticYear == otherDate.prolepticYear
|
||||
&& this.monthOfYear == otherDate.monthOfYear
|
||||
&& this.dayOfMonth == otherDate.dayOfMonth
|
||||
&& getChronology().equals(otherDate.getChronology());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -479,7 +479,7 @@ public final class IsoChronology extends AbstractChronology implements Serializa
|
|||
|
||||
@Override
|
||||
public int prolepticYear(Era era, int yearOfEra) {
|
||||
if (era instanceof IsoEra == false) {
|
||||
if (!(era instanceof IsoEra)) {
|
||||
throw new ClassCastException("Era must be IsoEra");
|
||||
}
|
||||
return (era == IsoEra.CE ? yearOfEra : 1 - yearOfEra);
|
||||
|
|
|
@ -202,10 +202,10 @@ public final class JapaneseChronology extends AbstractChronology implements Seri
|
|||
*/
|
||||
@Override
|
||||
public JapaneseDate date(Era era, int yearOfEra, int month, int dayOfMonth) {
|
||||
if (era instanceof JapaneseEra == false) {
|
||||
if (!(era instanceof JapaneseEra jera)) {
|
||||
throw new ClassCastException("Era must be JapaneseEra");
|
||||
}
|
||||
return JapaneseDate.of((JapaneseEra) era, yearOfEra, month, dayOfMonth);
|
||||
return JapaneseDate.of(jera, yearOfEra, month, dayOfMonth);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -697,11 +697,8 @@ public final class JapaneseDate
|
|||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof JapaneseDate) {
|
||||
JapaneseDate otherDate = (JapaneseDate) obj;
|
||||
return this.isoDate.equals(otherDate.isoDate);
|
||||
}
|
||||
return false;
|
||||
return (obj instanceof JapaneseDate otherDate)
|
||||
&& this.isoDate.equals(otherDate.isoDate);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -293,7 +293,7 @@ public final class MinguoChronology extends AbstractChronology implements Serial
|
|||
|
||||
@Override
|
||||
public int prolepticYear(Era era, int yearOfEra) {
|
||||
if (era instanceof MinguoEra == false) {
|
||||
if (!(era instanceof MinguoEra)) {
|
||||
throw new ClassCastException("Era must be MinguoEra");
|
||||
}
|
||||
return (era == MinguoEra.ROC ? yearOfEra : 1 - yearOfEra);
|
||||
|
|
|
@ -459,11 +459,8 @@ public final class MinguoDate
|
|||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof MinguoDate) {
|
||||
MinguoDate otherDate = (MinguoDate) obj;
|
||||
return this.isoDate.equals(otherDate.isoDate);
|
||||
}
|
||||
return false;
|
||||
return (obj instanceof MinguoDate otherDate)
|
||||
&& this.isoDate.equals(otherDate.isoDate);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -329,7 +329,7 @@ public final class ThaiBuddhistChronology extends AbstractChronology implements
|
|||
|
||||
@Override
|
||||
public int prolepticYear(Era era, int yearOfEra) {
|
||||
if (era instanceof ThaiBuddhistEra == false) {
|
||||
if (!(era instanceof ThaiBuddhistEra)) {
|
||||
throw new ClassCastException("Era must be BuddhistEra");
|
||||
}
|
||||
return (era == ThaiBuddhistEra.BE ? yearOfEra : 1 - yearOfEra);
|
||||
|
|
|
@ -459,11 +459,8 @@ public final class ThaiBuddhistDate
|
|||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof ThaiBuddhistDate) {
|
||||
ThaiBuddhistDate otherDate = (ThaiBuddhistDate) obj;
|
||||
return this.isoDate.equals(otherDate.isoDate);
|
||||
}
|
||||
return false;
|
||||
return (obj instanceof ThaiBuddhistDate otherDate)
|
||||
&& this.isoDate.equals(otherDate.isoDate);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue