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
|
@ -683,24 +683,24 @@ public final class LocalTime
|
|||
}
|
||||
|
||||
private int get0(TemporalField field) {
|
||||
switch ((ChronoField) field) {
|
||||
case NANO_OF_SECOND: return nano;
|
||||
case NANO_OF_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'NanoOfDay' for get() method, use getLong() instead");
|
||||
case MICRO_OF_SECOND: return nano / 1000;
|
||||
case MICRO_OF_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'MicroOfDay' for get() method, use getLong() instead");
|
||||
case MILLI_OF_SECOND: return nano / 1000_000;
|
||||
case MILLI_OF_DAY: return (int) (toNanoOfDay() / 1000_000);
|
||||
case SECOND_OF_MINUTE: return second;
|
||||
case SECOND_OF_DAY: return toSecondOfDay();
|
||||
case MINUTE_OF_HOUR: return minute;
|
||||
case MINUTE_OF_DAY: return hour * 60 + minute;
|
||||
case HOUR_OF_AMPM: return hour % 12;
|
||||
case CLOCK_HOUR_OF_AMPM: int ham = hour % 12; return (ham % 12 == 0 ? 12 : ham);
|
||||
case HOUR_OF_DAY: return hour;
|
||||
case CLOCK_HOUR_OF_DAY: return (hour == 0 ? 24 : hour);
|
||||
case AMPM_OF_DAY: return hour / 12;
|
||||
}
|
||||
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
|
||||
return switch ((ChronoField) field) {
|
||||
case NANO_OF_SECOND -> nano;
|
||||
case NANO_OF_DAY -> throw new UnsupportedTemporalTypeException("Invalid field 'NanoOfDay' for get() method, use getLong() instead");
|
||||
case MICRO_OF_SECOND -> nano / 1000;
|
||||
case MICRO_OF_DAY -> throw new UnsupportedTemporalTypeException("Invalid field 'MicroOfDay' for get() method, use getLong() instead");
|
||||
case MILLI_OF_SECOND -> nano / 1000_000;
|
||||
case MILLI_OF_DAY -> (int) (toNanoOfDay() / 1000_000);
|
||||
case SECOND_OF_MINUTE -> second;
|
||||
case SECOND_OF_DAY -> toSecondOfDay();
|
||||
case MINUTE_OF_HOUR -> minute;
|
||||
case MINUTE_OF_DAY -> hour * 60 + minute;
|
||||
case HOUR_OF_AMPM -> hour % 12;
|
||||
case CLOCK_HOUR_OF_AMPM -> { int ham = hour % 12; yield ham % 12 == 0 ? 12 : ham; }
|
||||
case HOUR_OF_DAY -> hour;
|
||||
case CLOCK_HOUR_OF_DAY -> (hour == 0 ? 24 : hour);
|
||||
case AMPM_OF_DAY -> hour / 12;
|
||||
default -> throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
|
||||
};
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
@ -857,24 +857,24 @@ public final class LocalTime
|
|||
public LocalTime with(TemporalField field, long newValue) {
|
||||
if (field instanceof ChronoField chronoField) {
|
||||
chronoField.checkValidValue(newValue);
|
||||
switch (chronoField) {
|
||||
case NANO_OF_SECOND: return withNano((int) newValue);
|
||||
case NANO_OF_DAY: return LocalTime.ofNanoOfDay(newValue);
|
||||
case MICRO_OF_SECOND: return withNano((int) newValue * 1000);
|
||||
case MICRO_OF_DAY: return LocalTime.ofNanoOfDay(newValue * 1000);
|
||||
case MILLI_OF_SECOND: return withNano((int) newValue * 1000_000);
|
||||
case MILLI_OF_DAY: return LocalTime.ofNanoOfDay(newValue * 1000_000);
|
||||
case SECOND_OF_MINUTE: return withSecond((int) newValue);
|
||||
case SECOND_OF_DAY: return plusSeconds(newValue - toSecondOfDay());
|
||||
case MINUTE_OF_HOUR: return withMinute((int) newValue);
|
||||
case MINUTE_OF_DAY: return plusMinutes(newValue - (hour * 60 + minute));
|
||||
case HOUR_OF_AMPM: return plusHours(newValue - (hour % 12));
|
||||
case CLOCK_HOUR_OF_AMPM: return plusHours((newValue == 12 ? 0 : newValue) - (hour % 12));
|
||||
case HOUR_OF_DAY: return withHour((int) newValue);
|
||||
case CLOCK_HOUR_OF_DAY: return withHour((int) (newValue == 24 ? 0 : newValue));
|
||||
case AMPM_OF_DAY: return plusHours((newValue - (hour / 12)) * 12);
|
||||
}
|
||||
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
|
||||
return switch (chronoField) {
|
||||
case NANO_OF_SECOND -> withNano((int) newValue);
|
||||
case NANO_OF_DAY -> LocalTime.ofNanoOfDay(newValue);
|
||||
case MICRO_OF_SECOND -> withNano((int) newValue * 1000);
|
||||
case MICRO_OF_DAY -> LocalTime.ofNanoOfDay(newValue * 1000);
|
||||
case MILLI_OF_SECOND -> withNano((int) newValue * 1000_000);
|
||||
case MILLI_OF_DAY -> LocalTime.ofNanoOfDay(newValue * 1000_000);
|
||||
case SECOND_OF_MINUTE -> withSecond((int) newValue);
|
||||
case SECOND_OF_DAY -> plusSeconds(newValue - toSecondOfDay());
|
||||
case MINUTE_OF_HOUR -> withMinute((int) newValue);
|
||||
case MINUTE_OF_DAY -> plusMinutes(newValue - (hour * 60 + minute));
|
||||
case HOUR_OF_AMPM -> plusHours(newValue - (hour % 12));
|
||||
case CLOCK_HOUR_OF_AMPM -> plusHours((newValue == 12 ? 0 : newValue) - (hour % 12));
|
||||
case HOUR_OF_DAY -> withHour((int) newValue);
|
||||
case CLOCK_HOUR_OF_DAY -> withHour((int) (newValue == 24 ? 0 : newValue));
|
||||
case AMPM_OF_DAY -> plusHours((newValue - (hour / 12)) * 12);
|
||||
default -> throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
|
||||
};
|
||||
}
|
||||
return field.adjustInto(this, newValue);
|
||||
}
|
||||
|
@ -1066,16 +1066,16 @@ public final class LocalTime
|
|||
@Override
|
||||
public LocalTime plus(long amountToAdd, TemporalUnit unit) {
|
||||
if (unit instanceof ChronoUnit chronoUnit) {
|
||||
switch (chronoUnit) {
|
||||
case NANOS: return plusNanos(amountToAdd);
|
||||
case MICROS: return plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
|
||||
case MILLIS: return 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 plusHours((amountToAdd % 2) * 12);
|
||||
}
|
||||
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
|
||||
return switch (chronoUnit) {
|
||||
case NANOS -> plusNanos(amountToAdd);
|
||||
case MICROS -> plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
|
||||
case MILLIS -> plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000);
|
||||
case SECONDS -> plusSeconds(amountToAdd);
|
||||
case MINUTES -> plusMinutes(amountToAdd);
|
||||
case HOURS -> plusHours(amountToAdd);
|
||||
case HALF_DAYS -> plusHours((amountToAdd % 2) * 12);
|
||||
default -> throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
|
||||
};
|
||||
}
|
||||
return unit.addTo(this, amountToAdd);
|
||||
}
|
||||
|
@ -1408,16 +1408,16 @@ public final class LocalTime
|
|||
LocalTime end = LocalTime.from(endExclusive);
|
||||
if (unit instanceof ChronoUnit chronoUnit) {
|
||||
long nanosUntil = end.toNanoOfDay() - toNanoOfDay(); // no overflow
|
||||
switch (chronoUnit) {
|
||||
case NANOS: return nanosUntil;
|
||||
case MICROS: return nanosUntil / 1000;
|
||||
case MILLIS: return nanosUntil / 1000_000;
|
||||
case SECONDS: return nanosUntil / NANOS_PER_SECOND;
|
||||
case MINUTES: return nanosUntil / NANOS_PER_MINUTE;
|
||||
case HOURS: return nanosUntil / NANOS_PER_HOUR;
|
||||
case HALF_DAYS: return nanosUntil / (12 * NANOS_PER_HOUR);
|
||||
}
|
||||
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
|
||||
return switch (chronoUnit) {
|
||||
case NANOS -> nanosUntil;
|
||||
case MICROS -> nanosUntil / 1000;
|
||||
case MILLIS -> nanosUntil / 1000_000;
|
||||
case SECONDS -> nanosUntil / NANOS_PER_SECOND;
|
||||
case MINUTES -> nanosUntil / NANOS_PER_MINUTE;
|
||||
case HOURS -> nanosUntil / NANOS_PER_HOUR;
|
||||
case HALF_DAYS -> nanosUntil / (12 * NANOS_PER_HOUR);
|
||||
default -> throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
|
||||
};
|
||||
}
|
||||
return unit.between(this, end);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue