1
0
Fork 0

forgotten design improve from Latitude/Longitude Objects introduction

This commit is contained in:
Xavier Fontanet 2024-06-22 19:47:05 +02:00
parent cbbc40c50a
commit 75cfcb1184
2 changed files with 19 additions and 11 deletions

View File

@ -3,24 +3,30 @@ package cat.hack3.codingtests.marsrover.cartography;
public record Coordinates(Latitude latitude, Longitude longitude) {
public static Coordinates of(long latitude, long longitude) {
return new Coordinates(new Latitude(latitude), new Longitude(longitude));
return new Coordinates(Latitude.of(latitude), Longitude.of(longitude));
}
public Coordinates ofUpdatedLatitude(long newLatitude) {
return Coordinates.of(newLatitude, longitude.pointer);
public Coordinates withUpdated(Latitude newLatitude) {
return Coordinates.of(newLatitude.pointer, longitude.pointer);
}
public Coordinates ofUpdatedLongitude(long newLongitude) {
return Coordinates.of(latitude.pointer, newLongitude);
public Coordinates withUpdated(Longitude newLongitude) {
return Coordinates.of(latitude.pointer, newLongitude.pointer);
}
record Latitude(long pointer) {
static Latitude of(long pointer) {
return new Latitude(pointer);
}
@Override
public String toString() {
return String.valueOf(pointer);
}
}
record Longitude(long pointer) {
static Longitude of(long pointer) {
return new Longitude(pointer);
}
@Override
public String toString() {
return String.valueOf(pointer);

View File

@ -1,7 +1,9 @@
package cat.hack3.codingtests.marsrover.cartography;
import cat.hack3.codingtests.marsrover.cartography.Coordinates.Latitude;
import cat.hack3.codingtests.marsrover.cartography.Coordinates.Longitude;
import java.util.List;
import java.util.logging.Logger;
/**
* Notice that the representation is the following:
@ -13,7 +15,7 @@ import java.util.logging.Logger;
* 3 3-1 3-2 3-3
*/
public class MarsMap {
Logger logger = Logger.getLogger(this.getClass().getName());
private static final int FIRST_POSITION_IN_MAP = 1;
private static final int INCREMENT_UNIT = 1;
@ -41,25 +43,25 @@ public class MarsMap {
private Coordinates getIncrementLatitude() {
var latitude = currentPosition.latitude();
long newLatitude = positionResolver.getIncremented(latitude);
return currentPosition.ofUpdatedLatitude(newLatitude);
return currentPosition.withUpdated(Latitude.of(newLatitude));
}
private Coordinates getDecrementLatitude() {
var latitude = currentPosition.latitude();
long newLatitude = positionResolver.getDecremented(latitude);
return currentPosition.ofUpdatedLatitude(newLatitude);
return currentPosition.withUpdated(Latitude.of(newLatitude));
}
private Coordinates getIncrementLongitude() {
var longitude = currentPosition.longitude();
long newLongitude = positionResolver.getIncremented(longitude);
return currentPosition.ofUpdatedLongitude(newLongitude);
return currentPosition.withUpdated(Longitude.of(newLongitude));
}
private Coordinates getDecrementLongitude() {
var longitude = currentPosition.longitude();
long newLongitude = positionResolver.getDecremented(longitude);
return currentPosition.ofUpdatedLongitude(newLongitude);
return currentPosition.withUpdated(Longitude.of(newLongitude));
}
public synchronized void updateLocation(Coordinates newPosition) {