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

@ -558,13 +558,13 @@ public final class Instant
*/
@Override // override for Javadoc and performance
public int get(TemporalField field) {
if (field instanceof ChronoField) {
switch ((ChronoField) field) {
case NANO_OF_SECOND: return nanos;
case MICRO_OF_SECOND: return nanos / 1000;
case MILLI_OF_SECOND: return nanos / 1000_000;
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
if (field instanceof ChronoField chronoField) {
return switch (chronoField) {
case NANO_OF_SECOND -> nanos;
case MICRO_OF_SECOND -> nanos / 1000;
case MILLI_OF_SECOND -> nanos / 1000_000;
default -> throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
};
}
return range(field).checkValidIntValue(field.getFrom(this), field);
}
@ -594,14 +594,14 @@ public final class Instant
*/
@Override
public long getLong(TemporalField field) {
if (field instanceof ChronoField) {
switch ((ChronoField) field) {
case NANO_OF_SECOND: return nanos;
case MICRO_OF_SECOND: return nanos / 1000;
case MILLI_OF_SECOND: return nanos / 1000_000;
case INSTANT_SECONDS: return seconds;
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
if (field instanceof ChronoField chronoField) {
return switch (chronoField) {
case NANO_OF_SECOND -> nanos;
case MICRO_OF_SECOND -> nanos / 1000;
case MILLI_OF_SECOND -> nanos / 1000_000;
case INSTANT_SECONDS -> seconds;
default -> throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
};
}
return field.getFrom(this);
}
@ -705,19 +705,19 @@ public final class Instant
public Instant with(TemporalField field, long newValue) {
if (field instanceof ChronoField chronoField) {
chronoField.checkValidValue(newValue);
switch (chronoField) {
case MILLI_OF_SECOND: {
return switch (chronoField) {
case MILLI_OF_SECOND -> {
int nval = (int) newValue * 1000_000;
return (nval != nanos ? create(seconds, nval) : this);
yield nval != nanos ? create(seconds, nval) : this;
}
case MICRO_OF_SECOND: {
case MICRO_OF_SECOND -> {
int nval = (int) newValue * 1000;
return (nval != nanos ? create(seconds, nval) : this);
yield nval != nanos ? create(seconds, nval) : this;
}
case NANO_OF_SECOND: return (newValue != nanos ? create(seconds, (int) newValue) : this);
case INSTANT_SECONDS: return (newValue != seconds ? create(newValue, nanos) : this);
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
case NANO_OF_SECOND -> newValue != nanos ? create(seconds, (int) newValue) : this;
case INSTANT_SECONDS -> newValue != seconds ? create(newValue, nanos) : this;
default -> throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
};
}
return field.adjustInto(this, newValue);
}
@ -848,18 +848,18 @@ public final class Instant
*/
@Override
public Instant plus(long amountToAdd, TemporalUnit unit) {
if (unit instanceof ChronoUnit) {
switch ((ChronoUnit) unit) {
case NANOS: return plusNanos(amountToAdd);
case MICROS: return plus(amountToAdd / 1000_000, (amountToAdd % 1000_000) * 1000);
case MILLIS: return plusMillis(amountToAdd);
case SECONDS: return plusSeconds(amountToAdd);
case MINUTES: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_MINUTE));
case HOURS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_HOUR));
case HALF_DAYS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY / 2));
case DAYS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY));
}
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
if (unit instanceof ChronoUnit chronoUnit) {
return switch (chronoUnit) {
case NANOS -> plusNanos(amountToAdd);
case MICROS -> plus(amountToAdd / 1000_000, (amountToAdd % 1000_000) * 1000);
case MILLIS -> plusMillis(amountToAdd);
case SECONDS -> plusSeconds(amountToAdd);
case MINUTES -> plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_MINUTE));
case HOURS -> plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_HOUR));
case HALF_DAYS -> plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY / 2));
case DAYS -> plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY));
default -> throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
};
}
return unit.addTo(this, amountToAdd);
}
@ -1143,17 +1143,17 @@ public final class Instant
public long until(Temporal endExclusive, TemporalUnit unit) {
Instant end = Instant.from(endExclusive);
if (unit instanceof ChronoUnit chronoUnit) {
switch (chronoUnit) {
case NANOS: return nanosUntil(end);
case MICROS: return nanosUntil(end) / 1000;
case MILLIS: return Math.subtractExact(end.toEpochMilli(), toEpochMilli());
case SECONDS: return secondsUntil(end);
case MINUTES: return secondsUntil(end) / SECONDS_PER_MINUTE;
case HOURS: return secondsUntil(end) / SECONDS_PER_HOUR;
case HALF_DAYS: return secondsUntil(end) / (12 * SECONDS_PER_HOUR);
case DAYS: return secondsUntil(end) / (SECONDS_PER_DAY);
}
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
return switch (chronoUnit) {
case NANOS -> nanosUntil(end);
case MICROS -> nanosUntil(end) / 1000;
case MILLIS -> Math.subtractExact(end.toEpochMilli(), toEpochMilli());
case SECONDS -> secondsUntil(end);
case MINUTES -> secondsUntil(end) / SECONDS_PER_MINUTE;
case HOURS -> secondsUntil(end) / SECONDS_PER_HOUR;
case HALF_DAYS -> secondsUntil(end) / (12 * SECONDS_PER_HOUR);
case DAYS -> secondsUntil(end) / (SECONDS_PER_DAY);
default -> throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
};
}
return unit.between(this, end);
}

