Skip to content

Commit

Permalink
8269124: Update java.time to use switch expressions (part II)
Browse files Browse the repository at this point in the history
Reviewed-by: dfuchs, vtewari, aefimov, iris, lancea, naoto
  • Loading branch information
pconcannon committed Jul 5, 2021
1 parent 675a952 commit 8a7b380
Show file tree
Hide file tree
Showing 16 changed files with 322 additions and 370 deletions.
94 changes: 47 additions & 47 deletions src/java.base/share/classes/java/time/Instant.java
Expand Up @@ -558,13 +558,13 @@ public ValueRange range(TemporalField field) {
*/
@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);
}
Expand Down Expand Up @@ -594,14 +594,14 @@ public int get(TemporalField field) {
*/
@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);
}
Expand Down Expand Up @@ -705,19 +705,19 @@ public Instant with(TemporalAdjuster adjuster) {
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);
}
Expand Down Expand Up @@ -848,18 +848,18 @@ public Instant plus(TemporalAmount amountToAdd) {
*/
@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);
}
Expand Down Expand Up @@ -1143,17 +1143,17 @@ public Temporal adjustInto(Temporal temporal) {
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);
}
Expand Down
138 changes: 62 additions & 76 deletions src/java.base/share/classes/java/time/LocalDate.java
Expand Up @@ -442,18 +442,11 @@ public static LocalDate parse(CharSequence text, DateTimeFormatter formatter) {
*/
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");
Expand All @@ -475,15 +468,8 @@ private static LocalDate create(int year, int month, int dayOfMonth) {
*/
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);
}
Expand Down Expand Up @@ -690,22 +676,22 @@ public long getLong(TemporalField field) {
}

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() {
Expand Down Expand Up @@ -1039,22 +1025,22 @@ public LocalDate with(TemporalAdjuster adjuster) {
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);
}
Expand Down Expand Up @@ -1250,17 +1236,17 @@ public LocalDate plus(TemporalAmount amountToAdd) {
@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);
}
Expand Down Expand Up @@ -1632,18 +1618,18 @@ public Temporal adjustInto(Temporal temporal) {
@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);
}
Expand Down

1 comment on commit 8a7b380

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.