Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8267587: Update java.util to use enhanced switch #4161

Closed
wants to merge 9 commits into from
@@ -1477,7 +1477,6 @@ public Calendar build() {
if (zone == null) {
zone = defaultTimeZone(locale);
}
Calendar cal;
if (type == null) {
type = locale.getUnicodeLocaleType("ca");
}
@@ -1489,28 +1488,24 @@ public Calendar build() {
type = "gregory";
}
}
switch (type) {
case "gregory":
cal = new GregorianCalendar(zone, locale, true);
break;
case "iso8601":
GregorianCalendar gcal = new GregorianCalendar(zone, locale, true);
// make gcal a proleptic Gregorian
gcal.setGregorianChange(new Date(Long.MIN_VALUE));
// and week definition to be compatible with ISO 8601
setWeekDefinition(MONDAY, 4);
cal = gcal;
break;
case "buddhist":
cal = new BuddhistCalendar(zone, locale);
cal.clear();
break;
case "japanese":
cal = new JapaneseImperialCalendar(zone, locale, true);
break;
default:
throw new IllegalArgumentException("unknown calendar type: " + type);
}
final Calendar cal = switch (type) {
case "gregory" -> new GregorianCalendar(zone, locale, true);
case "iso8601" -> {
GregorianCalendar gcal = new GregorianCalendar(zone, locale, true);
// make gcal a proleptic Gregorian
gcal.setGregorianChange(new Date(Long.MIN_VALUE));
// and week definition to be compatible with ISO 8601
setWeekDefinition(MONDAY, 4);
yield gcal;
}
case "buddhist" -> {
var buddhistCalendar = new BuddhistCalendar(zone, locale);
buddhistCalendar.clear();
yield buddhistCalendar;
}
case "japanese" -> new JapaneseImperialCalendar(zone, locale, true);
default -> throw new IllegalArgumentException("unknown calendar type: " + type);
amaembo marked this conversation as resolved.
Show resolved Hide resolved
amaembo marked this conversation as resolved.
Show resolved Hide resolved
};
cal.setLenient(lenient);
if (firstDayOfWeek != 0) {
cal.setFirstDayOfWeek(firstDayOfWeek);
@@ -1705,17 +1700,12 @@ private static Calendar createCalendar(TimeZone zone,
if (aLocale.hasExtensions()) {
String caltype = aLocale.getUnicodeLocaleType("ca");
if (caltype != null) {
switch (caltype) {
case "buddhist":
cal = new BuddhistCalendar(zone, aLocale);
break;
case "japanese":
cal = new JapaneseImperialCalendar(zone, aLocale);
break;
case "gregory":
cal = new GregorianCalendar(zone, aLocale);
break;
}
cal = switch (caltype) {
case "buddhist" -> new BuddhistCalendar(zone, aLocale);
case "japanese" -> new JapaneseImperialCalendar(zone, aLocale);
case "gregory" -> new GregorianCalendar(zone, aLocale);
default -> null;
};
}
}
if (cal == null) {
@@ -2267,25 +2257,13 @@ private String[] getFieldStrings(int field, int style, DateFormatSymbols symbols
return null;
}

String[] strings = null;
switch (field) {
case ERA:
strings = symbols.getEras();
break;

case MONTH:
strings = (baseStyle == LONG) ? symbols.getMonths() : symbols.getShortMonths();
break;

case DAY_OF_WEEK:
strings = (baseStyle == LONG) ? symbols.getWeekdays() : symbols.getShortWeekdays();
break;

case AM_PM:
strings = symbols.getAmPmStrings();
break;
}
return strings;
return switch (field) {
case ERA -> symbols.getEras();
case MONTH -> (baseStyle == LONG) ? symbols.getMonths() : symbols.getShortMonths();
case DAY_OF_WEEK -> (baseStyle == LONG) ? symbols.getWeekdays() : symbols.getShortWeekdays();
case AM_PM -> symbols.getAmPmStrings();
default -> null;
};
}

/**
@@ -2674,27 +2674,26 @@ public Formatter format(Locale l, String format, Object ... args) {
int index = fs.index();
try {
switch (index) {
case -2: // fixed string, "%n", or "%%"
fs.print(null, l);
break;
case -1: // relative index
if (last < 0 || (args != null && last > args.length - 1))
throw new MissingFormatArgumentException(fs.toString());
fs.print((args == null ? null : args[last]), l);
break;
case 0: // ordinary index
lasto++;
last = lasto;
if (args != null && lasto > args.length - 1)
throw new MissingFormatArgumentException(fs.toString());
fs.print((args == null ? null : args[lasto]), l);
break;
default: // explicit index
last = index - 1;
if (args != null && last > args.length - 1)
throw new MissingFormatArgumentException(fs.toString());
fs.print((args == null ? null : args[last]), l);
break;
case -2 -> // fixed string, "%n", or "%%"
fs.print(null, l);
case -1 -> { // relative index
if (last < 0 || (args != null && last > args.length - 1))
throw new MissingFormatArgumentException(fs.toString());
fs.print((args == null ? null : args[last]), l);
}
case 0 -> { // ordinary index
lasto++;
last = lasto;
if (args != null && lasto > args.length - 1)
throw new MissingFormatArgumentException(fs.toString());
fs.print((args == null ? null : args[lasto]), l);
}
default -> { // explicit index
last = index - 1;
if (args != null && last > args.length - 1)
throw new MissingFormatArgumentException(fs.toString());
fs.print((args == null ? null : args[last]), l);
}
}
} catch (IOException x) {
lastException = x;
@@ -4111,15 +4110,9 @@ private Appendable print(StringBuilder sb, Calendar t, char c, Locale l)
int i = t.get(Calendar.YEAR);
int size = 2;
switch (c) {
case DateTime.CENTURY:
i /= 100;
break;
case DateTime.YEAR_2:
i %= 100;
break;
case DateTime.YEAR_4:
size = 4;
break;
case DateTime.CENTURY -> i /= 100;
case DateTime.YEAR_2 -> i %= 100;
case DateTime.YEAR_4 -> size = 4;
}
Flags flags = Flags.ZERO_PAD;
sb.append(localizedMagnitude(null, i, flags, size, l));
@@ -4352,15 +4345,9 @@ private Appendable print(StringBuilder sb, TemporalAccessor t, char c,
int i = t.get(ChronoField.YEAR_OF_ERA);
int size = 2;
switch (c) {
case DateTime.CENTURY:
i /= 100;
break;
case DateTime.YEAR_2:
i %= 100;
break;
case DateTime.YEAR_4:
size = 4;
break;
case DateTime.CENTURY -> i /= 100;
case DateTime.YEAR_2 -> i %= 100;
case DateTime.YEAR_4 -> size = 4;
}
Flags flags = Flags.ZERO_PAD;
sb.append(localizedMagnitude(null, i, flags, size, l));
@@ -4836,46 +4823,16 @@ private static class DateTime {
static final char ISO_STANDARD_DATE = 'F'; // (%Y-%m-%d)

static boolean isValid(char c) {
switch (c) {
case HOUR_OF_DAY_0:
case HOUR_0:
case HOUR_OF_DAY:
case HOUR:
case MINUTE:
case NANOSECOND:
case MILLISECOND:
case MILLISECOND_SINCE_EPOCH:
case AM_PM:
case SECONDS_SINCE_EPOCH:
case SECOND:
case TIME:
case ZONE_NUMERIC:
case ZONE:

// Date
case NAME_OF_DAY_ABBREV:
case NAME_OF_DAY:
case NAME_OF_MONTH_ABBREV:
case NAME_OF_MONTH:
case CENTURY:
case DAY_OF_MONTH_0:
case DAY_OF_MONTH:
case NAME_OF_MONTH_ABBREV_X:
case DAY_OF_YEAR:
case MONTH:
case YEAR_2:
case YEAR_4:

// Composites
case TIME_12_HOUR:
case TIME_24_HOUR:
case DATE_TIME:
case DATE:
case ISO_STANDARD_DATE:
return true;
default:
return false;
}
return switch (c) {
case HOUR_OF_DAY_0, HOUR_0, HOUR_OF_DAY, HOUR, MINUTE, NANOSECOND, MILLISECOND, MILLISECOND_SINCE_EPOCH,
AM_PM, SECONDS_SINCE_EPOCH, SECOND, TIME, ZONE_NUMERIC, ZONE -> true;
// Date
case NAME_OF_DAY_ABBREV, NAME_OF_DAY, NAME_OF_MONTH_ABBREV, NAME_OF_MONTH, CENTURY, DAY_OF_MONTH_0,
DAY_OF_MONTH, NAME_OF_MONTH_ABBREV_X, DAY_OF_YEAR, MONTH, YEAR_2, YEAR_4 -> true;
// Composites
case TIME_12_HOUR, TIME_24_HOUR, DATE_TIME, DATE, ISO_STANDARD_DATE -> true;
default -> false;
};
}
}
}