@ -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);
}

@ -683,24 +683,24 @@ public final class LocalTime
}
private int get0(TemporalField field) {
switch ((ChronoField) field) {
case NANO_OF_SECOND: return nano;
case NANO_OF_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'NanoOfDay' for get() method, use getLong() instead");
case MICRO_OF_SECOND: return nano / 1000;
case MICRO_OF_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'MicroOfDay' for get() method, use getLong() instead");
case MILLI_OF_SECOND: return nano / 1000_000;
case MILLI_OF_DAY: return (int) (toNanoOfDay() / 1000_000);
case SECOND_OF_MINUTE: return second;
case SECOND_OF_DAY: return toSecondOfDay();
case MINUTE_OF_HOUR: return minute;
case MINUTE_OF_DAY: return hour * 60 + minute;
case HOUR_OF_AMPM: return hour % 12;
case CLOCK_HOUR_OF_AMPM: int ham = hour % 12; return (ham % 12 == 0 ? 12 : ham);
case HOUR_OF_DAY: return hour;
case CLOCK_HOUR_OF_DAY: return (hour == 0 ? 24 : hour);
case AMPM_OF_DAY: return hour / 12;
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
return switch ((ChronoField) field) {
case NANO_OF_SECOND -> nano;
case NANO_OF_DAY -> throw new UnsupportedTemporalTypeException("Invalid field 'NanoOfDay' for get() method, use getLong() instead");
case MICRO_OF_SECOND -> nano / 1000;
case MICRO_OF_DAY -> throw new UnsupportedTemporalTypeException("Invalid field 'MicroOfDay' for get() method, use getLong() instead");
case MILLI_OF_SECOND -> nano / 1000_000;
case MILLI_OF_DAY -> (int) (toNanoOfDay() / 1000_000);
case SECOND_OF_MINUTE -> second;
case SECOND_OF_DAY -> toSecondOfDay();
case MINUTE_OF_HOUR -> minute;
case MINUTE_OF_DAY -> hour * 60 + minute;
case HOUR_OF_AMPM -> hour % 12;
case CLOCK_HOUR_OF_AMPM -> { int ham = hour % 12; yield ham % 12 == 0 ? 12 : ham; }
case HOUR_OF_DAY -> hour;
case CLOCK_HOUR_OF_DAY -> (hour == 0 ? 24 : hour);
case AMPM_OF_DAY -> hour / 12;
default -> throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
};
}
//-----------------------------------------------------------------------
@ -857,24 +857,24 @@ public final class LocalTime
public LocalTime with(TemporalField field, long newValue) {
if (field instanceof ChronoField chronoField) {
chronoField.checkValidValue(newValue);
switch (chronoField) {
case NANO_OF_SECOND: return withNano((int) newValue);
case NANO_OF_DAY: return LocalTime.ofNanoOfDay(newValue);
case MICRO_OF_SECOND: return withNano((int) newValue * 1000);
case MICRO_OF_DAY: return LocalTime.ofNanoOfDay(newValue * 1000);
case MILLI_OF_SECOND: return withNano((int) newValue * 1000_000);
case MILLI_OF_DAY: return LocalTime.ofNanoOfDay(newValue * 1000_000);
case SECOND_OF_MINUTE: return withSecond((int) newValue);
case SECOND_OF_DAY: return plusSeconds(newValue - toSecondOfDay());
case MINUTE_OF_HOUR: return withMinute((int) newValue);
case MINUTE_OF_DAY: return plusMinutes(newValue - (hour * 60 + minute));
case HOUR_OF_AMPM: return plusHours(newValue - (hour % 12));
case CLOCK_HOUR_OF_AMPM: return plusHours((newValue == 12 ? 0 : newValue) - (hour % 12));
case HOUR_OF_DAY: return withHour((int) newValue);
case CLOCK_HOUR_OF_DAY: return withHour((int) (newValue == 24 ? 0 : newValue));
case AMPM_OF_DAY: return plusHours((newValue - (hour / 12)) * 12);
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
return switch (chronoField) {
case NANO_OF_SECOND -> withNano((int) newValue);
case NANO_OF_DAY -> LocalTime.ofNanoOfDay(newValue);
case MICRO_OF_SECOND -> withNano((int) newValue * 1000);
case MICRO_OF_DAY -> LocalTime.ofNanoOfDay(newValue * 1000);
case MILLI_OF_SECOND -> withNano((int) newValue * 1000_000);
case MILLI_OF_DAY -> LocalTime.ofNanoOfDay(newValue * 1000_000);
case SECOND_OF_MINUTE -> withSecond((int) newValue);
case SECOND_OF_DAY -> plusSeconds(newValue - toSecondOfDay());
case MINUTE_OF_HOUR -> withMinute((int) newValue);
case MINUTE_OF_DAY -> plusMinutes(newValue - (hour * 60 + minute));
case HOUR_OF_AMPM -> plusHours(newValue - (hour % 12));
case CLOCK_HOUR_OF_AMPM -> plusHours((newValue == 12 ? 0 : newValue) - (hour % 12));
case HOUR_OF_DAY -> withHour((int) newValue);
case CLOCK_HOUR_OF_DAY -> withHour((int) (newValue == 24 ? 0 : newValue));
case AMPM_OF_DAY -> plusHours((newValue - (hour / 12)) * 12);
default -> throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
};
}
return field.adjustInto(this, newValue);
}
@ -1066,16 +1066,16 @@ public final class LocalTime
@Override
public LocalTime plus(long amountToAdd, TemporalUnit 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);
case SECONDS: return plusSeconds(amountToAdd);
case MINUTES: return plusMinutes(amountToAdd);
case HOURS: return plusHours(amountToAdd);
case HALF_DAYS: return plusHours((amountToAdd % 2) * 12);
}
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
return switch (chronoUnit) {
case NANOS -> plusNanos(amountToAdd);
case MICROS -> plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
case MILLIS -> plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000);
case SECONDS -> plusSeconds(amountToAdd);
case MINUTES -> plusMinutes(amountToAdd);
case HOURS -> plusHours(amountToAdd);
case HALF_DAYS -> plusHours((amountToAdd % 2) * 12);
default -> throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
};
}
return unit.addTo(this, amountToAdd);
}
@ -1408,16 +1408,16 @@ public final class LocalTime
LocalTime end = LocalTime.from(endExclusive);
if (unit instanceof ChronoUnit chronoUnit) {
long nanosUntil = end.toNanoOfDay() - toNanoOfDay(); // no overflow
switch (chronoUnit) {
case NANOS: return nanosUntil;
case MICROS: return nanosUntil / 1000;
case MILLIS: return nanosUntil / 1000_000;
case SECONDS: return nanosUntil / NANOS_PER_SECOND;
case MINUTES: return nanosUntil / NANOS_PER_MINUTE;
case HOURS: return nanosUntil / NANOS_PER_HOUR;
case HALF_DAYS: return nanosUntil / (12 * NANOS_PER_HOUR);
}
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
return switch (chronoUnit) {
case NANOS -> nanosUntil;
case MICROS -> nanosUntil / 1000;
case MILLIS -> nanosUntil / 1000_000;
case SECONDS -> nanosUntil / NANOS_PER_SECOND;
case MINUTES -> nanosUntil / NANOS_PER_MINUTE;
case HOURS -> nanosUntil / NANOS_PER_HOUR;
case HALF_DAYS -> nanosUntil / (12 * NANOS_PER_HOUR);
default -> throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
};
}
return unit.between(this, end);
}

