Browse Source

Apply "switch expressions" where applicable

pull/28004/head
Sam Brannen 3 years ago
parent
commit
32cd73261a
  1. 46
      spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterFactory.java
  2. 18
      spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseConfigurerFactory.java
  3. 35
      spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java
  4. 32
      spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java

46
spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterFactory.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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 { @@ -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);

18
spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseConfigurerFactory.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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);

35
spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java

@ -1,5 +1,5 @@ @@ -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; @@ -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 @@ -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()) {

32
spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java

@ -1,5 +1,5 @@ @@ -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 @@ -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 @@ -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);

Loading…
Cancel
Save