moved to openjdk:17, refactoring, dependency update, code cleanup

This commit is contained in:
steam 2022-10-13 12:57:52 +02:00
parent f1d48b032d
commit 091015f3da
16 changed files with 47 additions and 54 deletions

View File

@ -1,3 +1,3 @@
language: java language: java
jdk: oraclejdk8 jdk: openjdk:17
script: mvn clean test script: mvn clean test

16
pom.xml
View File

@ -11,6 +11,10 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>2.0.3</slf4j.version>
<lombok.version>1.18.24</lombok.version>
<apache.commons.version>3.12.0</apache.commons.version>
<junit.version>4.13.2</junit.version>
</properties> </properties>
@ -18,25 +22,25 @@
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId> <artifactId>slf4j-simple</artifactId>
<version>1.7.25</version> <version>${slf4j.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.16.14</version> <version>${lombok.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>3.4</version> <version>${apache.commons.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.12</version> <version>${junit.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
@ -49,8 +53,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version> <version>3.6.1</version>
<configuration> <configuration>
<source>1.8</source> <source>17</source>
<target>1.8</target> <target>17</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>

View File

@ -36,7 +36,7 @@ public class LoopingConsole {
printUsageBanner(); printUsageBanner();
} }
if (input.trim().toLowerCase().equals(EXIT_COMMAND)) { if (input.trim().equalsIgnoreCase(EXIT_COMMAND)) {
System.out.println("exiting"); System.out.println("exiting");
return; return;
} }

View File

@ -3,23 +3,18 @@ package de.dj_steam.bot.domain;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import lombok.EqualsAndHashCode;
import lombok.Getter;
/** /**
* @author steam * @author steam
* * <p>
* this is a general command implementation. * this is a general command implementation.
* * <p>
* The arguments as {@link String} is intentionally, to be flexible * The arguments as {@link String} is intentionally, to be flexible
* define some different argument formats. * define some different argument formats.
* * <p>
* Use some kind of {@link List} for arguments in futher implementations. * Use some kind of {@link List} for arguments in futher implementations.
*/ */
@Getter public record Command(String command, Optional<String> arguments) {
@EqualsAndHashCode
public class Command {
public static final String TURN_LEFT = "LEFT"; public static final String TURN_LEFT = "LEFT";
public static final String TURN_RIGHT = "RIGHT"; public static final String TURN_RIGHT = "RIGHT";
@ -27,12 +22,4 @@ public class Command {
public static final String PLACE = "PLACE"; public static final String PLACE = "PLACE";
public static final String REPORT = "REPORT"; public static final String REPORT = "REPORT";
private String command;
private Optional<String> arguments;
public Command(final String command, Optional<String> arguments) {
this.command = command;
this.arguments = arguments;
}
} }

View File

@ -12,8 +12,8 @@ import lombok.ToString;
@ToString @ToString
@EqualsAndHashCode @EqualsAndHashCode
public class Position { public class Position {
private int x; private final int x;
private int y; private final int y;
public Position() { public Position() {
this.x = 0; this.x = 0;

View File

@ -7,8 +7,8 @@ import lombok.Getter;
*/ */
@Getter @Getter
public class ToyBotField { public class ToyBotField {
private int width = 4; private final int width = 4;
private int height = 4; private final int height = 4;
public boolean isInsideTheField(final Position position) { public boolean isInsideTheField(final Position position) {
return (position.getX() >= 0 && return (position.getX() >= 0 &&

View File

@ -11,10 +11,10 @@ import de.dj_steam.bot.domain.ToyBot;
*/ */
public class DirectionCalculator { public class DirectionCalculator {
private LoopedDirectionsArrayList<Direction> directions; private final LoopedDirectionsArrayList<Direction> directions;
public DirectionCalculator() { public DirectionCalculator() {
directions = new LoopedDirectionsArrayList(); directions = new LoopedDirectionsArrayList<>();
directions.addAll(Arrays.asList(Direction.values())); directions.addAll(Arrays.asList(Direction.values()));
} }
@ -22,9 +22,9 @@ public class DirectionCalculator {
Direction actualDirection = toyBot.getDirection(); Direction actualDirection = toyBot.getDirection();
if (turnDirection.getCommand().equals(Command.TURN_LEFT)) { if (turnDirection.command().equals(Command.TURN_LEFT)) {
return directions.get(directions.indexOf(actualDirection) - 1); return directions.get(directions.indexOf(actualDirection) - 1);
} else if (turnDirection.getCommand().equals(Command.TURN_RIGHT)) { } else if (turnDirection.command().equals(Command.TURN_RIGHT)) {
return directions.get(directions.indexOf(actualDirection) + 1); return directions.get(directions.indexOf(actualDirection) + 1);
} }
return toyBot.getDirection(); return toyBot.getDirection();

View File

@ -9,7 +9,7 @@ import de.dj_steam.bot.domain.ToyBotField;
* *
* this strategy defines how the robot will be manipulated * this strategy defines how the robot will be manipulated
* implement this, if new change variant is wanted. * implement this, if new change variant is wanted.
* * <p>
* use {@link Command} to define new commans to implement * use {@link Command} to define new commans to implement
*/ */

View File

@ -12,7 +12,7 @@ import de.dj_steam.bot.domain.ToyBotField;
public class MoveStrategy implements ChangingStrategy { public class MoveStrategy implements ChangingStrategy {
@Override @Override
public void change(final ToyBot toyBot, final ToyBotField toyBotField, final Command command) { 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(); Position newPosition = toyBot.getPosition();
// TODO - refactor this ugly code // TODO - refactor this ugly code

View File

@ -9,6 +9,8 @@ import de.dj_steam.bot.domain.Position;
import de.dj_steam.bot.domain.ToyBot; import de.dj_steam.bot.domain.ToyBot;
import de.dj_steam.bot.domain.ToyBotField; import de.dj_steam.bot.domain.ToyBotField;
import java.util.NoSuchElementException;
/** /**
* @author steam * @author steam
*/ */
@ -16,10 +18,10 @@ public class PlaceStrategy implements ChangingStrategy {
@Override @Override
public void change(ToyBot toyBot, ToyBotField toyBotField, Command command) { public void change(ToyBot toyBot, ToyBotField toyBotField, Command command) {
if (command.getCommand().equals(Command.PLACE) && if (command.command().equals(Command.PLACE) &&
isPlaceCommandValid(command) && isPlaceCommandValid(command) &&
canBePlaced(toyBotField, 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.setPosition(new Position(Integer.parseInt(arguments[0].trim()), Integer.parseInt(arguments[1].trim())));
toyBot.setDirection(Direction.valueOf(arguments[2].trim())); toyBot.setDirection(Direction.valueOf(arguments[2].trim()));
@ -27,8 +29,8 @@ public class PlaceStrategy implements ChangingStrategy {
} }
boolean isPlaceCommandValid(Command command) { boolean isPlaceCommandValid(Command command) {
if (command.getArguments().isPresent()) { if (command.arguments().isPresent()) {
String[] arguments = command.getArguments().get().split(","); String[] arguments = command.arguments().orElseThrow(NoSuchElementException::new).split(",");
return arguments.length == 3 && return arguments.length == 3 &&
StringUtils.isNumeric(arguments[0].trim()) && StringUtils.isNumeric(arguments[0].trim()) &&
StringUtils.isNumeric(arguments[1].trim()) && StringUtils.isNumeric(arguments[1].trim()) &&
@ -38,7 +40,7 @@ public class PlaceStrategy implements ChangingStrategy {
} }
boolean canBePlaced(final ToyBotField toyBotField, final Command command) { 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()))); return toyBotField.isInsideTheField(new Position(Integer.parseInt(arguments[0].trim()),Integer.parseInt(arguments[1].trim())));
} }
} }

View File

@ -10,7 +10,7 @@ import de.dj_steam.bot.domain.ToyBotField;
public class ReportStrategy implements ChangingStrategy { public class ReportStrategy implements ChangingStrategy {
@Override @Override
public void change(final ToyBot toyBot, final ToyBotField toyBotField, final Command command) { 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); printReport(toyBot);
} }
} }

View File

@ -10,7 +10,7 @@ import de.dj_steam.bot.moving.DirectionCalculator;
*/ */
public class TurnStrategy implements ChangingStrategy { public class TurnStrategy implements ChangingStrategy {
private DirectionCalculator directionCalculator; private final DirectionCalculator directionCalculator;
public TurnStrategy() { public TurnStrategy() {
directionCalculator = new DirectionCalculator(); directionCalculator = new DirectionCalculator();
@ -18,7 +18,7 @@ public class TurnStrategy implements ChangingStrategy {
@Override @Override
public void change(final ToyBot toyBot, final ToyBotField toyBotField, final Command command) { 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)); toyBot.setDirection(directionCalculator.calculateNewDirection(toyBot, command));
} }
} }

View File

@ -15,28 +15,28 @@ public class LoopingConsoleTest {
@Test(expected = InvalidUserInputException.class) @Test(expected = InvalidUserInputException.class)
public void createCommandExpectedUserInputExceptionOnEmptyInput() throws Exception { public void createCommandExpectedUserInputExceptionOnEmptyInput() {
LoopingConsole.createCommand(""); LoopingConsole.createCommand("");
} }
@Test(expected = InvalidUserInputException.class) @Test(expected = InvalidUserInputException.class)
public void createCommandExpectedUserInputExceptionOnMoreThenTwoParams() throws Exception { public void createCommandExpectedUserInputExceptionOnMoreThenTwoParams() {
LoopingConsole.createCommand("INPUT X,Y,F WRONG_PARAM"); LoopingConsole.createCommand("INPUT X,Y,F WRONG_PARAM");
} }
@Test @Test
public void createCommandWithoutParam() { public void createCommandWithoutParam() {
Command command = LoopingConsole.createCommand("INPUT"); Command command = LoopingConsole.createCommand("INPUT");
assertEquals("INPUT", command.getCommand()); assertEquals("INPUT", command.command());
assertFalse(command.getArguments().isPresent()); assertFalse(command.arguments().isPresent());
} }
@Test @Test
public void createCommandWithParam() { public void createCommandWithParam() {
Command command = LoopingConsole.createCommand("INPUT X,Y,F"); Command command = LoopingConsole.createCommand("INPUT X,Y,F");
assertEquals("INPUT", command.getCommand()); assertEquals("INPUT", command.command());
assertTrue(command.getArguments().isPresent()); assertTrue(command.arguments().isPresent());
assertEquals("X,Y,F", command.getArguments().get()); assertEquals("X,Y,F", command.arguments().get());
} }
} }

View File

@ -10,7 +10,7 @@ import org.junit.Test;
*/ */
public class ToyBotFieldTest { public class ToyBotFieldTest {
@Test @Test
public void isInsideTheField() throws Exception { public void isInsideTheField() {
ToyBotField toyBotField = new ToyBotField(); ToyBotField toyBotField = new ToyBotField();
assertTrue(toyBotField.isInsideTheField(new Position(0,0))); assertTrue(toyBotField.isInsideTheField(new Position(0,0)));
assertTrue(toyBotField.isInsideTheField(new Position(4,4))); assertTrue(toyBotField.isInsideTheField(new Position(4,4)));

View File

@ -26,7 +26,7 @@ public class RobotEngineTest {
private Command moveCommand; private Command moveCommand;
private Command placeCommand; private Command placeCommand;
private ToyBotField toyBotField = new ToyBotField(); private final ToyBotField toyBotField = new ToyBotField();
@Before @Before
public void setup() { public void setup() {

View File

@ -13,11 +13,11 @@ import de.dj_steam.bot.domain.Direction;
* @author steam * @author steam
*/ */
public class LoopedDirectionsArrayListTest { public class LoopedDirectionsArrayListTest {
private LoopedDirectionsArrayList directions; private LoopedDirectionsArrayList<Direction> directions;
@Before @Before
public void setup() { public void setup() {
directions = new LoopedDirectionsArrayList(); directions = new LoopedDirectionsArrayList<>();
directions.addAll(Arrays.asList(Direction.values())); directions.addAll(Arrays.asList(Direction.values()));
} }