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

View File

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