mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8269124: Update java.time to use switch expressions (part II)
Reviewed-by: dfuchs, vtewari, aefimov, iris, lancea, naoto
This commit is contained in:
parent
675a9520b2
commit
8a7b380ebb
16 changed files with 322 additions and 370 deletions
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue