Skip to content

Commit 458f989

Browse files
dreis2211wilkinsona
authored andcommitted
Use switch expressions where appropriate
See gh-31527
1 parent 836b08f commit 458f989

File tree

19 files changed

+136
-252
lines changed

19 files changed

+136
-252
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/AutoConfiguredHealthEndpointGroup.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,11 @@ public boolean showDetails(SecurityContext securityContext) {
9595
}
9696

9797
private boolean getShowResult(SecurityContext securityContext, Show show) {
98-
switch (show) {
99-
case NEVER:
100-
return false;
101-
case ALWAYS:
102-
return true;
103-
case WHEN_AUTHORIZED:
104-
return isAuthorized(securityContext);
105-
}
106-
throw new IllegalStateException("Unsupported 'show' value " + show);
98+
return switch (show) {
99+
case NEVER -> false;
100+
case ALWAYS -> true;
101+
case WHEN_AUTHORIZED -> isAuthorized(securityContext);
102+
};
107103
}
108104

109105
private boolean isAuthorized(SecurityContext securityContext) {

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ManagementErrorEndpoint.java

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,36 +76,27 @@ private ErrorAttributeOptions getErrorAttributeOptions(ServletWebRequest request
7676
}
7777

7878
private boolean includeStackTrace(ServletWebRequest request) {
79-
switch (this.errorProperties.getIncludeStacktrace()) {
80-
case ALWAYS:
81-
return true;
82-
case ON_PARAM:
83-
return getBooleanParameter(request, "trace");
84-
default:
85-
return false;
86-
}
79+
return switch (this.errorProperties.getIncludeStacktrace()) {
80+
case ALWAYS -> true;
81+
case ON_PARAM -> getBooleanParameter(request, "trace");
82+
default -> false;
83+
};
8784
}
8885

8986
private boolean includeMessage(ServletWebRequest request) {
90-
switch (this.errorProperties.getIncludeMessage()) {
91-
case ALWAYS:
92-
return true;
93-
case ON_PARAM:
94-
return getBooleanParameter(request, "message");
95-
default:
96-
return false;
97-
}
87+
return switch (this.errorProperties.getIncludeMessage()) {
88+
case ALWAYS -> true;
89+
case ON_PARAM -> getBooleanParameter(request, "message");
90+
default -> false;
91+
};
9892
}
9993

10094
private boolean includeBindingErrors(ServletWebRequest request) {
101-
switch (this.errorProperties.getIncludeBindingErrors()) {
102-
case ALWAYS:
103-
return true;
104-
case ON_PARAM:
105-
return getBooleanParameter(request, "errors");
106-
default:
107-
return false;
108-
}
95+
return switch (this.errorProperties.getIncludeBindingErrors()) {
96+
case ALWAYS -> true;
97+
case ON_PARAM -> getBooleanParameter(request, "errors");
98+
default -> false;
99+
};
109100
}
110101

111102
protected boolean getBooleanParameter(ServletWebRequest request, String parameterName) {

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/dynatrace/DynatraceMetricsExportAutoConfigurationTests.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,11 @@ static class CustomConfigConfiguration {
135135

136136
@Bean
137137
DynatraceConfig customConfig() {
138-
return (key) -> {
139-
switch (key) {
140-
case "dynatrace.uri":
141-
return "https://dynatrace.example.com";
142-
case "dynatrace.apiToken":
143-
return "abcde";
144-
case "dynatrace.deviceId":
145-
return "test";
146-
default:
147-
return null;
148-
}
138+
return (key) -> switch (key) {
139+
case "dynatrace.uri" -> "https://dynatrace.example.com";
140+
case "dynatrace.apiToken" -> "abcde";
141+
case "dynatrace.deviceId" -> "test";
142+
default -> null;
149143
};
150144
}
151145

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusPushGatewayManager.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,9 @@ private void shutdown(ShutdownOperation shutdownOperation) {
141141
}
142142
this.scheduled.cancel(false);
143143
switch (shutdownOperation) {
144-
case PUSH:
145-
case POST:
146-
post();
147-
break;
148-
case PUT:
149-
put();
150-
break;
151-
case DELETE:
152-
delete();
153-
break;
144+
case PUSH, POST -> post();
145+
case PUT -> put();
146+
case DELETE -> delete();
154147
}
155148
}
156149

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/quartz/QuartzEndpoint.java

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -265,26 +265,16 @@ private Map<String, Object> sanitizeJobDataMap(JobDataMap dataMap) {
265265
}
266266

267267
private static TemporalUnit temporalUnit(IntervalUnit unit) {
268-
switch (unit) {
269-
case DAY:
270-
return ChronoUnit.DAYS;
271-
case HOUR:
272-
return ChronoUnit.HOURS;
273-
case MINUTE:
274-
return ChronoUnit.MINUTES;
275-
case MONTH:
276-
return ChronoUnit.MONTHS;
277-
case SECOND:
278-
return ChronoUnit.SECONDS;
279-
case MILLISECOND:
280-
return ChronoUnit.MILLIS;
281-
case WEEK:
282-
return ChronoUnit.WEEKS;
283-
case YEAR:
284-
return ChronoUnit.YEARS;
285-
default:
286-
throw new IllegalArgumentException("Unknown IntervalUnit");
287-
}
268+
return switch (unit) {
269+
case DAY -> ChronoUnit.DAYS;
270+
case HOUR -> ChronoUnit.HOURS;
271+
case MINUTE -> ChronoUnit.MINUTES;
272+
case MONTH -> ChronoUnit.MONTHS;
273+
case SECOND -> ChronoUnit.SECONDS;
274+
case MILLISECOND -> ChronoUnit.MILLIS;
275+
case WEEK -> ChronoUnit.WEEKS;
276+
case YEAR -> ChronoUnit.YEARS;
277+
};
288278
}
289279

290280
/**

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnWebApplicationCondition.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,11 @@ public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeM
9999

100100
private ConditionOutcome isWebApplication(ConditionContext context, AnnotatedTypeMetadata metadata,
101101
boolean required) {
102-
switch (deduceType(metadata)) {
103-
case SERVLET:
104-
return isServletWebApplication(context);
105-
case REACTIVE:
106-
return isReactiveWebApplication(context);
107-
default:
108-
return isAnyWebApplication(context, required);
109-
}
102+
return switch (deduceType(metadata)) {
103+
case SERVLET -> isServletWebApplication(context);
104+
case REACTIVE -> isReactiveWebApplication(context);
105+
default -> isAnyWebApplication(context, required);
106+
};
110107
}
111108

112109
private ConditionOutcome isAnyWebApplication(ConditionContext context, boolean required) {

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,17 +315,13 @@ private void configureConstructorDetector(Jackson2ObjectMapperBuilder builder) {
315315
if (strategy != null) {
316316
builder.postConfigurer((objectMapper) -> {
317317
switch (strategy) {
318-
case USE_PROPERTIES_BASED:
318+
case USE_PROPERTIES_BASED ->
319319
objectMapper.setConstructorDetector(ConstructorDetector.USE_PROPERTIES_BASED);
320-
break;
321-
case USE_DELEGATING:
320+
case USE_DELEGATING ->
322321
objectMapper.setConstructorDetector(ConstructorDetector.USE_DELEGATING);
323-
break;
324-
case EXPLICIT_ONLY:
322+
case EXPLICIT_ONLY ->
325323
objectMapper.setConstructorDetector(ConstructorDetector.EXPLICIT_ONLY);
326-
break;
327-
default:
328-
objectMapper.setConstructorDetector(ConstructorDetector.DEFAULT);
324+
default -> objectMapper.setConstructorDetector(ConstructorDetector.DEFAULT);
329325
}
330326
});
331327
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/RedisSessionConfiguration.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,10 @@ class RedisSessionConfiguration {
5353
@Bean
5454
@ConditionalOnMissingBean
5555
ConfigureRedisAction configureRedisAction(RedisSessionProperties redisSessionProperties) {
56-
switch (redisSessionProperties.getConfigureAction()) {
57-
case NOTIFY_KEYSPACE_EVENTS:
58-
return new ConfigureNotifyKeyspaceEventsAction();
59-
case NONE:
60-
return ConfigureRedisAction.NO_OP;
61-
}
62-
throw new IllegalStateException(
63-
"Unsupported redis configure action '" + redisSessionProperties.getConfigureAction() + "'.");
56+
return switch (redisSessionProperties.getConfigureAction()) {
57+
case NOTIFY_KEYSPACE_EVENTS -> new ConfigureNotifyKeyspaceEventsAction();
58+
case NONE -> ConfigureRedisAction.NO_OP;
59+
};
6460
}
6561

6662
@Configuration(proxyBeanMethods = false)

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandler.java

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,11 @@ protected ErrorAttributeOptions getErrorAttributeOptions(ServerRequest request,
173173
* @return if the stacktrace attribute should be included
174174
*/
175175
protected boolean isIncludeStackTrace(ServerRequest request, MediaType produces) {
176-
switch (this.errorProperties.getIncludeStacktrace()) {
177-
case ALWAYS:
178-
return true;
179-
case ON_PARAM:
180-
return isTraceEnabled(request);
181-
default:
182-
return false;
183-
}
176+
return switch (this.errorProperties.getIncludeStacktrace()) {
177+
case ALWAYS -> true;
178+
case ON_PARAM -> isTraceEnabled(request);
179+
default -> false;
180+
};
184181
}
185182

186183
/**
@@ -190,14 +187,11 @@ protected boolean isIncludeStackTrace(ServerRequest request, MediaType produces)
190187
* @return if the message attribute should be included
191188
*/
192189
protected boolean isIncludeMessage(ServerRequest request, MediaType produces) {
193-
switch (this.errorProperties.getIncludeMessage()) {
194-
case ALWAYS:
195-
return true;
196-
case ON_PARAM:
197-
return isMessageEnabled(request);
198-
default:
199-
return false;
200-
}
190+
return switch (this.errorProperties.getIncludeMessage()) {
191+
case ALWAYS -> true;
192+
case ON_PARAM -> isMessageEnabled(request);
193+
default -> false;
194+
};
201195
}
202196

203197
/**
@@ -207,14 +201,11 @@ protected boolean isIncludeMessage(ServerRequest request, MediaType produces) {
207201
* @return if the errors attribute should be included
208202
*/
209203
protected boolean isIncludeBindingErrors(ServerRequest request, MediaType produces) {
210-
switch (this.errorProperties.getIncludeBindingErrors()) {
211-
case ALWAYS:
212-
return true;
213-
case ON_PARAM:
214-
return isBindingErrorsEnabled(request);
215-
default:
216-
return false;
217-
}
204+
return switch (this.errorProperties.getIncludeBindingErrors()) {
205+
case ALWAYS -> true;
206+
case ON_PARAM -> isBindingErrorsEnabled(request);
207+
default -> false;
208+
};
218209
}
219210

220211
/**

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorController.java

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,11 @@ protected ErrorAttributeOptions getErrorAttributeOptions(HttpServletRequest requ
131131
* @return if the stacktrace attribute should be included
132132
*/
133133
protected boolean isIncludeStackTrace(HttpServletRequest request, MediaType produces) {
134-
switch (getErrorProperties().getIncludeStacktrace()) {
135-
case ALWAYS:
136-
return true;
137-
case ON_PARAM:
138-
return getTraceParameter(request);
139-
default:
140-
return false;
141-
}
134+
return switch (getErrorProperties().getIncludeStacktrace()) {
135+
case ALWAYS -> true;
136+
case ON_PARAM -> getTraceParameter(request);
137+
default -> false;
138+
};
142139
}
143140

144141
/**
@@ -148,14 +145,11 @@ protected boolean isIncludeStackTrace(HttpServletRequest request, MediaType prod
148145
* @return if the message attribute should be included
149146
*/
150147
protected boolean isIncludeMessage(HttpServletRequest request, MediaType produces) {
151-
switch (getErrorProperties().getIncludeMessage()) {
152-
case ALWAYS:
153-
return true;
154-
case ON_PARAM:
155-
return getMessageParameter(request);
156-
default:
157-
return false;
158-
}
148+
return switch (getErrorProperties().getIncludeMessage()) {
149+
case ALWAYS -> true;
150+
case ON_PARAM -> getMessageParameter(request);
151+
default -> false;
152+
};
159153
}
160154

161155
/**
@@ -165,14 +159,11 @@ protected boolean isIncludeMessage(HttpServletRequest request, MediaType produce
165159
* @return if the errors attribute should be included
166160
*/
167161
protected boolean isIncludeBindingErrors(HttpServletRequest request, MediaType produces) {
168-
switch (getErrorProperties().getIncludeBindingErrors()) {
169-
case ALWAYS:
170-
return true;
171-
case ON_PARAM:
172-
return getErrorsParameter(request);
173-
default:
174-
return false;
175-
}
162+
return switch (getErrorProperties().getIncludeBindingErrors()) {
163+
case ALWAYS -> true;
164+
case ON_PARAM -> getErrorsParameter(request);
165+
default -> false;
166+
};
176167
}
177168

178169
/**

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/filter/FilterAnnotations.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,12 @@ private TypeFilter createTypeFilter(FilterType filterType, Class<?> filterClass)
8585
}
8686

8787
private TypeFilter createTypeFilter(FilterType filterType, String pattern) {
88-
switch (filterType) {
89-
case ASPECTJ:
90-
return new AspectJTypeFilter(pattern, this.classLoader);
91-
case REGEX:
92-
return new RegexPatternTypeFilter(Pattern.compile(pattern));
93-
}
94-
throw new IllegalArgumentException("Filter type not supported with String pattern: " + filterType);
88+
return switch (filterType) {
89+
case ASPECTJ -> new AspectJTypeFilter(pattern, this.classLoader);
90+
case REGEX -> new RegexPatternTypeFilter(Pattern.compile(pattern));
91+
default ->
92+
throw new IllegalArgumentException("Filter type not supported with String pattern: " + filterType);
93+
};
9594
}
9695

9796
@Override

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/LogUpdateEvent.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,8 @@ public class LogUpdateEvent extends UpdateEvent {
5454

5555
public void print() {
5656
switch (this.streamType) {
57-
case STD_OUT:
58-
System.out.println(this);
59-
return;
60-
case STD_ERR:
61-
System.err.println(this);
62-
return;
57+
case STD_OUT -> System.out.println(this);
58+
case STD_ERR -> System.err.println(this);
6359
}
6460
}
6561

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/AotProcessor.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,11 @@ private void registerEntryPointHint(DefaultGenerationContext generationContext,
167167
}
168168

169169
private Path getRoot(Kind kind) {
170-
switch (kind) {
171-
case SOURCE:
172-
return this.sourceOutput;
173-
case RESOURCE:
174-
return this.resourceOutput;
175-
case CLASS:
176-
return this.classOutput;
177-
}
178-
throw new IllegalStateException("Unsupported kind " + kind);
170+
return switch (kind) {
171+
case SOURCE -> this.sourceOutput;
172+
case RESOURCE -> this.resourceOutput;
173+
case CLASS -> this.classOutput;
174+
};
179175
}
180176

181177
private void writeHints(RuntimeHints hints) {

0 commit comments

Comments
 (0)