Skip to content

Commit 1d16797

Browse files
committed
8268469: Update java.time to use switch expressions
Reviewed-by: lancea, naoto, dfuchs, iris, chegar
1 parent ffa34ed commit 1d16797

22 files changed

+255
-336
lines changed

src/java.base/share/classes/java/time/Duration.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -720,13 +720,13 @@ public Duration plus(long amountToAdd, TemporalUnit unit) {
720720
return this;
721721
}
722722
if (unit instanceof ChronoUnit chronoUnit) {
723-
switch (chronoUnit) {
724-
case NANOS: return plusNanos(amountToAdd);
725-
case MICROS: return plusSeconds((amountToAdd / (1000_000L * 1000)) * 1000).plusNanos((amountToAdd % (1000_000L * 1000)) * 1000);
726-
case MILLIS: return plusMillis(amountToAdd);
727-
case SECONDS: return plusSeconds(amountToAdd);
728-
}
729-
return plusSeconds(Math.multiplyExact(unit.getDuration().seconds, amountToAdd));
723+
return switch (chronoUnit) {
724+
case NANOS -> plusNanos(amountToAdd);
725+
case MICROS -> plusSeconds((amountToAdd / (1000_000L * 1000)) * 1000).plusNanos((amountToAdd % (1000_000L * 1000)) * 1000);
726+
case MILLIS -> plusMillis(amountToAdd);
727+
case SECONDS -> plusSeconds(amountToAdd);
728+
default -> plusSeconds(Math.multiplyExact(unit.getDuration().seconds, amountToAdd));
729+
};
730730
}
731731
Duration duration = unit.getDuration().multipliedBy(amountToAdd);
732732
return plusSeconds(duration.getSeconds()).plusNanos(duration.getNano());