@ -445,12 +445,12 @@ public final class MonthDay
@Override
public long getLong(TemporalField field) {
if (field instanceof ChronoField chronoField) {
switch (chronoField) {
return 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;
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
case DAY_OF_MONTH -> day;
case MONTH_OF_YEAR -> month;
default -> throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
};
}
return field.getFrom(this);
}

@ -967,13 +967,12 @@ public final class OffsetDateTime
@Override
public OffsetDateTime with(TemporalField field, long newValue) {
if (field instanceof ChronoField chronoField) {
switch (chronoField) {
case INSTANT_SECONDS: return ofInstant(Instant.ofEpochSecond(newValue, getNano()), offset);
case OFFSET_SECONDS: {
return with(dateTime, ZoneOffset.ofTotalSeconds(chronoField.checkValidIntValue(newValue)));
}
}
return with(dateTime.with(field, newValue), offset);
return switch (chronoField) {
case INSTANT_SECONDS -> ofInstant(Instant.ofEpochSecond(newValue, getNano()), offset);
case OFFSET_SECONDS ->
with(dateTime, ZoneOffset.ofTotalSeconds(chronoField.checkValidIntValue(newValue)));
default -> with(dateTime.with(field, newValue), offset);
};
}
return field.adjustInto(this, newValue);
}

