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

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