Skip to content
Permalink
Browse files
8263668: Update java.time to use instanceof pattern variable
Reviewed-by: lancea, ryadav, naoto, rriggs, dfuchs, scolebourne, chegar
  • Loading branch information
pconcannon committed Apr 22, 2021
1 parent a93d911 commit 28af31db3494711ac1b12f930b6bb4ad646cefc4
Showing with 129 additions and 176 deletions.
  1. +3 −5 src/java.base/share/classes/java/time/Clock.java
  2. +8 −11 src/java.base/share/classes/java/time/Duration.java
  3. +6 −9 src/java.base/share/classes/java/time/Instant.java
  4. +1 −1 src/java.base/share/classes/java/time/LocalDate.java
  5. +5 −7 src/java.base/share/classes/java/time/LocalDateTime.java
  6. +9 −10 src/java.base/share/classes/java/time/LocalTime.java
  7. +5 −7 src/java.base/share/classes/java/time/MonthDay.java
  8. +7 −9 src/java.base/share/classes/java/time/OffsetDateTime.java
  9. +5 −7 src/java.base/share/classes/java/time/OffsetTime.java
  10. +4 −7 src/java.base/share/classes/java/time/Period.java
  11. +6 −6 src/java.base/share/classes/java/time/Year.java
  12. +9 −11 src/java.base/share/classes/java/time/YearMonth.java
  13. +2 −5 src/java.base/share/classes/java/time/ZoneId.java
  14. +4 −4 src/java.base/share/classes/java/time/ZonedDateTime.java
  15. +2 −2 src/java.base/share/classes/java/time/chrono/ChronoLocalDateImpl.java
  16. +2 −2 src/java.base/share/classes/java/time/chrono/ChronoLocalDateTimeImpl.java
  17. +5 −8 src/java.base/share/classes/java/time/chrono/ChronoPeriodImpl.java
  18. +4 −4 src/java.base/share/classes/java/time/chrono/ChronoZonedDateTime.java
  19. +1 −1 src/java.base/share/classes/java/time/chrono/HijrahChronology.java
  20. +2 −5 src/java.base/share/classes/java/time/chrono/HijrahDate.java
  21. +1 −1 src/java.base/share/classes/java/time/chrono/IsoChronology.java
  22. +2 −2 src/java.base/share/classes/java/time/chrono/JapaneseChronology.java
  23. +2 −5 src/java.base/share/classes/java/time/chrono/JapaneseDate.java
  24. +1 −1 src/java.base/share/classes/java/time/chrono/MinguoChronology.java
  25. +2 −5 src/java.base/share/classes/java/time/chrono/MinguoDate.java
  26. +1 −1 src/java.base/share/classes/java/time/chrono/ThaiBuddhistChronology.java
  27. +2 −5 src/java.base/share/classes/java/time/chrono/ThaiBuddhistDate.java
  28. +1 −1 src/java.base/share/classes/java/time/format/DateTimeFormatter.java
  29. +1 −1 src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java
  30. +1 −1 src/java.base/share/classes/java/time/format/Parsed.java
  31. +5 −6 src/java.base/share/classes/java/time/temporal/ValueRange.java
  32. +4 −6 src/java.base/share/classes/java/time/zone/ZoneOffsetTransition.java
  33. +10 −11 src/java.base/share/classes/java/time/zone/ZoneOffsetTransitionRule.java
  34. +6 −9 src/java.base/share/classes/java/time/zone/ZoneRules.java