@ -1180,16 +1180,16 @@ public final class OffsetTime
OffsetTime end = OffsetTime.from(endExclusive);
if (unit instanceof ChronoUnit chronoUnit) {
long nanosUntil = end.toEpochNano() - toEpochNano(); // no overflow
switch (chronoUnit) {
case NANOS: return nanosUntil;
case MICROS: return nanosUntil / 1000;
case MILLIS: return nanosUntil / 1000_000;
case SECONDS: return nanosUntil / NANOS_PER_SECOND;
case MINUTES: return nanosUntil / NANOS_PER_MINUTE;
case HOURS: return nanosUntil / NANOS_PER_HOUR;
case HALF_DAYS: return nanosUntil / (12 * NANOS_PER_HOUR);
}
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
return switch (chronoUnit) {
case NANOS -> nanosUntil;
case MICROS -> nanosUntil / 1000;
case MILLIS -> nanosUntil / 1000_000;
case SECONDS -> nanosUntil / NANOS_PER_SECOND;
case MINUTES -> nanosUntil / NANOS_PER_MINUTE;
case HOURS -> nanosUntil / NANOS_PER_HOUR;
case HALF_DAYS -> nanosUntil / (12 * NANOS_PER_HOUR);
default -> throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
};
}
return unit.between(this, end);
}

