try to reduce boilerplate files and code, also encapsulating even more the constructors
This commit is contained in:
parent
fd7306f1a1
commit
0455523ed9
|
@ -11,7 +11,14 @@ public class MarsRover implements RotableRiderRover {
|
|||
private final RoverActionReporter roverActionReporter;
|
||||
private Direction currentDirection;
|
||||
|
||||
public MarsRover(PlanetMap marsMap, Direction startingDirection) {
|
||||
public static class Provider implements RotableRiderRover.Provider {
|
||||
@Override
|
||||
public RotableRiderRover provide(PlanetMap map, Direction startingDirection) {
|
||||
return new MarsRover(map, startingDirection);
|
||||
}
|
||||
}
|
||||
|
||||
private MarsRover(PlanetMap marsMap, Direction startingDirection) {
|
||||
this.marsMap = marsMap;
|
||||
currentDirection = startingDirection;
|
||||
roverActionReporter = new RoverActionReporter();
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
package cat.hack3.codingtests.marsrover;
|
||||
|
||||
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
|
||||
import cat.hack3.codingtests.marsrover.api.RoverDeployer;
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
|
||||
|
||||
public class MarsRoverDeployer implements RoverDeployer {
|
||||
@Override
|
||||
public RotableRiderRover deploy(PlanetMap map, Direction startingDirection) {
|
||||
return new MarsRover(map, startingDirection);
|
||||
}
|
||||
}
|
|
@ -27,8 +27,15 @@ public class MarsMap implements PlanetMap {
|
|||
|
||||
private final MapIncrementalPositionResolver positionResolver;
|
||||
|
||||
public static class Provider implements PlanetMap.Provider {
|
||||
@Override
|
||||
public PlanetMap provide(int height, int width, Coordinates startingCoordinates, List<Coordinates> obstaclesLocalizations) {
|
||||
return new MarsMap(height, width, startingCoordinates, obstaclesLocalizations);
|
||||
}
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public MarsMap(int height, int width, Coordinates startingCoordinates, List<Coordinates>... obstaclesLocalizations) {
|
||||
private MarsMap(int height, int width, Coordinates startingCoordinates, List<Coordinates>... obstaclesLocalizations) {
|
||||
currentPosition = startingCoordinates;
|
||||
this.obstaclesLocalizations = obstaclesLocalizations.length > 0 ? obstaclesLocalizations[0] : List.of();
|
||||
positionResolver = new MapIncrementalPositionResolver(FIRST_POSITION_IN_MAP, INCREMENT_UNIT, height, width);
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
package cat.hack3.codingtests.marsrover.gps;
|
||||
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMapInitializer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MarsMapInitializer implements PlanetMapInitializer {
|
||||
@Override
|
||||
public PlanetMap setupNewMap(int height, int width, Coordinates startingCoordinates, List<Coordinates> obstaclesLocalizations) {
|
||||
return new MarsMap(height, width, startingCoordinates, obstaclesLocalizations);
|
||||
}
|
||||
@Override
|
||||
public PlanetMap setupNewMap(int height, int width, Coordinates startingCoordinates) {
|
||||
return new MarsMap(height, width, startingCoordinates);
|
||||
}
|
||||
}
|
|
@ -1,12 +1,15 @@
|
|||
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMapInitializer;
|
||||
import cat.hack3.codingtests.marsrover.MarsRover;
|
||||
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
|
||||
import cat.hack3.codingtests.marsrover.gps.MarsMap;
|
||||
|
||||
module mars.station {
|
||||
requires rover.api;
|
||||
requires java.logging;
|
||||
|
||||
provides PlanetMapInitializer
|
||||
with cat.hack3.codingtests.marsrover.gps.MarsMapInitializer;
|
||||
provides PlanetMap.Provider
|
||||
with MarsMap.Provider;
|
||||
|
||||
provides cat.hack3.codingtests.marsrover.api.RoverDeployer
|
||||
with cat.hack3.codingtests.marsrover.MarsRoverDeployer;
|
||||
provides RotableRiderRover.Provider
|
||||
with MarsRover.Provider;
|
||||
}
|
|
@ -2,6 +2,7 @@ package cat.hack3.codingtests.marsrover.api;
|
|||
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
|
||||
|
||||
public interface RotableRiderRover {
|
||||
enum Rotation {LEFT, RIGHT}
|
||||
|
@ -13,4 +14,8 @@ public interface RotableRiderRover {
|
|||
Coordinates getCurrentCoordinates();
|
||||
|
||||
Direction getCurrentDirection();
|
||||
|
||||
interface Provider {
|
||||
RotableRiderRover provide(PlanetMap map, Direction startingDirection);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
package cat.hack3.codingtests.marsrover.api;
|
||||
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
|
||||
|
||||
public interface RoverDeployer {
|
||||
RotableRiderRover deploy(PlanetMap map, Direction startingDirection);
|
||||
}
|
|
@ -1,6 +1,9 @@
|
|||
package cat.hack3.codingtests.marsrover.api.cartography;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PlanetMap {
|
||||
|
||||
Coordinates getNextPositionTowards(Direction direction);
|
||||
|
||||
void updateLocation(Coordinates newPosition);
|
||||
|
@ -8,4 +11,8 @@ public interface PlanetMap {
|
|||
boolean willCollideWithObstacle(Coordinates newCoordinates);
|
||||
|
||||
Coordinates getCurrentPosition();
|
||||
|
||||
interface Provider {
|
||||
PlanetMap provide(int height, int width, Coordinates startingCoordinates, List<Coordinates> obstaclesLocalizations);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
package cat.hack3.codingtests.marsrover.api.cartography;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PlanetMapInitializer {
|
||||
PlanetMap setupNewMap(int height, int width, Coordinates startingCoordinates, List<Coordinates> obstaclesLocalizations);
|
||||
PlanetMap setupNewMap(int height, int width, Coordinates startingCoordinates);
|
||||
}
|
|
@ -1,5 +1,11 @@
|
|||
package cat.hack3.codingtests.marsrover.api.commands;
|
||||
|
||||
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
|
||||
|
||||
public interface CommandFactory {
|
||||
RoverCommand createCommand(RoverCommand.Type type);
|
||||
|
||||
interface Provider {
|
||||
CommandFactory provide(RotableRiderRover rover);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
package cat.hack3.codingtests.marsrover.api.commands;
|
||||
|
||||
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
|
||||
|
||||
public interface CommandFactoryInitializer {
|
||||
CommandFactory initFor(RotableRiderRover rover);
|
||||
}
|
|
@ -8,7 +8,14 @@ import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
|
|||
public class RoverCommandFactory implements CommandFactory {
|
||||
private final RotableRiderRover rover;
|
||||
|
||||
public RoverCommandFactory(RotableRiderRover rover) {
|
||||
public static class Provider implements CommandFactory.Provider {
|
||||
@Override
|
||||
public CommandFactory provide(RotableRiderRover rover) {
|
||||
return new RoverCommandFactory(rover);
|
||||
}
|
||||
}
|
||||
|
||||
private RoverCommandFactory(RotableRiderRover rover) {
|
||||
this.rover = rover;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
package cat.hack3.codingtests.marsrover.commands;
|
||||
|
||||
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.CommandFactory;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.CommandFactoryInitializer;
|
||||
|
||||
public class RoverCommandFactoryInitializer implements CommandFactoryInitializer {
|
||||
@Override
|
||||
public CommandFactory initFor(RotableRiderRover rover) {
|
||||
return new RoverCommandFactory(rover);
|
||||
}
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
import cat.hack3.codingtests.marsrover.api.commands.CommandFactoryInitializer;
|
||||
import cat.hack3.codingtests.marsrover.commands.RoverCommandFactoryInitializer;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.CommandFactory;
|
||||
import cat.hack3.codingtests.marsrover.commands.RoverCommandFactory;
|
||||
|
||||
module rover.commands {
|
||||
requires rover.api;
|
||||
|
||||
provides CommandFactoryInitializer
|
||||
with RoverCommandFactoryInitializer;
|
||||
provides CommandFactory.Provider
|
||||
with RoverCommandFactory.Provider;
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>mars-rover</artifactId>
|
||||
<groupId>cat.hack3.codingtests</groupId>
|
||||
<version>10.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>station-plugin</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cat.hack3.codingtests</groupId>
|
||||
<artifactId>rover-api</artifactId>
|
||||
<version>1.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,14 +0,0 @@
|
|||
package cat.hack3.codingtests.marsrover.plugin.station;
|
||||
|
||||
import cat.hack3.codingtests.marsrover.api.commands.CommandFactoryInitializer;
|
||||
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
public class CommandFactoryInitializerProvider {
|
||||
public CommandFactoryInitializer getNextAvailable() {
|
||||
return ServiceLoader
|
||||
.load(CommandFactoryInitializer.class)
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
}
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
package cat.hack3.codingtests.marsrover.plugin.station;
|
||||
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMapInitializer;
|
||||
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
public class PlanetMapInitializerProvider {
|
||||
public PlanetMapInitializer getNextAvailable() {
|
||||
return ServiceLoader
|
||||
.load(PlanetMapInitializer.class)
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
}
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
package cat.hack3.codingtests.marsrover.plugin.station;
|
||||
|
||||
import cat.hack3.codingtests.marsrover.api.RoverDeployer;
|
||||
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
public class RoverDeployerProvider {
|
||||
public RoverDeployer getNextAvailable() {
|
||||
return ServiceLoader
|
||||
.load(RoverDeployer.class)
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
module station.plugin {
|
||||
requires rover.api;
|
||||
|
||||
uses cat.hack3.codingtests.marsrover.api.cartography.PlanetMapInitializer;
|
||||
uses cat.hack3.codingtests.marsrover.api.RoverDeployer;
|
||||
uses cat.hack3.codingtests.marsrover.api.commands.CommandFactoryInitializer;
|
||||
|
||||
exports cat.hack3.codingtests.marsrover.plugin.station;
|
||||
}
|
|
@ -6,11 +6,9 @@ import cat.hack3.codingtests.marsrover.api.cartography.Direction;
|
|||
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.CommandFactory;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
|
||||
import cat.hack3.codingtests.marsrover.plugin.station.CommandFactoryInitializerProvider;
|
||||
import cat.hack3.codingtests.marsrover.plugin.station.PlanetMapInitializerProvider;
|
||||
import cat.hack3.codingtests.marsrover.plugin.station.RoverDeployerProvider;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static cat.hack3.codingtests.marsrover.api.cartography.Direction.SOUTH;
|
||||
|
@ -26,14 +24,7 @@ public class CommonTests {
|
|||
protected RoverCommand turnLeftCommand;
|
||||
protected RoverCommand turnRightCommand;
|
||||
|
||||
private PlanetMapInitializerProvider gpsProvider;
|
||||
private RoverDeployerProvider roverProvider;
|
||||
private CommandFactoryInitializerProvider commandFactoryProvider;
|
||||
|
||||
protected void setup() {
|
||||
gpsProvider = new PlanetMapInitializerProvider();
|
||||
roverProvider = new RoverDeployerProvider();
|
||||
commandFactoryProvider = new CommandFactoryInitializerProvider();
|
||||
|
||||
int mapWidth = 10;
|
||||
int mapHeight = 10;
|
||||
|
@ -41,10 +32,14 @@ public class CommonTests {
|
|||
int longitudeStartingPoint = 3;
|
||||
var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint);
|
||||
|
||||
var marsMap = getMap(mapHeight, mapWidth, startingCoordinates);
|
||||
rover = getRover(marsMap);
|
||||
var marsMap = getProviderFor(PlanetMap.Provider.class)
|
||||
.provide(mapHeight, mapWidth, startingCoordinates, getObstacles());
|
||||
|
||||
var commandFactory = getCommandFactory(rover);
|
||||
rover = getProviderFor(RotableRiderRover.Provider.class)
|
||||
.provide(marsMap, CommonTests.STARTING_DIRECTION);
|
||||
|
||||
var commandFactory = getProviderFor(CommandFactory.Provider.class)
|
||||
.provide(rover);
|
||||
|
||||
moveForwardCommand = commandFactory.createCommand(MOVE_FORWARD);
|
||||
moveBackwardsCommand = commandFactory.createCommand(MOVE_BACKWARDS);
|
||||
|
@ -52,19 +47,11 @@ public class CommonTests {
|
|||
turnRightCommand = commandFactory.createCommand(TURN_RIGHT);
|
||||
}
|
||||
|
||||
private CommandFactory getCommandFactory(RotableRiderRover rover) {
|
||||
var commandFactoryInitializer = commandFactoryProvider.getNextAvailable();
|
||||
return commandFactoryInitializer.initFor(rover);
|
||||
}
|
||||
|
||||
private PlanetMap getMap(int mapHeight, int mapWidth, Coordinates startingCoordinates) {
|
||||
var planetInitializer = gpsProvider.getNextAvailable();
|
||||
return planetInitializer.setupNewMap(mapHeight, mapWidth, startingCoordinates, getObstacles());
|
||||
}
|
||||
|
||||
private RotableRiderRover getRover(PlanetMap marsMap) {
|
||||
var roverDeployer = roverProvider.getNextAvailable();
|
||||
return roverDeployer.deploy(marsMap, CommonTests.STARTING_DIRECTION);
|
||||
private <T> T getProviderFor(Class<T> interfaceProvider) {
|
||||
return ServiceLoader
|
||||
.load(interfaceProvider)
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
}
|
||||
|
||||
protected List<Coordinates> getObstacles() {
|
||||
|
|
|
@ -1,9 +1,16 @@
|
|||
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.CommandFactory;
|
||||
|
||||
module tests.suite {
|
||||
requires rover.api;
|
||||
requires station.plugin;
|
||||
requires mars.station;
|
||||
requires rover.commands;
|
||||
|
||||
uses PlanetMap.Provider;
|
||||
uses RotableRiderRover.Provider;
|
||||
uses CommandFactory.Provider;
|
||||
|
||||
requires org.testng;
|
||||
|
||||
exports cat.hack3.codingtests.marsrover.test;
|
||||
|
|
Loading…
Reference in New Issue