Skip to content

Use switch expressions where appropriate #31527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,11 @@ public boolean showDetails(SecurityContext securityContext) {
}

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

private boolean isAuthorized(SecurityContext securityContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,36 +76,27 @@ private ErrorAttributeOptions getErrorAttributeOptions(ServletWebRequest request
}

private boolean includeStackTrace(ServletWebRequest request) {
switch (this.errorProperties.getIncludeStacktrace()) {
case ALWAYS:
return true;
case ON_PARAM:
return getBooleanParameter(request, "trace");
default:
return false;
}
return switch (this.errorProperties.getIncludeStacktrace()) {
case ALWAYS -> true;
case ON_PARAM -> getBooleanParameter(request, "trace");
default -> false;
};
}

private boolean includeMessage(ServletWebRequest request) {
switch (this.errorProperties.getIncludeMessage()) {
case ALWAYS:
return true;
case ON_PARAM:
return getBooleanParameter(request, "message");
default:
return false;
}
return switch (this.errorProperties.getIncludeMessage()) {
case ALWAYS -> true;
case ON_PARAM -> getBooleanParameter(request, "message");
default -> false;
};
}

private boolean includeBindingErrors(ServletWebRequest request) {
switch (this.errorProperties.getIncludeBindingErrors()) {
case ALWAYS:
return true;
case ON_PARAM:
return getBooleanParameter(request, "errors");
default:
return false;
}
return switch (this.errorProperties.getIncludeBindingErrors()) {
case ALWAYS -> true;
case ON_PARAM -> getBooleanParameter(request, "errors");
default -> false;
};
}

protected boolean getBooleanParameter(ServletWebRequest request, String parameterName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,11 @@ static class CustomConfigConfiguration {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,9 @@ private void shutdown(ShutdownOperation shutdownOperation) {
}
this.scheduled.cancel(false);
switch (shutdownOperation) {
case PUSH:
case POST:
post();
break;
case PUT:
put();
break;
case DELETE:
delete();
break;
case PUSH, POST -> post();
case PUT -> put();
case DELETE -> delete();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,26 +265,16 @@ private Map<String, Object> sanitizeJobDataMap(JobDataMap dataMap) {
}

private static TemporalUnit temporalUnit(IntervalUnit unit) {
switch (unit) {
case DAY:
return ChronoUnit.DAYS;
case HOUR:
return ChronoUnit.HOURS;
case MINUTE:
return ChronoUnit.MINUTES;
case MONTH:
return ChronoUnit.MONTHS;
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");
}
return switch (unit) {
case DAY -> ChronoUnit.DAYS;
case HOUR -> ChronoUnit.HOURS;
case MINUTE -> ChronoUnit.MINUTES;
case MONTH -> ChronoUnit.MONTHS;
case SECOND -> ChronoUnit.SECONDS;
case MILLISECOND -> ChronoUnit.MILLIS;
case WEEK -> ChronoUnit.WEEKS;
case YEAR -> ChronoUnit.YEARS;
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,11 @@ public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeM

private ConditionOutcome isWebApplication(ConditionContext context, AnnotatedTypeMetadata metadata,
boolean required) {
switch (deduceType(metadata)) {
case SERVLET:
return isServletWebApplication(context);
case REACTIVE:
return isReactiveWebApplication(context);
default:
return isAnyWebApplication(context, required);
}
return switch (deduceType(metadata)) {
case SERVLET -> isServletWebApplication(context);
case REACTIVE -> isReactiveWebApplication(context);
default -> isAnyWebApplication(context, required);
};
}

private ConditionOutcome isAnyWebApplication(ConditionContext context, boolean required) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,13 @@ private void configureConstructorDetector(Jackson2ObjectMapperBuilder builder) {
if (strategy != null) {
builder.postConfigurer((objectMapper) -> {
switch (strategy) {
case USE_PROPERTIES_BASED:
case USE_PROPERTIES_BASED ->
objectMapper.setConstructorDetector(ConstructorDetector.USE_PROPERTIES_BASED);
break;
case USE_DELEGATING:
case USE_DELEGATING ->
objectMapper.setConstructorDetector(ConstructorDetector.USE_DELEGATING);
break;
case EXPLICIT_ONLY:
case EXPLICIT_ONLY ->
objectMapper.setConstructorDetector(ConstructorDetector.EXPLICIT_ONLY);
break;
default:
objectMapper.setConstructorDetector(ConstructorDetector.DEFAULT);
default -> objectMapper.setConstructorDetector(ConstructorDetector.DEFAULT);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,10 @@ class RedisSessionConfiguration {
@Bean
@ConditionalOnMissingBean
ConfigureRedisAction configureRedisAction(RedisSessionProperties redisSessionProperties) {
switch (redisSessionProperties.getConfigureAction()) {
case NOTIFY_KEYSPACE_EVENTS:
return new ConfigureNotifyKeyspaceEventsAction();
case NONE:
return ConfigureRedisAction.NO_OP;
}
throw new IllegalStateException(
"Unsupported redis configure action '" + redisSessionProperties.getConfigureAction() + "'.");
return switch (redisSessionProperties.getConfigureAction()) {
case NOTIFY_KEYSPACE_EVENTS -> new ConfigureNotifyKeyspaceEventsAction();
case NONE -> ConfigureRedisAction.NO_OP;
};
}

@Configuration(proxyBeanMethods = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,11 @@ protected ErrorAttributeOptions getErrorAttributeOptions(ServerRequest request,
* @return if the stacktrace attribute should be included
*/
protected boolean isIncludeStackTrace(ServerRequest request, MediaType produces) {
switch (this.errorProperties.getIncludeStacktrace()) {
case ALWAYS:
return true;
case ON_PARAM:
return isTraceEnabled(request);
default:
return false;
}
return switch (this.errorProperties.getIncludeStacktrace()) {
case ALWAYS -> true;
case ON_PARAM -> isTraceEnabled(request);
default -> false;
};
}

/**
Expand All @@ -190,14 +187,11 @@ protected boolean isIncludeStackTrace(ServerRequest request, MediaType produces)
* @return if the message attribute should be included
*/
protected boolean isIncludeMessage(ServerRequest request, MediaType produces) {
switch (this.errorProperties.getIncludeMessage()) {
case ALWAYS:
return true;
case ON_PARAM:
return isMessageEnabled(request);
default:
return false;
}
return switch (this.errorProperties.getIncludeMessage()) {
case ALWAYS -> true;
case ON_PARAM -> isMessageEnabled(request);
default -> false;
};
}

/**
Expand All @@ -207,14 +201,11 @@ protected boolean isIncludeMessage(ServerRequest request, MediaType produces) {
* @return if the errors attribute should be included
*/
protected boolean isIncludeBindingErrors(ServerRequest request, MediaType produces) {
switch (this.errorProperties.getIncludeBindingErrors()) {
case ALWAYS:
return true;
case ON_PARAM:
return isBindingErrorsEnabled(request);
default:
return false;
}
return switch (this.errorProperties.getIncludeBindingErrors()) {
case ALWAYS -> true;
case ON_PARAM -> isBindingErrorsEnabled(request);
default -> false;
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,11 @@ protected ErrorAttributeOptions getErrorAttributeOptions(HttpServletRequest requ
* @return if the stacktrace attribute should be included
*/
protected boolean isIncludeStackTrace(HttpServletRequest request, MediaType produces) {
switch (getErrorProperties().getIncludeStacktrace()) {
case ALWAYS:
return true;
case ON_PARAM:
return getTraceParameter(request);
default:
return false;
}
return switch (getErrorProperties().getIncludeStacktrace()) {
case ALWAYS -> true;
case ON_PARAM -> getTraceParameter(request);
default -> false;
};
}

/**
Expand All @@ -148,14 +145,11 @@ protected boolean isIncludeStackTrace(HttpServletRequest request, MediaType prod
* @return if the message attribute should be included
*/
protected boolean isIncludeMessage(HttpServletRequest request, MediaType produces) {
switch (getErrorProperties().getIncludeMessage()) {
case ALWAYS:
return true;
case ON_PARAM:
return getMessageParameter(request);
default:
return false;
}
return switch (getErrorProperties().getIncludeMessage()) {
case ALWAYS -> true;
case ON_PARAM -> getMessageParameter(request);
default -> false;
};
}

/**
Expand All @@ -165,14 +159,11 @@ protected boolean isIncludeMessage(HttpServletRequest request, MediaType produce
* @return if the errors attribute should be included
*/
protected boolean isIncludeBindingErrors(HttpServletRequest request, MediaType produces) {
switch (getErrorProperties().getIncludeBindingErrors()) {
case ALWAYS:
return true;
case ON_PARAM:
return getErrorsParameter(request);
default:
return false;
}
return switch (getErrorProperties().getIncludeBindingErrors()) {
case ALWAYS -> true;
case ON_PARAM -> getErrorsParameter(request);
default -> false;
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,12 @@ private TypeFilter createTypeFilter(FilterType filterType, Class<?> filterClass)
}

private TypeFilter createTypeFilter(FilterType filterType, String pattern) {
switch (filterType) {
case ASPECTJ:
return new AspectJTypeFilter(pattern, this.classLoader);
case REGEX:
return new RegexPatternTypeFilter(Pattern.compile(pattern));
}
throw new IllegalArgumentException("Filter type not supported with String pattern: " + filterType);
return switch (filterType) {
case ASPECTJ -> new AspectJTypeFilter(pattern, this.classLoader);
case REGEX -> new RegexPatternTypeFilter(Pattern.compile(pattern));
default ->
throw new IllegalArgumentException("Filter type not supported with String pattern: " + filterType);
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ public class LogUpdateEvent extends UpdateEvent {

public void print() {
switch (this.streamType) {
case STD_OUT:
System.out.println(this);
return;
case STD_ERR:
System.err.println(this);
return;
case STD_OUT -> System.out.println(this);
case STD_ERR -> System.err.println(this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,11 @@ private void registerEntryPointHint(DefaultGenerationContext generationContext,
}

private Path getRoot(Kind kind) {
switch (kind) {
case SOURCE:
return this.sourceOutput;
case RESOURCE:
return this.resourceOutput;
case CLASS:
return this.classOutput;
}
throw new IllegalStateException("Unsupported kind " + kind);
return switch (kind) {
case SOURCE -> this.sourceOutput;
case RESOURCE -> this.resourceOutput;
case CLASS -> this.classOutput;
};
}

private void writeHints(RuntimeHints hints) {
Expand Down
Loading