mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +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
|
@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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user