mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-05 00:56:58 +08:00
Replace multiple ifs with switch
See gh-39259
This commit is contained in:
parent
316b415e95
commit
0613034e19
@ -80,17 +80,11 @@ public class SnakeWebSocketHandler extends TextWebSocketHandler {
|
||||
@Override
|
||||
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
|
||||
String payload = message.getPayload();
|
||||
if ("west".equals(payload)) {
|
||||
this.snake.setDirection(Direction.WEST);
|
||||
}
|
||||
else if ("north".equals(payload)) {
|
||||
this.snake.setDirection(Direction.NORTH);
|
||||
}
|
||||
else if ("east".equals(payload)) {
|
||||
this.snake.setDirection(Direction.EAST);
|
||||
}
|
||||
else if ("south".equals(payload)) {
|
||||
this.snake.setDirection(Direction.SOUTH);
|
||||
switch (payload) {
|
||||
case "west" -> this.snake.setDirection(Direction.WEST);
|
||||
case "north" -> this.snake.setDirection(Direction.NORTH);
|
||||
case "east" -> this.snake.setDirection(Direction.EAST);
|
||||
case "south" -> this.snake.setDirection(Direction.SOUTH);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,17 +80,11 @@ public class SnakeWebSocketHandler extends TextWebSocketHandler {
|
||||
@Override
|
||||
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
|
||||
String payload = message.getPayload();
|
||||
if ("west".equals(payload)) {
|
||||
this.snake.setDirection(Direction.WEST);
|
||||
}
|
||||
else if ("north".equals(payload)) {
|
||||
this.snake.setDirection(Direction.NORTH);
|
||||
}
|
||||
else if ("east".equals(payload)) {
|
||||
this.snake.setDirection(Direction.EAST);
|
||||
}
|
||||
else if ("south".equals(payload)) {
|
||||
this.snake.setDirection(Direction.SOUTH);
|
||||
switch (payload) {
|
||||
case "west" -> this.snake.setDirection(Direction.WEST);
|
||||
case "north" -> this.snake.setDirection(Direction.NORTH);
|
||||
case "east" -> this.snake.setDirection(Direction.EAST);
|
||||
case "south" -> this.snake.setDirection(Direction.SOUTH);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,17 +80,11 @@ public class SnakeWebSocketHandler extends TextWebSocketHandler {
|
||||
@Override
|
||||
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
|
||||
String payload = message.getPayload();
|
||||
if ("west".equals(payload)) {
|
||||
this.snake.setDirection(Direction.WEST);
|
||||
}
|
||||
else if ("north".equals(payload)) {
|
||||
this.snake.setDirection(Direction.NORTH);
|
||||
}
|
||||
else if ("east".equals(payload)) {
|
||||
this.snake.setDirection(Direction.EAST);
|
||||
}
|
||||
else if ("south".equals(payload)) {
|
||||
this.snake.setDirection(Direction.SOUTH);
|
||||
switch (payload) {
|
||||
case "west" -> this.snake.setDirection(Direction.WEST);
|
||||
case "north" -> this.snake.setDirection(Direction.NORTH);
|
||||
case "east" -> this.snake.setDirection(Direction.EAST);
|
||||
case "south" -> this.snake.setDirection(Direction.SOUTH);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user