1
0
Fork 0

slight clean code

This commit is contained in:
Xavier Fontanet 2024-06-24 18:01:09 +02:00
parent 8595c1c83d
commit ed847ee613
2 changed files with 30 additions and 29 deletions

View File

@ -17,12 +17,14 @@ public class MapIncrementalPositionResolver {
this.maximumXPosition = maximumXPosition; this.maximumXPosition = maximumXPosition;
} }
long getIncremented(Longitude longitude) { Latitude getIncremented(Latitude latitude) {
return getIncremented(longitude.pointer(), maximumXPosition); long incrementedPosition = getIncremented(latitude.pointer(), maximumYPosition);
return Latitude.of(incrementedPosition);
} }
long getIncremented(Latitude latitude) { Longitude getIncremented(Longitude longitude) {
return getIncremented(latitude.pointer(), maximumYPosition); long incrementedPosition = getIncremented(longitude.pointer(), maximumXPosition);
return Longitude.of(incrementedPosition);
} }
private long getIncremented(long currentPosition, long threshold) { private long getIncremented(long currentPosition, long threshold) {
@ -31,12 +33,14 @@ public class MapIncrementalPositionResolver {
: ++currentPosition; : ++currentPosition;
} }
long getDecremented(Latitude latitude) { Latitude getDecremented(Latitude latitude) {
return getDecremented(latitude.pointer(), maximumYPosition); long decrementedPosition = getDecremented(latitude.pointer(), maximumYPosition);
return Latitude.of(decrementedPosition);
} }
long getDecremented(Longitude longitude) { Longitude getDecremented(Longitude longitude) {
return getDecremented(longitude.pointer(), maximumYPosition); long decrementedPosition = getDecremented(longitude.pointer(), maximumXPosition);
return Longitude.of(decrementedPosition);
} }
private long getDecremented(long currentPosition, long threshold) { private long getDecremented(long currentPosition, long threshold) {

View File

@ -1,8 +1,6 @@
package cat.hack3.codingtests.marsrover.gps; package cat.hack3.codingtests.marsrover.gps;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates; import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates.Latitude;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates.Longitude;
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;
@ -34,45 +32,44 @@ public class MarsMap implements PlanetMap {
} }
} }
@SafeVarargs private MarsMap(int height, int width, Coordinates startingCoordinates, List<Coordinates> obstaclesLocalizations) {
private MarsMap(int height, int width, Coordinates startingCoordinates, List<Coordinates>... obstaclesLocalizations) {
currentPosition = startingCoordinates; currentPosition = startingCoordinates;
this.obstaclesLocalizations = obstaclesLocalizations.length > 0 ? obstaclesLocalizations[0] : List.of(); this.obstaclesLocalizations = obstaclesLocalizations;
positionResolver = new MapIncrementalPositionResolver(FIRST_POSITION_IN_MAP, INCREMENT_UNIT, height, width); positionResolver = new MapIncrementalPositionResolver(FIRST_POSITION_IN_MAP, INCREMENT_UNIT, height, width);
} }
@Override @Override
public Coordinates getNextPositionTowards(Direction direction) { public Coordinates getNextPositionTowards(Direction direction) {
return switch (direction) { return switch (direction) {
case NORTH -> getDecrementLatitude(); case NORTH -> getDecrementedLatitude();
case SOUTH -> getIncrementLatitude(); case SOUTH -> getIncrementedLatitude();
case WEST -> getDecrementLongitude(); case WEST -> getDecrementedLongitude();
case EAST -> getIncrementLongitude(); case EAST -> getIncrementedLongitude();
}; };
} }
private Coordinates getIncrementLatitude() { private Coordinates getIncrementedLatitude() {
var latitude = currentPosition.latitude(); var latitude = currentPosition.latitude();
long newLatitude = positionResolver.getIncremented(latitude); var newLatitude = positionResolver.getIncremented(latitude);
return currentPosition.withUpdated(Latitude.of(newLatitude)); return currentPosition.withUpdated(newLatitude);
} }
private Coordinates getDecrementLatitude() { private Coordinates getDecrementedLatitude() {
var latitude = currentPosition.latitude(); var latitude = currentPosition.latitude();
long newLatitude = positionResolver.getDecremented(latitude); var newLatitude = positionResolver.getDecremented(latitude);
return currentPosition.withUpdated(Latitude.of(newLatitude)); return currentPosition.withUpdated(newLatitude);
} }
private Coordinates getIncrementLongitude() { private Coordinates getIncrementedLongitude() {
var longitude = currentPosition.longitude(); var longitude = currentPosition.longitude();
long newLongitude = positionResolver.getIncremented(longitude); var newLongitude = positionResolver.getIncremented(longitude);
return currentPosition.withUpdated(Longitude.of(newLongitude)); return currentPosition.withUpdated(newLongitude);
} }
private Coordinates getDecrementLongitude() { private Coordinates getDecrementedLongitude() {
var longitude = currentPosition.longitude(); var longitude = currentPosition.longitude();
long newLongitude = positionResolver.getDecremented(longitude); var newLongitude = positionResolver.getDecremented(longitude);
return currentPosition.withUpdated(Longitude.of(newLongitude)); return currentPosition.withUpdated(newLongitude);
} }
@Override @Override