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
jdk: oraclejdk8
jdk: openjdk:17
script: mvn clean test

16
pom.xml
View File

@ -11,6 +11,10 @@
<properties>
<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>
@ -18,25 +22,25 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
<version>${apache.commons.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
@ -49,8 +53,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>

View File

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

View File

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

View File

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

View File

@ -11,10 +11,10 @@ import de.dj_steam.bot.domain.ToyBot;
*/
public class DirectionCalculator {
private LoopedDirectionsArrayList<Direction> directions;
private final LoopedDirectionsArrayList<Direction> directions;
public DirectionCalculator() {
directions = new LoopedDirectionsArrayList();
directions = new LoopedDirectionsArrayList<>();
directions.addAll(Arrays.asList(Direction.values()));
}
@ -22,9 +22,9 @@ public class DirectionCalculator {
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);
} else if (turnDirection.getCommand().equals(Command.TURN_RIGHT)) {
} else if (turnDirection.command().equals(Command.TURN_RIGHT)) {
return directions.get(directions.indexOf(actualDirection) + 1);
}
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
* implement this, if new change variant is wanted.
*
* <p>
* 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 {
@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

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.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())));
}
}

View File

@ -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);
}
}

View File

@ -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));
}
}

View File

@ -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());
}
}

View File

@ -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)));

View File

@ -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() {

View File

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