@ -167,50 +167,21 @@ final class Ser implements Externalizable {
static void writeInternal(byte type, Object object, ObjectOutput out) throws IOException {
out.writeByte(type);
switch (type) {
case DURATION_TYPE:
((Duration) object).writeExternal(out);
break;
case INSTANT_TYPE:
((Instant) object).writeExternal(out);
break;
case LOCAL_DATE_TYPE:
((LocalDate) object).writeExternal(out);
break;
case LOCAL_DATE_TIME_TYPE:
((LocalDateTime) object).writeExternal(out);
break;
case LOCAL_TIME_TYPE:
((LocalTime) object).writeExternal(out);
break;
case ZONE_REGION_TYPE:
((ZoneRegion) object).writeExternal(out);
break;
case ZONE_OFFSET_TYPE:
((ZoneOffset) object).writeExternal(out);
break;
case ZONE_DATE_TIME_TYPE:
((ZonedDateTime) object).writeExternal(out);
break;
case OFFSET_TIME_TYPE:
((OffsetTime) object).writeExternal(out);
break;
case OFFSET_DATE_TIME_TYPE:
((OffsetDateTime) object).writeExternal(out);
break;
case YEAR_TYPE:
((Year) object).writeExternal(out);
break;
case YEAR_MONTH_TYPE:
((YearMonth) object).writeExternal(out);
break;
case MONTH_DAY_TYPE:
((MonthDay) object).writeExternal(out);
break;
case PERIOD_TYPE:
((Period) object).writeExternal(out);
break;
default:
throw new InvalidClassException("Unknown serialized type");
case DURATION_TYPE -> ((Duration) object).writeExternal(out);
case INSTANT_TYPE -> ((Instant) object).writeExternal(out);
case LOCAL_DATE_TYPE -> ((LocalDate) object).writeExternal(out);
case LOCAL_DATE_TIME_TYPE -> ((LocalDateTime) object).writeExternal(out);
case LOCAL_TIME_TYPE -> ((LocalTime) object).writeExternal(out);
case ZONE_REGION_TYPE -> ((ZoneRegion) object).writeExternal(out);
case ZONE_OFFSET_TYPE -> ((ZoneOffset) object).writeExternal(out);
case ZONE_DATE_TIME_TYPE -> ((ZonedDateTime) object).writeExternal(out);
case OFFSET_TIME_TYPE -> ((OffsetTime) object).writeExternal(out);
case OFFSET_DATE_TIME_TYPE -> ((OffsetDateTime) object).writeExternal(out);
case YEAR_TYPE -> ((Year) object).writeExternal(out);
case YEAR_MONTH_TYPE -> ((YearMonth) object).writeExternal(out);
case MONTH_DAY_TYPE -> ((MonthDay) object).writeExternal(out);
case PERIOD_TYPE -> ((Period) object).writeExternal(out);
default -> throw new InvalidClassException("Unknown serialized type");
}
}

@ -497,12 +497,12 @@ public final class Year
@Override
public long getLong(TemporalField 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);
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
return switch (chronoField) {
case YEAR_OF_ERA -> year < 1 ? 1 - year : year;
case YEAR -> year;
case ERA -> year < 1 ? 0 : 1;
default -> throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
};
}
return field.getFrom(this);
}
@ -621,12 +621,12 @@ public final class Year
public Year with(TemporalField field, long newValue) {
if (field instanceof ChronoField chronoField) {
chronoField.checkValidValue(newValue);
switch (chronoField) {
case YEAR_OF_ERA: return Year.of((int) (year < 1 ? 1 - newValue : newValue));
case YEAR: return Year.of((int) newValue);
case ERA: return (getLong(ERA) == newValue ? this : Year.of(1 - year));
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
return switch (chronoField) {
case YEAR_OF_ERA -> Year.of((int) (year < 1 ? 1 - newValue : newValue));
case YEAR -> Year.of((int) newValue);
case ERA -> getLong(ERA) == newValue ? this : Year.of(1 - year);
default -> throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
};
}
return field.adjustInto(this, newValue);
}
@ -708,14 +708,14 @@ public final class Year
@Override
public Year plus(long amountToAdd, TemporalUnit 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));
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 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);
}
@ -915,14 +915,14 @@ public final class Year
Year end = Year.from(endExclusive);
if (unit instanceof ChronoUnit chronoUnit) {
long yearsUntil = ((long) end.year) - year; // no overflow
switch (chronoUnit) {
case YEARS: return yearsUntil;
case DECADES: return yearsUntil / 10;
case CENTURIES: return yearsUntil / 100;
case MILLENNIA: return yearsUntil / 1000;
case ERAS: return end.getLong(ERA) - getLong(ERA);
}
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
return switch (chronoUnit) {
case YEARS -> yearsUntil;
case DECADES -> yearsUntil / 10;
case CENTURIES -> yearsUntil / 100;
case MILLENNIA -> yearsUntil / 1000;
case ERAS -> end.getLong(ERA) - getLong(ERA);
default -> throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
};
}
return unit.between(this, end);
}

