8269124: Update java.time to use switch expressions (part II)

Reviewed-by: dfuchs, vtewari, aefimov, iris, lancea, naoto
This commit is contained in:
Patrick Concannon 2021-07-05 09:08:13 +00:00
parent 675a9520b2
commit 8a7b380ebb
16 changed files with 322 additions and 370 deletions

View file

@ -442,18 +442,11 @@ public final class LocalDate
*/
private static LocalDate create(int year, int month, int dayOfMonth) {
if (dayOfMonth > 28) {
int dom = 31;
switch (month) {
case 2:
dom = (IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28);
break;
case 4:
case 6:
case 9:
case 11:
dom = 30;
break;
}
int dom = switch (month) {
case 2 -> (IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28);
case 4, 6, 9, 11 -> 30;
default -> 31;
};
if (dayOfMonth > dom) {
if (dayOfMonth == 29) {
throw new DateTimeException("Invalid date 'February 29' as '" + year + "' is not a leap year");
@ -475,15 +468,8 @@ public final class LocalDate
*/
private static LocalDate resolvePreviousValid(int year, int month, int day) {
switch (month) {
case 2:
day = Math.min(day, IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28);
break;
case 4:
case 6:
case 9:
case 11:
day = Math.min(day, 30);
break;
case 2 -> day = Math.min(day, IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28);
case 4, 6, 9, 11 -> day = Math.min(day, 30);
}
return new LocalDate(year, month, day);
}
@ -690,22 +676,22 @@ public final class LocalDate
}
private int get0(TemporalField field) {
switch ((ChronoField) field) {
case DAY_OF_WEEK: return getDayOfWeek().getValue();
case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1;
case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1;
case DAY_OF_MONTH: return day;
case DAY_OF_YEAR: return getDayOfYear();
case EPOCH_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'EpochDay' for get() method, use getLong() instead");
case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1;
case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1;
case MONTH_OF_YEAR: return month;
case PROLEPTIC_MONTH: throw new UnsupportedTemporalTypeException("Invalid field 'ProlepticMonth' for get() method, use getLong() instead");
case YEAR_OF_ERA: return (year >= 1 ? year : 1 - year);
case YEAR: return year;
case ERA: return (year >= 1 ? 1 : 0);
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
return switch ((ChronoField) field) {
case DAY_OF_WEEK -> getDayOfWeek().getValue();
case ALIGNED_DAY_OF_WEEK_IN_MONTH -> ((day - 1) % 7) + 1;
case ALIGNED_DAY_OF_WEEK_IN_YEAR -> ((getDayOfYear() - 1) % 7) + 1;
case DAY_OF_MONTH -> day;
case DAY_OF_YEAR -> getDayOfYear();
case EPOCH_DAY -> throw new UnsupportedTemporalTypeException("Invalid field 'EpochDay' for get() method, use getLong() instead");
case ALIGNED_WEEK_OF_MONTH -> ((day - 1) / 7) + 1;
case ALIGNED_WEEK_OF_YEAR -> ((getDayOfYear() - 1) / 7) + 1;
case MONTH_OF_YEAR -> month;
case PROLEPTIC_MONTH -> throw new UnsupportedTemporalTypeException("Invalid field 'ProlepticMonth' for get() method, use getLong() instead");
case YEAR_OF_ERA -> (year >= 1 ? year : 1 - year);
case YEAR -> year;
case ERA -> (year >= 1 ? 1 : 0);
default -> throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
};
}
private long getProlepticMonth() {
@ -1039,22 +1025,22 @@ public final class LocalDate
public LocalDate with(TemporalField field, long newValue) {
if (field instanceof ChronoField chronoField) {
chronoField.checkValidValue(newValue);
switch (chronoField) {
case DAY_OF_WEEK: return plusDays(newValue - getDayOfWeek().getValue());
case ALIGNED_DAY_OF_WEEK_IN_MONTH: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH));
case ALIGNED_DAY_OF_WEEK_IN_YEAR: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR));
case DAY_OF_MONTH: return withDayOfMonth((int) newValue);
case DAY_OF_YEAR: return withDayOfYear((int) newValue);
case EPOCH_DAY: return LocalDate.ofEpochDay(newValue);
case ALIGNED_WEEK_OF_MONTH: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_MONTH));
case ALIGNED_WEEK_OF_YEAR: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_YEAR));
case MONTH_OF_YEAR: return withMonth((int) newValue);
case PROLEPTIC_MONTH: return plusMonths(newValue - getProlepticMonth());
case YEAR_OF_ERA: return withYear((int) (year >= 1 ? newValue : 1 - newValue));
case YEAR: return withYear((int) newValue);
case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - year));
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
return switch (chronoField) {
case DAY_OF_WEEK -> plusDays(newValue - getDayOfWeek().getValue());
case ALIGNED_DAY_OF_WEEK_IN_MONTH -> plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH));
case ALIGNED_DAY_OF_WEEK_IN_YEAR -> plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR));
case DAY_OF_MONTH -> withDayOfMonth((int) newValue);
case DAY_OF_YEAR -> withDayOfYear((int) newValue);
case EPOCH_DAY -> LocalDate.ofEpochDay(newValue);
case ALIGNED_WEEK_OF_MONTH -> plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_MONTH));
case ALIGNED_WEEK_OF_YEAR -> plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_YEAR));
case MONTH_OF_YEAR -> withMonth((int) newValue);
case PROLEPTIC_MONTH -> plusMonths(newValue - getProlepticMonth());
case YEAR_OF_ERA -> withYear((int) (year >= 1 ? newValue : 1 - newValue));
case YEAR -> withYear((int) newValue);
case ERA -> (getLong(ERA) == newValue ? this : withYear(1 - year));
default -> throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
};
}
return field.adjustInto(this, newValue);
}
@ -1250,17 +1236,17 @@ public final class LocalDate
@Override
public LocalDate plus(long amountToAdd, TemporalUnit unit) {
if (unit instanceof ChronoUnit chronoUnit) {
switch (chronoUnit) {
case DAYS: return plusDays(amountToAdd);
case WEEKS: return plusWeeks(amountToAdd);
case MONTHS: return plusMonths(amountToAdd);
case YEARS: return plusYears(amountToAdd);
case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10));
case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100));
case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000));
case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd));
}
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
return switch (chronoUnit) {
case DAYS -> plusDays(amountToAdd);
case WEEKS -> plusWeeks(amountToAdd);
case MONTHS -> plusMonths(amountToAdd);
case YEARS -> plusYears(amountToAdd);
case DECADES -> plusYears(Math.multiplyExact(amountToAdd, 10));
case CENTURIES -> plusYears(Math.multiplyExact(amountToAdd, 100));
case MILLENNIA -> plusYears(Math.multiplyExact(amountToAdd, 1000));
case ERAS -> with(ERA, Math.addExact(getLong(ERA), amountToAdd));
default -> throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
};
}
return unit.addTo(this, amountToAdd);
}
@ -1632,18 +1618,18 @@ public final class LocalDate
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
LocalDate end = LocalDate.from(endExclusive);
if (unit instanceof ChronoUnit) {
switch ((ChronoUnit) unit) {
case DAYS: return daysUntil(end);
case WEEKS: return daysUntil(end) / 7;
case MONTHS: return monthsUntil(end);
case YEARS: return monthsUntil(end) / 12;
case DECADES: return monthsUntil(end) / 120;
case CENTURIES: return monthsUntil(end) / 1200;
case MILLENNIA: return monthsUntil(end) / 12000;
case ERAS: return end.getLong(ERA) - getLong(ERA);
}
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
if (unit instanceof ChronoUnit chronoUnit) {
return switch (chronoUnit) {
case DAYS -> daysUntil(end);
case WEEKS -> daysUntil(end) / 7;
case MONTHS -> monthsUntil(end);
case YEARS -> monthsUntil(end) / 12;
case DECADES -> monthsUntil(end) / 120;
case CENTURIES -> monthsUntil(end) / 1200;
case MILLENNIA -> monthsUntil(end) / 12000;
case ERAS -> end.getLong(ERA) - getLong(ERA);
default -> throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
};
}
return unit.between(this, end);
}