* this is a general command implementation. - * + *
* The arguments as {@link String} is intentionally, to be flexible * define some different argument formats. - * + *
* Use some kind of {@link List} for arguments in futher implementations.
*/
-@Getter
-@EqualsAndHashCode
-public class Command {
+public record Command(String command, Optional
* use {@link Command} to define new commans to implement
*/
diff --git a/src/main/java/de/dj_steam/bot/moving/strategy/MoveStrategy.java b/src/main/java/de/dj_steam/bot/moving/strategy/MoveStrategy.java
index 13f7e4d..3772d1b 100644
--- a/src/main/java/de/dj_steam/bot/moving/strategy/MoveStrategy.java
+++ b/src/main/java/de/dj_steam/bot/moving/strategy/MoveStrategy.java
@@ -12,7 +12,7 @@ import de.dj_steam.bot.domain.ToyBotField;
public class MoveStrategy implements ChangingStrategy {
@Override
public void change(final ToyBot toyBot, final ToyBotField toyBotField, final Command command) {
- if (command.getCommand().equals(Command.MOVE)) {
+ if (command.command().equals(Command.MOVE)) {
Position newPosition = toyBot.getPosition();
// TODO - refactor this ugly code
diff --git a/src/main/java/de/dj_steam/bot/moving/strategy/PlaceStrategy.java b/src/main/java/de/dj_steam/bot/moving/strategy/PlaceStrategy.java
index 9835963..d9d5e5c 100644
--- a/src/main/java/de/dj_steam/bot/moving/strategy/PlaceStrategy.java
+++ b/src/main/java/de/dj_steam/bot/moving/strategy/PlaceStrategy.java
@@ -9,6 +9,8 @@ import de.dj_steam.bot.domain.Position;
import de.dj_steam.bot.domain.ToyBot;
import de.dj_steam.bot.domain.ToyBotField;
+import java.util.NoSuchElementException;
+
/**
* @author steam
*/
@@ -16,10 +18,10 @@ public class PlaceStrategy implements ChangingStrategy {
@Override
public void change(ToyBot toyBot, ToyBotField toyBotField, Command command) {
- if (command.getCommand().equals(Command.PLACE) &&
+ if (command.command().equals(Command.PLACE) &&
isPlaceCommandValid(command) &&
canBePlaced(toyBotField, command)) {
- String[] arguments = command.getArguments().get().split(",");
+ String[] arguments = command.arguments().orElseThrow(NoSuchElementException::new).split(",");
toyBot.setPosition(new Position(Integer.parseInt(arguments[0].trim()), Integer.parseInt(arguments[1].trim())));
toyBot.setDirection(Direction.valueOf(arguments[2].trim()));
@@ -27,8 +29,8 @@ public class PlaceStrategy implements ChangingStrategy {
}
boolean isPlaceCommandValid(Command command) {
- if (command.getArguments().isPresent()) {
- String[] arguments = command.getArguments().get().split(",");
+ if (command.arguments().isPresent()) {
+ String[] arguments = command.arguments().orElseThrow(NoSuchElementException::new).split(",");
return arguments.length == 3 &&
StringUtils.isNumeric(arguments[0].trim()) &&
StringUtils.isNumeric(arguments[1].trim()) &&
@@ -38,7 +40,7 @@ public class PlaceStrategy implements ChangingStrategy {
}
boolean canBePlaced(final ToyBotField toyBotField, final Command command) {
- String[] arguments = command.getArguments().get().split(",");
+ String[] arguments = command.arguments().orElseThrow(NoSuchElementException::new).split(",");
return toyBotField.isInsideTheField(new Position(Integer.parseInt(arguments[0].trim()),Integer.parseInt(arguments[1].trim())));
}
}
diff --git a/src/main/java/de/dj_steam/bot/moving/strategy/ReportStrategy.java b/src/main/java/de/dj_steam/bot/moving/strategy/ReportStrategy.java
index 2e2199c..5f57e58 100644
--- a/src/main/java/de/dj_steam/bot/moving/strategy/ReportStrategy.java
+++ b/src/main/java/de/dj_steam/bot/moving/strategy/ReportStrategy.java
@@ -10,7 +10,7 @@ import de.dj_steam.bot.domain.ToyBotField;
public class ReportStrategy implements ChangingStrategy {
@Override
public void change(final ToyBot toyBot, final ToyBotField toyBotField, final Command command) {
- if (command.getCommand().equals(Command.REPORT)) {
+ if (command.command().equals(Command.REPORT)) {
printReport(toyBot);
}
}
diff --git a/src/main/java/de/dj_steam/bot/moving/strategy/TurnStrategy.java b/src/main/java/de/dj_steam/bot/moving/strategy/TurnStrategy.java
index a076af6..91c1448 100644
--- a/src/main/java/de/dj_steam/bot/moving/strategy/TurnStrategy.java
+++ b/src/main/java/de/dj_steam/bot/moving/strategy/TurnStrategy.java
@@ -10,7 +10,7 @@ import de.dj_steam.bot.moving.DirectionCalculator;
*/
public class TurnStrategy implements ChangingStrategy {
- private DirectionCalculator directionCalculator;
+ private final DirectionCalculator directionCalculator;
public TurnStrategy() {
directionCalculator = new DirectionCalculator();
@@ -18,7 +18,7 @@ public class TurnStrategy implements ChangingStrategy {
@Override
public void change(final ToyBot toyBot, final ToyBotField toyBotField, final Command command) {
- if (command.getCommand().equals(Command.TURN_LEFT) || command.getCommand().equals(Command.TURN_RIGHT)) {
+ if (command.command().equals(Command.TURN_LEFT) || command.command().equals(Command.TURN_RIGHT)) {
toyBot.setDirection(directionCalculator.calculateNewDirection(toyBot, command));
}
}
diff --git a/src/test/java/de/dj_steam/bot/cli/LoopingConsoleTest.java b/src/test/java/de/dj_steam/bot/cli/LoopingConsoleTest.java
index dcb3914..9d59ffe 100644
--- a/src/test/java/de/dj_steam/bot/cli/LoopingConsoleTest.java
+++ b/src/test/java/de/dj_steam/bot/cli/LoopingConsoleTest.java
@@ -15,28 +15,28 @@ public class LoopingConsoleTest {
@Test(expected = InvalidUserInputException.class)
- public void createCommandExpectedUserInputExceptionOnEmptyInput() throws Exception {
+ public void createCommandExpectedUserInputExceptionOnEmptyInput() {
LoopingConsole.createCommand("");
}
@Test(expected = InvalidUserInputException.class)
- public void createCommandExpectedUserInputExceptionOnMoreThenTwoParams() throws Exception {
+ public void createCommandExpectedUserInputExceptionOnMoreThenTwoParams() {
LoopingConsole.createCommand("INPUT X,Y,F WRONG_PARAM");
}
@Test
public void createCommandWithoutParam() {
Command command = LoopingConsole.createCommand("INPUT");
- assertEquals("INPUT", command.getCommand());
- assertFalse(command.getArguments().isPresent());
+ assertEquals("INPUT", command.command());
+ assertFalse(command.arguments().isPresent());
}
@Test
public void createCommandWithParam() {
Command command = LoopingConsole.createCommand("INPUT X,Y,F");
- assertEquals("INPUT", command.getCommand());
- assertTrue(command.getArguments().isPresent());
- assertEquals("X,Y,F", command.getArguments().get());
+ assertEquals("INPUT", command.command());
+ assertTrue(command.arguments().isPresent());
+ assertEquals("X,Y,F", command.arguments().get());
}
}
\ No newline at end of file
diff --git a/src/test/java/de/dj_steam/bot/domain/ToyBotFieldTest.java b/src/test/java/de/dj_steam/bot/domain/ToyBotFieldTest.java
index e2c9b41..2764099 100644
--- a/src/test/java/de/dj_steam/bot/domain/ToyBotFieldTest.java
+++ b/src/test/java/de/dj_steam/bot/domain/ToyBotFieldTest.java
@@ -10,7 +10,7 @@ import org.junit.Test;
*/
public class ToyBotFieldTest {
@Test
- public void isInsideTheField() throws Exception {
+ public void isInsideTheField() {
ToyBotField toyBotField = new ToyBotField();
assertTrue(toyBotField.isInsideTheField(new Position(0,0)));
assertTrue(toyBotField.isInsideTheField(new Position(4,4)));
diff --git a/src/test/java/de/dj_steam/bot/engine/RobotEngineTest.java b/src/test/java/de/dj_steam/bot/engine/RobotEngineTest.java
index c91c411..2150316 100644
--- a/src/test/java/de/dj_steam/bot/engine/RobotEngineTest.java
+++ b/src/test/java/de/dj_steam/bot/engine/RobotEngineTest.java
@@ -26,7 +26,7 @@ public class RobotEngineTest {
private Command moveCommand;
private Command placeCommand;
- private ToyBotField toyBotField = new ToyBotField();
+ private final ToyBotField toyBotField = new ToyBotField();
@Before
public void setup() {
diff --git a/src/test/java/de/dj_steam/bot/moving/LoopedDirectionsArrayListTest.java b/src/test/java/de/dj_steam/bot/moving/LoopedDirectionsArrayListTest.java
index 09951be..0b29845 100644
--- a/src/test/java/de/dj_steam/bot/moving/LoopedDirectionsArrayListTest.java
+++ b/src/test/java/de/dj_steam/bot/moving/LoopedDirectionsArrayListTest.java
@@ -13,11 +13,11 @@ import de.dj_steam.bot.domain.Direction;
* @author steam
*/
public class LoopedDirectionsArrayListTest {
- private LoopedDirectionsArrayList directions;
+ private LoopedDirectionsArrayList