@ -486,14 +486,14 @@ public final class YearMonth
@Override
public long getLong(TemporalField 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);
case YEAR: return year;
case ERA: return (year < 1 ? 0 : 1);
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
return switch (chronoField) {
case MONTH_OF_YEAR -> month;
case PROLEPTIC_MONTH -> getProlepticMonth();
case YEAR_OF_ERA -> (year < 1 ? 1 - year : year);
case YEAR -> year;
case ERA -> (year < 1 ? 0 : 1);
default -> throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
};
}
return field.getFrom(this);
}
@ -684,14 +684,14 @@ public final class YearMonth
public YearMonth with(TemporalField field, long newValue) {
if (field instanceof ChronoField chronoField) {
chronoField.checkValidValue(newValue);
switch (chronoField) {
case MONTH_OF_YEAR: return withMonth((int) newValue);
case PROLEPTIC_MONTH: return plusMonths(newValue - getProlepticMonth());
case YEAR_OF_ERA: return withYear((int) (year < 1 ? 1 - newValue : 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 MONTH_OF_YEAR -> withMonth((int) newValue);
case PROLEPTIC_MONTH -> plusMonths(newValue - getProlepticMonth());
case YEAR_OF_ERA -> withYear((int) (year < 1 ? 1 - newValue : 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);
}
@ -805,15 +805,15 @@ public final class YearMonth
@Override
public YearMonth plus(long amountToAdd, TemporalUnit 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));
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 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);
}
@ -1047,15 +1047,15 @@ public final class YearMonth
YearMonth end = YearMonth.from(endExclusive);
if (unit instanceof ChronoUnit chronoUnit) {
long monthsUntil = end.getProlepticMonth() - getProlepticMonth(); // no overflow
switch (chronoUnit) {
case MONTHS: return monthsUntil;
case YEARS: return monthsUntil / 12;
case DECADES: return monthsUntil / 120;
case CENTURIES: return monthsUntil / 1200;
case MILLENNIA: return monthsUntil / 12000;
case ERAS: return end.getLong(ERA) - getLong(ERA);
}
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
return switch (chronoUnit) {
case MONTHS -> monthsUntil;
case YEARS -> monthsUntil / 12;
case DECADES -> monthsUntil / 120;
case CENTURIES -> monthsUntil / 1200;
case MILLENNIA -> monthsUntil / 12000;
case ERAS -> end.getLong(ERA) - getLong(ERA);
default -> throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
};
}
return unit.between(this, end);
}

@ -1300,14 +1300,14 @@ public final class ZonedDateTime
@Override
public ZonedDateTime with(TemporalField field, long newValue) {
if (field instanceof ChronoField chronoField) {
switch (chronoField) {
case INSTANT_SECONDS:
return create(newValue, getNano(), zone);
case OFFSET_SECONDS:
return switch (chronoField) {
case INSTANT_SECONDS -> create(newValue, getNano(), zone);
case OFFSET_SECONDS -> {
ZoneOffset offset = ZoneOffset.ofTotalSeconds(chronoField.checkValidIntValue(newValue));
return resolveOffset(offset);
}
return resolveLocal(dateTime.with(field, newValue));
yield resolveOffset(offset);
}
default -> resolveLocal(dateTime.with(field, newValue));
};
}
return field.adjustInto(this, newValue);
}

@ -198,17 +198,17 @@ abstract class ChronoLocalDateImpl<D extends ChronoLocalDate>
@SuppressWarnings("unchecked")
public D plus(long amountToAdd, TemporalUnit unit) {
if (unit instanceof ChronoUnit chronoUnit) {
switch (chronoUnit) {
case DAYS: return plusDays(amountToAdd);
case WEEKS: return plusDays(Math.multiplyExact(amountToAdd, 7));
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 -> plusDays(Math.multiplyExact(amountToAdd, 7));
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 (D) ChronoLocalDate.super.plus(amountToAdd, unit);
}
@ -377,17 +377,17 @@ abstract class ChronoLocalDateImpl<D extends ChronoLocalDate>
Objects.requireNonNull(endExclusive, "endExclusive");
ChronoLocalDate end = getChronology().date(endExclusive);
if (unit instanceof ChronoUnit chronoUnit) {
switch (chronoUnit) {
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);
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);
};
}
Objects.requireNonNull(unit, "unit");
return unit.between(this, end);

@ -196,13 +196,12 @@ public interface ChronoZonedDateTime<D extends ChronoLocalDate>
@Override
default int get(TemporalField field) {
if (field instanceof ChronoField chronoField) {
switch (chronoField) {
case INSTANT_SECONDS:
return switch (chronoField) {
case INSTANT_SECONDS ->
throw new UnsupportedTemporalTypeException("Invalid field 'InstantSeconds' for get() method, use getLong() instead");
case OFFSET_SECONDS:
return getOffset().getTotalSeconds();
}
return toLocalDateTime().get(field);
case OFFSET_SECONDS -> getOffset().getTotalSeconds();
default -> toLocalDateTime().get(field);
};
}
return Temporal.super.get(field);
}

@ -284,14 +284,14 @@ final class ChronoZonedDateTimeImpl<D extends ChronoLocalDate>
@Override
public ChronoZonedDateTime<D> with(TemporalField field, long newValue) {
if (field instanceof ChronoField chronoField) {
switch (chronoField) {
case INSTANT_SECONDS: return plus(newValue - toEpochSecond(), SECONDS);
case OFFSET_SECONDS: {
return switch (chronoField) {
case INSTANT_SECONDS -> plus(newValue - toEpochSecond(), SECONDS);
case OFFSET_SECONDS -> {
ZoneOffset offset = ZoneOffset.ofTotalSeconds(chronoField.checkValidIntValue(newValue));
return create(dateTime.toInstant(offset), zone);
yield create(dateTime.toInstant(offset), zone);
}
}
return ofBest(dateTime.with(field, newValue), zone, offset);
default -> ofBest(dateTime.with(field, newValue), zone, offset);
};
}
return ChronoZonedDateTimeImpl.ensureValid(getChronology(), field.adjustInto(this, newValue));
}

@ -518,12 +518,10 @@ public final class HijrahChronology extends AbstractChronology implements Serial
*/
@Override
public HijrahEra eraOf(int eraValue) {
switch (eraValue) {
case 1:
return HijrahEra.AH;
default:
throw new DateTimeException("invalid Hijrah era");
}
return switch (eraValue) {
case 1 -> HijrahEra.AH;
default -> throw new DateTimeException("invalid Hijrah era");
};
}
@Override

@ -444,20 +444,19 @@ public final class JapaneseDate
@Override
public ValueRange range(TemporalField field) {
if (field instanceof ChronoField) {
if (field instanceof ChronoField chronoField) {
if (isSupported(field)) {
ChronoField f = (ChronoField) field;
switch (f) {
case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth());
case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear());
case YEAR_OF_ERA: {
return switch (chronoField) {
case DAY_OF_MONTH -> ValueRange.of(1, lengthOfMonth());
case DAY_OF_YEAR -> ValueRange.of(1, lengthOfYear());
case YEAR_OF_ERA -> {
Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE);
jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET);
jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth());
return ValueRange.of(1, jcal.getActualMaximum(Calendar.YEAR));
yield ValueRange.of(1, jcal.getActualMaximum(Calendar.YEAR));
}
}
return getChronology().range(f);
default -> getChronology().range(chronoField);
};
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}

@ -2557,13 +2557,13 @@ public final class DateTimeFormatterBuilder {
@Override
public String toString() {
// using ordinals to avoid javac synthetic inner class
switch (ordinal()) {
case 0: return "ParseCaseSensitive(true)";
case 1: return "ParseCaseSensitive(false)";
case 2: return "ParseStrict(true)";
case 3: return "ParseStrict(false)";
}
throw new IllegalStateException("Unreachable");
return switch (ordinal()) {
case 0 -> "ParseCaseSensitive(true)";
case 1 -> "ParseCaseSensitive(false)";
case 2 -> "ParseStrict(true)";
case 3 -> "ParseStrict(false)";
default -> throw new IllegalStateException("Unreachable");
};
}
}