Replace multiple ifs with switch

See gh-39259
This commit is contained in:
Tobias Lippert 2024-01-21 20:03:16 +01:00 committed by Phillip Webb
parent 316b415e95
commit 0613034e19
3 changed files with 15 additions and 33 deletions

View File

@ -80,17 +80,11 @@ public class SnakeWebSocketHandler extends TextWebSocketHandler {
@Override @Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
String payload = message.getPayload(); String payload = message.getPayload();
if ("west".equals(payload)) { switch (payload) {
this.snake.setDirection(Direction.WEST); case "west" -> this.snake.setDirection(Direction.WEST);
} case "north" -> this.snake.setDirection(Direction.NORTH);
else if ("north".equals(payload)) { case "east" -> this.snake.setDirection(Direction.EAST);
this.snake.setDirection(Direction.NORTH); case "south" -> this.snake.setDirection(Direction.SOUTH);
}
else if ("east".equals(payload)) {
this.snake.setDirection(Direction.EAST);
}
else if ("south".equals(payload)) {
this.snake.setDirection(Direction.SOUTH);
} }
} }

View File

@ -80,17 +80,11 @@ public class SnakeWebSocketHandler extends TextWebSocketHandler {
@Override @Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
String payload = message.getPayload(); String payload = message.getPayload();
if ("west".equals(payload)) { switch (payload) {
this.snake.setDirection(Direction.WEST); case "west" -> this.snake.setDirection(Direction.WEST);
} case "north" -> this.snake.setDirection(Direction.NORTH);
else if ("north".equals(payload)) { case "east" -> this.snake.setDirection(Direction.EAST);
this.snake.setDirection(Direction.NORTH); case "south" -> this.snake.setDirection(Direction.SOUTH);
}
else if ("east".equals(payload)) {
this.snake.setDirection(Direction.EAST);
}
else if ("south".equals(payload)) {
this.snake.setDirection(Direction.SOUTH);
} }
} }

View File

@ -80,17 +80,11 @@ public class SnakeWebSocketHandler extends TextWebSocketHandler {
@Override @Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
String payload = message.getPayload(); String payload = message.getPayload();
if ("west".equals(payload)) { switch (payload) {
this.snake.setDirection(Direction.WEST); case "west" -> this.snake.setDirection(Direction.WEST);
} case "north" -> this.snake.setDirection(Direction.NORTH);
else if ("north".equals(payload)) { case "east" -> this.snake.setDirection(Direction.EAST);
this.snake.setDirection(Direction.NORTH); case "south" -> this.snake.setDirection(Direction.SOUTH);
}
else if ("east".equals(payload)) {
this.snake.setDirection(Direction.EAST);
}
else if ("south".equals(payload)) {
this.snake.setDirection(Direction.SOUTH);
} }
} }