1
0
Fork 0

apply JPMS to user interface module and improve some namings

This commit is contained in:
Xavier Fontanet 2024-06-24 16:28:54 +02:00
parent 0455523ed9
commit 02d849f696
18 changed files with 104 additions and 80 deletions

View File

@ -13,7 +13,7 @@ public class MarsRover implements RotableRiderRover {
public static class Provider implements RotableRiderRover.Provider { public static class Provider implements RotableRiderRover.Provider {
@Override @Override
public RotableRiderRover provide(PlanetMap map, Direction startingDirection) { public RotableRiderRover provideWith(PlanetMap map, Direction startingDirection) {
return new MarsRover(map, startingDirection); return new MarsRover(map, startingDirection);
} }
} }

View File

@ -29,7 +29,7 @@ public class MarsMap implements PlanetMap {
public static class Provider implements PlanetMap.Provider { public static class Provider implements PlanetMap.Provider {
@Override @Override
public PlanetMap provide(int height, int width, Coordinates startingCoordinates, List<Coordinates> obstaclesLocalizations) { public PlanetMap provideWith(int height, int width, Coordinates startingCoordinates, List<Coordinates> obstaclesLocalizations) {
return new MarsMap(height, width, startingCoordinates, obstaclesLocalizations); return new MarsMap(height, width, startingCoordinates, obstaclesLocalizations);
} }
} }

View File

@ -14,7 +14,6 @@
<module>user-interface-console</module> <module>user-interface-console</module>
<module>rover-commands</module> <module>rover-commands</module>
<module>rover-api</module> <module>rover-api</module>
<module>station-plugin</module>
<module>tests-suite</module> <module>tests-suite</module>
</modules> </modules>

View File

@ -16,6 +16,6 @@ public interface RotableRiderRover {
Direction getCurrentDirection(); Direction getCurrentDirection();
interface Provider { interface Provider {
RotableRiderRover provide(PlanetMap map, Direction startingDirection); RotableRiderRover provideWith(PlanetMap map, Direction startingDirection);
} }
} }

View File

@ -13,6 +13,6 @@ public interface PlanetMap {
Coordinates getCurrentPosition(); Coordinates getCurrentPosition();
interface Provider { interface Provider {
PlanetMap provide(int height, int width, Coordinates startingCoordinates, List<Coordinates> obstaclesLocalizations); PlanetMap provideWith(int height, int width, Coordinates startingCoordinates, List<Coordinates> obstaclesLocalizations);
} }
} }

View File

@ -1,11 +0,0 @@
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);
}
}

View File

@ -0,0 +1,11 @@
package cat.hack3.codingtests.marsrover.api.commands;
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
public interface RoverCommandFactory {
RoverCommand create(RoverCommand.Type type);
interface Provider {
RoverCommandFactory provideWith(RotableRiderRover rover);
}
}

View File

