From 32cd73261a3094a289b8d0d62f76c8e413fe3f86 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 2 Feb 2022 16:56:53 +0100 Subject: [PATCH] Apply "switch expressions" where applicable --- .../standard/DateTimeFormatterFactory.java | 46 ++++++------------- .../EmbeddedDatabaseConfigurerFactory.java | 18 +++----- .../SQLErrorCodeSQLExceptionTranslator.java | 35 ++++++++------ .../MappingJackson2MessageConverter.java | 32 +++++-------- 4 files changed, 54 insertions(+), 77 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterFactory.java b/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterFactory.java index df4f277e5d..3d0f395a08 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterFactory.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterFactory.java @@ -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); diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseConfigurerFactory.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseConfigurerFactory.java index 09647976c0..f9b728acd0 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseConfigurerFactory.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseConfigurerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 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. @@ -42,16 +42,12 @@ final class EmbeddedDatabaseConfigurerFactory { public static EmbeddedDatabaseConfigurer getConfigurer(EmbeddedDatabaseType type) throws IllegalStateException { Assert.notNull(type, "EmbeddedDatabaseType is required"); try { - switch (type) { - case HSQL: - return HsqlEmbeddedDatabaseConfigurer.getInstance(); - case H2: - return H2EmbeddedDatabaseConfigurer.getInstance(); - case DERBY: - return DerbyEmbeddedDatabaseConfigurer.getInstance(); - default: - throw new UnsupportedOperationException("Embedded database type [" + type + "] is not supported"); - } + return switch (type) { + case HSQL -> HsqlEmbeddedDatabaseConfigurer.getInstance(); + case H2 -> H2EmbeddedDatabaseConfigurer.getInstance(); + case DERBY -> DerbyEmbeddedDatabaseConfigurer.getInstance(); + default -> throw new UnsupportedOperationException("Embedded database type [" + type + "] is not supported"); + }; } catch (ClassNotFoundException | NoClassDefFoundError ex) { throw new IllegalStateException("Driver for test database type [" + type + "] is not available", ex); diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java b/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java index f50380fcde..ad612b4baf 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 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. @@ -64,6 +64,7 @@ import org.springframework.util.function.SupplierUtils; * @author Rod Johnson * @author Thomas Risberg * @author Juergen Hoeller + * @author Sam Brannen * @see SQLErrorCodesFactory * @see SQLStateSQLExceptionTranslator */ @@ -359,39 +360,45 @@ public class SQLErrorCodeSQLExceptionTranslator extends AbstractFallbackSQLExcep // invoke constructor Constructor exceptionConstructor; - switch (constructorType) { - case MESSAGE_SQL_SQLEX_CONSTRUCTOR: + return switch (constructorType) { + case MESSAGE_SQL_SQLEX_CONSTRUCTOR -> { Class[] messageAndSqlAndSqlExArgsClass = new Class[] {String.class, String.class, SQLException.class}; Object[] messageAndSqlAndSqlExArgs = new Object[] {task, sql, sqlEx}; exceptionConstructor = exceptionClass.getConstructor(messageAndSqlAndSqlExArgsClass); - return (DataAccessException) exceptionConstructor.newInstance(messageAndSqlAndSqlExArgs); - case MESSAGE_SQL_THROWABLE_CONSTRUCTOR: + yield (DataAccessException) exceptionConstructor.newInstance(messageAndSqlAndSqlExArgs); + } + case MESSAGE_SQL_THROWABLE_CONSTRUCTOR -> { Class[] messageAndSqlAndThrowableArgsClass = new Class[] {String.class, String.class, Throwable.class}; Object[] messageAndSqlAndThrowableArgs = new Object[] {task, sql, sqlEx}; exceptionConstructor = exceptionClass.getConstructor(messageAndSqlAndThrowableArgsClass); - return (DataAccessException) exceptionConstructor.newInstance(messageAndSqlAndThrowableArgs); - case MESSAGE_SQLEX_CONSTRUCTOR: + yield (DataAccessException) exceptionConstructor.newInstance(messageAndSqlAndThrowableArgs); + } + case MESSAGE_SQLEX_CONSTRUCTOR -> { Class[] messageAndSqlExArgsClass = new Class[] {String.class, SQLException.class}; Object[] messageAndSqlExArgs = new Object[] {task + ": " + sqlEx.getMessage(), sqlEx}; exceptionConstructor = exceptionClass.getConstructor(messageAndSqlExArgsClass); - return (DataAccessException) exceptionConstructor.newInstance(messageAndSqlExArgs); - case MESSAGE_THROWABLE_CONSTRUCTOR: + yield (DataAccessException) exceptionConstructor.newInstance(messageAndSqlExArgs); + } + case MESSAGE_THROWABLE_CONSTRUCTOR -> { Class[] messageAndThrowableArgsClass = new Class[] {String.class, Throwable.class}; Object[] messageAndThrowableArgs = new Object[] {task + ": " + sqlEx.getMessage(), sqlEx}; exceptionConstructor = exceptionClass.getConstructor(messageAndThrowableArgsClass); - return (DataAccessException)exceptionConstructor.newInstance(messageAndThrowableArgs); - case MESSAGE_ONLY_CONSTRUCTOR: + yield (DataAccessException)exceptionConstructor.newInstance(messageAndThrowableArgs); + } + case MESSAGE_ONLY_CONSTRUCTOR -> { Class[] messageOnlyArgsClass = new Class[] {String.class}; Object[] messageOnlyArgs = new Object[] {task + ": " + sqlEx.getMessage()}; exceptionConstructor = exceptionClass.getConstructor(messageOnlyArgsClass); - return (DataAccessException) exceptionConstructor.newInstance(messageOnlyArgs); - default: + yield (DataAccessException) exceptionConstructor.newInstance(messageOnlyArgs); + } + default -> { if (logger.isWarnEnabled()) { logger.warn("Unable to find appropriate constructor of custom exception class [" + exceptionClass.getName() + "]"); } - return null; + yield null; } + }; } catch (Throwable ex) { if (logger.isWarnEnabled()) { diff --git a/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java b/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java index 429343f6b2..4428770630 100644 --- a/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java +++ b/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java @@ -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. @@ -179,16 +179,11 @@ public class MappingJackson2MessageConverter implements SmartMessageConverter, B public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException { Message message; try { - switch (this.targetType) { - case TEXT: - message = mapToTextMessage(object, session, this.objectMapper.writer()); - break; - case BYTES: - message = mapToBytesMessage(object, session, this.objectMapper.writer()); - break; - default: - message = mapToMessage(object, session, this.objectMapper.writer(), this.targetType); - } + message = switch (this.targetType) { + case TEXT -> mapToTextMessage(object, session, this.objectMapper.writer()); + case BYTES -> mapToBytesMessage(object, session, this.objectMapper.writer()); + default -> mapToMessage(object, session, this.objectMapper.writer(), this.targetType); + }; } catch (IOException ex) { throw new MessageConversionException("Could not map JSON object [" + object + "]", ex); @@ -242,16 +237,11 @@ public class MappingJackson2MessageConverter implements SmartMessageConverter, B Message message; try { - switch (this.targetType) { - case TEXT: - message = mapToTextMessage(object, session, objectWriter); - break; - case BYTES: - message = mapToBytesMessage(object, session, objectWriter); - break; - default: - message = mapToMessage(object, session, objectWriter, this.targetType); - } + message = switch (this.targetType) { + case TEXT -> mapToTextMessage(object, session, objectWriter); + case BYTES -> mapToBytesMessage(object, session, objectWriter); + default -> mapToMessage(object, session, objectWriter, this.targetType); + }; } catch (IOException ex) { throw new MessageConversionException("Could not map JSON object [" + object + "]", ex);