Skip to content

Commit 0613034

Browse files
tobias-lippertphilwebb
authored andcommitted
Replace multiple ifs with switch
See gh-39259
1 parent 316b415 commit 0613034

File tree

3 files changed

+15
-33
lines changed

3 files changed

+15
-33
lines changed

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-websocket-jetty/src/main/java/smoketest/websocket/jetty/snake/SnakeWebSocketHandler.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,11 @@ public void afterConnectionEstablished(WebSocketSession session) throws Exceptio
8080
@Override
8181
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
8282
String payload = message.getPayload();
83-
if ("west".equals(payload)) {
84-
this.snake.setDirection(Direction.WEST);
85-
}
86-
else if ("north".equals(payload)) {
87-
this.snake.setDirection(Direction.NORTH);
88-
}
89-
else if ("east".equals(payload)) {
90-
this.snake.setDirection(Direction.EAST);
91-
}
92-
else if ("south".equals(payload)) {
93-
this.snake.setDirection(Direction.SOUTH);
83+
switch (payload) {
84+
case "west" -> this.snake.setDirection(Direction.WEST);
85+
case "north" -> this.snake.setDirection(Direction.NORTH);
86+
case "east" -> this.snake.setDirection(Direction.EAST);
87+
case "south" -> this.snake.setDirection(Direction.SOUTH);
9488
}
9589
}
9690

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-websocket-tomcat/src/main/java/smoketest/websocket/tomcat/snake/SnakeWebSocketHandler.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,11 @@ public void afterConnectionEstablished(WebSocketSession session) throws Exceptio
8080
@Override
8181
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
8282
String payload = message.getPayload();
83-
if ("west".equals(payload)) {
84-
this.snake.setDirection(Direction.WEST);
85-
}
86-
else if ("north".equals(payload)) {
87-
this.snake.setDirection(Direction.NORTH);
88-
}
89-
else if ("east".equals(payload)) {
90-
this.snake.setDirection(Direction.EAST);
91-
}
92-
else if ("south".equals(payload)) {
93-
this.snake.setDirection(Direction.SOUTH);
83+
switch (payload) {
84+
case "west" -> this.snake.setDirection(Direction.WEST);
85+
case "north" -> this.snake.setDirection(Direction.NORTH);
86+
case "east" -> this.snake.setDirection(Direction.EAST);
87+
case "south" -> this.snake.setDirection(Direction.SOUTH);
9488
}
9589
}
9690

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-websocket-undertow/src/main/java/smoketest/websocket/undertow/snake/SnakeWebSocketHandler.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,11 @@ public void afterConnectionEstablished(WebSocketSession session) throws Exceptio
8080
@Override
8181
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
8282
String payload = message.getPayload();
83-
if ("west".equals(payload)) {
84-
this.snake.setDirection(Direction.WEST);
85-
}
86-
else if ("north".equals(payload)) {
87-
this.snake.setDirection(Direction.NORTH);
88-
}
89-
else if ("east".equals(payload)) {
90-
this.snake.setDirection(Direction.EAST);
91-
}
92-
else if ("south".equals(payload)) {
93-
this.snake.setDirection(Direction.SOUTH);
83+
switch (payload) {
84+
case "west" -> this.snake.setDirection(Direction.WEST);
85+
case "north" -> this.snake.setDirection(Direction.NORTH);
86+
case "east" -> this.snake.setDirection(Direction.EAST);
87+
case "south" -> this.snake.setDirection(Direction.SOUTH);
9488
}
9589
}
9690

0 commit comments

Comments
 (0)