8268469: Update java.time to use switch expressions

Reviewed-by: lancea, naoto, dfuchs, iris, chegar
This commit is contained in:
Patrick Concannon 2021-06-25 15:42:38 +00:00
parent ffa34ed429
commit 1d167978e5
22 changed files with 256 additions and 337 deletions

View file

@ -720,13 +720,13 @@ public final class Duration
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());

View file

@ -604,14 +604,13 @@ public final class LocalDate
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 final class LocalDate
*/
@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;
};
}
/**

View file

@ -1177,16 +1177,16 @@ public final class LocalDateTime
@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);
}

View file

@ -423,17 +423,11 @@ public enum Month implements TemporalAccessor, TemporalAdjuster {
* @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 enum Month implements TemporalAccessor, TemporalAdjuster {
* @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 enum Month implements TemporalAccessor, TemporalAdjuster {
* @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 enum Month implements TemporalAccessor, TemporalAdjuster {
*/
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;
};
}
/**

View file

@ -597,13 +597,12 @@ public final class OffsetDateTime
@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 final class OffsetDateTime
@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);
}

View file

@ -270,24 +270,23 @@ final class Ser implements Externalizable {
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");
};
}
/**

View file

@ -814,13 +814,12 @@ public final class ZonedDateTime
@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 final class ZonedDateTime
@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);
}

View file

@ -299,16 +299,16 @@ final class ChronoLocalDateTimeImpl<D extends ChronoLocalDate>
@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));
}

View file

@ -210,11 +210,11 @@ public interface ChronoZonedDateTime<D extends ChronoLocalDate>
@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);
}

View file

@ -536,21 +536,14 @@ public final class HijrahChronology extends AbstractChronology implements Serial
checkCalendarInit();
if (field instanceof ChronoField) {
ChronoField f = field;
switch (f) {
case DAY_OF_MONTH:
return ValueRange.of(1, 1, getMinimumMonthLength(), getMaximumMonthLength());
case DAY_OF_YEAR:
return ValueRange.of(1, getMaximumDayOfYear());
case ALIGNED_WEEK_OF_MONTH:
return ValueRange.of(1, 5);
case YEAR:
case YEAR_OF_ERA:
return ValueRange.of(getMinimumYear(), getMaximumYear());
case ERA:
return ValueRange.of(1, 1);
default:
return field.range();
}
return switch (f) {
case DAY_OF_MONTH -> ValueRange.of(1, 1, getMinimumMonthLength(), getMaximumMonthLength());
case DAY_OF_YEAR -> ValueRange.of(1, getMaximumDayOfYear());
case ALIGNED_WEEK_OF_MONTH -> ValueRange.of(1, 5);
case YEAR, YEAR_OF_ERA -> ValueRange.of(getMinimumYear(), getMaximumYear());
case ERA -> ValueRange.of(1, 1);
default -> field.range();
};
}
return field.range();
}

View file

@ -351,14 +351,14 @@ public final class HijrahDate
if (field instanceof 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 ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, 5); // TODO
return switch (f) {
case DAY_OF_MONTH -> ValueRange.of(1, lengthOfMonth());
case DAY_OF_YEAR -> ValueRange.of(1, lengthOfYear());
case ALIGNED_WEEK_OF_MONTH -> ValueRange.of(1, 5); // TODO
// TODO does the limited range of valid years cause years to
// start/end part way through? that would affect range
}
return getChronology().range(f);
default -> getChronology().range(f);
};
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
@ -368,22 +368,22 @@ public final class HijrahDate
@Override
public long getLong(TemporalField field) {
if (field instanceof ChronoField) {
switch ((ChronoField) field) {
case DAY_OF_WEEK: return getDayOfWeek();
case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((dayOfMonth - 1) % 7) + 1;
case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1;
case DAY_OF_MONTH: return this.dayOfMonth;
case DAY_OF_YEAR: return this.getDayOfYear();
case EPOCH_DAY: return toEpochDay();
case ALIGNED_WEEK_OF_MONTH: return ((dayOfMonth - 1) / 7) + 1;
case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1;
case MONTH_OF_YEAR: return monthOfYear;
case PROLEPTIC_MONTH: return getProlepticMonth();
case YEAR_OF_ERA: return prolepticYear;
case YEAR: return prolepticYear;
case ERA: return getEraValue();
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
return switch ((ChronoField) field) {
case DAY_OF_WEEK -> getDayOfWeek();
case ALIGNED_DAY_OF_WEEK_IN_MONTH -> ((dayOfMonth - 1) % 7) + 1;
case ALIGNED_DAY_OF_WEEK_IN_YEAR -> ((getDayOfYear() - 1) % 7) + 1;
case DAY_OF_MONTH -> this.dayOfMonth;
case DAY_OF_YEAR -> this.getDayOfYear();
case EPOCH_DAY -> toEpochDay();
case ALIGNED_WEEK_OF_MONTH -> ((dayOfMonth - 1) / 7) + 1;
case ALIGNED_WEEK_OF_YEAR -> ((getDayOfYear() - 1) / 7) + 1;
case MONTH_OF_YEAR -> monthOfYear;
case PROLEPTIC_MONTH -> getProlepticMonth();
case YEAR_OF_ERA -> prolepticYear;
case YEAR -> prolepticYear;
case ERA -> getEraValue();
default -> throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
};
}
return field.getFrom(this);
}
@ -398,22 +398,22 @@ public final class HijrahDate
// not using checkValidIntValue so EPOCH_DAY and PROLEPTIC_MONTH work
chrono.range(chronoField).checkValidValue(newValue, chronoField); // TODO: validate value
int nvalue = (int) newValue;
switch (chronoField) {
case DAY_OF_WEEK: return plusDays(newValue - getDayOfWeek());
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 resolvePreviousValid(prolepticYear, monthOfYear, nvalue);
case DAY_OF_YEAR: return plusDays(Math.min(nvalue, lengthOfYear()) - getDayOfYear());
case EPOCH_DAY: return new HijrahDate(chrono, newValue);
case ALIGNED_WEEK_OF_MONTH: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_MONTH)) * 7);
case ALIGNED_WEEK_OF_YEAR: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_YEAR)) * 7);
case MONTH_OF_YEAR: return resolvePreviousValid(prolepticYear, nvalue, dayOfMonth);
case PROLEPTIC_MONTH: return plusMonths(newValue - getProlepticMonth());
case YEAR_OF_ERA: return resolvePreviousValid(prolepticYear >= 1 ? nvalue : 1 - nvalue, monthOfYear, dayOfMonth);
case YEAR: return resolvePreviousValid(nvalue, monthOfYear, dayOfMonth);
case ERA: return resolvePreviousValid(1 - prolepticYear, monthOfYear, dayOfMonth);
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
return switch (chronoField) {
case DAY_OF_WEEK -> plusDays(newValue - getDayOfWeek());
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 -> resolvePreviousValid(prolepticYear, monthOfYear, nvalue);
case DAY_OF_YEAR -> plusDays(Math.min(nvalue, lengthOfYear()) - getDayOfYear());
case EPOCH_DAY -> new HijrahDate(chrono, newValue);
case ALIGNED_WEEK_OF_MONTH -> plusDays((newValue - getLong(ALIGNED_WEEK_OF_MONTH)) * 7);
case ALIGNED_WEEK_OF_YEAR -> plusDays((newValue - getLong(ALIGNED_WEEK_OF_YEAR)) * 7);
case MONTH_OF_YEAR -> resolvePreviousValid(prolepticYear, nvalue, dayOfMonth);
case PROLEPTIC_MONTH -> plusMonths(newValue - getProlepticMonth());
case YEAR_OF_ERA -> resolvePreviousValid(prolepticYear >= 1 ? nvalue : 1 - nvalue, monthOfYear, dayOfMonth);
case YEAR -> resolvePreviousValid(nvalue, monthOfYear, dayOfMonth);
case ERA -> resolvePreviousValid(1 - prolepticYear, monthOfYear, dayOfMonth);
default -> throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
};
}
return super.with(field, newValue);
}

View file

@ -128,14 +128,11 @@ public enum IsoEra implements Era {
* @throws DateTimeException if the value is invalid
*/
public static IsoEra of(int isoEra) {
switch (isoEra) {
case 0:
return BCE;
case 1:
return CE;
default:
throw new DateTimeException("Invalid era: " + isoEra);
}
return switch (isoEra) {
case 0 -> BCE;
case 1 -> CE;
default -> throw new DateTimeException("Invalid era: " + isoEra);
};
}
//-----------------------------------------------------------------------