@@ -730,11 +730,9 @@ public Instant instant() {
}
@Override
public boolean equals(Object obj) {
if (obj instanceof TickClock) {
TickClock other = (TickClock) obj;
return baseClock.equals(other.baseClock) && tickNanos == other.tickNanos;
}
return false;
return (obj instanceof TickClock other)
&& tickNanos == other.tickNanos
&& baseClock.equals(other.baseClock);
}
@Override
public int hashCode() {
@@ -719,8 +719,8 @@ public Duration plus(long amountToAdd, TemporalUnit unit) {
if (amountToAdd == 0) {
return this;
}
if (unit instanceof ChronoUnit) {
switch ((ChronoUnit) unit) {
if (unit instanceof ChronoUnit chronoUnit) {
switch (chronoUnit) {
case NANOS: return plusNanos(amountToAdd);
case MICROS: return plusSeconds((amountToAdd / (1000_000L * 1000)) * 1000).plusNanos((amountToAdd % (1000_000L * 1000)) * 1000);
case MILLIS: return plusMillis(amountToAdd);
@@ -1421,20 +1421,17 @@ public int compareTo(Duration otherDuration) {
* <p>
* The comparison is based on the total length of the durations.
*
* @param otherDuration the other duration, null returns false
* @param other the other duration, null returns false
* @return true if the other duration is equal to this one
*/
@Override
public boolean equals(Object otherDuration) {
if (this == otherDuration) {
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (otherDuration instanceof Duration) {
Duration other = (Duration) otherDuration;
return this.seconds == other.seconds &&
this.nanos == other.nanos;
}
return false;
return (other instanceof Duration otherDuration)
&& this.seconds == otherDuration.seconds
&& this.nanos == otherDuration.nanos;
}

/**
@@ -1292,20 +1292,17 @@ public boolean isBefore(Instant otherInstant) {
* <p>
* The comparison is based on the time-line position of the instants.
*
* @param otherInstant the other instant, null returns false
* @param other the other instant, null returns false
* @return true if the other instant is equal to this one
*/
@Override
public boolean equals(Object otherInstant) {
if (this == otherInstant) {
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (otherInstant instanceof Instant) {
Instant other = (Instant) otherInstant;
return this.seconds == other.seconds &&
this.nanos == other.nanos;
}
return false;
return (other instanceof Instant otherInstant)
&& this.seconds == otherInstant.seconds
&& this.nanos == otherInstant.nanos;
}

/**
@@ -1940,7 +1940,7 @@ public ZonedDateTime atStartOfDay(ZoneId zone) {
// need to handle case where there is a gap from 11:30 to 00:30
// standard ZDT factory would result in 01:00 rather than 00:30
LocalDateTime ldt = atTime(LocalTime.MIDNIGHT);
if (zone instanceof ZoneOffset == false) {
if (!(zone instanceof ZoneOffset)) {
ZoneRules rules = zone.getRules();
ZoneOffsetTransition trans = rules.getTransition(ldt);
if (trans != null && trans.isGap()) {
@@ -1683,7 +1683,7 @@ public Temporal adjustInto(Temporal temporal) {
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
LocalDateTime end = LocalDateTime.from(endExclusive);
if (unit instanceof ChronoUnit) {
if (unit instanceof ChronoUnit chronoUnit) {
if (unit.isTimeBased()) {
long amount = date.daysUntil(end.date);
if (amount == 0) {
@@ -1697,7 +1697,7 @@ public long until(Temporal endExclusive, TemporalUnit unit) {
amount++; // safe
timePart -= NANOS_PER_DAY; // safe
}
switch ((ChronoUnit) unit) {
switch (chronoUnit) {
case NANOS:
amount = Math.multiplyExact(amount, NANOS_PER_DAY);
break;
@@ -1935,11 +1935,9 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof LocalDateTime) {
LocalDateTime other = (LocalDateTime) obj;
return date.equals(other.date) && time.equals(other.time);
}
return false;
return (obj instanceof LocalDateTime other)
&& date.equals(other.date)
&& time.equals(other.time);
}

/**
@@ -1066,8 +1066,8 @@ public LocalTime plus(TemporalAmount amountToAdd) {
*/
@Override
public LocalTime plus(long amountToAdd, TemporalUnit unit) {
if (unit instanceof ChronoUnit) {
switch ((ChronoUnit) unit) {
if (unit instanceof ChronoUnit chronoUnit) {
switch (chronoUnit) {
case NANOS: return plusNanos(amountToAdd);
case MICROS: return plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
case MILLIS: return plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000);
@@ -1407,9 +1407,9 @@ public Temporal adjustInto(Temporal temporal) {
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
LocalTime end = LocalTime.from(endExclusive);
if (unit instanceof ChronoUnit) {
if (unit instanceof ChronoUnit chronoUnit) {
long nanosUntil = end.toNanoOfDay() - toNanoOfDay(); // no overflow
switch ((ChronoUnit) unit) {
switch (chronoUnit) {
case NANOS: return nanosUntil;
case MICROS: return nanosUntil / 1000;
case MILLIS: return nanosUntil / 1000_000;
@@ -1583,12 +1583,11 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof LocalTime) {
LocalTime other = (LocalTime) obj;
return hour == other.hour && minute == other.minute &&
second == other.second && nano == other.nano;
}
return false;
return (obj instanceof LocalTime other)
&& hour == other.hour
&& minute == other.minute
&& second == other.second
&& nano == other.nano;
}

/**
@@ -444,8 +444,8 @@ public int get(TemporalField field) {
*/
@Override
public long getLong(TemporalField field) {
if (field instanceof ChronoField) {
switch ((ChronoField) field) {
if (field instanceof ChronoField chronoField) {
switch (chronoField) {
// alignedDOW and alignedWOM not supported because they cannot be set in with()
case DAY_OF_MONTH: return day;
case MONTH_OF_YEAR: return month;
@@ -720,11 +720,9 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof MonthDay) {
MonthDay other = (MonthDay) obj;
return month == other.month && day == other.day;
}
return false;
return (obj instanceof MonthDay other)
&& month == other.month
&& day == other.day;
}

/**
@@ -596,8 +596,8 @@ public ValueRange range(TemporalField field) {
*/
@Override
public 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:
@@ -633,8 +633,8 @@ public int get(TemporalField field) {
*/
@Override
public 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();
}
@@ -1881,11 +1881,9 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof OffsetDateTime) {
OffsetDateTime other = (OffsetDateTime) obj;
return dateTime.equals(other.dateTime) && offset.equals(other.offset);
}
return false;
return (obj instanceof OffsetDateTime other)
&& dateTime.equals(other.dateTime)
&& offset.equals(other.offset);
}

/**
@@ -1178,9 +1178,9 @@ public Temporal adjustInto(Temporal temporal) {
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
OffsetTime end = OffsetTime.from(endExclusive);
if (unit instanceof ChronoUnit) {
if (unit instanceof ChronoUnit chronoUnit) {
long nanosUntil = end.toEpochNano() - toEpochNano(); // no overflow
switch ((ChronoUnit) unit) {
switch (chronoUnit) {
case NANOS: return nanosUntil;
case MICROS: return nanosUntil / 1000;
case MILLIS: return nanosUntil / 1000_000;
@@ -1360,11 +1360,9 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof OffsetTime) {
OffsetTime other = (OffsetTime) obj;
return time.equals(other.time) && offset.equals(other.offset);
}
return false;
return (obj instanceof OffsetTime other)
&& time.equals(other.time)
&& offset.equals(other.offset);
}

/**
@@ -992,13 +992,10 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Period) {
Period other = (Period) obj;
return years == other.years &&
months == other.months &&
days == other.days;
}
return false;
return (obj instanceof Period other)
&& years == other.years
&& months == other.months
&& days == other.days;
}

/**
@@ -496,8 +496,8 @@ public int get(TemporalField field) {
*/
@Override
public long getLong(TemporalField field) {
if (field instanceof ChronoField) {
switch ((ChronoField) field) {
if (field instanceof ChronoField chronoField) {
switch (chronoField) {
case YEAR_OF_ERA: return (year < 1 ? 1 - year : year);
case YEAR: return year;
case ERA: return (year < 1 ? 0 : 1);
@@ -708,8 +708,8 @@ public Year plus(TemporalAmount amountToAdd) {
*/
@Override
public Year plus(long amountToAdd, TemporalUnit unit) {
if (unit instanceof ChronoUnit) {
switch ((ChronoUnit) unit) {
if (unit instanceof ChronoUnit chronoUnit) {
switch (chronoUnit) {
case YEARS: return plusYears(amountToAdd);
case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10));
case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100));
@@ -914,9 +914,9 @@ public Temporal adjustInto(Temporal temporal) {
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
Year end = Year.from(endExclusive);
if (unit instanceof ChronoUnit) {
if (unit instanceof ChronoUnit chronoUnit) {
long yearsUntil = ((long) end.year) - year; // no overflow
switch ((ChronoUnit) unit) {
switch (chronoUnit) {
case YEARS: return yearsUntil;
case DECADES: return yearsUntil / 10;
case CENTURIES: return yearsUntil / 100;
@@ -485,8 +485,8 @@ public int get(TemporalField field) {
*/
@Override
public long getLong(TemporalField field) {
if (field instanceof ChronoField) {
switch ((ChronoField) field) {
if (field instanceof ChronoField chronoField) {
switch (chronoField) {
case MONTH_OF_YEAR: return month;
case PROLEPTIC_MONTH: return getProlepticMonth();
case YEAR_OF_ERA: return (year < 1 ? 1 - year : year);
@@ -805,8 +805,8 @@ public YearMonth plus(TemporalAmount amountToAdd) {
*/
@Override
public YearMonth plus(long amountToAdd, TemporalUnit unit) {
if (unit instanceof ChronoUnit) {
switch ((ChronoUnit) unit) {
if (unit instanceof ChronoUnit chronoUnit) {
switch (chronoUnit) {
case MONTHS: return plusMonths(amountToAdd);
case YEARS: return plusYears(amountToAdd);
case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10));
@@ -1046,9 +1046,9 @@ public Temporal adjustInto(Temporal temporal) {
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
YearMonth end = YearMonth.from(endExclusive);
if (unit instanceof ChronoUnit) {
if (unit instanceof ChronoUnit chronoUnit) {
long monthsUntil = end.getProlepticMonth() - getProlepticMonth(); // no overflow
switch ((ChronoUnit) unit) {
switch (chronoUnit) {
case MONTHS: return monthsUntil;
case YEARS: return monthsUntil / 12;
case DECADES: return monthsUntil / 120;
@@ -1168,11 +1168,9 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof YearMonth) {
YearMonth other = (YearMonth) obj;
return year == other.year && month == other.month;
}
return false;
return (obj instanceof YearMonth other)
&& year == other.year
&& month == other.month;
}

/**
@@ -602,11 +602,8 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof ZoneId) {
ZoneId other = (ZoneId) obj;
return getId().equals(other.getId());
}
return false;
return (obj instanceof ZoneId other)
&& getId().equals(other.getId());
}

/**
@@ -813,8 +813,8 @@ public ValueRange range(TemporalField field) {
*/
@Override // override for Javadoc and performance
public 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:
@@ -850,8 +850,8 @@ public int get(TemporalField field) {
*/
@Override
public 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();
}

1 comment on commit 28af31d

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 28af31d Apr 22, 2021

Please sign in to comment.