Skip to content
Permalink
Browse files
8268469: Update java.time to use switch expressions
Reviewed-by: lancea, naoto, dfuchs, iris, chegar
  • Loading branch information
pconcannon committed Jun 25, 2021
1 parent ffa34ed commit 1d167978e53603ccf1599f476143391e7db51992
Showing 22 changed files with 255 additions and 336 deletions.
@@ -720,13 +720,13 @@ public Duration plus(long amountToAdd, TemporalUnit unit) {
return this;
}
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);
case SECONDS: return plusSeconds(amountToAdd);
}
return plusSeconds(Math.multiplyExact(unit.getDuration().seconds, amountToAdd));
return switch (chronoUnit) {
case NANOS -> plusNanos(amountToAdd);
case MICROS -> plusSeconds((amountToAdd / (1000_000L * 1000)) * 1000).plusNanos((amountToAdd % (1000_000L * 1000)) * 1000);
case MILLIS -> plusMillis(amountToAdd);
case SECONDS -> plusSeconds(amountToAdd);
default -> plusSeconds(Math.multiplyExact(unit.getDuration().seconds, amountToAdd));
};
}
Duration duration = unit.getDuration().multipliedBy(amountToAdd);
return plusSeconds(duration.getSeconds()).plusNanos(duration.getNano());
@@ -604,14 +604,13 @@ public boolean isSupported(TemporalUnit unit) {
public ValueRange range(TemporalField field) {
if (field instanceof ChronoField chronoField) {
if (chronoField.isDateBased()) {
switch (chronoField) {
case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth());
case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear());
case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, getMonth() == Month.FEBRUARY && isLeapYear() == false ? 4 : 5);
case YEAR_OF_ERA:
return (getYear() <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE));
}
return field.range();
return switch (chronoField) {
case DAY_OF_MONTH -> ValueRange.of(1, lengthOfMonth());
case DAY_OF_YEAR -> ValueRange.of(1, lengthOfYear());
case ALIGNED_WEEK_OF_MONTH -> ValueRange.of(1, getMonth() == Month.FEBRUARY && !isLeapYear() ? 4 : 5);
case YEAR_OF_ERA -> (getYear() <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE));
default -> field.range();
};
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
@@ -866,17 +865,11 @@ public boolean isLeapYear() {
*/
@Override
public int lengthOfMonth() {
switch (month) {
case 2:
return (isLeapYear() ? 29 : 28);
case 4:
case 6:
case 9:
case 11:
return 30;
default:
return 31;
}
return switch (month) {
case 2 -> (isLeapYear() ? 29 : 28);
case 4, 6, 9, 11 -> 30;
default -> 31;
};
}

/**
@@ -1177,16 +1177,16 @@ public LocalDateTime plus(TemporalAmount amountToAdd) {
@Override
public LocalDateTime plus(long amountToAdd, TemporalUnit unit) {
if (unit instanceof ChronoUnit chronoUnit) {
switch (chronoUnit) {
case NANOS: return plusNanos(amountToAdd);
case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
case MILLIS: return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000);
case SECONDS: return plusSeconds(amountToAdd);
case MINUTES: return plusMinutes(amountToAdd);
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 switch (chronoUnit) {
case NANOS -> plusNanos(amountToAdd);
case MICROS -> plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
case MILLIS -> plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000);
case SECONDS -> plusSeconds(amountToAdd);
case MINUTES -> plusMinutes(amountToAdd);
case HOURS -> plusHours(amountToAdd);
case HALF_DAYS -> plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12); // no overflow (256 is multiple of 2)
default -> with(date.plus(amountToAdd, unit), time);
};
}
return unit.addTo(this, amountToAdd);
}
@@ -423,17 +423,11 @@ public Month minus(long months) {
* @return the length of this month in days, from 28 to 31
*/
public int length(boolean leapYear) {
switch (this) {
case FEBRUARY:
return (leapYear ? 29 : 28);
case APRIL:
case JUNE:
case SEPTEMBER:
case NOVEMBER:
return 30;
default:
return 31;
}
return switch (this) {
case FEBRUARY -> (leapYear ? 29 : 28);
case APRIL, JUNE, SEPTEMBER, NOVEMBER -> 30;
default -> 31;
};
}

/**
@@ -446,17 +440,11 @@ public int length(boolean leapYear) {
* @return the minimum length of this month in days, from 28 to 31
*/
public int minLength() {
switch (this) {
case FEBRUARY:
return 28;
case APRIL:
case JUNE:
case SEPTEMBER:
case NOVEMBER:
return 30;
default:
return 31;
}
return switch (this) {
case FEBRUARY -> 28;
case APRIL, JUNE, SEPTEMBER, NOVEMBER -> 30;
default -> 31;
};
}

/**
@@ -469,17 +457,11 @@ public int minLength() {
* @return the maximum length of this month in days, from 29 to 31
*/
public int maxLength() {
switch (this) {
case FEBRUARY:
return 29;
case APRIL:
case JUNE:
case SEPTEMBER:
case NOVEMBER:
return 30;
default:
return 31;
}
return switch (this) {
case FEBRUARY -> 29;
case APRIL, JUNE, SEPTEMBER, NOVEMBER -> 30;
default -> 31;
};
}

//-----------------------------------------------------------------------
@@ -494,33 +476,21 @@ public int maxLength() {
*/
public int firstDayOfYear(boolean leapYear) {
int leap = leapYear ? 1 : 0;
switch (this) {
case JANUARY:
return 1;
case FEBRUARY:
return 32;
case MARCH:
return 60 + leap;
case APRIL:
return 91 + leap;
case MAY:
return 121 + leap;
case JUNE:
return 152 + leap;
case JULY:
return 182 + leap;
case AUGUST:
return 213 + leap;
case SEPTEMBER:
return 244 + leap;
case OCTOBER:
return 274 + leap;
case NOVEMBER:
return 305 + leap;
case DECEMBER:
default:
return 335 + leap;
}
return switch (this) {
case JANUARY -> 1;
case FEBRUARY -> 32;
case MARCH -> 60 + leap;
case APRIL -> 91 + leap;
case MAY -> 121 + leap;
case JUNE -> 152 + leap;
case JULY -> 182 + leap;
case AUGUST -> 213 + leap;
case SEPTEMBER -> 244 + leap;
case OCTOBER -> 274 + leap;
case NOVEMBER -> 305 + leap;
// otherwise (DECEMBER)
default -> 335 + leap;
};
}

/**
@@ -597,13 +597,12 @@ public ValueRange range(TemporalField field) {
@Override
public int get(TemporalField 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:
return getOffset().getTotalSeconds();
}
return dateTime.get(field);
return switch (chronoField) {
case INSTANT_SECONDS -> throw new UnsupportedTemporalTypeException("Invalid field " +
"'InstantSeconds' for get() method, use getLong() instead");
case OFFSET_SECONDS -> getOffset().getTotalSeconds();
default -> dateTime.get(field);
};
}
return Temporal.super.get(field);
}
@@ -634,11 +633,11 @@ public int get(TemporalField field) {
@Override
public long getLong(TemporalField field) {
if (field instanceof ChronoField chronoField) {
switch (chronoField) {
case INSTANT_SECONDS: return toEpochSecond();
case OFFSET_SECONDS: return getOffset().getTotalSeconds();
}
return dateTime.getLong(field);
return switch (chronoField) {
case INSTANT_SECONDS -> toEpochSecond();
case OFFSET_SECONDS -> getOffset().getTotalSeconds();
default -> dateTime.getLong(field);
};
}
return field.getFrom(this);
}
@@ -270,24 +270,23 @@ static Serializable read(ObjectInput in) throws IOException, ClassNotFoundExcept

private static Serializable readInternal(byte type, ObjectInput in)
throws IOException, ClassNotFoundException {
switch (type) {
case DURATION_TYPE: return Duration.readExternal(in);
case INSTANT_TYPE: return Instant.readExternal(in);
case LOCAL_DATE_TYPE: return LocalDate.readExternal(in);
case LOCAL_DATE_TIME_TYPE: return LocalDateTime.readExternal(in);
case LOCAL_TIME_TYPE: return LocalTime.readExternal(in);
case ZONE_DATE_TIME_TYPE: return ZonedDateTime.readExternal(in);
case ZONE_OFFSET_TYPE: return ZoneOffset.readExternal(in);
case ZONE_REGION_TYPE: return ZoneRegion.readExternal(in);
case OFFSET_TIME_TYPE: return OffsetTime.readExternal(in);
case OFFSET_DATE_TIME_TYPE: return OffsetDateTime.readExternal(in);
case YEAR_TYPE: return Year.readExternal(in);
case YEAR_MONTH_TYPE: return YearMonth.readExternal(in);
case MONTH_DAY_TYPE: return MonthDay.readExternal(in);
case PERIOD_TYPE: return Period.readExternal(in);
default:
throw new StreamCorruptedException("Unknown serialized type");
}
return switch (type) {
case DURATION_TYPE -> Duration.readExternal(in);
case INSTANT_TYPE -> Instant.readExternal(in);
case LOCAL_DATE_TYPE -> LocalDate.readExternal(in);
case LOCAL_DATE_TIME_TYPE -> LocalDateTime.readExternal(in);
case LOCAL_TIME_TYPE -> LocalTime.readExternal(in);
case ZONE_DATE_TIME_TYPE -> ZonedDateTime.readExternal(in);
case ZONE_OFFSET_TYPE -> ZoneOffset.readExternal(in);
case ZONE_REGION_TYPE -> ZoneRegion.readExternal(in);
case OFFSET_TIME_TYPE -> OffsetTime.readExternal(in);
case OFFSET_DATE_TIME_TYPE -> OffsetDateTime.readExternal(in);
case YEAR_TYPE -> Year.readExternal(in);
case YEAR_MONTH_TYPE -> YearMonth.readExternal(in);
case MONTH_DAY_TYPE -> MonthDay.readExternal(in);
case PERIOD_TYPE -> Period.readExternal(in);
default -> throw new StreamCorruptedException("Unknown serialized type");
};
}

/**
@@ -814,13 +814,12 @@ public ValueRange range(TemporalField field) {
@Override // override for Javadoc and performance
public int get(TemporalField 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:
return getOffset().getTotalSeconds();
}
return dateTime.get(field);
return switch (chronoField) {
case INSTANT_SECONDS -> throw new UnsupportedTemporalTypeException("Invalid field " +
"'InstantSeconds' for get() method, use getLong() instead");
case OFFSET_SECONDS -> getOffset().getTotalSeconds();
default -> dateTime.get(field);
};
}
return ChronoZonedDateTime.super.get(field);
}
@@ -851,11 +850,11 @@ public int get(TemporalField field) {
@Override
public long getLong(TemporalField field) {
if (field instanceof ChronoField chronoField) {
switch (chronoField) {
case INSTANT_SECONDS: return toEpochSecond();
case OFFSET_SECONDS: return getOffset().getTotalSeconds();
}
return dateTime.getLong(field);
return switch (chronoField) {
case INSTANT_SECONDS -> toEpochSecond();
case OFFSET_SECONDS -> getOffset().getTotalSeconds();
default -> dateTime.getLong(field);
};
}
return field.getFrom(this);
}
@@ -299,16 +299,16 @@ public ChronoLocalDateTimeImpl<D> with(TemporalField field, long newValue) {
@Override
public ChronoLocalDateTimeImpl<D> plus(long amountToAdd, TemporalUnit unit) {
if (unit instanceof ChronoUnit chronoUnit) {
switch (chronoUnit) {
case NANOS: return plusNanos(amountToAdd);
case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
case MILLIS: return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000000);
case SECONDS: return plusSeconds(amountToAdd);
case MINUTES: return plusMinutes(amountToAdd);
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 switch (chronoUnit) {
case NANOS -> plusNanos(amountToAdd);
case MICROS -> plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
case MILLIS -> plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000000);
case SECONDS -> plusSeconds(amountToAdd);
case MINUTES -> plusMinutes(amountToAdd);
case HOURS -> plusHours(amountToAdd);
case HALF_DAYS -> plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12); // no overflow (256 is multiple of 2)
default -> with(date.plus(amountToAdd, unit), time);
};
}
return ChronoLocalDateTimeImpl.ensureValid(date.getChronology(), unit.addTo(this, amountToAdd));
}
@@ -210,11 +210,11 @@ default int get(TemporalField field) {
@Override
default long getLong(TemporalField field) {
if (field instanceof ChronoField chronoField) {
switch (chronoField) {
case INSTANT_SECONDS: return toEpochSecond();
case OFFSET_SECONDS: return getOffset().getTotalSeconds();
}
return toLocalDateTime().getLong(field);
return switch (chronoField) {
case INSTANT_SECONDS -> toEpochSecond();
case OFFSET_SECONDS -> getOffset().getTotalSeconds();
default -> toLocalDateTime().getLong(field);
};
}
return field.getFrom(this);
}

0 comments on commit 1d16797

Please sign in to comment.