src/java.base/share/classes/java/time/LocalDate.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -604,14 +604,13 @@ public boolean isSupported(TemporalUnit unit) {
604604
public ValueRange range(TemporalField field) {
605605
if (field instanceof ChronoField chronoField) {
606606
if (chronoField.isDateBased()) {
607-
switch (chronoField) {
608-
case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth());
609-
case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear());
610-
case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, getMonth() == Month.FEBRUARY && isLeapYear() == false ? 4 : 5);
611-
case YEAR_OF_ERA:
612-
return (getYear() <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE));
613-
}
614-
return field.range();
607+
return switch (chronoField) {
608+
case DAY_OF_MONTH -> ValueRange.of(1, lengthOfMonth());
609+
case DAY_OF_YEAR -> ValueRange.of(1, lengthOfYear());
610+
case ALIGNED_WEEK_OF_MONTH -> ValueRange.of(1, getMonth() == Month.FEBRUARY && !isLeapYear() ? 4 : 5);
611+
case YEAR_OF_ERA -> (getYear() <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE));
612+
default -> field.range();
613+
};
615614
}
616615
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
617616
}
@@ -866,17 +865,11 @@ public boolean isLeapYear() {
866865
*/
867866
@Override
868867
public int lengthOfMonth() {
869-
switch (month) {
870-
case 2:
871-
return (isLeapYear() ? 29 : 28);
872-
case 4:
873-
case 6:
874-
case 9:
875-
case 11:
876-
return 30;
877-
default:
878-
return 31;
879-
}
868+
return switch (month) {
869+
case 2 -> (isLeapYear() ? 29 : 28);
870+
case 4, 6, 9, 11 -> 30;
871+
default -> 31;
872+
};
880873
}
881874

882875
/**

src/java.base/share/classes/java/time/LocalDateTime.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,16 +1177,16 @@ public LocalDateTime plus(TemporalAmount amountToAdd) {
11771177
@Override
11781178
public LocalDateTime plus(long amountToAdd, TemporalUnit unit) {
11791179
if (unit instanceof ChronoUnit chronoUnit) {
1180-
switch (chronoUnit) {
1181-
case NANOS: return plusNanos(amountToAdd);
1182-
case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
1183-
case MILLIS: return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000);
1184-
case SECONDS: return plusSeconds(amountToAdd);
1185-
case MINUTES: return plusMinutes(amountToAdd);
1186-
case HOURS: return plusHours(amountToAdd);
1187-
case HALF_DAYS: return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12); // no overflow (256 is multiple of 2)
1188-
}
1189-
return with(date.plus(amountToAdd, unit), time);
1180+
return switch (chronoUnit) {
1181+
case NANOS -> plusNanos(amountToAdd);
1182+
case MICROS -> plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
1183+
case MILLIS -> plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000);
1184+
case SECONDS -> plusSeconds(amountToAdd);
1185+
case MINUTES -> plusMinutes(amountToAdd);
1186+
case HOURS -> plusHours(amountToAdd);
1187+
case HALF_DAYS -> plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12); // no overflow (256 is multiple of 2)
1188+
default -> with(date.plus(amountToAdd, unit), time);
1189+
};
11901190
}
11911191
return unit.addTo(this, amountToAdd);
11921192
}

src/java.base/share/classes/java/time/Month.java

Lines changed: 30 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -423,17 +423,11 @@ public Month minus(long months) {
423423
* @return the length of this month in days, from 28 to 31
424424
*/
425425
public int length(boolean leapYear) {
426-
switch (this) {
427-
case FEBRUARY:
428-
return (leapYear ? 29 : 28);
429-
case APRIL:
430-
case JUNE:
431-
case SEPTEMBER:
432-
case NOVEMBER:
433-
return 30;
434-
default:
435-
return 31;
436-
}
426+
return switch (this) {
427+
case FEBRUARY -> (leapYear ? 29 : 28);
428+
case APRIL, JUNE, SEPTEMBER, NOVEMBER -> 30;
429+
default -> 31;
430+
};
437431
}
438432

439433
/**
@@ -446,17 +440,11 @@ public int length(boolean leapYear) {
446440
* @return the minimum length of this month in days, from 28 to 31
447441
*/
448442
public int minLength() {
449-
switch (this) {
450-
case FEBRUARY:
451-
return 28;
452-
case APRIL:
453-
case JUNE:
454-
case SEPTEMBER:
455-
case NOVEMBER:
456-
return 30;
457-
default:
458-
return 31;
459-
}
443+
return switch (this) {
444+
case FEBRUARY -> 28;
445+
case APRIL, JUNE, SEPTEMBER, NOVEMBER -> 30;
446+
default -> 31;
447+
};
460448
}
461449

462450
/**
@@ -469,17 +457,11 @@ public int minLength() {
469457
* @return the maximum length of this month in days, from 29 to 31
470458
*/
471459
public int maxLength() {
472-
switch (this) {
473-
case FEBRUARY:
474-
return 29;
475-
case APRIL:
476-
case JUNE:
477-
case SEPTEMBER:
478-
case NOVEMBER:
479-
return 30;
480-
default:
481-
return 31;
482-
}
460+
return switch (this) {
461+
case FEBRUARY -> 29;
462+
case APRIL, JUNE, SEPTEMBER, NOVEMBER -> 30;
463+
default -> 31;
464+
};
483465
}
484466

485467
//-----------------------------------------------------------------------
@@ -494,33 +476,21 @@ public int maxLength() {
494476
*/
495477
public int firstDayOfYear(boolean leapYear) {
496478
int leap = leapYear ? 1 : 0;
497-
switch (this) {
498-
case JANUARY:
499-
return 1;
500-
case FEBRUARY:
501-
return 32;
502-
case MARCH:
503-
return 60 + leap;
504-
case APRIL:
505-
return 91 + leap;
506-
case MAY:
507-
return 121 + leap;
508-
case JUNE:
509-
return 152 + leap;
510-
case JULY:
511-
return 182 + leap;
512-
case AUGUST:
513-
return 213 + leap;
514-
case SEPTEMBER:
515-
return 244 + leap;
516-
case OCTOBER:
517-
return 274 + leap;
518-
case NOVEMBER:
519-
return 305 + leap;
520-
case DECEMBER:
521-
default:
522-
return 335 + leap;
523-
}
479+
return switch (this) {
480+
case JANUARY -> 1;
481+
case FEBRUARY -> 32;
482+
case MARCH -> 60 + leap;
483+
case APRIL -> 91 + leap;
484+
case MAY -> 121 + leap;
485+
case JUNE -> 152 + leap;
486+
case JULY -> 182 + leap;
487+
case AUGUST -> 213 + leap;
488+
case SEPTEMBER -> 244 + leap;
489+
case OCTOBER -> 274 + leap;
490+
case NOVEMBER -> 305 + leap;
491+
// otherwise (DECEMBER)
492+
default -> 335 + leap;
493+
};
524494
}
525495

526496
/**

src/java.base/share/classes/java/time/OffsetDateTime.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -597,13 +597,12 @@ public ValueRange range(TemporalField field) {
597597
@Override
598598
public int get(TemporalField field) {
599599
if (field instanceof ChronoField chronoField) {
600-
switch (chronoField) {
601-
case INSTANT_SECONDS:
602-
throw new UnsupportedTemporalTypeException("Invalid field 'InstantSeconds' for get() method, use getLong() instead");
603-
case OFFSET_SECONDS:
604-
return getOffset().getTotalSeconds();
605-
}
606-
return dateTime.get(field);
600+
return switch (chronoField) {
601+
case INSTANT_SECONDS -> throw new UnsupportedTemporalTypeException("Invalid field " +
602+
"'InstantSeconds' for get() method, use getLong() instead");
603+
case OFFSET_SECONDS -> getOffset().getTotalSeconds();
604+
default -> dateTime.get(field);
605+
};
607606
}
608607
return Temporal.super.get(field);
609608
}
@@ -634,11 +633,11 @@ public int get(TemporalField field) {
634633
@Override
635634
public long getLong(TemporalField field) {
636635
if (field instanceof ChronoField chronoField) {
637-
switch (chronoField) {
638-
case INSTANT_SECONDS: return toEpochSecond();
639-
case OFFSET_SECONDS: return getOffset().getTotalSeconds();
640-
}
641-
return dateTime.getLong(field);
636+
return switch (chronoField) {
637+
case INSTANT_SECONDS -> toEpochSecond();
638+
case OFFSET_SECONDS -> getOffset().getTotalSeconds();
639+
default -> dateTime.getLong(field);
640+
};
642641
}
643642
return field.getFrom(this);
644643
}

src/java.base/share/classes/java/time/Ser.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -270,24 +270,23 @@ static Serializable read(ObjectInput in) throws IOException, ClassNotFoundExcept
270270

271271
private static Serializable readInternal(byte type, ObjectInput in)
272272
throws IOException, ClassNotFoundException {
273-
switch (type) {
274-
case DURATION_TYPE: return Duration.readExternal(in);
275-
case INSTANT_TYPE: return Instant.readExternal(in);
276-
case LOCAL_DATE_TYPE: return LocalDate.readExternal(in);
277-
case LOCAL_DATE_TIME_TYPE: return LocalDateTime.readExternal(in);
278-
case LOCAL_TIME_TYPE: return LocalTime.readExternal(in);
279-
case ZONE_DATE_TIME_TYPE: return ZonedDateTime.readExternal(in);
280-
case ZONE_OFFSET_TYPE: return ZoneOffset.readExternal(in);
281-
case ZONE_REGION_TYPE: return ZoneRegion.readExternal(in);
282-
case OFFSET_TIME_TYPE: return OffsetTime.readExternal(in);
283-
case OFFSET_DATE_TIME_TYPE: return OffsetDateTime.readExternal(in);
284-
case YEAR_TYPE: return Year.readExternal(in);
285-
case YEAR_MONTH_TYPE: return YearMonth.readExternal(in);
286-
case MONTH_DAY_TYPE: return MonthDay.readExternal(in);
287-
case PERIOD_TYPE: return Period.readExternal(in);
288-
default:
289-
throw new StreamCorruptedException("Unknown serialized type");
290-
}
273+
return switch (type) {
274+
case DURATION_TYPE -> Duration.readExternal(in);
275+
case INSTANT_TYPE -> Instant.readExternal(in);
276+
case LOCAL_DATE_TYPE -> LocalDate.readExternal(in);
277+
case LOCAL_DATE_TIME_TYPE -> LocalDateTime.readExternal(in);
278+
case LOCAL_TIME_TYPE -> LocalTime.readExternal(in);
279+
case ZONE_DATE_TIME_TYPE -> ZonedDateTime.readExternal(in);
280+
case ZONE_OFFSET_TYPE -> ZoneOffset.readExternal(in);
281+
case ZONE_REGION_TYPE -> ZoneRegion.readExternal(in);
282+
case OFFSET_TIME_TYPE -> OffsetTime.readExternal(in);
283+
case OFFSET_DATE_TIME_TYPE -> OffsetDateTime.readExternal(in);
284+
case YEAR_TYPE -> Year.readExternal(in);
285+
case YEAR_MONTH_TYPE -> YearMonth.readExternal(in);
286+
case MONTH_DAY_TYPE -> MonthDay.readExternal(in);
287+
case PERIOD_TYPE -> Period.readExternal(in);
288+
default -> throw new StreamCorruptedException("Unknown serialized type");
289+
};
291290
}
292291

293292
/**

src/java.base/share/classes/java/time/ZonedDateTime.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -814,13 +814,12 @@ public ValueRange range(TemporalField field) {
814814
@Override // override for Javadoc and performance
815815
public int get(TemporalField field) {
816816
if (field instanceof ChronoField chronoField) {
817-
switch (chronoField) {
818-
case INSTANT_SECONDS:
819-
throw new UnsupportedTemporalTypeException("Invalid field 'InstantSeconds' for get() method, use getLong() instead");
820-
case OFFSET_SECONDS:
821-
return getOffset().getTotalSeconds();
822-
}
823-
return dateTime.get(field);
817+
return switch (chronoField) {
818+
case INSTANT_SECONDS -> throw new UnsupportedTemporalTypeException("Invalid field " +
819+
"'InstantSeconds' for get() method, use getLong() instead");
820+
case OFFSET_SECONDS -> getOffset().getTotalSeconds();
821+
default -> dateTime.get(field);
822+
};
824823
}
825824
return ChronoZonedDateTime.super.get(field);
826825
}
@@ -851,11 +850,11 @@ public int get(TemporalField field) {
851850
@Override
852851
public long getLong(TemporalField field) {
853852
if (field instanceof ChronoField chronoField) {
854-
switch (chronoField) {
855-
case INSTANT_SECONDS: return toEpochSecond();
856-
case OFFSET_SECONDS: return getOffset().getTotalSeconds();
857-
}
858-
return dateTime.getLong(field);
853+
return switch (chronoField) {
854+
case INSTANT_SECONDS -> toEpochSecond();
855+
case OFFSET_SECONDS -> getOffset().getTotalSeconds();
856+
default -> dateTime.getLong(field);
857+
};
859858
}
860859
return field.getFrom(this);
861860
}

src/java.base/share/classes/java/time/chrono/ChronoLocalDateTimeImpl.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,16 @@ public ChronoLocalDateTimeImpl<D> with(TemporalField field, long newValue) {
299299
@Override
300300
public ChronoLocalDateTimeImpl<D> plus(long amountToAdd, TemporalUnit unit) {
301301
if (unit instanceof ChronoUnit chronoUnit) {
302-
switch (chronoUnit) {
303-
case NANOS: return plusNanos(amountToAdd);
304-
case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
305-
case MILLIS: return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000000);
306-
case SECONDS: return plusSeconds(amountToAdd);
307-
case MINUTES: return plusMinutes(amountToAdd);
308-
case HOURS: return plusHours(amountToAdd);
309-
case HALF_DAYS: return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12); // no overflow (256 is multiple of 2)
310-
}
311-
return with(date.plus(amountToAdd, unit), time);
302+
return switch (chronoUnit) {
303+
case NANOS -> plusNanos(amountToAdd);
304+
case MICROS -> plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
305+
case MILLIS -> plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000000);
306+
case SECONDS -> plusSeconds(amountToAdd);
307+
case MINUTES -> plusMinutes(amountToAdd);
308+
case HOURS -> plusHours(amountToAdd);
309+
case HALF_DAYS -> plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12); // no overflow (256 is multiple of 2)
310+
default -> with(date.plus(amountToAdd, unit), time);
311+
};
312312
}
313313
return ChronoLocalDateTimeImpl.ensureValid(date.getChronology(), unit.addTo(this, amountToAdd));
314314
}

src/java.base/share/classes/java/time/chrono/ChronoZonedDateTime.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ default int get(TemporalField field) {
210210
@Override
211211
default long getLong(TemporalField field) {
212212
if (field instanceof ChronoField chronoField) {
213-
switch (chronoField) {
214-
case INSTANT_SECONDS: return toEpochSecond();
215-
case OFFSET_SECONDS: return getOffset().getTotalSeconds();
216-
}
217-
return toLocalDateTime().getLong(field);
213+
return switch (chronoField) {
214+
case INSTANT_SECONDS -> toEpochSecond();
215+
case OFFSET_SECONDS -> getOffset().getTotalSeconds();
216+
default -> toLocalDateTime().getLong(field);
217+
};
218218
}
219219
return field.getFrom(this);
220220
}

0 commit comments

Comments
 (0)