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

@ -2546,10 +2546,10 @@ public final class DateTimeFormatterBuilder {
public int parse(DateTimeParseContext context, CharSequence text, int position) {
// using ordinals to avoid javac synthetic inner class
switch (ordinal()) {
case 0: context.setCaseSensitive(true); break;
case 1: context.setCaseSensitive(false); break;
case 2: context.setStrict(true); break;
case 3: context.setStrict(false); break;
case 0 -> context.setCaseSensitive(true);
case 1 -> context.setCaseSensitive(false);
case 2 -> context.setStrict(true);
case 3 -> context.setStrict(false);
}
return position;
}
@ -2787,15 +2787,10 @@ public final class DateTimeFormatterBuilder {
}
} else {
switch (signStyle) {
case NORMAL:
case EXCEEDS_PAD:
case ALWAYS:
buf.append(decimalStyle.getNegativeSign());
break;
case NOT_NEGATIVE:
throw new DateTimeException("Field " + field +
" cannot be printed as the value " + value +
" cannot be negative according to the SignStyle");
case NORMAL, EXCEEDS_PAD, ALWAYS -> buf.append(decimalStyle.getNegativeSign());
case NOT_NEGATIVE -> throw new DateTimeException("Field " + field +
" cannot be printed as the value " + value +
" cannot be negative according to the SignStyle");
}
}
for (int i = 0; i < minWidth - str.length(); i++) {
@ -4128,13 +4123,11 @@ public final class DateTimeFormatterBuilder {
perLocale.put(locale, names);
cache.put(id, new SoftReference<>(perLocale));
}
switch (type) {
case STD:
return names[textStyle.zoneNameStyleIndex() + 1];
case DST:
return names[textStyle.zoneNameStyleIndex() + 3];
}
return names[textStyle.zoneNameStyleIndex() + 5];
return switch (type) {
case STD -> names[textStyle.zoneNameStyleIndex() + 1];
case DST -> names[textStyle.zoneNameStyleIndex() + 3];
default -> names[textStyle.zoneNameStyleIndex() + 5];
};
}
@Override

View file

@ -123,17 +123,11 @@ public enum SignStyle {
* @return
*/
boolean parse(boolean positive, boolean strict, boolean fixedWidth) {
switch (ordinal()) {
case 0: // NORMAL
// valid if negative or (positive and lenient)
return !positive || !strict;
case 1: // ALWAYS
case 4: // EXCEEDS_PAD
return true;
default:
// valid if lenient and not fixed width
return !strict && !fixedWidth;
}
return switch (ordinal()) {
case 0 -> !positive || !strict; // NORMAL - valid if negative or (positive and lenient)
case 1, 4 -> true; // ALWAYS, EXCEEDS_PAD
default -> !strict && !fixedWidth; // valid if lenient and not fixed width
};
}
}