Apply "switch expressions" where applicable

This commit is contained in:
Sam Brannen
2022-02-02 16:56:53 +01:00
parent 9a5ecd0c46
commit 32cd73261a
4 changed files with 54 additions and 77 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -139,14 +139,14 @@ public class DateTimeFormatterFactory {
@Nullable
private FormatStyle convertStyleCharacter(char c) {
switch (c) {
case 'S': return FormatStyle.SHORT;
case 'M': return FormatStyle.MEDIUM;
case 'L': return FormatStyle.LONG;
case 'F': return FormatStyle.FULL;
case '-': return null;
default: throw new IllegalArgumentException("Invalid style character '" + c + "'");
}
return switch (c) {
case 'S' -> FormatStyle.SHORT;
case 'M' -> FormatStyle.MEDIUM;
case 'L' -> FormatStyle.LONG;
case 'F' -> FormatStyle.FULL;
case '-' -> null;
default -> throw new IllegalArgumentException("Invalid style character '" + c + "'");
};
}
/**
@@ -183,28 +183,12 @@ public class DateTimeFormatterFactory {
dateTimeFormatter = DateTimeFormatterUtils.createStrictDateTimeFormatter(this.pattern);
}
else if (this.iso != null && this.iso != ISO.NONE) {
// TODO Use switch expression once spring-javaformat 0.0.30 has been released.
// See https://github.com/spring-io/spring-javaformat/issues/300
//
// dateTimeFormatter = switch (this.iso) {
// case DATE -> DateTimeFormatter.ISO_DATE;
// case TIME -> DateTimeFormatter.ISO_TIME;
// case DATE_TIME -> DateTimeFormatter.ISO_DATE_TIME;
// default -> throw new IllegalStateException("Unsupported ISO format: " + this.iso);
// };
switch (this.iso) {
case DATE:
dateTimeFormatter = DateTimeFormatter.ISO_DATE;
break;
case TIME:
dateTimeFormatter = DateTimeFormatter.ISO_TIME;
break;
case DATE_TIME:
dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;
break;
default:
throw new IllegalStateException("Unsupported ISO format: " + this.iso);
}
dateTimeFormatter = switch (this.iso) {
case DATE -> DateTimeFormatter.ISO_DATE;
case TIME -> DateTimeFormatter.ISO_TIME;
case DATE_TIME -> DateTimeFormatter.ISO_DATE_TIME;
default -> throw new IllegalStateException("Unsupported ISO format: " + this.iso);
};
}
else if (this.dateStyle != null && this.timeStyle != null) {
dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(this.dateStyle, this.timeStyle);