@ -2,25 +2,25 @@ package cat.hack3.codingtests.marsrover.commands;
import cat.hack3.codingtests.marsrover.api.RotableRiderRover; import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.commands.CommandFactory;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand; import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory;
public class RoverCommandFactory implements CommandFactory { public class RoverCommandFactoryImpl implements RoverCommandFactory {
private final RotableRiderRover rover; private final RotableRiderRover rover;
public static class Provider implements CommandFactory.Provider { public static class Provider implements RoverCommandFactory.Provider {
@Override @Override
public CommandFactory provide(RotableRiderRover rover) { public RoverCommandFactory provideWith(RotableRiderRover rover) {
return new RoverCommandFactory(rover); return new RoverCommandFactoryImpl(rover);
} }
} }
private RoverCommandFactory(RotableRiderRover rover) { private RoverCommandFactoryImpl(RotableRiderRover rover) {
this.rover = rover; this.rover = rover;
} }
@Override @Override
public RoverCommand createCommand(RoverCommand.Type type) { public RoverCommand create(RoverCommand.Type type) {
return switch (type) { return switch (type) {
case MOVE_FORWARD -> new MoveForwardCommand(rover); case MOVE_FORWARD -> new MoveForwardCommand(rover);
case MOVE_BACKWARDS -> new MoveBackwardsCommand(rover); case MOVE_BACKWARDS -> new MoveBackwardsCommand(rover);

View File

@ -1,9 +1,9 @@
import cat.hack3.codingtests.marsrover.api.commands.CommandFactory; import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory;
import cat.hack3.codingtests.marsrover.commands.RoverCommandFactory; import cat.hack3.codingtests.marsrover.commands.RoverCommandFactoryImpl;
module rover.commands { module rover.commands {
requires rover.api; requires rover.api;
provides CommandFactory.Provider provides RoverCommandFactory.Provider
with RoverCommandFactory.Provider; with RoverCommandFactoryImpl.Provider;
} }

View File

@ -17,12 +17,6 @@
<version>1.0</version> <version>1.0</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>station-plugin</artifactId>
<version>10.0</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>cat.hack3.codingtests</groupId> <groupId>cat.hack3.codingtests</groupId>
<artifactId>rover-commands</artifactId> <artifactId>rover-commands</artifactId>

View File

@ -4,8 +4,8 @@ import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates; import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.api.cartography.Direction; import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap; 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.api.commands.RoverCommand;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory;
import java.util.List; import java.util.List;
import java.util.ServiceLoader; import java.util.ServiceLoader;
@ -25,29 +25,28 @@ public class CommonTests {
protected RoverCommand turnRightCommand; protected RoverCommand turnRightCommand;
protected void setup() { protected void setup() {
int mapWidth = 10; int mapWidth = 10;
int mapHeight = 10; int mapHeight = 10;
int latitudeStartingPoint = 2; int latitudeStartingPoint = 2;
int longitudeStartingPoint = 3; int longitudeStartingPoint = 3;
var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint); var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint);
var marsMap = getProviderFor(PlanetMap.Provider.class) var marsMap = getImplProviderOf(PlanetMap.Provider.class)
.provide(mapHeight, mapWidth, startingCoordinates, getObstacles()); .provideWith(mapHeight, mapWidth, startingCoordinates, getObstacles());
rover = getProviderFor(RotableRiderRover.Provider.class) rover = getImplProviderOf(RotableRiderRover.Provider.class)
.provide(marsMap, CommonTests.STARTING_DIRECTION); .provideWith(marsMap, CommonTests.STARTING_DIRECTION);
var commandFactory = getProviderFor(CommandFactory.Provider.class) var commandFactory = getImplProviderOf(RoverCommandFactory.Provider.class)
.provide(rover); .provideWith(rover);
moveForwardCommand = commandFactory.createCommand(MOVE_FORWARD); moveForwardCommand = commandFactory.create(MOVE_FORWARD);
moveBackwardsCommand = commandFactory.createCommand(MOVE_BACKWARDS); moveBackwardsCommand = commandFactory.create(MOVE_BACKWARDS);
turnLeftCommand = commandFactory.createCommand(TURN_LEFT); turnLeftCommand = commandFactory.create(TURN_LEFT);
turnRightCommand = commandFactory.createCommand(TURN_RIGHT); turnRightCommand = commandFactory.create(TURN_RIGHT);
} }
private <T> T getProviderFor(Class<T> interfaceProvider) { private <T> T getImplProviderOf(Class<T> interfaceProvider) {
return ServiceLoader return ServiceLoader
.load(interfaceProvider) .load(interfaceProvider)
.findFirst() .findFirst()

View File

@ -1,6 +1,6 @@
import cat.hack3.codingtests.marsrover.api.RotableRiderRover; import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap; import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
import cat.hack3.codingtests.marsrover.api.commands.CommandFactory; import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory;
module tests.suite { module tests.suite {
requires rover.api; requires rover.api;
@ -9,7 +9,7 @@ module tests.suite {
uses PlanetMap.Provider; uses PlanetMap.Provider;
uses RotableRiderRover.Provider; uses RotableRiderRover.Provider;
uses CommandFactory.Provider; uses RoverCommandFactory.Provider;
requires org.testng; requires org.testng;

View File

@ -10,23 +10,31 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>user-interface-console</artifactId> <artifactId>user-interface-console</artifactId>
<dependencies>
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>rover-api</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>mars-station</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>rover-commands</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties> <properties>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target> <maven.compiler.target>17</maven.compiler.target>
</properties> </properties>
<dependencies>
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>mars-station</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>rover-commands</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
</project> </project>

View File

@ -1,6 +1,6 @@
package cat.hack3.codingtests.marsrover.ui.console; package cat.hack3.codingtests.marsrover.ui.console;
import cat.hack3.codingtests.marsrover.RotableRiderRover; import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import java.util.Scanner; import java.util.Scanner;
@ -26,8 +26,8 @@ public class ClientCommandInterface {
private void start() { private void start() {
output(PresentationMessage.INTRO); output(PresentationMessage.INTRO);
//RotableRiderRover rover = roverInitializer.autoInitialize(); RotableRiderRover rover = roverInitializer.autoInitialize();
RotableRiderRover rover = roverInitializer.initializeFromUserInputs(); //RotableRiderRover rover = roverInitializer.initializeFromUserInputs();
var commandsPerformer = new RoverCommandsPerformer(reader, rover); var commandsPerformer = new RoverCommandsPerformer(reader, rover);
commandsPerformer.acceptCommandsUntilExitSignal(); commandsPerformer.acceptCommandsUntilExitSignal();

View File

@ -1,8 +1,8 @@
package cat.hack3.codingtests.marsrover.ui.console; package cat.hack3.codingtests.marsrover.ui.console;
import cat.hack3.codingtests.marsrover.RotableRiderRover; import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand; import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
import cat.hack3.codingtests.marsrover.commands.api.RoverCommandFactory; import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory;
import java.util.Scanner; import java.util.Scanner;
@ -13,7 +13,6 @@ import static cat.hack3.codingtests.marsrover.ui.console.UICommons.output;
public class RoverCommandsPerformer { public class RoverCommandsPerformer {
private final Scanner reader; private final Scanner reader;
RoverCommandFactory roverCommandFactory;
private final RoverCommand moveForwardCommand; private final RoverCommand moveForwardCommand;
private final RoverCommand moveBackwardsCommand; private final RoverCommand moveBackwardsCommand;
private final RoverCommand turnLeftCommand; private final RoverCommand turnLeftCommand;
@ -21,11 +20,13 @@ public class RoverCommandsPerformer {
public RoverCommandsPerformer(Scanner reader, RotableRiderRover rover) { public RoverCommandsPerformer(Scanner reader, RotableRiderRover rover) {
this.reader = reader; this.reader = reader;
roverCommandFactory = new RoverCommandFactory(rover); var roverCommandFactory = UICommons
moveForwardCommand = roverCommandFactory.createCommand(MOVE_FORWARD); .getImplProviderOf(RoverCommandFactory.Provider.class)
moveBackwardsCommand = roverCommandFactory.createCommand(MOVE_BACKWARDS); .provideWith(rover);
turnLeftCommand = roverCommandFactory.createCommand(TURN_LEFT); moveForwardCommand = roverCommandFactory.create(MOVE_FORWARD);
turnRightCommand = roverCommandFactory.createCommand(TURN_RIGHT); moveBackwardsCommand = roverCommandFactory.create(MOVE_BACKWARDS);
turnLeftCommand = roverCommandFactory.create(TURN_LEFT);
turnRightCommand = roverCommandFactory.create(TURN_RIGHT);
} }
public void acceptCommandsUntilExitSignal() { public void acceptCommandsUntilExitSignal() {

View File

@ -1,10 +1,9 @@
package cat.hack3.codingtests.marsrover.ui.console; package cat.hack3.codingtests.marsrover.ui.console;
import cat.hack3.codingtests.marsrover.MarsRover; import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates; import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.api.cartography.Direction; import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import cat.hack3.codingtests.marsrover.gps.MarsMap; import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
import java.util.InputMismatchException; import java.util.InputMismatchException;
import java.util.List; import java.util.List;
@ -36,7 +35,7 @@ public class RoverInitializer {
Direction startingDirection = directionRetriever.retrieveDirection(); Direction startingDirection = directionRetriever.retrieveDirection();
RotableRiderRover rover = deployRover(mapHeight, mapWidth, latitudeStartingPoint, longitudeStartingPoint, startingDirection); RotableRiderRover rover = deployRover(mapHeight, mapWidth, latitudeStartingPoint, longitudeStartingPoint, startingDirection, List.of());
output(PresentationMessage.READY_MESSAGE); output(PresentationMessage.READY_MESSAGE);
reader.next(); //input ignored reader.next(); //input ignored
@ -65,10 +64,12 @@ public class RoverInitializer {
return deployRover(10, 10, 2, 3, Direction.SOUTH, obstacles); return deployRover(10, 10, 2, 3, Direction.SOUTH, obstacles);
} }
@SafeVarargs private RotableRiderRover deployRover(int mapHeight, int mapWidth, int latitudeStartingPoint, int longitudeStartingPoint, Direction startingDirection, List<Coordinates> obstaclesLocalizations) {
private RotableRiderRover deployRover(int mapHeight, int mapWidth, int latitudeStartingPoint, int longitudeStartingPoint, Direction startingDirection, List<Coordinates>... obstaclesLocalizations) {
var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint); var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint);
var marsMap = new MarsMap(mapHeight, mapWidth, startingCoordinates, obstaclesLocalizations);
return new MarsRover(marsMap, startingDirection); var mapProvider = UICommons.getImplProviderOf(PlanetMap.Provider.class);
var roverProvider = UICommons.getImplProviderOf(RotableRiderRover.Provider.class);
var marsMap = mapProvider.provideWith(mapHeight, mapWidth, startingCoordinates, obstaclesLocalizations);
return roverProvider.provideWith(marsMap, startingDirection);
} }
} }

View File

@ -1,5 +1,7 @@
package cat.hack3.codingtests.marsrover.ui.console; package cat.hack3.codingtests.marsrover.ui.console;
import java.util.ServiceLoader;
public class UICommons { public class UICommons {
public static final String QUIT_COMMAND_TEXT = "q"; public static final String QUIT_COMMAND_TEXT = "q";
@ -15,4 +17,11 @@ public class UICommons {
static void output(String message) { static void output(String message) {
System.out.println(message); System.out.println(message);
} }
static <T> T getImplProviderOf(Class<T> interfaceProvider) {
return ServiceLoader
.load(interfaceProvider)
.findFirst()
.orElseThrow();
}
} }

View File

@ -0,0 +1,13 @@
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory;
module ui.console {
requires rover.api;
requires mars.station;
requires rover.commands;
uses PlanetMap.Provider;
uses RotableRiderRover.Provider;
uses RoverCommandFactory.Provider;
}