RFR: 8267587: Update java.util to use enhanced switch [v5]

Chris Hegarty chegar at openjdk.java.net
Tue May 25 15:54:57 UTC 2021


On Tue, 25 May 2021 11:49:18 GMT, Tagir F. Valeev <tvaleev at openjdk.org> wrote:

>> Inspired by PR#4088. Most of the changes are done automatically using IntelliJ IDEA refactoring. Some manual adjustments are also performed, including indentations, moving comments, extracting common cast out of switch expression branches, etc.
>> 
>> I also noticed that there are some switches having one branch only in JapaneseImperialCalendar.java. Probably it would be better to replace them with `if` statement?
>
> Tagir F. Valeev has updated the pull request incrementally with one additional commit since the last revision:
> 
>   More vertical alignment

src/java.base/share/classes/java/util/Calendar.java line 1507:

> 1505:                 }
> 1506:                 case "japanese" -> cal = new JapaneseImperialCalendar(zone, locale, true);
> 1507:                 default -> throw new IllegalArgumentException("unknown calendar type: " + type);

In this case, it would be good to yield the result of the switch expression and assign that to a local, rather than assigning in each of the case arms, e.g:

    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);
    };

-------------

PR: https://git.openjdk.java.net/jdk/pull/4161


More information about the core-libs-dev mailing list