Use switch expressions where appropriate

See gh-31527
pull/31539/head
dreis2211 2 years ago committed by Andy Wilkinson
parent 836b08f49d
commit 458f989cf3

@ -95,15 +95,11 @@ class AutoConfiguredHealthEndpointGroup implements HealthEndpointGroup {
} }
private boolean getShowResult(SecurityContext securityContext, Show show) { private boolean getShowResult(SecurityContext securityContext, Show show) {
switch (show) { return switch (show) {
case NEVER: case NEVER -> false;
return false; case ALWAYS -> true;
case ALWAYS: case WHEN_AUTHORIZED -> isAuthorized(securityContext);
return true; };
case WHEN_AUTHORIZED:
return isAuthorized(securityContext);
}
throw new IllegalStateException("Unsupported 'show' value " + show);
} }
private boolean isAuthorized(SecurityContext securityContext) { private boolean isAuthorized(SecurityContext securityContext) {

@ -76,36 +76,27 @@ public class ManagementErrorEndpoint {
} }
private boolean includeStackTrace(ServletWebRequest request) { private boolean includeStackTrace(ServletWebRequest request) {
switch (this.errorProperties.getIncludeStacktrace()) { return switch (this.errorProperties.getIncludeStacktrace()) {
case ALWAYS: case ALWAYS -> true;
return true; case ON_PARAM -> getBooleanParameter(request, "trace");
case ON_PARAM: default -> false;
return getBooleanParameter(request, "trace"); };
default:
return false;
}
} }
private boolean includeMessage(ServletWebRequest request) { private boolean includeMessage(ServletWebRequest request) {
switch (this.errorProperties.getIncludeMessage()) { return switch (this.errorProperties.getIncludeMessage()) {
case ALWAYS: case ALWAYS -> true;
return true; case ON_PARAM -> getBooleanParameter(request, "message");
case ON_PARAM: default -> false;
return getBooleanParameter(request, "message"); };
default:
return false;
}
} }
private boolean includeBindingErrors(ServletWebRequest request) { private boolean includeBindingErrors(ServletWebRequest request) {
switch (this.errorProperties.getIncludeBindingErrors()) { return switch (this.errorProperties.getIncludeBindingErrors()) {
case ALWAYS: case ALWAYS -> true;
return true; case ON_PARAM -> getBooleanParameter(request, "errors");
case ON_PARAM: default -> false;
return getBooleanParameter(request, "errors"); };
default:
return false;
}
} }
protected boolean getBooleanParameter(ServletWebRequest request, String parameterName) { protected boolean getBooleanParameter(ServletWebRequest request, String parameterName) {

@ -135,17 +135,11 @@ class DynatraceMetricsExportAutoConfigurationTests {
@Bean @Bean
DynatraceConfig customConfig() { DynatraceConfig customConfig() {
return (key) -> { return (key) -> switch (key) {
switch (key) { case "dynatrace.uri" -> "https://dynatrace.example.com";
case "dynatrace.uri": case "dynatrace.apiToken" -> "abcde";
return "https://dynatrace.example.com"; case "dynatrace.deviceId" -> "test";
case "dynatrace.apiToken": default -> null;
return "abcde";
case "dynatrace.deviceId":
return "test";
default:
return null;
}
}; };
} }

@ -141,16 +141,9 @@ public class PrometheusPushGatewayManager {
} }
this.scheduled.cancel(false); this.scheduled.cancel(false);
switch (shutdownOperation) { switch (shutdownOperation) {
case PUSH: case PUSH, POST -> post();
case POST: case PUT -> put();
post(); case DELETE -> delete();
break;
case PUT:
put();
break;
case DELETE:
delete();
break;
} }
} }

@ -265,26 +265,16 @@ public class QuartzEndpoint {
} }
private static TemporalUnit temporalUnit(IntervalUnit unit) { private static TemporalUnit temporalUnit(IntervalUnit unit) {
switch (unit) { return switch (unit) {
case DAY: case DAY -> ChronoUnit.DAYS;
return ChronoUnit.DAYS; case HOUR -> ChronoUnit.HOURS;
case HOUR: case MINUTE -> ChronoUnit.MINUTES;
return ChronoUnit.HOURS; case MONTH -> ChronoUnit.MONTHS;
case MINUTE: case SECOND -> ChronoUnit.SECONDS;
return ChronoUnit.MINUTES; case MILLISECOND -> ChronoUnit.MILLIS;
case MONTH: case WEEK -> ChronoUnit.WEEKS;
return ChronoUnit.MONTHS; case YEAR -> ChronoUnit.YEARS;
case SECOND: };
return ChronoUnit.SECONDS;
case MILLISECOND:
return ChronoUnit.MILLIS;
case WEEK:
return ChronoUnit.WEEKS;
case YEAR:
return ChronoUnit.YEARS;
default:
throw new IllegalArgumentException("Unknown IntervalUnit");
}
} }
/** /**

@ -99,14 +99,11 @@ class OnWebApplicationCondition extends FilteringSpringBootCondition {
private ConditionOutcome isWebApplication(ConditionContext context, AnnotatedTypeMetadata metadata, private ConditionOutcome isWebApplication(ConditionContext context, AnnotatedTypeMetadata metadata,
boolean required) { boolean required) {
switch (deduceType(metadata)) { return switch (deduceType(metadata)) {
case SERVLET: case SERVLET -> isServletWebApplication(context);
return isServletWebApplication(context); case REACTIVE -> isReactiveWebApplication(context);
case REACTIVE: default -> isAnyWebApplication(context, required);
return isReactiveWebApplication(context); };
default:
return isAnyWebApplication(context, required);
}
} }
private ConditionOutcome isAnyWebApplication(ConditionContext context, boolean required) { private ConditionOutcome isAnyWebApplication(ConditionContext context, boolean required) {

@ -315,17 +315,13 @@ public class JacksonAutoConfiguration {
if (strategy != null) { if (strategy != null) {
builder.postConfigurer((objectMapper) -> { builder.postConfigurer((objectMapper) -> {
switch (strategy) { switch (strategy) {
case USE_PROPERTIES_BASED: case USE_PROPERTIES_BASED ->
objectMapper.setConstructorDetector(ConstructorDetector.USE_PROPERTIES_BASED); objectMapper.setConstructorDetector(ConstructorDetector.USE_PROPERTIES_BASED);
break; case USE_DELEGATING ->
case USE_DELEGATING:
objectMapper.setConstructorDetector(ConstructorDetector.USE_DELEGATING); objectMapper.setConstructorDetector(ConstructorDetector.USE_DELEGATING);
break; case EXPLICIT_ONLY ->
case EXPLICIT_ONLY:
objectMapper.setConstructorDetector(ConstructorDetector.EXPLICIT_ONLY); objectMapper.setConstructorDetector(ConstructorDetector.EXPLICIT_ONLY);
break; default -> objectMapper.setConstructorDetector(ConstructorDetector.DEFAULT);
default:
objectMapper.setConstructorDetector(ConstructorDetector.DEFAULT);
} }
}); });
} }

@ -53,14 +53,10 @@ class RedisSessionConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
ConfigureRedisAction configureRedisAction(RedisSessionProperties redisSessionProperties) { ConfigureRedisAction configureRedisAction(RedisSessionProperties redisSessionProperties) {
switch (redisSessionProperties.getConfigureAction()) { return switch (redisSessionProperties.getConfigureAction()) {
case NOTIFY_KEYSPACE_EVENTS: case NOTIFY_KEYSPACE_EVENTS -> new ConfigureNotifyKeyspaceEventsAction();
return new ConfigureNotifyKeyspaceEventsAction(); case NONE -> ConfigureRedisAction.NO_OP;
case NONE: };
return ConfigureRedisAction.NO_OP;
}
throw new IllegalStateException(
"Unsupported redis configure action '" + redisSessionProperties.getConfigureAction() + "'.");
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)

@ -173,14 +173,11 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa
* @return if the stacktrace attribute should be included * @return if the stacktrace attribute should be included
*/ */
protected boolean isIncludeStackTrace(ServerRequest request, MediaType produces) { protected boolean isIncludeStackTrace(ServerRequest request, MediaType produces) {
switch (this.errorProperties.getIncludeStacktrace()) { return switch (this.errorProperties.getIncludeStacktrace()) {
case ALWAYS: case ALWAYS -> true;
return true; case ON_PARAM -> isTraceEnabled(request);
case ON_PARAM: default -> false;
return isTraceEnabled(request); };
default:
return false;
}
} }
/** /**
@ -190,14 +187,11 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa
* @return if the message attribute should be included * @return if the message attribute should be included
*/ */
protected boolean isIncludeMessage(ServerRequest request, MediaType produces) { protected boolean isIncludeMessage(ServerRequest request, MediaType produces) {
switch (this.errorProperties.getIncludeMessage()) { return switch (this.errorProperties.getIncludeMessage()) {
case ALWAYS: case ALWAYS -> true;
return true; case ON_PARAM -> isMessageEnabled(request);
case ON_PARAM: default -> false;
return isMessageEnabled(request); };
default:
return false;
}
} }
/** /**
@ -207,14 +201,11 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa
* @return if the errors attribute should be included * @return if the errors attribute should be included
*/ */
protected boolean isIncludeBindingErrors(ServerRequest request, MediaType produces) { protected boolean isIncludeBindingErrors(ServerRequest request, MediaType produces) {
switch (this.errorProperties.getIncludeBindingErrors()) { return switch (this.errorProperties.getIncludeBindingErrors()) {
case ALWAYS: case ALWAYS -> true;
return true; case ON_PARAM -> isBindingErrorsEnabled(request);
case ON_PARAM: default -> false;
return isBindingErrorsEnabled(request); };
default:
return false;
}
} }
/** /**

@ -131,14 +131,11 @@ public class BasicErrorController extends AbstractErrorController {
* @return if the stacktrace attribute should be included * @return if the stacktrace attribute should be included
*/ */
protected boolean isIncludeStackTrace(HttpServletRequest request, MediaType produces) { protected boolean isIncludeStackTrace(HttpServletRequest request, MediaType produces) {
switch (getErrorProperties().getIncludeStacktrace()) { return switch (getErrorProperties().getIncludeStacktrace()) {
case ALWAYS: case ALWAYS -> true;
return true; case ON_PARAM -> getTraceParameter(request);
case ON_PARAM: default -> false;
return getTraceParameter(request); };
default:
return false;
}
} }
/** /**
@ -148,14 +145,11 @@ public class BasicErrorController extends AbstractErrorController {
* @return if the message attribute should be included * @return if the message attribute should be included
*/ */
protected boolean isIncludeMessage(HttpServletRequest request, MediaType produces) { protected boolean isIncludeMessage(HttpServletRequest request, MediaType produces) {
switch (getErrorProperties().getIncludeMessage()) { return switch (getErrorProperties().getIncludeMessage()) {
case ALWAYS: case ALWAYS -> true;
return true; case ON_PARAM -> getMessageParameter(request);
case ON_PARAM: default -> false;
return getMessageParameter(request); };
default:
return false;
}
} }
/** /**
@ -165,14 +159,11 @@ public class BasicErrorController extends AbstractErrorController {
* @return if the errors attribute should be included * @return if the errors attribute should be included
*/ */
protected boolean isIncludeBindingErrors(HttpServletRequest request, MediaType produces) { protected boolean isIncludeBindingErrors(HttpServletRequest request, MediaType produces) {
switch (getErrorProperties().getIncludeBindingErrors()) { return switch (getErrorProperties().getIncludeBindingErrors()) {
case ALWAYS: case ALWAYS -> true;
return true; case ON_PARAM -> getErrorsParameter(request);
case ON_PARAM: default -> false;
return getErrorsParameter(request); };
default:
return false;
}
} }
/** /**

@ -85,13 +85,12 @@ public class FilterAnnotations implements Iterable<TypeFilter> {
} }
private TypeFilter createTypeFilter(FilterType filterType, String pattern) { private TypeFilter createTypeFilter(FilterType filterType, String pattern) {
switch (filterType) { return switch (filterType) {
case ASPECTJ: case ASPECTJ -> new AspectJTypeFilter(pattern, this.classLoader);
return new AspectJTypeFilter(pattern, this.classLoader); case REGEX -> new RegexPatternTypeFilter(Pattern.compile(pattern));
case REGEX: default ->
return new RegexPatternTypeFilter(Pattern.compile(pattern));
}
throw new IllegalArgumentException("Filter type not supported with String pattern: " + filterType); throw new IllegalArgumentException("Filter type not supported with String pattern: " + filterType);
};
} }
@Override @Override

@ -54,12 +54,8 @@ public class LogUpdateEvent extends UpdateEvent {
public void print() { public void print() {
switch (this.streamType) { switch (this.streamType) {
case STD_OUT: case STD_OUT -> System.out.println(this);
System.out.println(this); case STD_ERR -> System.err.println(this);
return;
case STD_ERR:
System.err.println(this);
return;
} }
} }

@ -167,15 +167,11 @@ public class AotProcessor {
} }
private Path getRoot(Kind kind) { private Path getRoot(Kind kind) {
switch (kind) { return switch (kind) {
case SOURCE: case SOURCE -> this.sourceOutput;
return this.sourceOutput; case RESOURCE -> this.resourceOutput;
case RESOURCE: case CLASS -> this.classOutput;
return this.resourceOutput; };
case CLASS:
return this.classOutput;
}
throw new IllegalStateException("Unsupported kind " + kind);
} }
private void writeHints(RuntimeHints hints) { private void writeHints(RuntimeHints hints) {

@ -362,14 +362,11 @@ public class SpringApplication {
} }
private Class<? extends StandardEnvironment> deduceEnvironmentClass() { private Class<? extends StandardEnvironment> deduceEnvironmentClass() {
switch (this.webApplicationType) { return switch (this.webApplicationType) {
case SERVLET: case SERVLET -> ApplicationServletEnvironment.class;
return ApplicationServletEnvironment.class; case REACTIVE -> ApplicationReactiveWebEnvironment.class;
case REACTIVE: default -> ApplicationEnvironment.class;
return ApplicationReactiveWebEnvironment.class; };
default:
return ApplicationEnvironment.class;
}
} }
private void prepareContext(DefaultBootstrapContext bootstrapContext, ConfigurableApplicationContext context, private void prepareContext(DefaultBootstrapContext bootstrapContext, ConfigurableApplicationContext context,
@ -457,14 +454,11 @@ public class SpringApplication {
if (this.environment != null) { if (this.environment != null) {
return this.environment; return this.environment;
} }
switch (this.webApplicationType) { return switch (this.webApplicationType) {
case SERVLET: case SERVLET -> new ApplicationServletEnvironment();
return new ApplicationServletEnvironment(); case REACTIVE -> new ApplicationReactiveWebEnvironment();
case REACTIVE: default -> new ApplicationEnvironment();
return new ApplicationReactiveWebEnvironment(); };
default:
return new ApplicationEnvironment();
}
} }
/** /**

@ -245,23 +245,12 @@ public class DeferredLog implements Log {
static void logTo(Log log, LogLevel level, Object message, Throwable throwable) { static void logTo(Log log, LogLevel level, Object message, Throwable throwable) {
switch (level) { switch (level) {
case TRACE: case TRACE -> log.trace(message, throwable);
log.trace(message, throwable); case DEBUG -> log.debug(message, throwable);
return; case INFO -> log.info(message, throwable);
case DEBUG: case WARN -> log.warn(message, throwable);
log.debug(message, throwable); case ERROR -> log.error(message, throwable);
return; case FATAL -> log.fatal(message, throwable);
case INFO:
log.info(message, throwable);
return;
case WARN:
log.warn(message, throwable);
return;
case ERROR:
log.error(message, throwable);
return;
case FATAL:
log.fatal(message, throwable);
} }
} }

@ -704,15 +704,11 @@ public class JettyServletWebServerFactory extends AbstractServletWebServerFactor
} }
private String getSameSiteComment(SameSite sameSite) { private String getSameSiteComment(SameSite sameSite) {
switch (sameSite) { return switch (sameSite) {
case NONE: case NONE -> HttpCookie.SAME_SITE_NONE_COMMENT;
return HttpCookie.SAME_SITE_NONE_COMMENT; case LAX -> HttpCookie.SAME_SITE_LAX_COMMENT;
case LAX: case STRICT -> HttpCookie.SAME_SITE_STRICT_COMMENT;
return HttpCookie.SAME_SITE_LAX_COMMENT; };
case STRICT:
return HttpCookie.SAME_SITE_STRICT_COMMENT;
}
throw new IllegalStateException("Unsupported SameSite value " + sameSite);
} }
private SameSite getSameSite(Cookie cookie) { private SameSite getSameSite(Cookie cookie) {

@ -34,20 +34,13 @@ public class Location {
} }
public Location getAdjacentLocation(Direction direction) { public Location getAdjacentLocation(Direction direction) {
switch (direction) { return switch (direction) {
case NORTH: case NORTH -> new Location(this.x, this.y - SnakeUtils.GRID_SIZE);
return new Location(this.x, this.y - SnakeUtils.GRID_SIZE); case SOUTH -> new Location(this.x, this.y + SnakeUtils.GRID_SIZE);
case SOUTH: case EAST -> new Location(this.x + SnakeUtils.GRID_SIZE, this.y);
return new Location(this.x, this.y + SnakeUtils.GRID_SIZE); case WEST -> new Location(this.x - SnakeUtils.GRID_SIZE, this.y);
case EAST: case NONE -> this;
return new Location(this.x + SnakeUtils.GRID_SIZE, this.y); };
case WEST:
return new Location(this.x - SnakeUtils.GRID_SIZE, this.y);
case NONE:
// fall through
default:
return this;
}
} }
@Override @Override

@ -34,20 +34,13 @@ public class Location {
} }
public Location getAdjacentLocation(Direction direction) { public Location getAdjacentLocation(Direction direction) {
switch (direction) { return switch (direction) {
case NORTH: case NORTH -> new Location(this.x, this.y - SnakeUtils.GRID_SIZE);
return new Location(this.x, this.y - SnakeUtils.GRID_SIZE); case SOUTH -> new Location(this.x, this.y + SnakeUtils.GRID_SIZE);
case SOUTH: case EAST -> new Location(this.x + SnakeUtils.GRID_SIZE, this.y);
return new Location(this.x, this.y + SnakeUtils.GRID_SIZE); case WEST -> new Location(this.x - SnakeUtils.GRID_SIZE, this.y);
case EAST: case NONE -> this;
return new Location(this.x + SnakeUtils.GRID_SIZE, this.y); };
case WEST:
return new Location(this.x - SnakeUtils.GRID_SIZE, this.y);
case NONE:
// fall through
default:
return this;
}
} }
@Override @Override

@ -34,20 +34,13 @@ public class Location {
} }
public Location getAdjacentLocation(Direction direction) { public Location getAdjacentLocation(Direction direction) {
switch (direction) { return switch (direction) {
case NORTH: case NORTH -> new Location(this.x, this.y - SnakeUtils.GRID_SIZE);
return new Location(this.x, this.y - SnakeUtils.GRID_SIZE); case SOUTH -> new Location(this.x, this.y + SnakeUtils.GRID_SIZE);
case SOUTH: case EAST -> new Location(this.x + SnakeUtils.GRID_SIZE, this.y);
return new Location(this.x, this.y + SnakeUtils.GRID_SIZE); case WEST -> new Location(this.x - SnakeUtils.GRID_SIZE, this.y);
case EAST: case NONE -> this;
return new Location(this.x + SnakeUtils.GRID_SIZE, this.y); };
case WEST:
return new Location(this.x - SnakeUtils.GRID_SIZE, this.y);
case NONE:
// fall through
default:
return this;
}
} }
@Override @Override

Loading…
Cancel
Save