View file

@ -312,21 +312,21 @@ public final class MinguoChronology extends AbstractChronology implements Serial
//-----------------------------------------------------------------------
@Override
public ValueRange range(ChronoField field) {
switch (field) {
case PROLEPTIC_MONTH: {
return switch (field) {
case PROLEPTIC_MONTH -> {
ValueRange range = PROLEPTIC_MONTH.range();
return ValueRange.of(range.getMinimum() - YEARS_DIFFERENCE * 12L, range.getMaximum() - YEARS_DIFFERENCE * 12L);
yield ValueRange.of(range.getMinimum() - YEARS_DIFFERENCE * 12L, range.getMaximum() - YEARS_DIFFERENCE * 12L);
}
case YEAR_OF_ERA: {
case YEAR_OF_ERA -> {
ValueRange range = YEAR.range();
return ValueRange.of(1, range.getMaximum() - YEARS_DIFFERENCE, -range.getMinimum() + 1 + YEARS_DIFFERENCE);
yield ValueRange.of(1, range.getMaximum() - YEARS_DIFFERENCE, -range.getMinimum() + 1 + YEARS_DIFFERENCE);
}
case YEAR: {
case YEAR -> {
ValueRange range = YEAR.range();
return ValueRange.of(range.getMinimum() - YEARS_DIFFERENCE, range.getMaximum() - YEARS_DIFFERENCE);
yield ValueRange.of(range.getMinimum() - YEARS_DIFFERENCE, range.getMaximum() - YEARS_DIFFERENCE);
}
}
return field.range();
default -> field.range();
};
}
//-----------------------------------------------------------------------

View file

@ -315,25 +315,23 @@ public final class MinguoDate
if (getLong(chronoField) == newValue) {
return this;
}
switch (chronoField) {
case PROLEPTIC_MONTH:
return switch (chronoField) {
case PROLEPTIC_MONTH -> {
getChronology().range(chronoField).checkValidValue(newValue, chronoField);
return plusMonths(newValue - getProlepticMonth());
case YEAR_OF_ERA:
case YEAR:
case ERA: {
int nvalue = getChronology().range(chronoField).checkValidIntValue(newValue, chronoField);
switch (chronoField) {
case YEAR_OF_ERA:
return with(isoDate.withYear(getProlepticYear() >= 1 ? nvalue + YEARS_DIFFERENCE : (1 - nvalue) + YEARS_DIFFERENCE));
case YEAR:
return with(isoDate.withYear(nvalue + YEARS_DIFFERENCE));
case ERA:
return with(isoDate.withYear((1 - getProlepticYear()) + YEARS_DIFFERENCE));
}
yield plusMonths(newValue - getProlepticMonth());
}
}
return with(isoDate.with(field, newValue));
case YEAR_OF_ERA -> {
int nvalue = getChronology().range(chronoField).checkValidIntValue(newValue, chronoField);
yield with(isoDate.withYear(getProlepticYear() >= 1 ? nvalue + YEARS_DIFFERENCE : (1 - nvalue) + YEARS_DIFFERENCE));
}
case YEAR -> {
int nvalue = getChronology().range(chronoField).checkValidIntValue(newValue, chronoField);
yield with(isoDate.withYear(nvalue + YEARS_DIFFERENCE));
}
case ERA -> with(isoDate.withYear((1 - getProlepticYear()) + YEARS_DIFFERENCE));
default -> with(isoDate.with(field, newValue));
};
}
return super.with(field, newValue);
}

View file

@ -135,14 +135,11 @@ public enum MinguoEra implements Era {
* @throws DateTimeException if the value is invalid
*/
public static MinguoEra of(int minguoEra) {
switch (minguoEra) {
case 0:
return BEFORE_ROC;
case 1:
return ROC;
default:
throw new DateTimeException("Invalid era: " + minguoEra);
}
return switch (minguoEra) {
case 0 -> BEFORE_ROC;
case 1 -> ROC;
default -> throw new DateTimeException("Invalid era: " + minguoEra);
};
}
//-----------------------------------------------------------------------

View file

@ -245,18 +245,18 @@ final class Ser implements Externalizable {
private static Serializable readInternal(byte type, ObjectInput in)
throws IOException, ClassNotFoundException {
switch (type) {
case CHRONO_TYPE: return (Serializable)AbstractChronology.readExternal(in);
case CHRONO_LOCAL_DATE_TIME_TYPE: return (Serializable)ChronoLocalDateTimeImpl.readExternal(in);
case CHRONO_ZONE_DATE_TIME_TYPE: return (Serializable)ChronoZonedDateTimeImpl.readExternal(in);
case JAPANESE_DATE_TYPE: return JapaneseDate.readExternal(in);
case JAPANESE_ERA_TYPE: return JapaneseEra.readExternal(in);
case HIJRAH_DATE_TYPE: return HijrahDate.readExternal(in);
case MINGUO_DATE_TYPE: return MinguoDate.readExternal(in);
case THAIBUDDHIST_DATE_TYPE: return ThaiBuddhistDate.readExternal(in);
case CHRONO_PERIOD_TYPE: return ChronoPeriodImpl.readExternal(in);
default: throw new StreamCorruptedException("Unknown serialized type");
}
return switch (type) {
case CHRONO_TYPE -> (Serializable) AbstractChronology.readExternal(in);
case CHRONO_LOCAL_DATE_TIME_TYPE -> (Serializable) ChronoLocalDateTimeImpl.readExternal(in);
case CHRONO_ZONE_DATE_TIME_TYPE -> (Serializable) ChronoZonedDateTimeImpl.readExternal(in);
case JAPANESE_DATE_TYPE -> JapaneseDate.readExternal(in);
case JAPANESE_ERA_TYPE -> JapaneseEra.readExternal(in);
case HIJRAH_DATE_TYPE -> HijrahDate.readExternal(in);
case MINGUO_DATE_TYPE -> MinguoDate.readExternal(in);
case THAIBUDDHIST_DATE_TYPE -> ThaiBuddhistDate.readExternal(in);
case CHRONO_PERIOD_TYPE -> ChronoPeriodImpl.readExternal(in);
default -> throw new StreamCorruptedException("Unknown serialized type");
};
}
/**

View file

@ -348,21 +348,21 @@ public final class ThaiBuddhistChronology extends AbstractChronology implements
//-----------------------------------------------------------------------
@Override
public ValueRange range(ChronoField field) {
switch (field) {
case PROLEPTIC_MONTH: {
return switch (field) {
case PROLEPTIC_MONTH -> {
ValueRange range = PROLEPTIC_MONTH.range();
return ValueRange.of(range.getMinimum() + YEARS_DIFFERENCE * 12L, range.getMaximum() + YEARS_DIFFERENCE * 12L);
yield ValueRange.of(range.getMinimum() + YEARS_DIFFERENCE * 12L, range.getMaximum() + YEARS_DIFFERENCE * 12L);
}
case YEAR_OF_ERA: {
case YEAR_OF_ERA -> {
ValueRange range = YEAR.range();
return ValueRange.of(1, -(range.getMinimum() + YEARS_DIFFERENCE) + 1, range.getMaximum() + YEARS_DIFFERENCE);
yield ValueRange.of(1, -(range.getMinimum() + YEARS_DIFFERENCE) + 1, range.getMaximum() + YEARS_DIFFERENCE);
}
case YEAR: {
case YEAR -> {
ValueRange range = YEAR.range();
return ValueRange.of(range.getMinimum() + YEARS_DIFFERENCE, range.getMaximum() + YEARS_DIFFERENCE);
yield ValueRange.of(range.getMinimum() + YEARS_DIFFERENCE, range.getMaximum() + YEARS_DIFFERENCE);
}
}
return field.range();
default -> field.range();
};
}
//-----------------------------------------------------------------------

View file

@ -135,14 +135,11 @@ public enum ThaiBuddhistEra implements Era {
* @throws DateTimeException if the era is invalid
*/
public static ThaiBuddhistEra of(int thaiBuddhistEra) {
switch (thaiBuddhistEra) {
case 0:
return BEFORE_BE;
case 1:
return BE;
default:
throw new DateTimeException("Invalid era: " + thaiBuddhistEra);
}
return switch (thaiBuddhistEra) {
case 0 -> BEFORE_BE;
case 1 -> BE;
default -> throw new DateTimeException("Invalid era: " + thaiBuddhistEra);
};
}
//-----------------------------------------------------------------------

View file

@ -2546,10 +2546,10 @@ public final class DateTimeFormatterBuilder {
public int parse(DateTimeParseContext context, CharSequence text, int position) {
// using ordinals to avoid javac synthetic inner class
switch (ordinal()) {
case 0: context.setCaseSensitive(true); break;
case 1: context.setCaseSensitive(false); break;
case 2: context.setStrict(true); break;
case 3: context.setStrict(false); break;
case 0 -> context.setCaseSensitive(true);
case 1 -> context.setCaseSensitive(false);
case 2 -> context.setStrict(true);
case 3 -> context.setStrict(false);
}
return position;
}
@ -2787,15 +2787,10 @@ public final class DateTimeFormatterBuilder {
}
} else {
switch (signStyle) {
case NORMAL:
case EXCEEDS_PAD:
case ALWAYS:
buf.append(decimalStyle.getNegativeSign());
break;
case NOT_NEGATIVE:
throw new DateTimeException("Field " + field +
" cannot be printed as the value " + value +
" cannot be negative according to the SignStyle");
case NORMAL, EXCEEDS_PAD, ALWAYS -> buf.append(decimalStyle.getNegativeSign());
case NOT_NEGATIVE -> throw new DateTimeException("Field " + field +
" cannot be printed as the value " + value +
" cannot be negative according to the SignStyle");
}
}
for (int i = 0; i < minWidth - str.length(); i++) {
@ -4128,13 +4123,11 @@ public final class DateTimeFormatterBuilder {
perLocale.put(locale, names);
cache.put(id, new SoftReference<>(perLocale));
}
switch (type) {
case STD:
return names[textStyle.zoneNameStyleIndex() + 1];
case DST:
return names[textStyle.zoneNameStyleIndex() + 3];
}
return names[textStyle.zoneNameStyleIndex() + 5];
return switch (type) {
case STD -> names[textStyle.zoneNameStyleIndex() + 1];
case DST -> names[textStyle.zoneNameStyleIndex() + 3];
default -> names[textStyle.zoneNameStyleIndex() + 5];
};
}
@Override

View file

@ -123,17 +123,11 @@ public enum SignStyle {
* @return
*/
boolean parse(boolean positive, boolean strict, boolean fixedWidth) {
switch (ordinal()) {
case 0: // NORMAL
// valid if negative or (positive and lenient)
return !positive || !strict;
case 1: // ALWAYS
case 4: // EXCEEDS_PAD
return true;
default:
// valid if lenient and not fixed width
return !strict && !fixedWidth;
}
return switch (ordinal()) {
case 0 -> !positive || !strict; // NORMAL - valid if negative or (positive and lenient)
case 1, 4 -> true; // ALWAYS, EXCEEDS_PAD
default -> !strict && !fixedWidth; // valid if lenient and not fixed width
};
}
}

View file

@ -703,16 +703,13 @@ public final class IsoFields {
@SuppressWarnings("unchecked")
@Override
public <R extends Temporal> R addTo(R temporal, long amount) {
switch (this) {
case WEEK_BASED_YEARS:
return (R) temporal.with(WEEK_BASED_YEAR,
Math.addExact(temporal.get(WEEK_BASED_YEAR), amount));
case QUARTER_YEARS:
return (R) temporal.plus(amount / 4, YEARS)
.plus((amount % 4) * 3, MONTHS);
default:
throw new IllegalStateException("Unreachable");
}
return switch (this) {
case WEEK_BASED_YEARS -> (R) temporal.with(WEEK_BASED_YEAR,
Math.addExact(temporal.get(WEEK_BASED_YEAR), amount));
case QUARTER_YEARS -> (R) temporal.plus(amount / 4, YEARS)
.plus((amount % 4) * 3, MONTHS);
default -> throw new IllegalStateException("Unreachable");
};
}
@Override
@ -720,15 +717,12 @@ public final class IsoFields {
if (temporal1Inclusive.getClass() != temporal2Exclusive.getClass()) {
return temporal1Inclusive.until(temporal2Exclusive, this);
}
switch(this) {
case WEEK_BASED_YEARS:
return Math.subtractExact(temporal2Exclusive.getLong(WEEK_BASED_YEAR),
temporal1Inclusive.getLong(WEEK_BASED_YEAR));
case QUARTER_YEARS:
return temporal1Inclusive.until(temporal2Exclusive, MONTHS) / 3;
default:
throw new IllegalStateException("Unreachable");
}
return switch (this) {
case WEEK_BASED_YEARS -> Math.subtractExact(temporal2Exclusive.getLong(WEEK_BASED_YEAR),
temporal1Inclusive.getLong(WEEK_BASED_YEAR));
case QUARTER_YEARS -> temporal1Inclusive.until(temporal2Exclusive, MONTHS) / 3;
default -> throw new IllegalStateException("Unreachable");
};
}
@Override

View file

@ -191,16 +191,12 @@ final class Ser implements Externalizable {
private static Serializable readInternal(byte type, DataInput in)
throws IOException, ClassNotFoundException {
switch (type) {
case ZRULES:
return ZoneRules.readExternal(in);
case ZOT:
return ZoneOffsetTransition.readExternal(in);
case ZOTRULE:
return ZoneOffsetTransitionRule.readExternal(in);
default:
throw new StreamCorruptedException("Unknown serialized type");
}
return switch (type) {
case ZRULES -> ZoneRules.readExternal(in);
case ZOT -> ZoneOffsetTransition.readExternal(in);
case ZOTRULE -> ZoneOffsetTransitionRule.readExternal(in);
default -> throw new StreamCorruptedException("Unknown serialized type");
};
}
/**